views:

462

answers:

3

This problem has been bothering me for days. Hopefully someone has come across this before and has developed a workaround.

So I've developed a middleware Java app which during its execution invokes a SOAP action. Now, the behavior of this bit of code under the 1.6 JDK is working well:

        // inside a try-catch block
        SOAPConnectionFactory soapConnectionFactory = 
                     SOAPConnectionFactory.newInstance();
        SOAPConnection connection = 
                  soapConnectionFactory.createConnection();
        MessageFactory messageFactory = MessageFactory.newInstance();          
        SOAPMessage soapMsg = messageFactory.createMessage();
        String soapAction = getHeaderUrl() + "/" + method + "\r\n";
        MimeHeaders headers = soapMsg.getMimeHeaders(); 
        headers.addHeader("SOAPAction", soapAction); 

       // review the SOAPAction header
        int header_count = 
           soapMsg.getMimeHeaders().getHeader("SOAPAction").length;
        out("SOAPActions (from Soap header): ");
        if (header_count == 0) out ("No SOAPActions defined.");
        for (int i=0; i<header_count; i++)
            out("SOAPAction["+i+"] = \"" + 
               soapMsg.getMimeHeaders().getHeader("SOAPAction")[i] 
                  + "\""); 


        SOAPPart soapPart = soapMsg.getSOAPPart();            
        StreamSource s = new StreamSource(new StringReader(msg));
        soapPart.setContent(s);
        soapMsg.saveChanges();
        ByteArrayOutputStream req_bytes = new ByteArrayOutputStream();
        soapMsg.writeTo(req_bytes);
        String req = new String(req_bytes.toByteArray());         
        SOAPMessage reply = connection.call(soapMsg, Url);

However, when I use this exact same code under 1.3 or 1.4 JDK with the SOAP libraries linked, everything above works/compiles/executes, except for the fact that the SOAPAction field of the header is blank. Weirdly enough, when I check what the value of that header is (the lines immediately following setting up the header) the appropriate value is displayed. However, when it goes over the wire, the field is empty. As this is the field which indicates the resource I want to the processor, my messages are getting turned back at the door.

Has anyone encountered this problem before? If so, are there possible workarounds? (I am, of course, willing to use another library if required.)

UPDATE:

The libraries on my classpath are as follows:

  • Xerces (resolver.jar, serializer.jar, xercesImpl.jar, xml-apis.jar)
  • Saaj (saaj.jar)
  • Axis (axis.jar
  • JAX-RPC (jaxrpc.jar)
  • Standard SOAP (activation.jar, mail.jar, soap.jar, xerces.jar)
  • Commons logging and discovery

Thanks in advance!

A: 

Peculiar. You say you're linking in the SOAP libraries - which ones, exactly?

I wouldn't hurt to try a different SOAP stack, such as CXF or JAX-WS-RI, although I'd surprised if either of those two supported java 1.3.

skaffman
Thanks. Will try JAX-WS-RI.Also added the libraries to the initial post.
Joe
A: 

It looks like this may actually be an oversight or a bug, but at any rate my workaround was to handle the lower-level HTTP stuff manually, i.e.:

        URL u = new URL(Url);
        URLConnection uc = u.openConnection();
        HttpURLConnection  conn = (HttpURLConnection) uc;

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("SOAPAction", getHeaderUrl() + "/" + method);
        conn.setRequestProperty("Content-type", "text/xml");

        OutputStream out = conn.getOutputStream();
        Writer wr = new OutputStreamWriter(out);

        wr.write(msg);
        wr.flush();
        wr.close();

        InputStream in = conn.getInputStream();
        String rsp="";
        int c;
        while ((c = in.read()) != -1) { 
            System.out.write(c);
            rsp = rsp.concat(String.valueOf((char)c));
        }
        in.close();
        rsp = deSOAP(rsp);
        response = response.fromXML(rsp);
        return response;

Turns out this is significantly more concise than the SOAP-based solution as well.

Joe
A: 

Dear Joe,

Your explanation is appreciated. Actually i am looking is same. I tried one my webservices using SoapUI tool. it works fine, but couldn't complete via HTTPpost method. By using your technique i easily get the output

Cheers
vidhya

vidhyadharan