Search in sources :

Example 1 with PhaseInterceptorChain

use of org.apache.cxf.phase.PhaseInterceptorChain in project tesb-rt-se by Talend.

the class DemoInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    PhaseInterceptorChain pic = (PhaseInterceptorChain) message.getInterceptorChain();
    if (!somethingMayHaveChanged(pic)) {
        return;
    }
    System.out.println("Phase: " + phase);
    System.out.println("        out: " + MessageUtils.isOutbound(message));
    System.out.println("   contents: " + message.getContentFormats());
    System.out.println("       keys: " + message.keySet());
    XMLStreamReader reader = message.getContent(XMLStreamReader.class);
    if (reader != null) {
        // On an incoming message, once we have the XMLStreamReader,
        // we can get the current event and the element Name
        int event = reader.getEventType();
        switch(event) {
            case XMLStreamReader.START_ELEMENT:
            case XMLStreamReader.END_ELEMENT:
                System.out.println("      reader: " + event + "   qname: " + reader.getName());
                break;
            default:
                System.out.println("      reader: " + event);
        }
    }
    List<?> params = message.getContent(List.class);
    if (params != null) {
        System.out.println("      params: " + params);
    }
    System.out.println("      chain: ");
    printInterceptorChain(pic);
    System.out.println();
    System.out.println();
}
Also used : PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) XMLStreamReader(javax.xml.stream.XMLStreamReader)

Example 2 with PhaseInterceptorChain

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

the class ClientImpl method onMessage.

public void onMessage(Message message) {
    if (bus == null) {
        throw new IllegalStateException("Message received on a Client that has been closed or destroyed.");
    }
    Endpoint endpoint = message.getExchange().getEndpoint();
    if (endpoint == null) {
        // in this case correlation will occur outside the transport,
        // however there's a possibility that the endpoint may have been
        // rebased in the meantime, so that the response will be mediated
        // via a set of in interceptors provided by a *different* endpoint
        // 
        endpoint = getConduitSelector().getEndpoint();
        message.getExchange().put(Endpoint.class, endpoint);
    }
    message = endpoint.getBinding().createMessage(message);
    message.getExchange().setInMessage(message);
    message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    message.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
    PhaseManager pm = bus.getExtension(PhaseManager.class);
    List<Interceptor<? extends Message>> i1 = bus.getInInterceptors();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Interceptors contributed by bus: " + i1);
    }
    List<Interceptor<? extends Message>> i2 = getInInterceptors();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Interceptors contributed by client: " + i2);
    }
    List<Interceptor<? extends Message>> i3 = endpoint.getInInterceptors();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Interceptors contributed by endpoint: " + i3);
    }
    List<Interceptor<? extends Message>> i4 = endpoint.getBinding().getInInterceptors();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Interceptors contributed by binding: " + i4);
    }
    PhaseInterceptorChain chain;
    if (endpoint.getService().getDataBinding() instanceof InterceptorProvider) {
        InterceptorProvider p = (InterceptorProvider) endpoint.getService().getDataBinding();
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Interceptors contributed by databinding: " + p.getInInterceptors());
        }
        chain = inboundChainCache.get(pm.getInPhases(), i1, i2, i3, i4, p.getInInterceptors());
    } else {
        chain = inboundChainCache.get(pm.getInPhases(), i1, i2, i3, i4);
    }
    message.setInterceptorChain(chain);
    chain.setFaultObserver(outFaultObserver);
    modifyChain(chain, message, true);
    modifyChain(chain, message.getExchange().getOutMessage(), true);
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    // execute chain
    ClientCallback callback = message.getExchange().get(ClientCallback.class);
    try {
        if (callback != null) {
            if (callback.isCancelled()) {
                completeExchange(message.getExchange());
                return;
            }
            callback.start(message);
        }
        String startingAfterInterceptorID = (String) message.get(InterceptorChain.STARTING_AFTER_INTERCEPTOR_ID);
        String startingInterceptorID = (String) message.get(InterceptorChain.STARTING_AT_INTERCEPTOR_ID);
        if (startingAfterInterceptorID != null) {
            chain.doInterceptStartingAfter(message, startingAfterInterceptorID);
        } else if (startingInterceptorID != null) {
            chain.doInterceptStartingAt(message, startingInterceptorID);
        } else if (message.getContent(Exception.class) != null) {
            outFaultObserver.onMessage(message);
        } else {
            callback = message.getExchange().get(ClientCallback.class);
            if (callback != null && !isPartialResponse(message)) {
                try {
                    chain.doIntercept(message);
                } catch (Throwable error) {
                    // so that asyn callback handler get chance to
                    // handle non-runtime exceptions
                    message.getExchange().setInMessage(message);
                    Map<String, Object> resCtx = CastUtils.cast((Map<?, ?>) message.getExchange().getOutMessage().get(Message.INVOCATION_CONTEXT));
                    resCtx = CastUtils.cast((Map<?, ?>) resCtx.get(RESPONSE_CONTEXT));
                    if (resCtx != null) {
                        setResponseContext(resCtx);
                    }
                    // remove callback so that it won't be invoked twice
                    callback = message.getExchange().remove(ClientCallback.class);
                    if (callback != null) {
                        callback.handleException(resCtx, error);
                    }
                }
            } else {
                chain.doIntercept(message);
            }
        }
        callback = message.getExchange().get(ClientCallback.class);
        if (callback == null || isPartialResponse(message)) {
            return;
        }
        // remove callback so that it won't be invoked twice
        callback = message.getExchange().remove(ClientCallback.class);
        if (callback != null) {
            message.getExchange().setInMessage(message);
            Map<String, Object> resCtx = CastUtils.cast((Map<?, ?>) message.getExchange().getOutMessage().get(Message.INVOCATION_CONTEXT));
            resCtx = CastUtils.cast((Map<?, ?>) resCtx.get(RESPONSE_CONTEXT));
            if (resCtx != null && responseContext != null) {
                setResponseContext(resCtx);
            }
            try {
                Object[] obj = processResult(message, message.getExchange(), null, resCtx);
                callback.handleResponse(resCtx, obj);
            } catch (Throwable ex) {
                callback.handleException(resCtx, ex);
            }
        }
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        synchronized (message.getExchange()) {
            if (!isPartialResponse(message) || message.getContent(Exception.class) != null) {
                message.getExchange().put(FINISHED, Boolean.TRUE);
                message.getExchange().setInMessage(message);
                message.getExchange().notifyAll();
            }
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) PhaseManager(org.apache.cxf.phase.PhaseManager) Message(org.apache.cxf.message.Message) InterceptorProvider(org.apache.cxf.interceptor.InterceptorProvider) AbstractBasicInterceptorProvider(org.apache.cxf.interceptor.AbstractBasicInterceptorProvider) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UncheckedException(org.apache.cxf.common.i18n.UncheckedException) Interceptor(org.apache.cxf.interceptor.Interceptor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Example 3 with PhaseInterceptorChain

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

the class AbstractFaultChainInitiatorObserver method onMessage.

public void onMessage(Message message) {
    assert null != message;
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Exchange exchange = message.getExchange();
        Message faultMessage;
        if (isOutboundObserver()) {
            Exception ex = message.getContent(Exception.class);
            if (!(ex instanceof Fault)) {
                ex = new Fault(ex);
            }
            FaultMode mode = message.get(FaultMode.class);
            faultMessage = exchange.getOutMessage();
            if (null == faultMessage) {
                faultMessage = new MessageImpl();
                faultMessage.setExchange(exchange);
                faultMessage = exchange.getEndpoint().getBinding().createMessage(faultMessage);
            }
            faultMessage.setContent(Exception.class, ex);
            if (null != mode) {
                faultMessage.put(FaultMode.class, mode);
            }
            // CXF-3981
            if (message.get("javax.xml.ws.addressing.context.inbound") != null) {
                faultMessage.put("javax.xml.ws.addressing.context.inbound", message.get("javax.xml.ws.addressing.context.inbound"));
            }
            exchange.setOutMessage(null);
            exchange.setOutFaultMessage(faultMessage);
            if (message.get(BindingFaultInfo.class) != null) {
                faultMessage.put(BindingFaultInfo.class, message.get(BindingFaultInfo.class));
            }
        } else {
            faultMessage = message;
            exchange.setInMessage(null);
            exchange.setInFaultMessage(faultMessage);
        }
        // setup chain
        PhaseInterceptorChain chain = new PhaseInterceptorChain(getPhases());
        initializeInterceptors(faultMessage.getExchange(), chain);
        faultMessage.setInterceptorChain(chain);
        try {
            chain.doIntercept(faultMessage);
        } catch (RuntimeException exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw exc;
        } catch (Exception exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw new RuntimeException(exc);
        }
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) FaultMode(org.apache.cxf.message.FaultMode) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) MessageImpl(org.apache.cxf.message.MessageImpl) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo)

Example 4 with PhaseInterceptorChain

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

the class TestBase method setUp.

@Before
public void setUp() throws Exception {
    SortedSet<Phase> phases = new TreeSet<>();
    Phase phase1 = new Phase("phase1", 1);
    Phase phase2 = new Phase("phase2", 2);
    Phase phase3 = new Phase("phase3", 3);
    Phase phase4 = new Phase(Phase.WRITE_ENDING, 4);
    phases.add(phase1);
    phases.add(phase2);
    phases.add(phase3);
    phases.add(phase4);
    phases.add(new Phase(Phase.POST_LOGICAL, 5));
    phases.add(new Phase(Phase.INVOKE, 6));
    chain = new PhaseInterceptorChain(phases);
    soapMessage = TestUtil.createEmptySoapMessage(Soap11.getInstance(), chain);
}
Also used : PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Phase(org.apache.cxf.phase.Phase) TreeSet(java.util.TreeSet) Before(org.junit.Before)

Example 5 with PhaseInterceptorChain

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

the class AbstractFaultChainInitiatorObserver method onMessage.

public void onMessage(Message message) {
    assert null != message;
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Exchange exchange = message.getExchange();
        Message faultMessage;
        if (isOutboundObserver()) {
            Exception ex = message.getContent(Exception.class);
            if (!(ex instanceof Fault)) {
                ex = new Fault(ex);
            }
            FaultMode mode = message.get(FaultMode.class);
            faultMessage = exchange.getOutMessage();
            if (null == faultMessage) {
                faultMessage = new MessageImpl();
                faultMessage.setExchange(exchange);
                faultMessage = exchange.getEndpoint().getBinding().createMessage(faultMessage);
            }
            faultMessage.setContent(Exception.class, ex);
            if (null != mode) {
                faultMessage.put(FaultMode.class, mode);
            }
            // CXF-3981
            if (message.get("javax.xml.ws.addressing.context.inbound") != null) {
                faultMessage.put("javax.xml.ws.addressing.context.inbound", message.get("javax.xml.ws.addressing.context.inbound"));
            }
            exchange.setOutMessage(null);
            exchange.setOutFaultMessage(faultMessage);
            if (message.get(BindingFaultInfo.class) != null) {
                faultMessage.put(BindingFaultInfo.class, message.get(BindingFaultInfo.class));
            }
        } else {
            faultMessage = message;
            exchange.setInMessage(null);
            exchange.setInFaultMessage(faultMessage);
        }
        // setup chain
        PhaseInterceptorChain chain = new PhaseInterceptorChain(getPhases());
        initializeInterceptors(faultMessage.getExchange(), chain);
        faultMessage.setInterceptorChain(chain);
        try {
            chain.doIntercept(faultMessage);
        } catch (RuntimeException exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw exc;
        } catch (Exception exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw new RuntimeException(exc);
        }
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) FaultMode(org.apache.cxf.message.FaultMode) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) MessageImpl(org.apache.cxf.message.MessageImpl) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo)

Aggregations

PhaseInterceptorChain (org.apache.cxf.phase.PhaseInterceptorChain)34 Message (org.apache.cxf.message.Message)26 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 PhaseManager (org.apache.cxf.phase.PhaseManager)9 IOException (java.io.IOException)8 MessageImpl (org.apache.cxf.message.MessageImpl)8 TreeSet (java.util.TreeSet)7 Test (org.junit.Test)7 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)6 Fault (org.apache.cxf.interceptor.Fault)6 Phase (org.apache.cxf.phase.Phase)6 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