Search in sources :

Example 26 with MessageObserver

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

the class SOAPConnectionImpl method createMessageObserver.

@SuppressWarnings("unchecked")
private MessageObserver createMessageObserver(final Conduit c) {
    return new MessageObserver() {

        public void onMessage(Message inMessage) {
            LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
            try {
                IOUtils.copy(inMessage.getContent(InputStream.class), bout);
                inMessage.getExchange().put(InputStream.class, bout.createInputStream());
                Map<String, List<String>> inHeaders = (Map<String, List<String>>) inMessage.get(Message.PROTOCOL_HEADERS);
                inMessage.getExchange().put(Message.PROTOCOL_HEADERS, inHeaders);
                c.close(inMessage);
            } catch (IOException e) {
                // ignore
                Logger.getLogger(SOAPConnectionImpl.class).trace(e);
            }
        }
    };
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project tomee 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 28 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project tomee 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 29 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf 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 30 with MessageObserver

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

the class ContextUtils method createDecoupledDestination.

public static Destination createDecoupledDestination(Exchange exchange, final EndpointReferenceType reference) {
    final EndpointInfo ei = exchange.getEndpoint().getEndpointInfo();
    return new Destination() {

        public EndpointReferenceType getAddress() {
            return reference;
        }

        public Conduit getBackChannel(Message inMessage) throws IOException {
            Bus bus = inMessage.getExchange().getBus();
            // this is a response targeting a decoupled endpoint.   Treat it as a oneway so
            // we don't wait for a response.
            inMessage.getExchange().setOneWay(true);
            ConduitInitiator conduitInitiator = bus.getExtension(ConduitInitiatorManager.class).getConduitInitiatorForUri(reference.getAddress().getValue());
            if (conduitInitiator != null) {
                Conduit c = conduitInitiator.getConduit(ei, reference, bus);
                // ensure decoupled back channel input stream is closed
                c.setMessageObserver(new MessageObserver() {

                    public void onMessage(Message m) {
                        InputStream is = m.getContent(InputStream.class);
                        if (is != null) {
                            try {
                                is.close();
                            } catch (Exception e) {
                            // ignore
                            }
                        }
                    }
                });
                return c;
            }
            return null;
        }

        public MessageObserver getMessageObserver() {
            return null;
        }

        public void shutdown() {
        }

        public void setMessageObserver(MessageObserver observer) {
        }
    };
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Destination(org.apache.cxf.transport.Destination) Bus(org.apache.cxf.Bus) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) Conduit(org.apache.cxf.transport.Conduit) InputStream(java.io.InputStream) ConduitInitiatorManager(org.apache.cxf.transport.ConduitInitiatorManager) ConduitInitiator(org.apache.cxf.transport.ConduitInitiator) 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