-
-
Save ajaysolleti/590dded2f565c1bdf85d to your computer and use it in GitHub Desktop.
Adding header elements into a SOAP envelope and setting timeout with JAX-WS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment