Search in sources :

Example 51 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project tomee by apache.

the class JAXRSInInterceptor method createOutMessage.

private Message createOutMessage(Message inMessage, Response r) {
    Endpoint e = inMessage.getExchange().getEndpoint();
    Message mout = e.getBinding().createMessage();
    mout.setContent(List.class, new MessageContentsList(r));
    mout.setExchange(inMessage.getExchange());
    mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
    inMessage.getExchange().setOutMessage(mout);
    if (r.getStatus() >= Response.Status.BAD_REQUEST.getStatusCode()) {
        inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
    }
    return mout;
}
Also used : Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList)

Example 52 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project tomee by apache.

the class JAXRSInvoker method invoke.

public Object invoke(Exchange exchange, Object request) {
    MessageContentsList responseList = checkExchangeForResponse(exchange);
    if (responseList != null) {
        return responseList;
    }
    AsyncResponse asyncResp = exchange.get(AsyncResponse.class);
    if (asyncResp != null) {
        AsyncResponseImpl asyncImpl = (AsyncResponseImpl) asyncResp;
        asyncImpl.prepareContinuation();
        try {
            asyncImpl.handleTimeout();
            return handleAsyncResponse(exchange, asyncImpl);
        } catch (Throwable t) {
            return handleAsyncFault(exchange, asyncImpl, t);
        }
    }
    ResourceProvider provider = getResourceProvider(exchange);
    Object rootInstance = null;
    Message inMessage = exchange.getInMessage();
    try {
        rootInstance = getServiceObject(exchange);
        Object serviceObject = getActualServiceObject(exchange, rootInstance);
        return invoke(exchange, request, serviceObject);
    } catch (WebApplicationException ex) {
        responseList = checkExchangeForResponse(exchange);
        if (responseList != null) {
            return responseList;
        }
        return handleFault(ex, inMessage);
    } finally {
        boolean suspended = isSuspended(exchange);
        if (suspended || exchange.isOneWay() || inMessage.get(Message.THREAD_CONTEXT_SWITCHED) != null) {
            ServerProviderFactory.clearThreadLocalProxies(inMessage);
        }
        if (suspended || isServiceObjectRequestScope(inMessage)) {
            persistRoots(exchange, rootInstance, provider);
        } else {
            provider.releaseInstance(inMessage, rootInstance);
        }
    }
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) Message(org.apache.cxf.message.Message) WebApplicationException(javax.ws.rs.WebApplicationException) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) AsyncResponse(javax.ws.rs.container.AsyncResponse)

Example 53 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project tomee by apache.

the class ClientRequestContextImpl method doSetEntity.

private void doSetEntity(Object entity) {
    Object actualEntity = InjectionUtils.getEntity(entity);
    m.setContent(List.class, actualEntity == null ? new MessageContentsList() : new MessageContentsList(actualEntity));
    if (entity != null) {
        final Type type;
        if (GenericEntity.class.isAssignableFrom(entity.getClass())) {
            type = ((GenericEntity<?>) entity).getType();
        } else {
            type = entity.getClass();
        }
        m.put(Type.class, type);
        m.remove("org.apache.cxf.empty.request");
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) MessageContentsList(org.apache.cxf.message.MessageContentsList)

Example 54 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class ServiceInvokerInterceptor method handleMessage.

public void handleMessage(final Message message) {
    final Exchange exchange = message.getExchange();
    final Endpoint endpoint = exchange.getEndpoint();
    final Service service = endpoint.getService();
    final Invoker invoker = service.getInvoker();
    Runnable invocation = new Runnable() {

        public void run() {
            Exchange runableEx = message.getExchange();
            Object result = invoker.invoke(runableEx, getInvokee(message));
            if (!exchange.isOneWay()) {
                Endpoint ep = exchange.getEndpoint();
                Message outMessage = runableEx.getOutMessage();
                if (outMessage == null) {
                    // perf: size 16 / factor 1 to avoid resize operation
                    outMessage = new MessageImpl(16, 1);
                    outMessage.setExchange(exchange);
                    outMessage = ep.getBinding().createMessage(outMessage);
                    exchange.setOutMessage(outMessage);
                }
                copyJaxwsProperties(message, outMessage);
                if (result != null) {
                    MessageContentsList resList = null;
                    if (result instanceof MessageContentsList) {
                        resList = (MessageContentsList) result;
                    } else if (result instanceof List) {
                        resList = new MessageContentsList((List<?>) result);
                    } else if (result.getClass().isArray()) {
                        resList = new MessageContentsList((Object[]) result);
                    } else {
                        outMessage.setContent(Object.class, result);
                    }
                    if (resList != null) {
                        outMessage.setContent(List.class, resList);
                    }
                }
            }
        }
    };
    Executor executor = getExecutor(endpoint);
    Executor executor2 = exchange.get(Executor.class);
    if (executor2 == executor || executor == null || !(message.getInterceptorChain() instanceof PhaseInterceptorChain)) {
        // already executing on the appropriate executor
        invocation.run();
    } else {
        exchange.put(Executor.class, executor);
        // The current thread holds the lock on PhaseInterceptorChain.
        // In order to avoid the executor threads deadlocking on any of
        // synchronized PhaseInterceptorChain methods the current thread
        // needs to release the chain lock and re-acquire it after the
        // executor thread is done
        final PhaseInterceptorChain chain = (PhaseInterceptorChain) message.getInterceptorChain();
        final AtomicBoolean contextSwitched = new AtomicBoolean();
        final FutureTask<Object> o = new FutureTask<Object>(invocation, null) {

            @Override
            protected void done() {
                super.done();
                if (contextSwitched.get()) {
                    PhaseInterceptorChain.setCurrentMessage(chain, null);
                    message.remove(Message.THREAD_CONTEXT_SWITCHED);
                }
                chain.releaseChain();
            }

            @Override
            public void run() {
                if (PhaseInterceptorChain.setCurrentMessage(chain, message)) {
                    contextSwitched.set(true);
                    message.put(Message.THREAD_CONTEXT_SWITCHED, true);
                }
                synchronized (chain) {
                    super.run();
                }
            }
        };
        synchronized (chain) {
            executor.execute(o);
            // the task will already be done if the executor uses the current thread
            // but the chain lock status still needs to be re-set
            chain.releaseAndAcquireChain();
        }
        try {
            o.get();
        } catch (InterruptedException e) {
            throw new Fault(e);
        } catch (ExecutionException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            }
            throw new Fault(e.getCause());
        }
    }
}
Also used : PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) Service(org.apache.cxf.service.Service) Exchange(org.apache.cxf.message.Exchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Executor(java.util.concurrent.Executor) Endpoint(org.apache.cxf.endpoint.Endpoint) Invoker(org.apache.cxf.service.invoker.Invoker) FutureTask(java.util.concurrent.FutureTask) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 55 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class AbstractJAXWSMethodInvoker method invoke.

@Override
protected Object invoke(Exchange exchange, final Object serviceObject, Method m, List<Object> params) {
    JaxwsServerHandler h = exchange.get(JaxwsServerHandler.class);
    if (h != null && h.isDone()) {
        BindingOperationInfo bop = exchange.getBindingOperationInfo();
        if (bop.isUnwrapped()) {
            exchange.put(BindingOperationInfo.class, bop.getWrappedOperation());
        }
        try {
            return new MessageContentsList(h.getObject());
        } catch (ExecutionException ex) {
            exchange.getInMessage().put(FaultMode.class, FaultMode.CHECKED_APPLICATION_FAULT);
            throw createFault(ex.getCause(), m, params, true);
        } catch (Exception ex) {
            throw createFault(ex.getCause(), m, params, false);
        }
    }
    Object o = super.invoke(exchange, serviceObject, m, params);
    if (h != null && !h.hasContinuation()) {
        h.waitForDone();
        BindingOperationInfo bop = exchange.getBindingOperationInfo();
        if (bop.isUnwrapped()) {
            exchange.put(BindingOperationInfo.class, bop.getWrappedOperation());
        }
        try {
            return new MessageContentsList(h.getObject());
        } catch (ExecutionException ex) {
            exchange.getInMessage().put(FaultMode.class, FaultMode.CHECKED_APPLICATION_FAULT);
            throw createFault(ex.getCause(), m, params, true);
        } catch (Exception ex) {
            throw createFault(ex.getCause(), m, params, false);
        }
    }
    return o;
}
Also used : FaultMode(org.apache.cxf.message.FaultMode) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Aggregations

MessageContentsList (org.apache.cxf.message.MessageContentsList)69 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)24 Exchange (org.apache.cxf.message.Exchange)23 Message (org.apache.cxf.message.Message)22 Fault (org.apache.cxf.interceptor.Fault)19 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)18 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Endpoint (org.apache.cxf.endpoint.Endpoint)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 Response (javax.ws.rs.core.Response)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 Method (java.lang.reflect.Method)9 OperationInfo (org.apache.cxf.service.model.OperationInfo)9 XMLStreamException (javax.xml.stream.XMLStreamException)8 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)8 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)8 MessageInfo (org.apache.cxf.service.model.MessageInfo)8 Test (org.junit.Test)8 Service (org.apache.cxf.service.Service)7