Last active
August 20, 2018 12:45
-
-
Save gbzarelli/48d110d8e63a0926efabdc1038b29c82 to your computer and use it in GitHub Desktop.
Add SOAP header object using pure 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
private static IniciarAulaRetorno iniciarAulaPratica(IniciarAulaDados dados) { | |
MonitAulas service = new MonitAulas(); | |
MonitAulasSoap port = service.getMonitAulasSoap(); | |
BindingProvider bindingProvider = (BindingProvider) port; | |
Binding binding = bindingProvider.getBinding(); | |
/* For error 307 | |
String WS_URL = "https://renach2.es.gov.br/WebServices/MonitAulasPraticas/MonitAulasPraticas.asmx?wsdl"; | |
Map<String, Object> req_ctx = bindingProvider.getRequestContext(); | |
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL); | |
*/ | |
List<Handler> handlersList = new ArrayList<>(); | |
JAXBElement<MonitAulaHeader> createMonitAulaHeader = new ObjectFactory().createMonitAulaHeader(new MonitAulaHeader()); | |
handlersList.add(new SOAPHandler<SOAPMessageContext>() { | |
@Override | |
public boolean handleMessage(final SOAPMessageContext context) { | |
try { | |
// checking whether handled message is outbound one as per Martin Strauss answer | |
final Boolean outbound = (Boolean) context.get("javax.xml.ws.handler.message.outbound"); | |
if (outbound != null && outbound) { | |
// obtaining marshaller which should marshal instance to xml | |
final Marshaller marshaller = JAXBContext.newInstance(MonitAulaHeader.class).createMarshaller(); | |
// adding header because otherwise it's null | |
// if get return null, call addHeader | |
SOAPHeader soapHeader = context.getMessage().getSOAPPart().getEnvelope().getHeader(); | |
// marshalling instance (appending) to SOAP header's xml node | |
marshaller.marshal(createMonitAulaHeader, soapHeader); | |
} | |
} catch (final Exception e) { | |
throw new RuntimeException(e); | |
} | |
return true; | |
} | |
@Override | |
public Set<QName> getHeaders() { | |
Set<QName> ts = new HashSet<>(); | |
ts.add(createMonitAulaHeader.getName()); | |
return ts; | |
} | |
@Override | |
public boolean handleFault(SOAPMessageContext context) { | |
return false; | |
} | |
@Override | |
public void close(MessageContext context) { | |
} | |
}); | |
binding.setHandlerChain(handlersList); | |
return port.iniciarAula(dados); | |
} | |
//reference: https://stackoverflow.com/questions/10654608/add-soap-header-object-using-pure-jax-ws |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment