Java Code Examples for javax.xml.ws.handler.LogicalMessageContext#get()

The following examples show how to use javax.xml.ws.handler.LogicalMessageContext#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalHandlerHandleMessageReturnFalseClientInBound() throws Exception {
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (!outbound) {
                return false;
            }

            return true;
        }
    };
    TestHandler<LogicalMessageContext> handler3 = new TestHandler<>(false);
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);

    addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, handler3, soapHandler1);

    handlerTest.ping();

    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(2, handler2.getHandleMessageInvoked());
    assertEquals(2, handler3.getHandleMessageInvoked());
    assertEquals(2, soapHandler1.getHandleMessageInvoked());

    assertEquals("close must be called", 1, handler1.getCloseInvoked());
    assertEquals("close must be called", 1, handler2.getCloseInvoked());
    assertEquals("close must be called", 1, handler3.getCloseInvoked());
    assertEquals("close must be called", 1, soapHandler1.getCloseInvoked());
    assertTrue(soapHandler1.getInvokeOrderOfClose()
               < handler3.getInvokeOrderOfClose());
    assertTrue(handler3.getInvokeOrderOfClose()
               < handler2.getInvokeOrderOfClose());
    assertTrue(handler2.getInvokeOrderOfClose()
               < handler1.getInvokeOrderOfClose());
}
 
Example 2
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalHandlerHandleMessageThrowsProtocolExceptionClientOutbound() throws Exception {
    final String clientHandlerMessage = "handler1 client side";

    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outbound) {
                throw new ProtocolException(clientHandlerMessage);
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);

    addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, soapHandler1);

    try {
        handlerTest.ping();
        fail("did not get expected exception");
    } catch (ProtocolException e) {
        assertEquals(clientHandlerMessage, e.getMessage());
    }

    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(1, handler2.getHandleMessageInvoked());
    assertEquals(0, soapHandler1.getHandleMessageInvoked());

    assertEquals(0, handler2.getHandleFaultInvoked());
    assertEquals(1, handler1.getHandleFaultInvoked());
    assertEquals(0, soapHandler1.getHandleFaultInvoked());

    assertEquals(1, handler1.getCloseInvoked());
    assertEquals(1, handler2.getCloseInvoked());
    assertEquals(0, soapHandler1.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose()
               < handler1.getInvokeOrderOfClose());
}
 
Example 3
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalHandlerHandleMessageThrowsProtocolExceptionClientInbound() throws Exception {
    final String clientHandlerMessage = "handler1 client side";

    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (!outbound) {
                throw new ProtocolException(clientHandlerMessage);
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);

    addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, soapHandler1);

    try {
        handlerTest.ping();
        //fail("did not get expected exception");
    } catch (ProtocolException e) {
        assertEquals(clientHandlerMessage, e.getMessage());
    }

    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(2, handler2.getHandleMessageInvoked());
    assertEquals(2, soapHandler1.getHandleMessageInvoked());

    assertEquals(0, handler2.getHandleFaultInvoked());
    assertEquals(0, handler1.getHandleFaultInvoked());
    assertEquals(0, soapHandler1.getHandleFaultInvoked());

    assertEquals(1, handler1.getCloseInvoked());
    assertEquals(1, handler2.getCloseInvoked());
    assertEquals(1, soapHandler1.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose()
               < handler1.getInvokeOrderOfClose());
}
 
Example 4
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalHandlerHandleMessageThrowsRuntimeExceptionClientOutbound() throws Exception {
    final String clientHandlerMessage = "handler1 client side";

    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outbound) {
                throw new RuntimeException(clientHandlerMessage);
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);

    addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, soapHandler1);

    try {
        handlerTest.ping();
        fail("did not get expected exception");
    } catch (RuntimeException e) {
        assertTrue(e.getMessage().contains(clientHandlerMessage));
    }

    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(1, handler2.getHandleMessageInvoked());
    assertEquals(0, soapHandler1.getHandleMessageInvoked());

    assertEquals(0, handler2.getHandleFaultInvoked());
    assertEquals(0, handler1.getHandleFaultInvoked());
    assertEquals(0, soapHandler1.getHandleFaultInvoked());

    assertEquals(1, handler1.getCloseInvoked());
    assertEquals(1, handler2.getCloseInvoked());
    assertEquals(0, soapHandler1.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose()
               < handler1.getInvokeOrderOfClose());
}
 
Example 5
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogicalHandlerHandleMessageThrowsRuntimeExceptionClientInbound() throws Exception {
    final String clientHandlerMessage = "handler1 client side";

    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {
        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (!outbound) {
                throw new RuntimeException(clientHandlerMessage);
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);

    addHandlersToChain((BindingProvider)handlerTest, handler1, handler2, soapHandler1);

    try {
        handlerTest.ping();
        fail("did not get expected exception");
    } catch (RuntimeException e) {
        assertTrue(e.getMessage().contains(clientHandlerMessage));
    }

    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(2, handler2.getHandleMessageInvoked());
    assertEquals(2, soapHandler1.getHandleMessageInvoked());

    assertEquals(0, handler2.getHandleFaultInvoked());
    assertEquals(0, handler1.getHandleFaultInvoked());
    assertEquals(0, soapHandler1.getHandleFaultInvoked());

    assertEquals(1, handler1.getCloseInvoked());
    assertEquals(1, handler2.getCloseInvoked());
    assertEquals(1, soapHandler1.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose()
               < handler1.getInvokeOrderOfClose());
}
 
Example 6
Source File: SmallNumberHandler.java    From cxf with Apache License 2.0 4 votes vote down vote up
public final boolean handleMessage(LogicalMessageContext messageContext) {
    System.out.println("LogicalMessageHandler handleMessage called");

    try {
        boolean outbound = (Boolean)messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outbound) {
            // get the LogicalMessage from our context
            //
            LogicalMessage msg = messageContext.getMessage();

            // check the payload, if its an AddNumbers request, we'll intervene
            //
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Object payload = msg.getPayload(jaxbContext);
            if (payload instanceof JAXBElement) {
                payload = ((JAXBElement<?>)payload).getValue();
            }

            if (payload instanceof AddNumbers) {
                AddNumbers req = (AddNumbers)payload;

                // now, if the arguments are small, let's do the calculation here
                //
                int a = req.getArg0();
                int b = req.getArg1();

                if (isSmall(a) && isSmall(b)) {
                    int answer = a + b;

                    //System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
                    // ok, we've done the calculation, so build the
                    // response and set it as the payload of the message

                    AddNumbersResponse resp = new AddNumbersResponse();
                    resp.setReturn(answer);
                    msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp),
                                   jaxbContext);

                    Source src = msg.getPayload();
                    msg.setPayload(src);

                    payload = msg.getPayload(jaxbContext);
                    if (payload instanceof JAXBElement) {
                        payload = ((JAXBElement<?>)payload).getValue();
                    }

                    AddNumbersResponse resp2 = (AddNumbersResponse)payload;
                    if (resp2 == resp) {
                        throw new WebServiceException("Shouldn't be the same object");
                    }

                    // finally, return false, indicating that request
                    // processing stops here and our answer will be
                    // returned to the client
                    return false;
                }
            }
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }

}
 
Example 7
Source File: SmallNumberHandler.java    From cxf with Apache License 2.0 4 votes vote down vote up
public final boolean handleMessage(LogicalMessageContext messageContext) {
    //System.out.println("LogicalMessageHandler handleMessage called");

    try {
        boolean outbound = (Boolean)messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outbound) {
            // get the LogicalMessage from our context
            LogicalMessage msg = messageContext.getMessage();

            // check the payload, if its an AddNumbers request, we'll intervene
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Object payload = msg.getPayload(jaxbContext);
            if (payload instanceof JAXBElement) {
                payload = ((JAXBElement<?>)payload).getValue();
            }

            if (payload instanceof AddNumbers) {
                AddNumbers req = (AddNumbers)payload;

                // now, if the arguments are small, let's do the calculation here
                int a = req.getArg0();
                int b = req.getArg1();

                if (isSmall(a) && isSmall(b)) {
                    int answer = a + b;

                    //System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
                    // ok, we've done the calculation, so build the
                    // response and set it as the payload of the message

                    AddNumbersResponse resp = new AddNumbersResponse();
                    resp.setReturn(answer);
                    msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp),
                                   jaxbContext);

                    Source src = msg.getPayload();
                    msg.setPayload(src);

                    payload = msg.getPayload(jaxbContext);
                    if (payload instanceof JAXBElement) {
                        payload = ((JAXBElement<?>)payload).getValue();
                    }

                    AddNumbersResponse resp2 = (AddNumbersResponse)payload;
                    if (resp2 == resp) {
                        throw new WebServiceException("Shouldn't be the same object");
                    }

                    // finally, return false, indicating that request
                    // processing stops here and our answer will be
                    // returned to the client
                    return false;
                }
            }
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }

}
 
Example 8
Source File: HandlerInvocationTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSOAPHandlerHandleMessageReturnTrueClient() throws Exception {
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(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());
    }
}