Search in sources :

Example 36 with MessageObserver

use of org.apache.cxf.transport.MessageObserver 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)

Example 37 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class SOAPHandlerInterceptor method handleAbort.

private void handleAbort(SoapMessage message, MessageContext context) {
    if (isRequestor(message)) {
        // client side outbound
        if (getInvoker(message).isOutbound()) {
            message.getInterceptorChain().abort();
            MessageObserver observer = message.getExchange().get(MessageObserver.class);
            if (!message.getExchange().isOneWay() && observer != null) {
                Endpoint e = message.getExchange().getEndpoint();
                Message responseMsg = new MessageImpl();
                responseMsg.setExchange(message.getExchange());
                responseMsg = e.getBinding().createMessage(responseMsg);
                // the request message becomes the response message
                message.getExchange().setInMessage(responseMsg);
                SOAPMessage soapMessage = ((SOAPMessageContext) context).getMessage();
                if (soapMessage != null) {
                    responseMsg.setContent(SOAPMessage.class, soapMessage);
                    XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(soapMessage);
                    responseMsg.setContent(XMLStreamReader.class, xmlReader);
                }
                responseMsg.put(InterceptorChain.STARTING_AT_INTERCEPTOR_ID, SOAPHandlerInterceptor.class.getName());
                observer.onMessage(responseMsg);
            }
        // We dont call onCompletion here, as onCompletion will be called by inbound
        // LogicalHandlerInterceptor
        } else {
            // client side inbound - Normal handler message processing
            // stops, but the inbound interceptor chain still continues, dispatch the message
            // By onCompletion here, we can skip following Logical handlers
            onCompletion(message);
        }
    } else {
        if (!getInvoker(message).isOutbound()) {
            // server side inbound
            message.getInterceptorChain().abort();
            Endpoint e = message.getExchange().getEndpoint();
            if (!message.getExchange().isOneWay()) {
                Message responseMsg = new MessageImpl();
                responseMsg.setExchange(message.getExchange());
                responseMsg = e.getBinding().createMessage(responseMsg);
                message.getExchange().setOutMessage(responseMsg);
                SOAPMessage soapMessage = ((SOAPMessageContext) context).getMessage();
                responseMsg.setContent(SOAPMessage.class, soapMessage);
                InterceptorChain chain = OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange());
                responseMsg.setInterceptorChain(chain);
                // so the idea of starting interceptor chain from any
                // specified point does not work
                // well for outbound case, as many outbound interceptors
                // have their ending interceptors.
                // For example, we can not skip MessageSenderInterceptor.
                chain.doInterceptStartingAfter(responseMsg, SoapPreProtocolOutInterceptor.class.getName());
            }
        } else {
        // server side outbound - Normal handler message processing
        // stops, but still continue the outbound interceptor chain, dispatch the message
        }
    }
}
Also used : InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) MessageObserver(org.apache.cxf.transport.MessageObserver) XMLStreamReader(javax.xml.stream.XMLStreamReader) SoapPreProtocolOutInterceptor(org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor) Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 38 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class CorbaDestination method setMessageObserver.

public synchronized void setMessageObserver(MessageObserver observer) {
    if (observer != incomingObserver) {
        MessageObserver old = incomingObserver;
        incomingObserver = observer;
        if (observer != null) {
            if (old == null) {
                activate();
            }
        } else {
            if (old != null) {
                deactivate();
            }
        }
    }
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver)

Example 39 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class MtomServerTest method servStatic.

/**
 * Serve static file
 */
private void servStatic(final URL resource, final String add) throws Exception {
    Bus bus = getStaticBus();
    DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class);
    DestinationFactory df = dfm.getDestinationFactory("http://cxf.apache.org/transports/http/configuration");
    EndpointInfo ei = new EndpointInfo();
    ei.setAddress(add);
    Destination d = df.getDestination(ei, bus);
    d.setMessageObserver(new MessageObserver() {

        public void onMessage(Message message) {
            try {
                // HTTP seems to need this right now...
                ExchangeImpl ex = new ExchangeImpl();
                ex.setInMessage(message);
                Conduit backChannel = message.getDestination().getBackChannel(message);
                MessageImpl res = new MessageImpl();
                ex.setOutMessage(res);
                res.put(Message.CONTENT_TYPE, "text/xml");
                backChannel.prepare(res);
                OutputStream out = res.getContent(OutputStream.class);
                InputStream is = resource.openStream();
                IOUtils.copy(is, out, 2048);
                out.flush();
                out.close();
                is.close();
                backChannel.close(res);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : Bus(org.apache.cxf.Bus) DestinationFactory(org.apache.cxf.transport.DestinationFactory) Destination(org.apache.cxf.transport.Destination) MessageObserver(org.apache.cxf.transport.MessageObserver) DestinationFactoryManager(org.apache.cxf.transport.DestinationFactoryManager) Message(org.apache.cxf.message.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Conduit(org.apache.cxf.transport.Conduit) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 40 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project camel by apache.

the class CamelDestinationTest method testRoundTripDestination.

@Test
public void testRoundTripDestination() throws Exception {
    inMessage = null;
    EndpointInfo conduitEpInfo = new EndpointInfo();
    conduitEpInfo.setAddress("camel://direct:Producer");
    // set up the conduit send to be true
    CamelConduit conduit = setupCamelConduit(conduitEpInfo, true, false);
    final Message outMessage = new MessageImpl();
    endpointInfo.setAddress("camel://direct:EndpointA");
    final CamelDestination destination = setupCamelDestination(endpointInfo, true);
    // set up MessageObserver for handling the conduit message
    MessageObserver observer = new MessageObserver() {

        public void onMessage(Message m) {
            try {
                Exchange exchange = new ExchangeImpl();
                exchange.setInMessage(m);
                m.setExchange(exchange);
                verifyReceivedMessage(m, "HelloWorld");
                //verifyHeaders(m, outMessage);
                // setup the message for
                Conduit backConduit;
                backConduit = getBackChannel(destination, m);
                // wait for the message to be got from the conduit
                Message replyMessage = new MessageImpl();
                sendoutMessage(backConduit, replyMessage, true, "HelloWorld Response");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    MockEndpoint error = context.getEndpoint("mock:error", MockEndpoint.class);
    error.expectedMessageCount(0);
    //this call will active the camelDestination
    destination.setMessageObserver(observer);
    // set is one way false for get response from destination
    // need to use another thread to send the request message
    sendoutMessage(conduit, outMessage, false, "HelloWorld");
    // wait for the message to be got from the destination,
    // create the thread to handler the Destination incoming message
    verifyReceivedMessage(inMessage, "HelloWorld Response");
    error.assertIsSatisfied();
    destination.shutdown();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Conduit(org.apache.cxf.transport.Conduit) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) Test(org.junit.Test)

Aggregations

MessageObserver (org.apache.cxf.transport.MessageObserver)43 Message (org.apache.cxf.message.Message)34 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)21 Conduit (org.apache.cxf.transport.Conduit)14 IOException (java.io.IOException)13 Exchange (org.apache.cxf.message.Exchange)12 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)11 Test (org.junit.Test)11 MessageImpl (org.apache.cxf.message.MessageImpl)10 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)10 Bus (org.apache.cxf.Bus)8 QName (javax.xml.namespace.QName)7 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)6 Fault (org.apache.cxf.interceptor.Fault)5 Destination (org.apache.cxf.transport.Destination)5 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 HashMap (java.util.HashMap)4 List (java.util.List)4 ExtensionManagerBus (org.apache.cxf.bus.extension.ExtensionManagerBus)4