Search in sources :

Example 81 with Exchange

use of org.apache.cxf.message.Exchange in project cxf by apache.

the class InternalContextUtils method rebaseResponse.

/**
 * Rebase response on replyTo
 *
 * @param reference the replyTo reference
 * @param inMAPs the inbound MAPs
 * @param inMessage the current message
 */
// CHECKSTYLE:OFF Max executable statement count limitation
public static void rebaseResponse(EndpointReferenceType reference, AddressingProperties inMAPs, final Message inMessage) {
    String namespaceURI = inMAPs.getNamespaceURI();
    if (!ContextUtils.retrievePartialResponseSent(inMessage)) {
        ContextUtils.storePartialResponseSent(inMessage);
        Exchange exchange = inMessage.getExchange();
        Message fullResponse = exchange.getOutMessage();
        Message partialResponse = ContextUtils.createMessage(exchange);
        ensurePartialResponseMAPs(partialResponse, namespaceURI);
        // ensure the inbound MAPs are available in the partial response
        // message (used to determine relatesTo etc.)
        ContextUtils.propogateReceivedMAPs(inMAPs, partialResponse);
        Destination target = inMessage.getDestination();
        if (target == null) {
            return;
        }
        try {
            if (reference == null) {
                reference = ContextUtils.getNoneEndpointReference();
            }
            Conduit backChannel = target.getBackChannel(inMessage);
            Exception exception = inMessage.getContent(Exception.class);
            // TODO:Look at how to refactor
            if (backChannel != null && !inMessage.getExchange().isOneWay() && ContextUtils.isFault(inMessage)) {
                // send the fault message to faultTo Endpoint
                exchange.setOutMessage(ContextUtils.createMessage(exchange));
                exchange.put(ConduitSelector.class, new NullConduitSelector());
                exchange.put("org.apache.cxf.http.no_io_exceptions", true);
                Destination destination = createDecoupledDestination(exchange, reference);
                exchange.setDestination(destination);
                if (ContextUtils.retrieveAsyncPostResponseDispatch(inMessage)) {
                    DelegatingInputStream in = inMessage.getContent(DelegatingInputStream.class);
                    if (in != null) {
                        in.cacheInput();
                    }
                    inMessage.getInterceptorChain().reset();
                    // cleanup pathinfo
                    if (inMessage.get(Message.PATH_INFO) != null) {
                        inMessage.remove(Message.PATH_INFO);
                    }
                    inMessage.getInterceptorChain().doIntercept(inMessage);
                }
                // send the partial response to requester
                partialResponse.put("forced.faultstring", "The server sent HTTP status code :" + inMessage.getExchange().get(Message.RESPONSE_CODE));
                partialResponse.setContent(Exception.class, exception);
                partialResponse.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, inMessage.get(Message.PROTOCOL_HEADERS));
                partialResponse.put(org.apache.cxf.message.Message.ENCODING, inMessage.get(Message.ENCODING));
                partialResponse.put(ContextUtils.ACTION, inMessage.get(ContextUtils.ACTION));
                partialResponse.put("javax.xml.ws.addressing.context.inbound", inMessage.get("javax.xml.ws.addressing.context.inbound"));
                partialResponse.put("javax.xml.ws.addressing.context.outbound", inMessage.get("javax.xml.ws.addressing.context.outbound"));
                exchange.setOutMessage(partialResponse);
                PhaseInterceptorChain newChian = ((PhaseInterceptorChain) inMessage.getInterceptorChain()).cloneChain();
                partialResponse.setInterceptorChain(newChian);
                exchange.setDestination(target);
                exchange.setOneWay(false);
                exchange.put(ConduitSelector.class, new PreexistingConduitSelector(backChannel, exchange.getEndpoint()));
                if (newChian != null && !newChian.doIntercept(partialResponse) && partialResponse.getContent(Exception.class) != null) {
                    if (partialResponse.getContent(Exception.class) instanceof Fault) {
                        throw (Fault) partialResponse.getContent(Exception.class);
                    }
                    throw new Fault(partialResponse.getContent(Exception.class));
                }
                return;
            }
            if (backChannel != null) {
                partialResponse.put(Message.PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE);
                partialResponse.put(Message.EMPTY_PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE);
                boolean robust = MessageUtils.getContextualBoolean(inMessage, Message.ROBUST_ONEWAY, false);
                if (robust) {
                    BindingOperationInfo boi = exchange.getBindingOperationInfo();
                    // insert the executor in the exchange to fool the OneWayProcessorInterceptor
                    exchange.put(Executor.class, getExecutor(inMessage));
                    // pause dispatch on current thread and resume...
                    inMessage.getInterceptorChain().pause();
                    inMessage.getInterceptorChain().resume();
                    MessageObserver faultObserver = inMessage.getInterceptorChain().getFaultObserver();
                    if (null != inMessage.getContent(Exception.class) && null != faultObserver) {
                        // return the fault over the response fault channel
                        inMessage.getExchange().setOneWay(false);
                        faultObserver.onMessage(inMessage);
                        return;
                    }
                    // restore the BOI for the partial response handling
                    exchange.put(BindingOperationInfo.class, boi);
                }
                // set up interceptor chains and send message
                InterceptorChain chain = fullResponse != null ? fullResponse.getInterceptorChain() : OutgoingChainInterceptor.getOutInterceptorChain(exchange);
                exchange.setOutMessage(partialResponse);
                partialResponse.setInterceptorChain(chain);
                exchange.put(ConduitSelector.class, new PreexistingConduitSelector(backChannel, exchange.getEndpoint()));
                if (ContextUtils.retrieveAsyncPostResponseDispatch(inMessage) && !robust) {
                    // need to suck in all the data from the input stream as
                    // the transport might discard any data on the stream when this
                    // thread unwinds or when the empty response is sent back
                    DelegatingInputStream in = inMessage.getContent(DelegatingInputStream.class);
                    if (in != null) {
                        in.cacheInput();
                    }
                }
                if (chain != null && !chain.doIntercept(partialResponse) && partialResponse.getContent(Exception.class) != null) {
                    if (partialResponse.getContent(Exception.class) instanceof Fault) {
                        throw (Fault) partialResponse.getContent(Exception.class);
                    }
                    throw new Fault(partialResponse.getContent(Exception.class));
                }
                if (chain != null) {
                    chain.reset();
                }
                exchange.put(ConduitSelector.class, new NullConduitSelector());
                if (fullResponse == null) {
                    fullResponse = ContextUtils.createMessage(exchange);
                }
                exchange.setOutMessage(fullResponse);
                Destination destination = createDecoupledDestination(exchange, reference);
                exchange.setDestination(destination);
                if (ContextUtils.retrieveAsyncPostResponseDispatch(inMessage) && !robust) {
                    // cleanup pathinfo
                    if (inMessage.get(Message.PATH_INFO) != null) {
                        inMessage.remove(Message.PATH_INFO);
                    }
                    // pause dispatch on current thread ...
                    inMessage.getInterceptorChain().pause();
                    try {
                        // ... and resume on executor thread
                        getExecutor(inMessage).execute(new Runnable() {

                            public void run() {
                                inMessage.getInterceptorChain().resume();
                            }
                        });
                    } catch (RejectedExecutionException e) {
                        LOG.warning("Executor queue is full, use the caller thread." + "  Users can specify a larger executor queue to avoid this.");
                        // only block the thread if the prop is unset or set to false, otherwise let it go
                        if (!MessageUtils.getContextualBoolean(inMessage, "org.apache.cxf.oneway.rejected_execution_exception")) {
                            // the executor queue is full, so run the task in the caller thread
                            inMessage.getInterceptorChain().resume();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, "SERVER_TRANSPORT_REBASE_FAILURE_MSG", e);
        }
    }
}
Also used : Destination(org.apache.cxf.transport.Destination) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault) WebFault(javax.xml.ws.WebFault) PreexistingConduitSelector(org.apache.cxf.endpoint.PreexistingConduitSelector) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NullConduitSelector(org.apache.cxf.endpoint.NullConduitSelector) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Conduit(org.apache.cxf.transport.Conduit) DelegatingInputStream(org.apache.cxf.io.DelegatingInputStream)

Example 82 with Exchange

use of org.apache.cxf.message.Exchange in project cxf by apache.

the class MAPAggregatorImpl method getReplyTo.

private EndpointReferenceType getReplyTo(Message message, EndpointReferenceType originalReplyTo) {
    Exchange exchange = message.getExchange();
    Endpoint info = exchange.getEndpoint();
    if (info == null) {
        return originalReplyTo;
    }
    synchronized (info) {
        EndpointInfo ei = info.getEndpointInfo();
        Destination dest = ei.getProperty(DECOUPLED_DESTINATION, Destination.class);
        if (dest == null) {
            dest = createDecoupledDestination(message);
            if (dest != null) {
                info.getEndpointInfo().setProperty(DECOUPLED_DESTINATION, dest);
            }
        }
        if (dest != null) {
            // if the decoupled endpoint context prop is set and the address is relative, return the absolute url.
            final String replyTo = dest.getAddress().getAddress().getValue();
            if (replyTo.startsWith("/")) {
                String debase = (String) message.getContextualProperty(WSAContextUtils.DECOUPLED_ENDPOINT_BASE_PROPERTY);
                if (debase != null) {
                    return EndpointReferenceUtils.getEndpointReference(debase + replyTo);
                }
            }
            return dest.getAddress();
        }
    }
    return originalReplyTo;
}
Also used : Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Destination(org.apache.cxf.transport.Destination) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 83 with Exchange

use of org.apache.cxf.message.Exchange in project cxf by apache.

the class MAPAggregatorTest method setUpMessage.

private Message setUpMessage(SetupMessageArgs args) throws Exception {
    Message message = getMessage();
    Exchange exchange = getExchange();
    setUpOutbound(message, exchange, args.outbound, args.fault);
    setUpMessageProperty(message, REQUESTOR_ROLE, Boolean.valueOf(args.requestor));
    if (args.outbound && args.requestor) {
        if (args.usingAddressing) {
            setUpConduit(message, exchange);
        }
        setUpUsingAddressing(message, exchange, args.usingAddressing);
        if (args.usingAddressing) {
            setUpRequestor(message, exchange, args.oneway, args.mapsInContext, args.decoupled, args.zeroLengthAction);
        }
    } else if (!args.requestor) {
        SetupResponderArgs srArgs = new SetupResponderArgs();
        srArgs.oneway = args.oneway;
        srArgs.outbound = args.outbound;
        srArgs.decoupled = args.decoupled;
        srArgs.zeroLengthAction = args.zeroLengthAction;
        srArgs.fault = args.fault;
        srArgs.noMessageId = args.noMessageId;
        Endpoint endpoint = control.createMock(Endpoint.class);
        exchange.getEndpoint();
        EasyMock.expectLastCall().andReturn(endpoint).anyTimes();
        setUpResponder(message, exchange, srArgs, endpoint);
        endpoint.getOutInterceptors();
        EasyMock.expectLastCall().andReturn(new ArrayList<Interceptor<? extends Message>>()).anyTimes();
        Service serv = control.createMock(Service.class);
        endpoint.getService();
        EasyMock.expectLastCall().andReturn(serv).anyTimes();
        serv.getOutInterceptors();
        EasyMock.expectLastCall().andReturn(new ArrayList<Interceptor<? extends Message>>()).anyTimes();
    }
    control.replay();
    return message;
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) Endpoint(org.apache.cxf.endpoint.Endpoint) Service(org.apache.cxf.service.Service) Interceptor(org.apache.cxf.interceptor.Interceptor)

Example 84 with Exchange

use of org.apache.cxf.message.Exchange in project cxf by apache.

the class MAPAggregatorTest method testTwoWayRequestWithReplyToNone.

@Test(expected = SoapFault.class)
public void testTwoWayRequestWithReplyToNone() throws Exception {
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    exchange.setOutMessage(message);
    message.setExchange(exchange);
    setUpMessageProperty(message, REQUESTOR_ROLE, Boolean.FALSE);
    AddressingProperties maps = new AddressingProperties();
    EndpointReferenceType replyTo = new EndpointReferenceType();
    replyTo.setAddress(ContextUtils.getAttributedURI(Names.WSA_NONE_ADDRESS));
    maps.setReplyTo(replyTo);
    AttributedURIType id = ContextUtils.getAttributedURI("urn:uuid:12345");
    maps.setMessageID(id);
    maps.setAction(ContextUtils.getAttributedURI(""));
    setUpMessageProperty(message, ADDRESSING_PROPERTIES_OUTBOUND, maps);
    setUpMessageProperty(message, "org.apache.cxf.ws.addressing.map.fault.name", "NoneAddress");
    aggregator.mediate(message, false);
}
Also used : Exchange(org.apache.cxf.message.Exchange) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) Message(org.apache.cxf.message.Message) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 85 with Exchange

use of org.apache.cxf.message.Exchange in project cxf by apache.

the class DecoupledFaultHandlerTest method testOnewayFault.

@Test
public void testOnewayFault() {
    DecoupledFaultHandler handler = new DecoupledFaultHandler() {

        protected Destination createDecoupledDestination(Exchange exchange, EndpointReferenceType epr) {
            assertEquals("http://bar", epr.getAddress().getValue());
            return EasyMock.createMock(Destination.class);
        }
    };
    SoapMessage message = new SoapMessage(new MessageImpl());
    QName qname = new QName("http://cxf.apache.org/mustunderstand", "TestMU");
    message.getHeaders().add(new Header(qname, new Object()));
    AddressingProperties maps = new AddressingProperties();
    EndpointReferenceType faultTo = new EndpointReferenceType();
    faultTo.setAddress(new AttributedURIType());
    faultTo.getAddress().setValue("http://bar");
    maps.setFaultTo(faultTo);
    message.put(ContextUtils.getMAPProperty(false, false, false), maps);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    exchange.setInMessage(message);
    exchange.setOneWay(true);
    handler.handleFault(message);
    assertTrue(message.getHeaders().isEmpty());
    assertFalse(exchange.isOneWay());
    assertSame(message, exchange.getOutMessage());
    assertNotNull(exchange.getDestination());
}
Also used : Exchange(org.apache.cxf.message.Exchange) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Test(org.junit.Test)

Aggregations

Exchange (org.apache.cxf.message.Exchange)272 Message (org.apache.cxf.message.Message)151 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)131 MessageImpl (org.apache.cxf.message.MessageImpl)118 Test (org.junit.Test)93 Endpoint (org.apache.cxf.endpoint.Endpoint)66 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)50 QName (javax.xml.namespace.QName)42 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)33 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)32 Bus (org.apache.cxf.Bus)30 Fault (org.apache.cxf.interceptor.Fault)27 AddressingProperties (org.apache.cxf.ws.addressing.AddressingProperties)26 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)25 Conduit (org.apache.cxf.transport.Conduit)25 MessageContentsList (org.apache.cxf.message.MessageContentsList)22 SOAPMessage (javax.xml.soap.SOAPMessage)19 XMLStreamReader (javax.xml.stream.XMLStreamReader)19 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)19