Search in sources :

Example 6 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project tomee by apache.

the class AbstractConduitSelector method createConduit.

protected Conduit createConduit(Message message, Exchange exchange, ConduitInitiator conduitInitiator) throws IOException {
    Conduit c;
    synchronized (endpoint) {
        if (!conduits.isEmpty()) {
            c = findCompatibleConduit(message);
            if (c != null) {
                return c;
            }
        }
        EndpointInfo ei = endpoint.getEndpointInfo();
        String add = (String) message.get(Message.ENDPOINT_ADDRESS);
        String basePath = (String) message.get(Message.BASE_PATH);
        if (StringUtils.isEmpty(add) || add.equals(ei.getAddress())) {
            c = conduitInitiator.getConduit(ei, exchange.getBus());
            replaceEndpointAddressPropertyIfNeeded(message, add, c);
        } else {
            EndpointReferenceType epr = new EndpointReferenceType();
            AttributedURIType ad = new AttributedURIType();
            ad.setValue(StringUtils.isEmpty(basePath) ? add : basePath);
            epr.setAddress(ad);
            c = conduitInitiator.getConduit(ei, epr, exchange.getBus());
        }
        MessageObserver observer = exchange.get(MessageObserver.class);
        if (observer != null) {
            c.setMessageObserver(observer);
        } else {
            getLogger().warning("MessageObserver not found");
        }
        conduits.add(c);
    }
    return c;
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) MessageObserver(org.apache.cxf.transport.MessageObserver) Conduit(org.apache.cxf.transport.Conduit) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType)

Example 7 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project tomee by apache.

the class ServerImpl method stop.

public void stop() {
    if (stopped) {
        return;
    }
    LOG.fine("Server is stopping.");
    for (Closeable c : endpoint.getCleanupHooks()) {
        try {
            c.close();
        } catch (IOException e) {
        // ignore
        }
    }
    if (slcMgr != null) {
        slcMgr.stopServer(this);
    }
    MessageObserver mo = getDestination().getMessageObserver();
    if (mo instanceof MultipleEndpointObserver) {
        ((MultipleEndpointObserver) mo).getEndpoints().remove(endpoint);
        if (((MultipleEndpointObserver) mo).getEndpoints().isEmpty()) {
            getDestination().setMessageObserver(null);
        }
    } else {
        getDestination().setMessageObserver(null);
    }
    stopped = true;
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) MultipleEndpointObserver(org.apache.cxf.transport.MultipleEndpointObserver) Closeable(java.io.Closeable) IOException(java.io.IOException)

Example 8 with MessageObserver

use of org.apache.cxf.transport.MessageObserver 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;
    Map<String, Object> resContext = 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();
        // on message
        if (context == null) {
            context = new HashMap<>();
        }
        Map<String, Object> 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 ResponseContext(responseContext);
            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
                            Message inMsg = message.getExchange().getInMessage();
                            Map<String, Object> ctx = responseContext.get(Thread.currentThread());
                            List<Object> 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 {
        // ensure ResponseContext has HTTP RESPONSE CODE
        if (null != exchange) {
            Integer responseCode = (Integer) exchange.get(Message.RESPONSE_CODE);
            resContext.put(MessageContext.HTTP_RESPONSE_CODE, responseCode);
            resContext.put(org.apache.cxf.message.Message.RESPONSE_CODE, responseCode);
            setResponseContext(resContext);
        }
        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 9 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class ClientImpl method setExchangeProperties.

protected void setExchangeProperties(Exchange exchange, Endpoint endpoint, BindingOperationInfo boi) {
    if (endpoint != null) {
        exchange.put(Endpoint.class, endpoint);
        exchange.put(Service.class, endpoint.getService());
        exchange.put(Binding.class, endpoint.getBinding());
    }
    if (boi != null) {
        exchange.put(BindingOperationInfo.class, boi);
    }
    if (exchange.isSynchronous() || executor == null) {
        exchange.put(MessageObserver.class, this);
    } else {
        exchange.put(Executor.class, executor);
        exchange.put(MessageObserver.class, new MessageObserver() {

            public void onMessage(final Message message) {
                if (!message.getExchange().containsKey(Executor.class.getName() + ".USING_SPECIFIED")) {
                    executor.execute(new Runnable() {

                        public void run() {
                            ClientImpl.this.onMessage(message);
                        }
                    });
                } else {
                    ClientImpl.this.onMessage(message);
                }
            }
        });
    }
    exchange.put(Retryable.class, this);
    exchange.put(Client.class, this);
    exchange.put(Bus.class, bus);
    if (endpoint != null) {
        EndpointInfo endpointInfo = endpoint.getEndpointInfo();
        if (boi != null) {
            exchange.put(Message.WSDL_OPERATION, boi.getName());
        }
        QName serviceQName = endpointInfo.getService().getName();
        exchange.put(Message.WSDL_SERVICE, serviceQName);
        QName interfaceQName = endpointInfo.getService().getInterface().getName();
        exchange.put(Message.WSDL_INTERFACE, interfaceQName);
        QName portQName = endpointInfo.getName();
        exchange.put(Message.WSDL_PORT, portQName);
        URI wsdlDescription = endpointInfo.getProperty("URI", URI.class);
        if (wsdlDescription == null) {
            String address = endpointInfo.getAddress();
            try {
                wsdlDescription = new URI(address + "?wsdl");
            } catch (URISyntaxException e) {
            // do nothing
            }
            endpointInfo.setProperty("URI", wsdlDescription);
        }
        exchange.put(Message.WSDL_DESCRIPTION, wsdlDescription);
    }
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) QName(javax.xml.namespace.QName) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 10 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class ServerImpl method stop.

public void stop() {
    if (stopped) {
        return;
    }
    LOG.fine("Server is stopping.");
    for (Closeable c : endpoint.getCleanupHooks()) {
        try {
            c.close();
        } catch (IOException e) {
        // ignore
        }
    }
    if (slcMgr != null) {
        slcMgr.stopServer(this);
    }
    MessageObserver mo = getDestination().getMessageObserver();
    if (mo instanceof MultipleEndpointObserver) {
        ((MultipleEndpointObserver) mo).getEndpoints().remove(endpoint);
        if (((MultipleEndpointObserver) mo).getEndpoints().isEmpty()) {
            getDestination().setMessageObserver(null);
        }
    } else {
        getDestination().setMessageObserver(null);
    }
    stopped = true;
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) MultipleEndpointObserver(org.apache.cxf.transport.MultipleEndpointObserver) Closeable(java.io.Closeable) IOException(java.io.IOException)

Aggregations

MessageObserver (org.apache.cxf.transport.MessageObserver)43 Message (org.apache.cxf.message.Message)34 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)21 Conduit (org.apache.cxf.transport.Conduit)14 IOException (java.io.IOException)13 Exchange (org.apache.cxf.message.Exchange)12 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)11 Test (org.junit.Test)11 MessageImpl (org.apache.cxf.message.MessageImpl)10 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)10 Bus (org.apache.cxf.Bus)8 QName (javax.xml.namespace.QName)7 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)6 Fault (org.apache.cxf.interceptor.Fault)5 Destination (org.apache.cxf.transport.Destination)5 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 HashMap (java.util.HashMap)4 List (java.util.List)4 ExtensionManagerBus (org.apache.cxf.bus.extension.ExtensionManagerBus)4