Java Code Examples for org.apache.cxf.interceptor.InterceptorChain#doIntercept()

The following examples show how to use org.apache.cxf.interceptor.InterceptorChain#doIntercept() . 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: RedeliveryQueueImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void redeliver() throws Exception {
    LOG.log(Level.INFO, "Redelivering ... for " + (1 + retries));
    String restartingPhase;
    if (message.getContent(Exception.class) != null) {
        message.removeContent(Exception.class);
        message.getExchange().put(Exception.class, null);

        // clean-up message for redelivery
        closeStreamResources();
        message.removeContent(Node.class);
    }


    InputStream is = null;
    CachedOutputStream cos = (CachedOutputStream)message.get(RMMessageConstants.SAVED_CONTENT);
    is = cos.getInputStream();
    message.setContent(InputStream.class, is);
    message = message.getExchange().getEndpoint().getBinding().createMessage(message);
    restartingPhase = Phase.POST_STREAM;
    // skip some interceptor chain phases for redelivery
    InterceptorChain chain = getRedeliveryInterceptorChain(message, restartingPhase);
    ListIterator<Interceptor<? extends Message>> iterator = chain.getIterator();
    while (iterator.hasNext()) {
        Interceptor<? extends Message> incept = iterator.next();
        if (incept.getClass().getName().equals(RMCaptureInInterceptor.class.getName())) {
            chain.remove(incept);
        }
    }
    message.getExchange().setInMessage(message);
    message.setInterceptorChain(chain);
    chain.doIntercept(message);
    Exception ex = message.getContent(Exception.class);
    if (null != ex) {
        throw ex;
    }
}
 
Example 3
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 4
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 5
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 6
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();
        }
    }
}