Java Code Examples for org.apache.cxf.message.Exchange#get()

The following examples show how to use org.apache.cxf.message.Exchange#get() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: fuchsia   File: ExchangeUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void closeConduit(Exchange exchange) throws IOException {
    ConduitSelector conduitSelector = null;
    synchronized (exchange) {
        conduitSelector = exchange.get(ConduitSelector.class);
        if (conduitSelector != null) {
            exchange.remove(ConduitSelector.class.getName());
        }
    }

    Conduit selectedConduit = null;
    Message message = exchange.getInMessage() == null ? exchange
            .getOutMessage() : exchange.getInMessage();

    if (conduitSelector != null && message != null) {
        selectedConduit = conduitSelector.selectConduit(message);
        selectedConduit.close(message);
    }

    //TODO the line below was removed, check the impact on the protobuffer importer/exporter
    //selectedConduit.close(message);
}
 
Example 2
@Override
public void handleFault(Message message) {
    Exchange ex = message.getExchange();
    if (ex.get("org.apache.cxf.management.counter.enabled") != null) {
        if (ex.isOneWay()) {
            // do nothing, done by the ResponseTimeInvokerInterceptor
        } else {
            FaultMode faultMode = message.get(FaultMode.class);
            if (faultMode == null) {
                // client side exceptions don't have FaultMode set un the message properties (as of 2.1.4)
                faultMode = FaultMode.RUNTIME_FAULT;
            }
            ex.put(FaultMode.class, faultMode);
            endHandlingMessage(ex);
        }
    }
}
 
Example 3
Source Project: cxf   File: FailoverTargetSelector.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if the exchange is suitable for a failover.
 *
 * @param exchange the current Exchange
 * @return boolean true if a failover should be attempted
 */
protected boolean requiresFailover(Exchange exchange, Exception ex) {
    getLogger().log(Level.FINE,
                    "CHECK_LAST_INVOKE_FAILED",
                    new Object[] {ex != null});
    Throwable curr = ex;
    boolean failover = false;
    while (curr != null) {
        failover = curr instanceof java.io.IOException;
        curr = curr.getCause();
    }
    if (ex != null) {
        getLogger().log(Level.INFO,
                        "CHECK_FAILURE_IN_TRANSPORT",
                        new Object[] {ex, failover});
    }

    if (isSupportNotAvailableErrorsOnly() && exchange.get(Message.RESPONSE_CODE) != null) {
        failover = PropertyUtils.isTrue(exchange.get("org.apache.cxf.transport.service_not_available"));
    }

    return failover;
}
 
Example 4
Source Project: tomee   File: EjbMethodInvoker.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Object performInvocation(final Exchange exchange, final Object serviceObject,
                                   final Method m, final Object[] paramArray) throws Exception {
    InvocationContext invContext = exchange.get(InvocationContext.class);
    invContext.setParameters(paramArray);
    Object res = invContext.proceed();

    EjbMessageContext ctx = (EjbMessageContext) invContext.getContextData();

    Map<String, Object> handlerProperties = (Map<String, Object>) exchange
        .get(HANDLER_PROPERTIES);
    addHandlerProperties(ctx, handlerProperties);

    updateWebServiceContext(exchange, ctx);

    return res;
}
 
Example 5
Source Project: cxf   File: AbstractClient.java    License: Apache License 2.0 5 votes vote down vote up
private Integer getResponseCode(Exchange exchange) {
    Integer responseCode = (Integer)exchange.get(Message.RESPONSE_CODE);
    if (responseCode == null && exchange.getInMessage() != null) {
        responseCode = (Integer)exchange.getInMessage().get(Message.RESPONSE_CODE);
    }
    if (responseCode == null && exchange.isOneWay() && !state.getBaseURI().toString().startsWith("http")) {
        responseCode = 202;
    }
    return responseCode;
}
 
Example 6
Source Project: fuchsia   File: ExchangeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Exception getException(Exchange exchange) {
    Exception throwable = exchange.get(Exception.class);

    if (exchange.getInFaultMessage() != null) {
        return exchange.getInFaultMessage().getContent(Exception.class);
    } else if (exchange.getOutFaultMessage() != null) {
        return exchange.getOutFaultMessage().getContent(Exception.class);
    }

    if (throwable != null) {
        return throwable;
    }

    throwable = getException(exchange.getOutMessage());

    if (throwable != null) {
        return throwable;
    }

    throwable = getException(exchange.getInMessage());

    if (throwable != null) {
        return throwable;
    }

    return null;
}
 
Example 7
protected HandlerChainInvoker getInvoker(T message) {
    Exchange ex = message.getExchange();
    HandlerChainInvoker invoker =
        ex.get(HandlerChainInvoker.class);
    if (null == invoker) {
        invoker = new HandlerChainInvoker(binding.getHandlerChain(),
                                          isOutbound(message));
        ex.put(HandlerChainInvoker.class, invoker);
    }

    boolean outbound = isOutbound(message, ex);
    if (outbound) {
        invoker.setOutbound();
    } else {
        invoker.setInbound();
    }
    invoker.setRequestor(isRequestor(message));

    if (ex.isOneWay()
        || ((isRequestor(message) && !outbound)
            || (!isRequestor(message) && outbound))) {
        invoker.setResponseExpected(false);
    } else {
        invoker.setResponseExpected(true);
    }

    return invoker;
}
 
Example 8
protected void endHandlingMessage(Exchange ex) {
    if (null == ex) {
        return;
    }
    MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);
    if (null != mhtr) {
        mhtr.endHandling();
        mhtr.setFaultMode(ex.get(FaultMode.class));
        increaseCounter(ex, mhtr);

    } // else can't get the MessageHandling Infor
}
 
Example 9
Source Project: cxf   File: JAXRSInvoker.java    License: Apache License 2.0 5 votes vote down vote up
private ResourceProvider getResourceProvider(Exchange exchange) {
    Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
    if (provider == null) {
        OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
        ClassResourceInfo cri = ori.getClassResourceInfo();
        return cri.getResourceProvider();
    }
    return (ResourceProvider)provider;
}
 
Example 10
Source Project: cxf   File: JAXRSInvoker.java    License: Apache License 2.0 5 votes vote down vote up
public Object getServiceObject(Exchange exchange) {

        Object root = exchange.remove(JAXRSUtils.ROOT_INSTANCE);
        if (root != null) {
            return root;
        }

        OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
        ClassResourceInfo cri = ori.getClassResourceInfo();

        return cri.getResourceProvider().getInstance(exchange.getInMessage());
    }
 
Example 11
protected void setOneWayMessage(Exchange ex) {
    MessageHandlingTimeRecorder mhtr = ex.get(MessageHandlingTimeRecorder.class);
    if (null == mhtr) {
        mhtr = new MessageHandlingTimeRecorder(ex);
    } else {
        mhtr.endHandling();
    }
    mhtr.setOneWay(true);
    increaseCounter(ex, mhtr);
}
 
Example 12
Source Project: cxf   File: RMInInterceptorTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testAppMessage(boolean onServer, boolean deferredAbort)
    throws SequenceFault, RMException, NoSuchMethodException {
    Method m1 = RMInInterceptor.class.getDeclaredMethod("processAcknowledgments",
        new Class[] {RMEndpoint.class, RMProperties.class, ProtocolVariation.class});
    Method m2 = RMInInterceptor.class.getDeclaredMethod("processAcknowledgmentRequests",
        new Class[] {Destination.class, Message.class});
    Method m3 = RMInInterceptor.class.getDeclaredMethod("processSequence",
        new Class[] {Destination.class, Message.class});
    Method m4 = RMInInterceptor.class.getDeclaredMethod("processDeliveryAssurance",
        new Class[] {RMProperties.class});
    interceptor =
        EasyMock.createMockBuilder(RMInInterceptor.class)
            .addMockedMethods(m1, m2, m3, m4).createMock(control);
    Message message = setupInboundMessage("greetMe", true);
    Destination d = control.createMock(Destination.class);
    EasyMock.expect(manager.getDestination(message)).andReturn(d);
    interceptor.processAcknowledgments(rme, rmps, ProtocolVariation.RM10WSA200408);
    EasyMock.expectLastCall();
    interceptor.processAcknowledgmentRequests(d, message);
    EasyMock.expectLastCall();
    interceptor.processSequence(d, message);
    EasyMock.expectLastCall();
    interceptor.processDeliveryAssurance(rmps);
    EasyMock.expectLastCall();
    EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(null);

    Exchange ex = control.createMock(Exchange.class);
    message.getExchange();
    EasyMock.expectLastCall().andReturn(ex).anyTimes();
    ex.get("deferred.uncorrelated.message.abort");
    EasyMock.expectLastCall().andReturn(Boolean.TRUE);
    InterceptorChain chain = control.createMock(InterceptorChain.class);
    message.getInterceptorChain();
    EasyMock.expectLastCall().andReturn(chain);
    chain.abort();
    EasyMock.expectLastCall();

    control.replay();
    interceptor.handle(message);
}
 
Example 13
Source Project: cxf   File: AbstractLoggingInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
public void createExchangeId(Message message) {
    Exchange exchange = message.getExchange();
    String exchangeId = (String)exchange.get(LogEvent.KEY_EXCHANGE_ID);
    if (exchangeId == null) {
        exchangeId = UUID.randomUUID().toString();
        exchange.put(LogEvent.KEY_EXCHANGE_ID, exchangeId);
    }
}
 
Example 14
Source Project: cxf   File: JsonpJaxrsWriterInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
protected String getCallbackValue(Message message) {
    Exchange exchange = message.getExchange();
    return (String) exchange.get(JsonpInInterceptor.CALLBACK_KEY);
}
 
Example 15
Source Project: cxf   File: JAXRSInvoker.java    License: Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}
 
Example 16
Source Project: cxf   File: JAXRSInvoker.java    License: Apache License 2.0 4 votes vote down vote up
protected Object getActualServiceObject(Exchange exchange, Object rootInstance) {

        Object last = exchange.get(LAST_SERVICE_OBJECT);
        return last !=  null ? last : rootInstance;
    }
 
Example 17
public void handleMessage(Message outMessage) throws Fault {
    ClientProviderFactory pf = ClientProviderFactory.getInstance(outMessage);
    if (pf == null) {
        return;
    }

    // create an empty proxy output stream that the filter can interact with
    // and save a reference for later
    ProxyOutputStream pos = new ProxyOutputStream();
    outMessage.setContent(OutputStream.class, pos);
    outMessage.setContent(ProxyOutputStream.class, pos);

    List<ProviderInfo<ClientRequestFilter>> filters = pf.getClientRequestFilters();
    if (!filters.isEmpty()) {

        final Exchange exchange = outMessage.getExchange();
        final ClientRequestContext context = new ClientRequestContextImpl(outMessage, false);
        for (ProviderInfo<ClientRequestFilter> filter : filters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, outMessage);
            try {
                filter.getProvider().filter(context);

                @SuppressWarnings("unchecked")
                Map<String, List<Object>> headers = CastUtils.cast((Map<String, List<Object>>) 
                                                                   outMessage.get(Message.PROTOCOL_HEADERS));
                HttpUtils.convertHeaderValuesToString(headers, false);

                Response response = outMessage.getExchange().get(Response.class);
                if (response != null) {
                    outMessage.getInterceptorChain().abort();

                    Message inMessage = new MessageImpl();
                    inMessage.setExchange(exchange);
                    inMessage.put(Message.RESPONSE_CODE, response.getStatus());
                    inMessage.put(Message.PROTOCOL_HEADERS, response.getMetadata());
                    exchange.setInMessage(inMessage);

                    MessageObserver observer = exchange.get(MessageObserver.class);
                    observer.onMessage(inMessage);
                    return;
                }
            } catch (IOException ex) {
                throw new ProcessingException(ex);
            }
        }
    }
}
 
Example 18
Source Project: cxf   File: FailoverTargetSelector.java    License: Apache License 2.0 4 votes vote down vote up
private Exception getExceptionIfPresent(Exchange exchange) {
    Message outMessage = exchange.getOutMessage();
    return outMessage.get(Exception.class) != null
        ? outMessage.get(Exception.class)
            : exchange.get(Exception.class);
}
 
Example 19
Source Project: cxf   File: AbstractJsonpOutInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
protected String getCallbackValue(Message message) {
    Exchange exchange = message.getExchange();
    return (String) exchange.get(JsonpInInterceptor.CALLBACK_KEY);
}
 
Example 20
Source Project: cxf   File: JMSConduit.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Send the JMS message and if the MEP is not oneway receive the response.
 *
 * @param exchange the Exchange containing the outgoing message
 * @param request  the payload of the outgoing JMS message
 */
public void sendExchange(final Exchange exchange, final Object request) {
    LOG.log(Level.FINE, "JMSConduit send message");

    final Message outMessage = exchange.getOutMessage() == null
        ? exchange.getOutFaultMessage()
        : exchange.getOutMessage();
    if (outMessage == null) {
        throw new RuntimeException("Exchange to be sent has no outMessage");
    }

    jmsConfig.ensureProperlyConfigured();
    assertIsNotTextMessageAndMtom(outMessage);

    try (ResourceCloser closer = new ResourceCloser()) {
        Connection c;

        if (jmsConfig.isOneSessionPerConnection()) {
            c = closer.register(JMSFactory.createConnection(jmsConfig));
            c.start();
        } else {
            c = getConnection();
        }

        Session session = closer.register(c.createSession(false, 
                                                          Session.AUTO_ACKNOWLEDGE));

        if (exchange.isOneWay()) {
            sendMessage(request, outMessage, null, null, closer, session);
        } else {
            sendAndReceiveMessage(exchange, request, outMessage, closer, session);
        }
    } catch (JMSException e) {
        if (this.jmsListener != null) {
            this.jmsListener.shutdown();
        }
        this.jmsListener = null;
        // Close connection so it will be refreshed on next try
        if (!jmsConfig.isOneSessionPerConnection()) {
            if (exchange.get(JMSUtil.JMS_MESSAGE_CONSUMER) != null) {
                ResourceCloser.close(exchange.get(JMSUtil.JMS_MESSAGE_CONSUMER));
            }
            ResourceCloser.close(connection);
            this.connection = null;
            jmsConfig.resetCachedReplyDestination();
        }
        this.staticReplyDestination = null;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            // Ignore
        }
        throw JMSUtil.convertJmsException(e);
    }
}