Search in sources :

Example 6 with LogicalMessageContext

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) WebServiceException(javax.xml.ws.WebServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) WebServiceException(javax.xml.ws.WebServiceException)

Example 7 with LogicalMessageContext

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());
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) PingException(org.apache.handler_test.PingException) ProtocolException(javax.xml.ws.ProtocolException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) TransformerException(javax.xml.transform.TransformerException) WebServiceException(javax.xml.ws.WebServiceException) Source(javax.xml.transform.Source) StringWriter(java.io.StringWriter) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 8 with LogicalMessageContext

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());
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) JAXBContext(javax.xml.bind.JAXBContext) PingResponse(org.apache.handler_test.types.PingResponse) LogicalMessage(javax.xml.ws.LogicalMessage) PingException(org.apache.handler_test.PingException) ProtocolException(javax.xml.ws.ProtocolException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) TransformerException(javax.xml.transform.TransformerException) WebServiceException(javax.xml.ws.WebServiceException) PingOneWay(org.apache.handler_test.types.PingOneWay) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 9 with LogicalMessageContext

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());
    }
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) Source(javax.xml.transform.Source) PingException(org.apache.handler_test.PingException) ProtocolException(javax.xml.ws.ProtocolException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) TransformerException(javax.xml.transform.TransformerException) WebServiceException(javax.xml.ws.WebServiceException) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 10 with LogicalMessageContext

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();
}
Also used : Binding(javax.xml.ws.Binding) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageObserver(org.apache.cxf.transport.MessageObserver) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)13 WebServiceException (javax.xml.ws.WebServiceException)9 Test (org.junit.Test)9 HandlerTest (org.apache.handler_test.HandlerTest)7 Source (javax.xml.transform.Source)6 LogicalMessage (javax.xml.ws.LogicalMessage)6 TransformerException (javax.xml.transform.TransformerException)5 ProtocolException (javax.xml.ws.ProtocolException)5 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)5 PingException (org.apache.handler_test.PingException)5 JAXBContext (javax.xml.bind.JAXBContext)4 TransformerFactory (javax.xml.transform.TransformerFactory)4 StreamResult (javax.xml.transform.stream.StreamResult)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)3 PingOneWay (org.apache.handler_test.types.PingOneWay)3 Element (org.w3c.dom.Element)3 ArrayList (java.util.ArrayList)2