use of javax.xml.ws.handler.soap.SOAPMessageContext in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderInBound.
@Test
public void testChangeSOAPHeaderInBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
try {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outboundProperty.booleanValue()) {
// change mustUnderstand to false
SOAPMessage message = smc.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
Element headerElementNew = (Element) soapHeader.getFirstChild();
SoapVersion soapVersion = Soap11.getInstance();
Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
attr.setValue("false");
headerElementNew.setAttributeNodeNS(attr);
}
} catch (Exception e) {
throw new Fault(e);
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
// This is to set direction to inbound
expect(exchange.getOutMessage()).andReturn(null);
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
message.setContent(XMLStreamReader.class, reader);
Object[] headerInfo = prepareSOAPHeader();
message.setContent(Node.class, headerInfo[0]);
Node node = ((Element) headerInfo[1]).getFirstChild();
message.getHeaders().add(new Header(new QName(node.getNamespaceURI(), node.getLocalName()), node));
control.replay();
SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
li.handleMessage(message);
control.verify();
// Verify SOAPMessage header
SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
Element headerElementNew = DOMUtils.getFirstElement(soapMessageNew.getSOAPHeader());
SoapVersion soapVersion = Soap11.getInstance();
assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
// Verify XMLStreamReader
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
QName qn = xmlReader.getName();
assertEquals("sendReceiveData", qn.getLocalPart());
// Verify Header Element
Iterator<Header> iter = message.getHeaders().iterator();
Element requiredHeader = null;
while (iter.hasNext()) {
Header localHdr = iter.next();
if (localHdr.getObject() instanceof Element) {
Element elem = (Element) localHdr.getObject();
if ("http://apache.org/hello_world_rpclit/types".equals(elem.getNamespaceURI()) && "header1".equals(elem.getLocalName())) {
requiredHeader = (Element) localHdr.getObject();
break;
}
}
}
assertNotNull("Should have found header1", requiredHeader);
assertEquals("false", requiredHeader.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
}
use of javax.xml.ws.handler.soap.SOAPMessageContext in project cxf by apache.
the class SOAPHandlerInterceptor method handleAbort.
private void handleAbort(SoapMessage message, MessageContext context) {
if (isRequestor(message)) {
// client side outbound
if (getInvoker(message).isOutbound()) {
message.getInterceptorChain().abort();
MessageObserver observer = message.getExchange().get(MessageObserver.class);
if (!message.getExchange().isOneWay() && observer != null) {
Endpoint e = message.getExchange().getEndpoint();
Message responseMsg = new MessageImpl();
responseMsg.setExchange(message.getExchange());
responseMsg = e.getBinding().createMessage(responseMsg);
// the request message becomes the response message
message.getExchange().setInMessage(responseMsg);
SOAPMessage soapMessage = ((SOAPMessageContext) context).getMessage();
if (soapMessage != null) {
responseMsg.setContent(SOAPMessage.class, soapMessage);
XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(soapMessage);
responseMsg.setContent(XMLStreamReader.class, xmlReader);
}
responseMsg.put(InterceptorChain.STARTING_AT_INTERCEPTOR_ID, SOAPHandlerInterceptor.class.getName());
observer.onMessage(responseMsg);
}
// We dont call onCompletion here, as onCompletion will be called by inbound
// LogicalHandlerInterceptor
} else {
// client side inbound - Normal handler message processing
// stops, but the inbound interceptor chain still continues, dispatch the message
// By onCompletion here, we can skip following Logical handlers
onCompletion(message);
}
} else {
if (!getInvoker(message).isOutbound()) {
// server side inbound
message.getInterceptorChain().abort();
Endpoint e = message.getExchange().getEndpoint();
if (!message.getExchange().isOneWay()) {
Message responseMsg = new MessageImpl();
responseMsg.setExchange(message.getExchange());
responseMsg = e.getBinding().createMessage(responseMsg);
message.getExchange().setOutMessage(responseMsg);
SOAPMessage soapMessage = ((SOAPMessageContext) context).getMessage();
responseMsg.setContent(SOAPMessage.class, soapMessage);
InterceptorChain chain = OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange());
responseMsg.setInterceptorChain(chain);
// so the idea of starting interceptor chain from any
// specified point does not work
// well for outbound case, as many outbound interceptors
// have their ending interceptors.
// For example, we can not skip MessageSenderInterceptor.
chain.doInterceptStartingAfter(responseMsg, SoapPreProtocolOutInterceptor.class.getName());
}
} else {
// server side outbound - Normal handler message processing
// stops, but still continue the outbound interceptor chain, dispatch the message
}
}
}
use of javax.xml.ws.handler.soap.SOAPMessageContext in project cxf by apache.
the class JaxWsClientTest method testSoapHandler.
@Test
public void testSoapHandler() {
URL url = getClass().getResource("/wsdl/hello_world.wsdl");
javax.xml.ws.Service s = javax.xml.ws.Service.create(url, SERVICE_NAME);
Greeter greeter = s.getPort(PORT_NAME, Greeter.class);
d.setMessageObserver(new MessageReplayObserver("sayHiResponse.xml"));
@SuppressWarnings("rawtypes") List<Handler> chain = ((BindingProvider) greeter).getBinding().getHandlerChain();
chain.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext context) {
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
headers = CastUtils.cast((Map<?, ?>) context.get(MessageContext.HTTP_REQUEST_HEADERS));
if (headers == null) {
headers = new HashMap<>();
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
}
headers.put("My-Custom-Header", Collections.singletonList("value"));
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
((BindingProvider) greeter).getBinding().setHandlerChain(chain);
String response = greeter.sayHi();
assertNotNull(response);
assertTrue("custom header should be present", headers.containsKey("My-Custom-Header"));
assertTrue("existing SOAPAction header should not be removed", headers.containsKey("SOAPAction"));
}
use of javax.xml.ws.handler.soap.SOAPMessageContext in project cxf by apache.
the class HandlerInvocationTest method testServerEndpointRemoteFault.
@Test
public void testServerEndpointRemoteFault() throws PingException {
TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
public boolean handleFault(LogicalMessageContext ctx) {
super.handleFault(ctx);
try {
Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outbound) {
LogicalMessage msg = ctx.getMessage();
String payload = convertDOMToString(msg.getPayload());
assertTrue(payload.indexOf("<faultstring>" + "servant throws SOAPFaultException" + "</faultstring>") > -1);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
return true;
}
private String convertDOMToString(Source s) throws TransformerException {
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.transform(s, streamResult);
return stringWriter.toString();
}
};
TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
TestSOAPHandler soapHandler2 = new TestSOAPHandler(false) {
public boolean handleFault(SOAPMessageContext ctx) {
super.handleFault(ctx);
try {
Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outbound) {
SOAPEnvelope env = ctx.getMessage().getSOAPPart().getEnvelope();
assertTrue("expected SOAPFault in SAAJ model", env.getBody().hasFault());
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
return true;
}
};
addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
try {
handlerTest.pingWithArgs("servant throw SOAPFaultException");
fail("did not get expected Exception");
} catch (SOAPFaultException sfe) {
// expected
}
assertEquals(1, handler1.getHandleMessageInvoked());
assertEquals(1, handler2.getHandleMessageInvoked());
assertEquals(1, soapHandler1.getHandleMessageInvoked());
assertEquals(1, soapHandler2.getHandleMessageInvoked());
assertEquals(1, handler2.getHandleFaultInvoked());
assertEquals(1, handler1.getHandleFaultInvoked());
assertEquals(1, soapHandler1.getHandleFaultInvoked());
assertEquals(1, soapHandler2.getHandleFaultInvoked());
assertEquals(1, handler1.getCloseInvoked());
assertEquals(1, handler2.getCloseInvoked());
assertEquals(1, soapHandler1.getCloseInvoked());
assertEquals(1, soapHandler2.getCloseInvoked());
assertTrue(handler2.getInvokeOrderOfClose() < handler1.getInvokeOrderOfClose());
}
use of javax.xml.ws.handler.soap.SOAPMessageContext in project jbossws-cxf by jbossws.
the class EndpointHandler method handleInbound.
@Override
protected boolean handleInbound(SOAPMessageContext msgContext) {
log.info("handleInbound");
try {
SOAPMessage soapMessage = ((SOAPMessageContext) msgContext).getMessage();
SOAPBody soapBody = soapMessage.getSOAPBody();
SOAPBodyElement soapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();
SOAPElement soapElement = (SOAPElement) soapBodyElement.getChildElements().next();
String value = soapElement.getValue();
soapElement.setValue(value + "|EpIn");
} catch (SOAPException e) {
throw new WebServiceException(e);
}
return true;
}
Aggregations