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

The following examples show how to use org.apache.cxf.message.Exchange#getOutMessage() . 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 File: JAXWSMethodInvokerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testFaultAvoidHeadersCopy() throws Throwable {
    ExceptionService serviceObject = new ExceptionService();
    Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[]{});

    Exchange ex = new ExchangeImpl();
    prepareInMessage(ex, false);


    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[]{}));
        fail("Expected fault");
    } catch (Fault fault) {
        Message outMsg = ex.getOutMessage();
        assertNull(outMsg);
    }
}
 
Example 2
Source File: JAXWSMethodInvokerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testFaultHeadersCopy() throws Throwable {
    ExceptionService serviceObject = new ExceptionService();
    Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[]{});

    Exchange ex = new ExchangeImpl();
    prepareInMessage(ex, true);
    Message msg = new MessageImpl();
    SoapMessage outMessage = new SoapMessage(msg);
    ex.setOutMessage(outMessage);

    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);

    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[]{}));
        fail("Expected fault");
    } catch (Fault fault) {
        Message outMsg = ex.getOutMessage();
        assertNotNull(outMsg);
        @SuppressWarnings("unchecked")
        List<Header> headers = (List<Header>)outMsg.get(Header.HEADER_LIST);
        assertEquals(1, headers.size());
        assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
    }
}
 
Example 3
Source File: ExchangeUtils.java    From fuchsia with 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 4
Source File: AuthenticationHandler.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param inMessage
 * @return Message
 */
private Message getOutMessage(Message inMessage)
{
    Exchange exchange = inMessage.getExchange();
    Message outMessage = exchange.getOutMessage();
    if (outMessage == null)
    {
        Endpoint endpoint = exchange.get(Endpoint.class);
        outMessage = endpoint.getBinding().createMessage();
        exchange.setOutMessage(outMessage);
    }
    outMessage.putAll(inMessage);

    return outMessage;
}
 
Example 5
Source File: BasicAuthenticationInterceptor.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
private Message getOutMessage(Message inMessage) {
    Exchange exchange = inMessage.getExchange();
    Message outMessage = exchange.getOutMessage();
    if (outMessage == null) {
        Endpoint endpoint = exchange.get(Endpoint.class);
        outMessage = endpoint.getBinding().createMessage();
        exchange.setOutMessage(outMessage);
    }
    outMessage.putAll(inMessage);
    return outMessage;
}
 
Example 6
Source File: ContextUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Propagate inbound MAPs onto full reponse & fault messages.
 *
 * @param inMAPs the inbound MAPs
 * @param exchange the current Exchange
 */
public static void propogateReceivedMAPs(AddressingProperties inMAPs,
                                          Exchange exchange) {
    if (exchange.getOutMessage() == null) {
        exchange.setOutMessage(createMessage(exchange));
    }
    propogateReceivedMAPs(inMAPs, exchange.getOutMessage());
    if (exchange.getOutFaultMessage() == null) {
        exchange.setOutFaultMessage(createMessage(exchange));
    }
    propogateReceivedMAPs(inMAPs, exchange.getOutFaultMessage());
}
 
Example 7
Source File: MessageSenderInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static Conduit getConduit(Message message) throws IOException {
    Exchange exchange = message.getExchange();
    Conduit conduit = exchange.getConduit(message);
    if (conduit == null
        && (exchange.getOutMessage() != null
            || exchange.getOutFaultMessage() != null)) {
        conduit = OutgoingChainInterceptor.getBackChannelConduit(message);
    }
    return conduit;
}
 
Example 8
Source File: OutgoingChainInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(Message message) {
    Exchange ex = message.getExchange();
    BindingOperationInfo binding = ex.getBindingOperationInfo();
    //if we get this far, we're going to be outputting some valid content, but we COULD
    //also be "echoing" some of the content from the input.   Thus, we need to
    //mark it as requiring the input to be cached.
    if (message.getExchange().get(CACHE_INPUT_PROPERTY) == null) {
        message.put(CACHE_INPUT_PROPERTY, Boolean.TRUE);
    }
    if (null != binding && null != binding.getOperationInfo() && binding.getOperationInfo().isOneWay()) {
        closeInput(message);
        return;
    }
    Message out = ex.getOutMessage();
    if (out != null) {
        try {
            getBackChannelConduit(message);
        } catch (IOException ioe) {
            throw new Fault(ioe);
        }
        if (binding != null) {
            out.put(MessageInfo.class, binding.getOperationInfo().getOutput());
            out.put(BindingMessageInfo.class, binding.getOutput());
        }

        InterceptorChain outChain = out.getInterceptorChain();
        if (outChain == null) {
            outChain = OutgoingChainInterceptor.getChain(ex, chainCache);
            out.setInterceptorChain(outChain);
        } else if (outChain.getState() == InterceptorChain.State.PAUSED) {
            outChain.resume();
            return;
        }
        outChain.doIntercept(out);

    }
}
 
Example 9
Source File: BackChannelConduit.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void sendExchange(Exchange exchange, final Object replyObj) {
    if (exchange.isOneWay()) {
        //Don't need to send anything
        return;
    }
    final Message outMessage = exchange.getOutMessage();
    try (ResourceCloser closer = new ResourceCloser()) {
        send(outMessage, replyObj, closer);
    } catch (JMSException ex) {
        throw JMSUtil.convertJmsException(ex);
    }
}
 
Example 10
Source File: HTTPConduit.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void propagateConduit(Exchange exchange, Message in) {
    if (exchange != null) {
        Message out = exchange.getOutMessage();
        if (out != null) {
            in.put(Conduit.class, out.get(Conduit.class));
        }
    }
}
 
Example 11
Source File: FailoverTargetSelector.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean performFailover(Exchange exchange, InvocationContext invocation) {
    Exception prevExchangeFault = (Exception)exchange.remove(Exception.class.getName());
    Message outMessage = exchange.getOutMessage();
    Exception prevMessageFault = outMessage.getContent(Exception.class);
    outMessage.setContent(Exception.class, null);
    overrideAddressProperty(invocation.getContext());

    Retryable retry = exchange.get(Retryable.class);
    exchange.clear();
    boolean failover = false;
    if (retry != null) {
        try {
            failover = true;
            long delay = getDelayBetweenRetries();
            if (delay > 0) {
                Thread.sleep(delay);
            }
            retry.invoke(invocation.getBindingOperationInfo(),
                         invocation.getParams(),
                         invocation.getContext(),
                         exchange);
        } catch (Exception e) {
            if (exchange.get(Exception.class) != null) {
                exchange.put(Exception.class, prevExchangeFault);
            }
            if (outMessage.getContent(Exception.class) != null) {
                outMessage.setContent(Exception.class,
                                      prevMessageFault);
            }
        }
    }
    return failover;
}
 
Example 12
Source File: MAPAggregatorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setUpExchangeOutbound(Exchange exchange,
                                   Message message,
                                   boolean outbound,
                                   boolean fault) {
    if (fault) {
        exchange.getOutFaultMessage();
    } else {
        exchange.getOutMessage();
    }
    EasyMock.expectLastCall().andReturn(outbound ? message : null).anyTimes();
    //exchange.setOutMessage(outbound ? message : new MessageImpl());
}
 
Example 13
Source File: AbstractJAXWSMethodInvoker.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void updateWebServiceContext(Exchange exchange, MessageContext ctx) {
    // Guard against wrong type associated with header list.
    // Need to copy header only if the message is going out.
    if (ctx.containsKey(Header.HEADER_LIST)
            && ctx.get(Header.HEADER_LIST) instanceof List<?>) {
        List<?> list = (List<?>) ctx.get(Header.HEADER_LIST);
        if (list != null && !list.isEmpty()) {
            SoapMessage sm = (SoapMessage) createResponseMessage(exchange);
            if (sm != null) {
                Iterator<?> iter = list.iterator();
                while (iter.hasNext()) {
                    sm.getHeaders().add((Header) iter.next());
                }
            }
        }
    }
    if (exchange.getOutMessage() != null) {
        Message out = exchange.getOutMessage();
        if (out.containsKey(Message.PROTOCOL_HEADERS)) {
            Map<String, List<String>> heads = CastUtils
                .cast((Map<?, ?>)exchange.getOutMessage().get(Message.PROTOCOL_HEADERS));
            if (heads.containsKey("Content-Type")) {
                List<String> ct = heads.get("Content-Type");
                exchange.getOutMessage().put(Message.CONTENT_TYPE, ct.get(0));
                heads.remove("Content-Type");
            }
        }
    }
}
 
Example 14
Source File: BasicAuthInterceptor.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
private Message getOutMessage(Message inMessage) {
    Exchange exchange = inMessage.getExchange();
    Message outMessage = exchange.getOutMessage();
    if (outMessage == null) {
        Endpoint endpoint = exchange.get(Endpoint.class);
        outMessage = endpoint.getBinding().createMessage();
        exchange.setOutMessage(outMessage);
    }
    outMessage.putAll(inMessage);
    return outMessage;
}
 
Example 15
Source File: ColocMessageObserver.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void onMessage(Message m) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("Processing Message at collocated endpoint.  Request message: " + m);
        }
        Exchange ex = new ExchangeImpl();
        setExchangeProperties(ex, m);

        Message inMsg = endpoint.getBinding().createMessage();
        MessageImpl.copyContent(m, inMsg);

        //Copy Request Context to Server inBound Message
        //TODO a Context Filter Strategy required.
        inMsg.putAll(m);

        inMsg.put(COLOCATED, Boolean.TRUE);
        inMsg.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
        inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
        BindingOperationInfo boi = ex.getBindingOperationInfo();
        OperationInfo oi = boi != null ? boi.getOperationInfo() : null;
        if (oi != null) {
            inMsg.put(MessageInfo.class, oi.getInput());
        }
        ex.setInMessage(inMsg);
        inMsg.setExchange(ex);

        if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Build inbound interceptor chain.");
        }

        //Add all interceptors between USER_LOGICAL and INVOKE.
        SortedSet<Phase> phases = new TreeSet<>(bus.getExtension(PhaseManager.class).getInPhases());
        ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.INVOKE);
        InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, phases);
        chain.add(addColocInterceptors());
        inMsg.setInterceptorChain(chain);

        //Convert the coloc object type if necessary
        BindingOperationInfo bop = m.getExchange().getBindingOperationInfo();
        OperationInfo soi = bop != null ? bop.getOperationInfo() : null;
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)) {
                // converting source -> pojo
                ColocUtil.convertSourceToObject(inMsg);
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(inMsg);
            }
        }
        chain.doIntercept(inMsg);
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(ex.getOutMessage());
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertSourceToObject(ex.getOutMessage());
            }
        }
        //Set Server OutBound Message onto InBound Exchange.
        setOutBoundMessage(ex, m.getExchange());
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
 
Example 16
Source File: FailoverTargetSelector.java    From cxf with 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 17
Source File: JMSConduit.java    From cxf with 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);
    }
}
 
Example 18
Source File: InternalContextUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Rebase response on replyTo
 *
 * @param reference the replyTo reference
 * @param inMAPs the inbound MAPs
 * @param inMessage the current message
 */
//CHECKSTYLE:OFF Max executable statement count limitation
public static void rebaseResponse(EndpointReferenceType reference,
                                  AddressingProperties inMAPs,
                                  final Message inMessage) {

    String namespaceURI = inMAPs.getNamespaceURI();
    if (!ContextUtils.retrievePartialResponseSent(inMessage)) {
        ContextUtils.storePartialResponseSent(inMessage);
        Exchange exchange = inMessage.getExchange();
        Message fullResponse = exchange.getOutMessage();
        Message partialResponse = ContextUtils.createMessage(exchange);
        ensurePartialResponseMAPs(partialResponse, namespaceURI);

        // ensure the inbound MAPs are available in the partial response
        // message (used to determine relatesTo etc.)
        ContextUtils.propogateReceivedMAPs(inMAPs, partialResponse);
        Destination target = inMessage.getDestination();
        if (target == null) {
            return;
        }

        try {
            if (reference == null) {
                reference = ContextUtils.getNoneEndpointReference();
            }
            Conduit backChannel = target.getBackChannel(inMessage);
            if (backChannel != null) {
                partialResponse.put(Message.PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE);
                partialResponse.put(Message.EMPTY_PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE);
                boolean robust = MessageUtils.getContextualBoolean(inMessage, Message.ROBUST_ONEWAY, false);

                if (robust) {
                    BindingOperationInfo boi = exchange.getBindingOperationInfo();
                    // insert the executor in the exchange to fool the OneWayProcessorInterceptor
                    exchange.put(Executor.class, getExecutor(inMessage));
                    // pause dispatch on current thread and resume...
                    inMessage.getInterceptorChain().pause();
                    inMessage.getInterceptorChain().resume();
                    // restore the BOI for the partial response handling
                    exchange.put(BindingOperationInfo.class, boi);
                }


                // set up interceptor chains and send message
                InterceptorChain chain =
                    fullResponse != null
                    ? fullResponse.getInterceptorChain()
                    : OutgoingChainInterceptor.getOutInterceptorChain(exchange);
                exchange.setOutMessage(partialResponse);
                partialResponse.setInterceptorChain(chain);
                exchange.put(ConduitSelector.class,
                             new PreexistingConduitSelector(backChannel,
                                                            exchange.getEndpoint()));

                if (chain != null && !chain.doIntercept(partialResponse)
                    && partialResponse.getContent(Exception.class) != null) {
                    if (partialResponse.getContent(Exception.class) instanceof Fault) {
                        throw (Fault)partialResponse.getContent(Exception.class);
                    }
                    throw new Fault(partialResponse.getContent(Exception.class));
                }
                if (chain != null) {
                    chain.reset();
                }
                exchange.put(ConduitSelector.class, new NullConduitSelector());

                if (fullResponse == null) {
                    fullResponse = ContextUtils.createMessage(exchange);
                }
                exchange.setOutMessage(fullResponse);

                Destination destination = createDecoupledDestination(
                    exchange,
                    reference);
                exchange.setDestination(destination);

            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, "SERVER_TRANSPORT_REBASE_FAILURE_MSG", e);
        }
    }
}
 
Example 19
Source File: AbstractJAXWSHandlerInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private boolean isOutbound(T message, Exchange ex) {
    return message == ex.getOutMessage()
        || message == ex.getOutFaultMessage();
}
 
Example 20
Source File: ContextUtils.java    From cxf with Apache License 2.0 2 votes vote down vote up
/**
 * Determine if message is outbound.
 *
 * @param message the current Message
 * @return true iff the message direction is outbound
 */
public static boolean isOutbound(Message message) {
    Exchange exchange = message.getExchange();
    return exchange != null
           && message == exchange.getOutMessage();
}