use of javax.xml.ws.handler.LogicalMessageContext in project jbossws-cxf by jbossws.
the class LogicalSourceHandler method appendHandlerName.
public boolean appendHandlerName(MessageContext msgContext, String direction) {
try {
// Get the payload as Source
LogicalMessageContext logicalContext = (LogicalMessageContext) msgContext;
Source source = logicalContext.getMessage().getPayload();
TransformerFactory tf = TransformerFactory.newInstance();
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
tf.newTransformer().transform(source, new StreamResult(baos));
// Parse the payload and extract the value
Element root = DOMUtils.parse(new ByteArrayInputStream(baos.toByteArray()), getDocumentBuilder());
String oldValue = DOMUtils.getTextContent(root);
String newValue = oldValue + ":" + direction + ":LogicalSourceHandler";
root.setTextContent(newValue);
log.debug("oldValue: " + oldValue);
log.debug("newValue: " + newValue);
// Set the updated payload
source = new DOMSource(root);
logicalContext.getMessage().setPayload(source);
return true;
} catch (RuntimeException rte) {
throw rte;
} catch (Exception ex) {
throw new WebServiceException(ex);
}
}
use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.
the class HandlerInvocationTest method testServerEndpointRemoteFault.
@Test
public void testServerEndpointRemoteFault() throws PingException {
TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(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.LogicalMessageContext in project cxf by apache.
the class HandlerInvocationTest method testLogicalHandlerHandleMessageReturnFalseClientOutBound.
@Test
public void testLogicalHandlerHandleMessageReturnFalseClientOutBound() throws Exception {
final String clientHandlerMessage = "handler2 client side";
TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
public boolean handleMessage(LogicalMessageContext ctx) {
super.handleMessage(ctx);
try {
Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
LogicalMessage msg = ctx.getMessage();
assertNotNull("logical message is null", msg);
JAXBContext jaxbCtx = JAXBContext.newInstance(PackageUtils.getPackageName(PingOneWay.class));
PingResponse resp = new PingResponse();
resp.getHandlersInfo().add(clientHandlerMessage);
msg.setPayload(resp, jaxbCtx);
return false;
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
return true;
}
};
TestHandler<LogicalMessageContext> handler3 = new TestHandler<LogicalMessageContext>(false);
TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, handler3, soapHandler1);
List<String> resp = handlerTest.ping();
assertEquals(clientHandlerMessage, resp.get(0));
assertEquals("the first handler must be invoked twice", 2, handler1.getHandleMessageInvoked());
assertEquals("the second handler must be invoked once only on outbound", 1, handler2.getHandleMessageInvoked());
assertEquals("the third handler must not be invoked", 0, handler3.getHandleMessageInvoked());
assertEquals("the last handler must not be invoked", 0, soapHandler1.getHandleMessageInvoked());
// outbound MEP processing ceased, the message direction was changed to inbound, essentially this is
// only one MEP. So close is called only once at the end of inbound MEP, and the close order is
// reversed to the outbound handler invoking order.
assertEquals("close must be called", 1, handler1.getCloseInvoked());
assertEquals("close must be called", 1, handler2.getCloseInvoked());
assertEquals("close must be called", 0, handler3.getCloseInvoked());
assertEquals("close must be called", 0, soapHandler1.getCloseInvoked());
assertTrue(handler2.getInvokeOrderOfClose() < handler1.getInvokeOrderOfClose());
}
use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.
the class HandlerInvocationTest method testSOAPHandlerHandleMessageReturnTrueClient.
@Test
public void testSOAPHandlerHandleMessageReturnTrueClient() throws Exception {
TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
public boolean handleMessage(LogicalMessageContext ctx) {
super.handleMessage(ctx);
try {
Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!outbound) {
LogicalMessage msg = ctx.getMessage();
Source source = msg.getPayload();
assertNotNull(source);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
return true;
}
};
TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
List<String> resp = handlerTest.ping();
assertNotNull(resp);
assertEquals("handle message was not invoked", 2, handler1.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 2, handler2.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 2, soapHandler1.getHandleMessageInvoked());
assertEquals("handle message was not invoked", 2, soapHandler2.getHandleMessageInvoked());
assertEquals("close must be called", 1, handler1.getCloseInvoked());
assertEquals("close must be called", 1, handler2.getCloseInvoked());
assertEquals("close must be called", 1, soapHandler1.getCloseInvoked());
assertEquals("close must be called", 1, soapHandler2.getCloseInvoked());
assertTrue(soapHandler2.getInvokeOrderOfClose() < soapHandler1.getInvokeOrderOfClose());
assertTrue(soapHandler1.getInvokeOrderOfClose() < handler2.getInvokeOrderOfClose());
assertTrue(handler2.getInvokeOrderOfClose() < handler1.getInvokeOrderOfClose());
// the server has encoded into the response the order in
// which the handlers have been invoked, parse it and make
// sure everything is ok expected order for inbound interceptors
String[] handlerNames = { "soapHandler4", "soapHandler3", "handler2", "handler1", "servant", "handler1", "handler2", "soapHandler3", "soapHandler4" };
assertEquals(handlerNames.length, resp.size());
Iterator<String> iter = resp.iterator();
for (String expected : handlerNames) {
assertEquals(expected, iter.next());
}
}
use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.
the class LogicalHandlerInterceptorTest method xtestReturnFalseClientSide.
// JAX-WS spec: If handler returns false, for a request-response MEP, if the message
// direction is reversed during processing of a request message then the message
// becomes a response message.
// NOTE: commented out as this has been covered by other tests.
@Test
@org.junit.Ignore
public void xtestReturnFalseClientSide() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new LogicalHandler<LogicalMessageContext>() {
public void close(MessageContext arg0) {
}
public boolean handleFault(LogicalMessageContext messageContext) {
return true;
}
public boolean handleMessage(LogicalMessageContext messageContext) {
LogicalMessage msg = messageContext.getMessage();
AddNumbersResponse resp = new AddNumbersResponse();
resp.setReturn(11);
msg.setPayload(resp, null);
return false;
}
});
HandlerChainInvoker invoker1 = new HandlerChainInvoker(list);
IMocksControl control1 = createNiceControl();
Binding binding1 = control1.createMock(Binding.class);
@SuppressWarnings("rawtypes") List<Handler> hList = CastUtils.cast(list);
expect(binding1.getHandlerChain()).andReturn(hList).anyTimes();
Exchange exchange1 = control1.createMock(Exchange.class);
expect(exchange1.get(HandlerChainInvoker.class)).andReturn(invoker1).anyTimes();
Message outMessage = new MessageImpl();
outMessage.setExchange(exchange1);
InterceptorChain chain = control1.createMock(InterceptorChain.class);
outMessage.setInterceptorChain(chain);
chain.abort();
EasyMock.expectLastCall();
MessageObserver observer = control1.createMock(MessageObserver.class);
expect(exchange1.get(MessageObserver.class)).andReturn(observer).anyTimes();
observer.onMessage(isA(Message.class));
EasyMock.expectLastCall();
control1.replay();
LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding1);
li.handleMessage(outMessage);
control1.verify();
}
Aggregations