This problem is also found in the ticket reports here: https://java.net/jira/browse/WSIT-1081
I solved this problem by not using a handler but adding a header when creating a port like this:
Web-Service-Creation
WebServiceEndpoint endpoint = w.getWebServiceEndpointPort(new MTOMFeature());
Map<String, Object> ctxt = ((BindingProvider) endpoint).getRequestContext();
// Enable HTTP chunking mode, otherwise HttpURLConnection buffers
ctxt.put("com.sun.xml.ws.transport.http.client.streaming.chunk.size", 8192);
WSBindingProvider bp = (WSBindingProvider) endpoint;
bp.setOutboundHeaders(Headers.create(JAXBContext.newInstance(WSSecurityHeader.class),
new WSSecurityHeader()));
Since Header-creation is not provided with methods, JAXB is used for complex header types.
WSSecurityHeader.java
@XmlRootElement(name = "Security")
public class WSSecurityHeader {
@XmlElement(name = "UsernameToken")
public WSSecurityUsernameToken usernameToken = new WSSecurityUsernameToken();
...
}
Disadvantages
The client needs many libraries that this code is working: JAXB, JAX-WS, StreamBuffer, Policy, Stax-Ex, gmbal-api-only, which is horrible for just adding a header. (Especially if you are working with a fat client without maven support)