How to make JAXWS send content-length header on SOAP responses

Neck picture Neck · Sep 17, 2011 · Viewed 8.9k times · Source

Basically I made a Webservice using JAXWS. It uses SOAP and works. However on some request some clients crash/error, it seems to be linked to the fact the JAXWS webservice does not send the content-length header. Is there a way to add it?

Using: openjdk-6 (6b20)

My class looks like this:

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;

@WebService
public class SOAP {
public static void main(String[] args) {
    try {
        SOAP server = new SOAP();
        Endpoint endpoint = Endpoint.publish('localhost:1234', server);
    } catch (Exception e) {
                Utils.getLog("SOAP").fatal("General Error", e);
    }
}
}

Using tcpdump confirms there is no content-length in the response:

0x0000:  4500 00b0 4da5 4000 4006 20d5 5cf3 1021  E...M.@.@...\..!
0x0010:  5cf3 01c7 13b2 bcea a9be 716e 14c3 16a1  \.........qn....
0x0020:  8018 006c cc70 0000 0101 080a 2e1b 3ccc  ...l.p........<.
0x0030:  2e18 23a2 4854 5450 2f31 2e31 2032 3030  ..#.HTTP/1.1.200
0x0040:  204f 4b0d 0a54 7261 6e73 6665 722d 656e  .OK..Transfer-en
0x0050:  636f 6469 6e67 3a20 6368 756e 6b65 640d  coding:.chunked.
0x0060:  0a43 6f6e 7465 6e74 2d74 7970 653a 2074  .Content-type:.t
0x0070:  6578 742f 786d 6c3b 6368 6172 7365 743d  ext/xml;charset=
0x0080:  2275 7466 2d38 220d 0a44 6174 653a 2053  "utf-8"..Date:.S
0x0090:  6174 2c20 3137 2053 6570 2032 3031 3120  at,.17.Sep.2011.
0x00a0:  3134 3a31 393a 3337 2047 4d54 0d0a 0d0a  14:19:37.GMT....

So my question is, is there a way to tell the webservice to send the content-length along with the reply? Or a way to append it before the reply is sent?

Answer

Simeon G picture Simeon G · Sep 17, 2011

You can add HTTP headers to the outgoing response by writing a handler. Your handler will extend the standard JAX-WS handler and use the MessageContext in the handleMessage(C context) method to modify the HTTP_RESPONSE_HEADER property (actually a key in the map). So for example your handler will be:

public class MyHandler<SOAPMessageContext> implements Handler {

    public boolean handleMessage(SOAPMessageContext c) {
        if((Boolean)c.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
            c.put(SOAPMessageContext.HTTP_RESPONSE_HEADERS,"your header stuff here");
            return true;
        }
    }
 }

You will need to create a handler-chain file and add an annotation pointing to that file in your endpoint class. The annotation is @HandlerChain(file="...");

Once you do this you should be able to modify HTTP headers on in and outbound. HTH.