Java Code Examples for org.apache.cxf.message.Message#setInterceptorChain()

The following examples show how to use org.apache.cxf.message.Message#setInterceptorChain() . 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: LogicalHandlerInInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void handleAbort(Message message, W3CDOMStreamWriter writer) {
    message.getInterceptorChain().abort();

    if (!message.getExchange().isOneWay()) {
        //server side inbound
        Endpoint e = message.getExchange().getEndpoint();
        Message responseMsg = new MessageImpl();
        responseMsg.setExchange(message.getExchange());
        responseMsg = e.getBinding().createMessage(responseMsg);

        message.getExchange().setOutMessage(responseMsg);
        XMLStreamReader reader = message.getContent(XMLStreamReader.class);
        if (reader == null && writer != null) {
            reader = StaxUtils.createXMLStreamReader(writer.getDocument());
        }

        InterceptorChain chain = OutgoingChainInterceptor
            .getOutInterceptorChain(message.getExchange());
        responseMsg.setInterceptorChain(chain);
        responseMsg.put("LogicalHandlerInterceptor.INREADER", reader);

        chain.doIntercept(responseMsg);
    }
}
 
Example 2
Source File: JAXRSInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message createOutMessage(Message inMessage, Response r) {
    Endpoint e = inMessage.getExchange().getEndpoint();
    Message mout = e.getBinding().createMessage();
    mout.setContent(List.class, new MessageContentsList(r));
    mout.setExchange(inMessage.getExchange());
    mout.setInterceptorChain(
         OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
    inMessage.getExchange().setOutMessage(mout);
    if (r.getStatus() >= Response.Status.BAD_REQUEST.getStatusCode()) {
        inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
    }
    return mout;
}
 
Example 3
Source File: ThrottlingInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message createOutMessage(Message inMessage) {
    Endpoint e = inMessage.getExchange().getEndpoint();
    Message mout = e.getBinding().createMessage();
    mout.setExchange(inMessage.getExchange());
    mout.setInterceptorChain(
         OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
    inMessage.getExchange().setOutMessage(mout);
    inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
    return mout;
}
 
Example 4
Source File: ColocOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void invokeInboundChain(Exchange ex, Endpoint ep) {
    Message m = getInBoundMessage(ex);
    Message inMsg = ep.getBinding().createMessage();
    MessageImpl.copyContent(m, inMsg);

    //Copy Response Context to Client inBound Message
    //TODO a Context Filter Strategy required.
    inMsg.putAll(m);

    inMsg.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
    inMsg.setExchange(ex);

    Exception exc = inMsg.getContent(Exception.class);
    if (exc != null) {
        ex.setInFaultMessage(inMsg);
        ColocInFaultObserver observer = new ColocInFaultObserver(bus);
        observer.onMessage(inMsg);
    } else {
        //Handle Response
        ex.setInMessage(inMsg);
        PhaseManager pm = bus.getExtension(PhaseManager.class);
        SortedSet<Phase> phases = new TreeSet<>(pm.getInPhases());
        ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.PRE_INVOKE);

        InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, phases);
        inMsg.setInterceptorChain(chain);
        chain.doIntercept(inMsg);
    }
    ex.put(ClientImpl.FINISHED, Boolean.TRUE);
}
 
Example 5
Source File: ColocInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(Message msg) throws Fault {
    Exchange ex = msg.getExchange();
    if (ex.isOneWay()) {
        return;
    }

    Bus bus = ex.getBus();
    SortedSet<Phase> phases = new TreeSet<>(bus.getExtension(PhaseManager.class).getOutPhases());

    //TODO Set Coloc FaultObserver chain
    ColocUtil.setPhases(phases, Phase.SETUP, Phase.USER_LOGICAL);
    InterceptorChain chain = ColocUtil.getOutInterceptorChain(ex, phases);

    if (LOG.isLoggable(Level.FINER)) {
        LOG.finer("Processing Message at collocated endpoint.  Response message: " + msg);
    }

    //Initiate OutBound Processing
    BindingOperationInfo boi = ex.getBindingOperationInfo();
    Message outBound = ex.getOutMessage();
    if (boi != null) {
        outBound.put(MessageInfo.class,
                     boi.getOperationInfo().getOutput());
    }

    outBound.put(Message.INBOUND_MESSAGE, Boolean.FALSE);
    outBound.setInterceptorChain(chain);
    chain.doIntercept(outBound);
}
 
Example 6
Source File: ClientMessageObserver.java    From cxf with Apache License 2.0 5 votes vote down vote up
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();
            }
        }
    }
 
Example 7
Source File: OutgoingChainInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(Message message) {
    Exchange ex = message.getExchange();
    BindingOperationInfo binding = ex.getBindingOperationInfo();
    //if we get this far, we're going to be outputting some valid content, but we COULD
    //also be "echoing" some of the content from the input.   Thus, we need to
    //mark it as requiring the input to be cached.
    if (message.getExchange().get(CACHE_INPUT_PROPERTY) == null) {
        message.put(CACHE_INPUT_PROPERTY, Boolean.TRUE);
    }
    if (null != binding && null != binding.getOperationInfo() && binding.getOperationInfo().isOneWay()) {
        closeInput(message);
        return;
    }
    Message out = ex.getOutMessage();
    if (out != null) {
        try {
            getBackChannelConduit(message);
        } catch (IOException ioe) {
            throw new Fault(ioe);
        }
        if (binding != null) {
            out.put(MessageInfo.class, binding.getOperationInfo().getOutput());
            out.put(BindingMessageInfo.class, binding.getOutput());
        }

        InterceptorChain outChain = out.getInterceptorChain();
        if (outChain == null) {
            outChain = OutgoingChainInterceptor.getChain(ex, chainCache);
            out.setInterceptorChain(outChain);
        } else if (outChain.getState() == InterceptorChain.State.PAUSED) {
            outChain.resume();
            return;
        }
        outChain.doIntercept(out);

    }
}
 
Example 8
Source File: BasicAuthenticationInterceptorTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
private Message createEmptyMessage() {
    Exchange exchange = new ExchangeImpl();
    exchange.setInMessage(inMessageMock);
    exchange.setOutMessage(outMessageMock);
    exchange.setDestination(destinationMock);

    Message message = new MessageImpl();
    message.setExchange(exchange);
    message.setInterceptorChain(interceptorChainMock);
    return message;
}
 
Example 9
Source File: WSDLGetInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    String method = (String)message.get(Message.HTTP_REQUEST_METHOD);
    String query = (String)message.get(Message.QUERY_STRING);

    if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
        return;
    }

    String baseUri = (String)message.get(Message.REQUEST_URL);
    String ctx = (String)message.get(Message.PATH_INFO);

    WSDLGetUtils utils = (WSDLGetUtils)message.getContextualProperty(WSDLGetUtils.class.getName());
    if (utils == null) {
        utils = new WSDLGetUtils();
        message.put(WSDLGetUtils.class, utils);
    }
    Map<String, String> map = UrlUtils.parseQueryString(query);
    if (isRecognizedQuery(map)) {
        Document doc = getDocument(utils, message, baseUri, map, ctx);

        Endpoint e = message.getExchange().getEndpoint();
        Message mout = new MessageImpl();
        mout.setExchange(message.getExchange());
        mout = e.getBinding().createMessage(mout);
        mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange()));
        message.getExchange().setOutMessage(mout);

        mout.put(DOCUMENT_HOLDER, doc);
        mout.put(Message.CONTENT_TYPE, "text/xml");

        // just remove the interceptor which should not be used
        cleanUpOutInterceptors(mout);

        // notice this is being added after the purge above, don't swap the order!
        mout.getInterceptorChain().add(wsdlGetOutInterceptor);

        message.getExchange().put(TRANSFORM_SKIP, Boolean.TRUE);
        // skip the service executor and goto the end of the chain.
        message.getInterceptorChain().doInterceptStartingAt(
                message,
                OutgoingChainInterceptor.class.getName());
    }
}
 
Example 10
Source File: InternalContextUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
            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();
                    // 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 (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);

            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, "SERVER_TRANSPORT_REBASE_FAILURE_MSG", e);
        }
    }
}
 
Example 11
Source File: ColocMessageObserver.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void onMessage(Message m) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("Processing Message at collocated endpoint.  Request message: " + m);
        }
        Exchange ex = new ExchangeImpl();
        setExchangeProperties(ex, m);

        Message inMsg = endpoint.getBinding().createMessage();
        MessageImpl.copyContent(m, inMsg);

        //Copy Request Context to Server inBound Message
        //TODO a Context Filter Strategy required.
        inMsg.putAll(m);

        inMsg.put(COLOCATED, Boolean.TRUE);
        inMsg.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
        inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
        BindingOperationInfo boi = ex.getBindingOperationInfo();
        OperationInfo oi = boi != null ? boi.getOperationInfo() : null;
        if (oi != null) {
            inMsg.put(MessageInfo.class, oi.getInput());
        }
        ex.setInMessage(inMsg);
        inMsg.setExchange(ex);

        if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Build inbound interceptor chain.");
        }

        //Add all interceptors between USER_LOGICAL and INVOKE.
        SortedSet<Phase> phases = new TreeSet<>(bus.getExtension(PhaseManager.class).getInPhases());
        ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.INVOKE);
        InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, phases);
        chain.add(addColocInterceptors());
        inMsg.setInterceptorChain(chain);

        //Convert the coloc object type if necessary
        BindingOperationInfo bop = m.getExchange().getBindingOperationInfo();
        OperationInfo soi = bop != null ? bop.getOperationInfo() : null;
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)) {
                // converting source -> pojo
                ColocUtil.convertSourceToObject(inMsg);
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(inMsg);
            }
        }
        chain.doIntercept(inMsg);
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(ex.getOutMessage());
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertSourceToObject(ex.getOutMessage());
            }
        }
        //Set Server OutBound Message onto InBound Exchange.
        setOutBoundMessage(ex, m.getExchange());
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
 
Example 12
Source File: AbstractClient.java    From cxf with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 13
Source File: EjbInterceptor.java    From tomee with Apache License 2.0 4 votes vote down vote up
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
    Endpoint endpoint = this.exchange.get(Endpoint.class);
    Service service = endpoint.getService();
    Binding binding = ((JaxWsEndpointImpl) endpoint).getJaxwsBinding();

    this.exchange.put(InvocationContext.class, context);

    if (binding.getHandlerChain() == null || binding.getHandlerChain().isEmpty()) {
        // no handlers so let's just directly invoke the bean
        log.debug("No handlers found.");

        EjbMethodInvoker invoker = (EjbMethodInvoker) service.getInvoker();
        return invoker.directEjbInvoke(this.exchange, this.method, this.params);

    } else {
        // have handlers so have to run handlers now and redo data binding
        // as handlers can change the soap message
        log.debug("Handlers found.");

        Message inMessage = exchange.getInMessage();
        PhaseInterceptorChain chain = new PhaseInterceptorChain(bus.getExtension(PhaseManager.class).getInPhases());

        chain.setFaultObserver(endpoint.getOutFaultObserver());

        /*
         * Since we have to re-do data binding and the XMLStreamReader
         * contents are already consumed by prior data binding step
         * we have to reinitialize the XMLStreamReader from the SOAPMessage
         * created by SAAJInInterceptor.
         */
        if (inMessage instanceof SoapMessage) {
            try {
                reserialize((SoapMessage) inMessage);
            } catch (Exception e) {
                throw new ServerRuntimeException("Failed to reserialize soap message", e);
            }
        } else {
            // TODO: how to handle XML/HTTP binding?
        }

        this.exchange.setOutMessage(null);

        // install default interceptors
        chain.add(new ServiceInvokerInterceptor());
        //chain.add(new OutgoingChainInterceptor()); // it is already in the enclosing chain, if we add it there we are in the tx so we write the message in the tx!

        // See http://cwiki.apache.org/CXF20DOC/interceptors.html
        // install Holder and Wrapper interceptors
        chain.add(new WrapperClassInInterceptor());
        chain.add(new HolderInInterceptor());

        // install interceptors for handler processing
        chain.add(new MustUnderstandInterceptor());
        chain.add(new LogicalHandlerInInterceptor(binding));
        chain.add(new SOAPHandlerInterceptor(binding));

        // install data binding interceptors - todo: check we need it
        copyDataBindingInterceptors(chain, inMessage.getInterceptorChain());

        InterceptorChain oldChain = inMessage.getInterceptorChain();
        inMessage.setInterceptorChain(chain);
        try {
            chain.doIntercept(inMessage);
        } finally {
            inMessage.setInterceptorChain(oldChain);
        }

        // TODO: the result should be deserialized from SOAPMessage
        Object result = getResult();

        return result;
    }
}