Search in sources :

Example 11 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.

the class LogicalHandlerInterceptorTest method testInterceptSuccess.

@Test
public void testInterceptSuccess() {
    List<LogicalHandler<?>> list = new ArrayList<LogicalHandler<?>>();
    list.add(new LogicalHandler<LogicalMessageContext>() {

        public void close(MessageContext arg0) {
        }

        public boolean handleFault(LogicalMessageContext arg0) {
            return true;
        }

        public boolean handleMessage(LogicalMessageContext arg0) {
            return true;
        }
    });
    @SuppressWarnings("rawtypes") List<Handler> hList = CastUtils.cast(list);
    expect(binding.getHandlerChain()).andReturn(hList).anyTimes();
    expect(invoker.getLogicalHandlers()).andReturn(list);
    expect(message.getExchange()).andReturn(exchange).anyTimes();
    expect(message.get(Message.REQUESTOR_ROLE)).andReturn(Boolean.TRUE).anyTimes();
    expect(message.keySet()).andReturn(new TreeSet<String>()).anyTimes();
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker);
    expect(exchange.getOutMessage()).andReturn(message);
    expect(invoker.invokeLogicalHandlers(eq(true), isA(LogicalMessageContext.class))).andReturn(true);
    control.replay();
    LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding);
    assertEquals("unexpected phase", "pre-protocol-frontend", li.getPhase());
    li.handleMessage(message);
    control.verify();
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TreeSet(java.util.TreeSet) LogicalHandler(javax.xml.ws.handler.LogicalHandler) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) Test(org.junit.Test)

Example 12 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project jbossws-cxf by jbossws.

the class LogicalJAXBHandler method appendHandlerName.

@SuppressWarnings("unchecked")
private boolean appendHandlerName(MessageContext msgContext, String direction) {
    try {
        // Get the payload as Source
        LogicalMessageContext logicalContext = (LogicalMessageContext) msgContext;
        JAXBContext jaxb = JAXBContext.newInstance(Echo.class.getPackage().getName());
        Object payload = logicalContext.getMessage().getPayload(jaxb);
        JAXBElement<Object> jaxbElement = null;
        if (payload instanceof JAXBElement) {
            jaxbElement = (JAXBElement<Object>) payload;
            payload = jaxbElement.getValue();
        }
        if (payload instanceof Echo) {
            Echo echo = (Echo) payload;
            String value = echo.getString1();
            echo.setString1(value + ":" + direction + ":LogicalJAXBHandler");
        } else if (payload instanceof EchoResponse) {
            EchoResponse echo = (EchoResponse) payload;
            String value = echo.getResult();
            echo.setResult(value + ":" + direction + ":LogicalJAXBHandler");
        } else {
            throw new WebServiceException("Invalid payload type: " + payload);
        }
        if (jaxbElement != null) {
            jaxbElement.setValue(payload);
            payload = jaxbElement;
        }
        // Set the updated payload
        logicalContext.getMessage().setPayload(payload, jaxb);
        return true;
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new WebServiceException(ex);
    }
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) WebServiceException(javax.xml.ws.WebServiceException) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) WebServiceException(javax.xml.ws.WebServiceException)

Example 13 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project jbossws-cxf by jbossws.

the class LogicalSourceHandler method appendHandlerName.

private 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());
        Element element = DOMUtils.getFirstChildElement(root);
        String oldValue = DOMUtils.getTextContent(element);
        String newValue = oldValue + ":" + direction + ":LogicalSourceHandler";
        element.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)

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