You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@xml.apache.org by Tiago Fernandes Thomaz <ti...@optimus.pt> on 2002/03/20 23:52:04 UTC

Client calling a service implemented by an EJB? (my code in here. ..)

Please tell me what can I do to get this running?

                                      /// The Client: ///

public class MySOAPClient
{
  /** Creates new Class */
  public MySOAPClient(){
  }

  public static void main(String args[]) throws Exception
  {
    if (args.length != 1)
    {
      System.err.println ("Usage: java " + MySOAPClient.class.getName() + "
Currency");
      System.exit (1);
    }

    String encodingStyleURI = "http://schemas.xmlsoap.org/soap/encoding/";

    URL url = new URL ("http://localhost:8080/soap/servlet/rpcrouter");
    String code = "1.11356179";
    String currency = args[0];

    Vector params = new Vector(0);
    params.addElement(new Parameter("sCustCode", String.class, code,
"http://schemas.xmlsoap.org/soap/encoding/"));
    params.addElement(new Parameter("sCurrency", Long.class, currency,
"http://schemas.xmlsoap.org/soap/encoding/"));

    SOAPContext ctx = new SOAPContext();
    ctx.setRootPart("Context-Type", "text/xml");

    Header hdr = new Header();

    //Build the call
    Call call = new Call("urn:GetCustCode", "getCustomerId", params, hdr,
encodingStyleURI, ctx);

    Response resp = null;
    try
    {
      resp = call.invoke(url, "" );
      SOAPContext cont = resp.getSOAPContext();
    }
    catch (SOAPException e)
    {
      System.out.println("MySOAPClient.java - Error: call.invoke()!");
      System.err.println("Caught SOAPException (" + e.getFaultCode() + "): "
+ e.getMessage());
      return;
    }

    //Check Response
    if(resp.generatedFault())
    {
      System.out.println("generatedFault = true! ");
      Fault fault = resp.getFault();
      System.out.println("Error occured!");
      System.out.println("Fault code was: " + fault.getFaultCode());
      System.out.println("Fault string was: " + fault.getFaultString());
    }
    else
    {
      System.out.println("generatedFault = false! ");
      Parameter result = resp.getReturnValue();
    }
  }
}

                                                       /// The Server: ///

public class Listener extends HttpServlet
{
  String sSOAPRes3 = null;

  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
  {
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
  {
    try
    {
      System.out.println("I'm in!");

      GetCustByMsisdnHome  custHome  = null;
      String               sResp     = null;
      String               sResp2  = null;

      // Get a naming context
      InitialContext jndiContext = new InitialContext();
      // Get a reference to the Interest Bean
      Object ref  = jndiContext.lookup("GetCustByMsisdn");
      // Get a reference from this to the Bean's Home interface
      custHome = (GetCustByMsisdnHome) PortableRemoteObject.narrow(ref,
GetCustByMsisdnHome.class);

      // Create an GetCustByMsisdn object from the Home interface
      GetCustByMsisdn bean = custHome.create();
      System.out.println("Bean instance created!");

      long t = 7;
      String z = "1.11341678";
      sResp2 = bean.getCustomerId(z, t);

      sSOAPRes3 = "<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                + "
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                + "  <SOAP-ENV:Body> "
                + "      <ns1:getCustomerIdResponse
xmlns:ns1=\"urn:GetCustCode\"
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                + sResp2
                + "      </ns1:getCustomerIdResponse> "
                + "  </SOAP-ENV:Body> "
                + "</SOAP-ENV:Envelope>";

      bean.remove();

      response.setContentType("text/xml");

      PrintWriter out = response.getWriter();
      out.write(sSOAPRes3);
      out.flush();
    }
    catch(Exception e)
    {
      System.out.println("Listener.java - Error: "+e.getMessage());
    }
  }
}

                                                  /// The Bean: ///

public class GetCustByMsisdnBean implements SessionBean
{
  public GetCustByMsisdnBean()
  {
  }

  public String getCustomerId(String sCustCode, long sCurrency) throws
Exception
  {
    String sReply1 = null;
    String sReply2 = null;
    String sReply3 = null;

    Connection con = null;

    try
    {
      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource)ic.lookup("java:/BSCS_DB");
      con = ds.getConnection();
      String sSQL = "select c.customer_id, cc.ccline2 from customer_all c,
ccontact_all cc where c.custcode = ? and c.fc_id = ? and c.customer_id =
cc.customer_id ";
      PreparedStatement prepStmt = con.prepareStatement(sSQL);
      prepStmt.setString(1, sCustCode);
      prepStmt.setLong(2, sCurrency);
      for(ResultSet rs = prepStmt.executeQuery(); rs.next();)
      {
        sReply1 = rs.getString(1);
        sReply2 = rs.getString(2);
        sReply3 = sReply1 + " - " + sReply2;
      }

      prepStmt.close();
    }
    catch(Exception e)
    {
      System.out.println("GetCustByMsisdn.java: getCustomerId() Error!: " +
e.getMessage());
    }
    finally
    {
      con.close();
      System.out.println("Result: " + sReply3);
    }
    return sReply3;
  }

  public void ejbCreate()
  {
  }

  public void ejbPostCreate()
  {
  }

  public void ejbRemove()
  {
  }

  public void ejbActivate()
  {
  }

  public void ejbPassivate()
  {
  }

  public void setSessionContext(SessionContext sessioncontext)
  {
  }
}

----------------------

As you can see I'm calling a service that is implemented in a bean hosted by
JBOSS. Can I do the SOAP request like this? What am I doing wrong? 


-------------------------
Tiago Fernandes Thomaz


-----Original Message-----
From: Beau Anderson [mailto:beau@dpn.com]
Sent: quarta-feira, 20 de Março de 2002 21:24
To: soap-user@xml.apache.org
Subject: Re: [RE: [Data Compression using Apache SOAP]]


please post your code and indicate where
it is placed on the server.

----- Original Message -----
From: "Tiago Fernandes Thomaz" <ti...@optimus.pt>
To: <so...@xml.apache.org>
Sent: Wednesday, March 20, 2002 12:58 PM
Subject: RE: [RE: [Data Compression using Apache SOAP]]


> I only have to set the method, the targetURI (which must match that of the
> deployment descriptor), required input parameter values, the endpoint and
> thats it? RPCrouter servlet will "guide" my request get the method return
> and wrap it up in a SOAP envelope to client? Is that it?
> If so, could you explain why I'm getting the following error:
>
> Fault code: SOAP-ENV:Server.BadTargetObjectURI
> Fault string: Unable to resolve target object: package.MyClass
>
> Tiago Fernandes Thomaz