Search in sources :

Example 1 with PhaseManager

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

the class DemoInterceptor method addInterceptors.

/**
 * This method will add a DemoInterceptor into every in and every out phase
 * of the interceptor chains.
 *
 * @param provider
 */
public static void addInterceptors(InterceptorProvider provider) {
    PhaseManager phases = BusFactory.getDefaultBus().getExtension(PhaseManager.class);
    for (Phase p : phases.getInPhases()) {
        provider.getInInterceptors().add(new DemoInterceptor(p.getName()));
        provider.getInFaultInterceptors().add(new DemoInterceptor(p.getName()));
    }
    for (Phase p : phases.getOutPhases()) {
        provider.getOutInterceptors().add(new DemoInterceptor(p.getName()));
        provider.getOutFaultInterceptors().add(new DemoInterceptor(p.getName()));
    }
}
Also used : Phase(org.apache.cxf.phase.Phase) PhaseManager(org.apache.cxf.phase.PhaseManager)

Example 2 with PhaseManager

use of org.apache.cxf.phase.PhaseManager 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 PhaseManager

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

the class SpringBusFactoryTest method testPhases.

@Test
public void testPhases() {
    Bus bus = new SpringBusFactory().createBus();
    PhaseManager cxfPM = bus.getExtension(PhaseManager.class);
    PhaseManager defaultPM = new PhaseManagerImpl();
    SortedSet<Phase> cxfPhases = cxfPM.getInPhases();
    SortedSet<Phase> defaultPhases = defaultPM.getInPhases();
    assertEquals(defaultPhases.size(), cxfPhases.size());
    assertEquals(cxfPhases, defaultPhases);
    cxfPhases = cxfPM.getOutPhases();
    defaultPhases = defaultPM.getOutPhases();
    assertEquals(defaultPhases.size(), cxfPhases.size());
    assertEquals(cxfPhases, defaultPhases);
}
Also used : Bus(org.apache.cxf.Bus) Phase(org.apache.cxf.phase.Phase) PhaseManager(org.apache.cxf.phase.PhaseManager) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Example 4 with PhaseManager

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

the class OutgoingChainInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    control = EasyMock.createNiceControl();
    phases = new ArrayList<>();
    phases.add(new Phase(Phase.SEND, 1000));
    empty = new ArrayList<>();
    bus = control.createMock(Bus.class);
    PhaseManager pm = new PhaseManagerImpl();
    EasyMock.expect(bus.getExtension(PhaseManager.class)).andReturn(pm).anyTimes();
    service = control.createMock(Service.class);
    endpoint = control.createMock(Endpoint.class);
    binding = control.createMock(Binding.class);
    EasyMock.expect(endpoint.getBinding()).andStubReturn(binding);
    MessageImpl m = new MessageImpl();
    EasyMock.expect(binding.createMessage()).andStubReturn(m);
    EasyMock.expect(endpoint.getService()).andReturn(service).anyTimes();
    EasyMock.expect(endpoint.getOutInterceptors()).andReturn(empty);
    EasyMock.expect(service.getOutInterceptors()).andReturn(empty);
    EasyMock.expect(bus.getOutInterceptors()).andReturn(empty);
    bopInfo = control.createMock(BindingOperationInfo.class);
    opInfo = control.createMock(OperationInfo.class);
    mInfo = control.createMock(MessageInfo.class);
    bmInfo = control.createMock(BindingMessageInfo.class);
    EasyMock.expect(bopInfo.getOperationInfo()).andReturn(opInfo).times(3);
    EasyMock.expect(opInfo.getOutput()).andReturn(mInfo);
    EasyMock.expect(opInfo.isOneWay()).andReturn(false);
    EasyMock.expect(bopInfo.getOutput()).andReturn(bmInfo);
    control.replay();
}
Also used : Binding(org.apache.cxf.binding.Binding) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Bus(org.apache.cxf.Bus) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Phase(org.apache.cxf.phase.Phase) PhaseManager(org.apache.cxf.phase.PhaseManager) Service(org.apache.cxf.service.Service) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) MessageImpl(org.apache.cxf.message.MessageImpl) Before(org.junit.Before)

Example 5 with PhaseManager

use of org.apache.cxf.phase.PhaseManager 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());
    chain.setFaultObserver(setupInFaultObserver(cfg));
    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)

Aggregations

PhaseManager (org.apache.cxf.phase.PhaseManager)16 Message (org.apache.cxf.message.Message)12 Bus (org.apache.cxf.Bus)9 Interceptor (org.apache.cxf.interceptor.Interceptor)8 PhaseInterceptorChain (org.apache.cxf.phase.PhaseInterceptorChain)8 InterceptorProvider (org.apache.cxf.interceptor.InterceptorProvider)5 Phase (org.apache.cxf.phase.Phase)5 Endpoint (org.apache.cxf.endpoint.Endpoint)4 AbstractBasicInterceptorProvider (org.apache.cxf.interceptor.AbstractBasicInterceptorProvider)4 HashMap (java.util.HashMap)3 Binding (org.apache.cxf.binding.Binding)3 PhaseManagerImpl (org.apache.cxf.bus.managers.PhaseManagerImpl)3 AbstractOutDatabindingInterceptor (org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor)3 AbstractPhaseInterceptor (org.apache.cxf.phase.AbstractPhaseInterceptor)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 WeakHashMap (java.util.WeakHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2