Search in sources :

Example 31 with Bus

use of org.apache.cxf.Bus in project cxf 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) {
                        responseContext.put(Thread.currentThread(), 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) {
                responseContext.put(Thread.currentThread(), 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 32 with Bus

use of org.apache.cxf.Bus in project cxf by apache.

the class ClientImpl method doInvoke.

private Object[] doInvoke(final ClientCallback callback, BindingOperationInfo oi, Object[] params, Map<String, Object> context, Exchange exchange) throws Exception {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (exchange == null) {
            exchange = new ExchangeImpl();
        }
        exchange.setSynchronous(callback == null);
        Endpoint endpoint = getEndpoint();
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
        }
        Message message = endpoint.getBinding().createMessage();
        // Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
        // on message
        Map<String, Object> reqContext = null;
        Map<String, Object> resContext = null;
        if (context == null) {
            context = new HashMap<>();
        }
        reqContext = CastUtils.cast((Map<?, ?>) context.get(REQUEST_CONTEXT));
        resContext = CastUtils.cast((Map<?, ?>) context.get(RESPONSE_CONTEXT));
        if (reqContext == null) {
            reqContext = new HashMap<>(getRequestContext());
            context.put(REQUEST_CONTEXT, reqContext);
        }
        if (resContext == null) {
            resContext = new HashMap<>();
            context.put(RESPONSE_CONTEXT, resContext);
        }
        message.put(Message.INVOCATION_CONTEXT, context);
        setContext(reqContext, message);
        exchange.putAll(reqContext);
        setParameters(params, message);
        if (null != oi) {
            exchange.setOneWay(oi.getOutput() == null);
        }
        exchange.setOutMessage(message);
        exchange.put(ClientCallback.class, callback);
        setOutMessageProperties(message, oi);
        setExchangeProperties(exchange, endpoint, oi);
        PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
        message.setInterceptorChain(chain);
        if (callback == null) {
            chain.setFaultObserver(outFaultObserver);
        } else {
            // We need to wrap the outFaultObserver if the callback is not null
            // calling the conduitSelector.complete to make sure the fail over feature works
            chain.setFaultObserver(new MessageObserver() {

                public void onMessage(Message message) {
                    Exception ex = message.getContent(Exception.class);
                    if (ex != null) {
                        completeExchange(message.getExchange());
                        if (message.getContent(Exception.class) == null) {
                            // handle the right response
                            List<Object> resList = null;
                            Message inMsg = message.getExchange().getInMessage();
                            Map<String, Object> ctx = responseContext.get(Thread.currentThread());
                            resList = CastUtils.cast(inMsg.getContent(List.class));
                            Object[] result = resList == null ? null : resList.toArray();
                            callback.handleResponse(ctx, result);
                            return;
                        }
                    }
                    outFaultObserver.onMessage(message);
                }
            });
        }
        prepareConduitSelector(message);
        // add additional interceptors and such
        modifyChain(chain, message, false);
        try {
            chain.doIntercept(message);
        } catch (Fault fault) {
            enrichFault(fault);
            throw fault;
        }
        if (callback != null) {
            return null;
        }
        return processResult(message, exchange, oi, resContext);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Fault(org.apache.cxf.interceptor.Fault) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UncheckedException(org.apache.cxf.common.i18n.UncheckedException) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 33 with Bus

use of org.apache.cxf.Bus in project cxf by apache.

the class CXFBusImplTest method testBusID.

@Test
public void testBusID() {
    Bus bus = new ExtensionManagerBus();
    String id = bus.getId();
    assertEquals("The bus id should be cxf", id, Bus.DEFAULT_BUS_ID + Math.abs(bus.hashCode()));
    bus.setId("test");
    assertEquals("The bus id should be changed", "test", bus.getId());
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) Test(org.junit.Test)

Example 34 with Bus

use of org.apache.cxf.Bus in project cxf by apache.

the class CXFBusImplTest method testExtensions.

@Test
public void testExtensions() {
    Bus bus = new ExtensionManagerBus();
    String extension = "CXF";
    bus.setExtension(extension, String.class);
    assertSame(extension, bus.getExtension(String.class));
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) Test(org.junit.Test)

Example 35 with Bus

use of org.apache.cxf.Bus in project cxf by apache.

the class CXFBusImplTest method testShutdownWithBusLifecycle.

@Test
public void testShutdownWithBusLifecycle() {
    final Bus bus = new ExtensionManagerBus();
    BusLifeCycleManager lifeCycleManager = bus.getExtension(BusLifeCycleManager.class);
    BusLifeCycleListener listener = EasyMock.createMock(BusLifeCycleListener.class);
    EasyMock.reset(listener);
    listener.preShutdown();
    EasyMock.expectLastCall();
    listener.postShutdown();
    EasyMock.expectLastCall();
    EasyMock.replay(listener);
    lifeCycleManager.registerLifeCycleListener(listener);
    bus.shutdown(true);
    EasyMock.verify(listener);
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) BusLifeCycleManager(org.apache.cxf.buslifecycle.BusLifeCycleManager) BusLifeCycleListener(org.apache.cxf.buslifecycle.BusLifeCycleListener) ExtensionManagerBus(org.apache.cxf.bus.extension.ExtensionManagerBus) Test(org.junit.Test)

Aggregations

Bus (org.apache.cxf.Bus)1144 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)800 URL (java.net.URL)748 QName (javax.xml.namespace.QName)436 Service (javax.xml.ws.Service)400 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)354 Test (org.junit.Test)219 HashMap (java.util.HashMap)63 Message (org.apache.cxf.message.Message)60 WebClient (org.apache.cxf.jaxrs.client.WebClient)50 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)48 Client (org.apache.cxf.endpoint.Client)43 Greeter (org.apache.hello_world.Greeter)42 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)41 SOAPService (org.apache.hello_world.services.SOAPService)41 JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)40 Endpoint (org.apache.cxf.endpoint.Endpoint)38 ExtensionManagerBus (org.apache.cxf.bus.extension.ExtensionManagerBus)36 STSClient (org.apache.cxf.ws.security.trust.STSClient)36 Document (org.w3c.dom.Document)36