Skip to content

Instantly share code, notes, and snippets.

@jeffbicca
Created December 2, 2013 17:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jeffbicca/7753014 to your computer and use it in GitHub Desktop.
Save jeffbicca/7753014 to your computer and use it in GitHub Desktop.
Adding header elements into a SOAP envelope and setting timeout with JAX-WS.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:via="http://mycompany.com.br/">
<soapenv:Header>
<wshh:userId xmlns:wshh='http://mycompany.com.br/wsheaderhandlers'>12345</wshh:userId>
<wshh:ip xmlns:wshh='http://mycompany.com.br/wsheaderhandlers'>10.10.10.10</wshh:ip>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceRef;
import javax.xml.ws.handler.Handler;
public class WebServiceCaller implements Serializable {
private static final int MINUTES = 60000;
@WebServiceRef(wsdlLocation = "http://localhost/WebService.wsdl")
WebService webService;
public void callWebService(String userId, String ip) {
Service port = captureAndConfigurePort(userId, ip);
port.callMethod();
}
private Service captureAndConfigurePort(String userId, String ip) {
Service port = webService.getWebServicePort();
Binding binding = ((BindingProvider) port).getBinding();
List<Handler> handlerChain = binding.getHandlerChain();
handlerChain.add(new WebServiceHandler(userId, ip));
binding.setHandlerChain(handlerChain);
Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();
requestContext.put("javax.xml.ws.client.receiveTimeout", 30 * MINUTES);
return port;
}
}
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class WebServiceHandler implements SOAPHandler<SOAPMessageContext> {
private String userId;
private String ip;
public WebServiceRelatoriosHandler(String userId, String ip) {
this.userId = userId;
this.ip = ip;
}
public boolean handleMessage(SOAPMessageContext messageContext) {
SOAPMessage msg = messageContext.getMessage();
if((Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
try {
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPElement el = header.addChildElement(envelope.createName("userId", "wshh", "http://mycompany.com.br/wsheaderhandlers"));
el.setValue(userId);
el = header.addChildElement(envelope.createName("ip", "wshh", "http://mycompany.com.br/wsheaderhandlers"));
el.setValue(ip);
msg.saveChanges();
} catch (SOAPException e) {
return false;
}
}
return true;
}
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
public void close(MessageContext messageContext) { }
public Set getHeaders() {
return new HashSet();
}
}
@dachosys
Copy link

Thanks a lot! Almost 3 days trying to insert thinks on soap header. Now, finally i got it.

I had to modify SOAPHeader header = envelope.addHeader(); by SOAPHeader header = envelope.getHeader();

It was conflicting with the existing soap header.

@TehBakker
Copy link

Doesn't this approach has issue with Multi-threads environment ?

@dachosys
Copy link

Have no tested yet.

@BE-Arbiter
Copy link

Thank you very much !

@DSEVCENCO
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment