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

The following examples show how to use javax.xml.ws.handler.LogicalMessageContext#getMessage() . 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: ModifyNumberHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
public final boolean handleMessage(LogicalMessageContext messageContext) {
    //System.out.println("LogicalMessageHandler handleMessage called");

    try {
        // 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);
        Object value = payload;
        if (payload instanceof JAXBElement) {
            value = ((JAXBElement<?>)payload).getValue();
        }

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

            int a = req.getArg0();
            req.setArg0(a * 10);
            msg.setPayload(payload, jaxbContext);
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }

}
 
Example 2
Source File: DispatchHandlerInvocationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean handleMessage(LogicalMessageContext ctx) {
    LogicalMessage msg = ctx.getMessage();

    Source payload = msg.getPayload();
    assertNotNull(payload);

    return true;
}
 
Example 3
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 4
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 5
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());
    }
}