简体   繁体   中英

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

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?

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.

I had a similar issue where a provider would not accept a chunked message... it would not set the Content-length in the header either and would actually chunk the message and the web service provider would reject my call... This was using JAX-WS with Axis2 1.6 as the JAX-WS runtime.

I tried add the HTTP headers to manually add Content-length, but this would cause other issues. I eventually got it to work by setting the HTTPConstants.CHUNKED to false on the SOAPMessageContext in a Handler.

I blogged about here: http://btarlton.blogspot.com/2013/03/jax-ws-with-axis2-how-to-disable.html#!/2013/03/jax-ws-with-axis2-how-to-disable.html

With Axis2 as the JAX-WS runtime, this was the only fix I could find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM