Search in sources :

Example 6 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.

the class JAXWSMethodInvokerTest method testProviderInterpretNullAsOneway.

@Test
public void testProviderInterpretNullAsOneway() throws Throwable {
    NullableProviderService serviceObject = new NullableProviderService();
    Method serviceMethod = NullableProviderService.class.getMethod("invoke", new Class[] { Source.class });
    Exchange ex = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    inMessage.setInterceptorChain(new PhaseInterceptorChain(new TreeSet<Phase>()));
    ex.setInMessage(inMessage);
    inMessage.setExchange(ex);
    inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    // request-response with non-null response
    ex.setOneWay(false);
    MessageContentsList obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertEquals(1, obj.size());
    assertNotNull(obj.get(0));
    assertFalse(ex.isOneWay());
    // oneway with non-null response
    ex.setOneWay(true);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway not set so
    // default should be true
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway disabled
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.FALSE);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertEquals(1, obj.size());
    assertNull(obj.get(0));
    assertFalse(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway explicitly enabled
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.TRUE);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
}
Also used : Exchange(org.apache.cxf.message.Exchange) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) MessageContentsList(org.apache.cxf.message.MessageContentsList) TreeSet(java.util.TreeSet) StreamSource(javax.xml.transform.stream.StreamSource) Method(java.lang.reflect.Method) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 7 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain 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 8 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.

the class ClientMessageObserver method onMessage.

public void onMessage(Message m) {
    Message message = cfg.getConduitSelector().getEndpoint().getBinding().createMessage(m);
    message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    message.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
    PhaseInterceptorChain chain = AbstractClient.setupInInterceptorChain(cfg);
    message.setInterceptorChain(chain);
    message.getExchange().setInMessage(message);
    Bus bus = cfg.getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        // execute chain
        chain.doIntercept(message);
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
        synchronized (message.getExchange()) {
            message.getExchange().notifyAll();
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

Example 9 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.

the class AbstractClient method setupInInterceptorChain.

protected static PhaseInterceptorChain setupInInterceptorChain(ClientConfiguration cfg) {
    PhaseManager pm = cfg.getBus().getExtension(PhaseManager.class);
    List<Interceptor<? extends Message>> i1 = cfg.getBus().getInInterceptors();
    List<Interceptor<? extends Message>> i2 = cfg.getInInterceptors();
    List<Interceptor<? extends Message>> i3 = cfg.getConduitSelector().getEndpoint().getInInterceptors();
    PhaseInterceptorChain chain = new PhaseChainCache().get(pm.getInPhases(), i1, i2, i3);
    chain.add(new ClientResponseFilterInterceptor());
    return chain;
}
Also used : ClientResponseFilterInterceptor(org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) PhaseManager(org.apache.cxf.phase.PhaseManager) Message(org.apache.cxf.message.Message) StaxInEndingInterceptor(org.apache.cxf.interceptor.StaxInEndingInterceptor) ClientRequestFilterInterceptor(org.apache.cxf.jaxrs.client.spec.ClientRequestFilterInterceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ClientResponseFilterInterceptor(org.apache.cxf.jaxrs.client.spec.ClientResponseFilterInterceptor) AbstractOutDatabindingInterceptor(org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseChainCache(org.apache.cxf.phase.PhaseChainCache)

Example 10 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.

the class AbstractClient method createMessage.

protected Message createMessage(Object body, String httpMethod, MultivaluedMap<String, String> headers, URI currentURI, Exchange exchange, Map<String, Object> invocationContext, boolean proxy) {
    checkClosed();
    Message m = cfg.getConduitSelector().getEndpoint().getBinding().createMessage();
    m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    m.put(Message.INBOUND_MESSAGE, Boolean.FALSE);
    setRequestMethod(m, httpMethod);
    m.put(Message.PROTOCOL_HEADERS, headers);
    if (currentURI.isAbsolute() && currentURI.getScheme().startsWith(HTTP_SCHEME)) {
        m.put(Message.ENDPOINT_ADDRESS, currentURI.toString());
    } else {
        m.put(Message.ENDPOINT_ADDRESS, state.getBaseURI().toString());
    }
    Object requestURIProperty = cfg.getRequestContext().get(Message.REQUEST_URI);
    if (requestURIProperty == null) {
        m.put(Message.REQUEST_URI, currentURI.toString());
    } else {
        m.put(Message.REQUEST_URI, requestURIProperty.toString());
    }
    String ct = headers.getFirst(HttpHeaders.CONTENT_TYPE);
    m.put(Message.CONTENT_TYPE, ct);
    body = checkIfBodyEmpty(body, ct);
    setEmptyRequestPropertyIfNeeded(m, body);
    m.setContent(List.class, getContentsList(body));
    m.put(URITemplate.TEMPLATE_PARAMETERS, getState().getTemplates());
    PhaseInterceptorChain chain = setupOutInterceptorChain(cfg);
    chain.setFaultObserver(setupInFaultObserver(cfg));
    m.setInterceptorChain(chain);
    exchange = createExchange(m, exchange);
    exchange.put(Message.REST_MESSAGE, Boolean.TRUE);
    exchange.setOneWay("true".equals(headers.getFirst(Message.ONE_WAY_REQUEST)));
    exchange.put(Retryable.class, new RetryableImpl());
    // context
    setContexts(m, exchange, invocationContext, proxy);
    // setup conduit selector
    prepareConduitSelector(m, currentURI, proxy);
    return m;
}
Also used : PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message)

Aggregations

PhaseInterceptorChain (org.apache.cxf.phase.PhaseInterceptorChain)32 Message (org.apache.cxf.message.Message)24 Bus (org.apache.cxf.Bus)14 Interceptor (org.apache.cxf.interceptor.Interceptor)12 Endpoint (org.apache.cxf.endpoint.Endpoint)10 Exchange (org.apache.cxf.message.Exchange)10 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)9 IOException (java.io.IOException)8 PhaseManager (org.apache.cxf.phase.PhaseManager)8 TreeSet (java.util.TreeSet)7 MessageImpl (org.apache.cxf.message.MessageImpl)7 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)6 Phase (org.apache.cxf.phase.Phase)6 Test (org.junit.Test)6 Fault (org.apache.cxf.interceptor.Fault)5 InterceptorProvider (org.apache.cxf.interceptor.InterceptorProvider)5 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)5 SOAPMessage (javax.xml.soap.SOAPMessage)4 MessageContentsList (org.apache.cxf.message.MessageContentsList)4 URISyntaxException (java.net.URISyntaxException)3