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

The following examples show how to use org.apache.cxf.message.Exchange#setOutMessage() . 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 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 2
Source File: RMSoapOutInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private SoapMessage setupOutboundMessage() throws Exception {
    Exchange ex = new ExchangeImpl();
    Message message = new MessageImpl();
    SoapMessage soapMessage = new SoapMessage(message);
    RMProperties rmps = new RMProperties();
    rmps.exposeAs(RM10Constants.NAMESPACE_URI);
    RMContextUtils.storeRMProperties(soapMessage, rmps, true);
    AddressingProperties maps = new AddressingProperties();
    RMContextUtils.storeMAPs(maps, soapMessage, true, false);
    ex.setOutMessage(soapMessage);
    soapMessage.setExchange(ex);
    MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
    SOAPMessage soap = factory.createMessage();
    QName bodyName = new QName("http://cxf.apache.org", "dummy", "d");
    soap.getSOAPBody().addBodyElement(bodyName);
    soapMessage.setContent(SOAPMessage.class, soap);
    return soapMessage;
}
 
Example 3
Source File: MessageIdAsCorrelationIdJMSConduitTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void sendAndReceive(boolean synchronous, String replyDestination) throws InterruptedException {
    EndpointReferenceType target = new EndpointReferenceType();

    TestReceiver receiver = new TestReceiver(connectionFactory, SERVICE_QUEUE, true);
    receiver.runAsync();

    JMSConfiguration jmsConfig = new JMSConfiguration();
    jmsConfig.setTargetDestination(SERVICE_QUEUE);
    jmsConfig.setConnectionFactory(connectionFactory);
    jmsConfig.setUseConduitIdSelector(false);
    jmsConfig.setReplyDestination(replyDestination);

    JMSConduit conduit = new JMSConduit(target, jmsConfig, BusFactory.getDefaultBus());
    Exchange exchange = new ExchangeImpl();
    exchange.setSynchronous(synchronous);
    Message message = new MessageImpl();
    exchange.setOutMessage(message);
    conduit.sendExchange(exchange, "Request");
    waitForAsyncReply(exchange);
    receiver.close();
    assertNotNull("No reply received within 2 seconds", exchange.getInMessage());
    JMSMessageHeadersType inHeaders = (JMSMessageHeadersType)exchange.getInMessage()
        .get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
    assertEquals(receiver.getRequestMessageId(), inHeaders.getJMSCorrelationID());
    conduit.close();
}
 
Example 4
Source File: ProviderFactoryTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Message prepareFaultMessage(String contentType, String acceptType) {
    Message message = new MessageImpl();
    Map<String, List<String>> headers = new MetadataMap<String, String>();
    message.put(Message.PROTOCOL_HEADERS, headers);
    Exchange exchange = new ExchangeImpl();
    exchange.setInMessage(null);
    exchange.setInFaultMessage(message);
    if (acceptType != null) {
        headers.put("Accept", Collections.singletonList(acceptType));
        exchange.setOutMessage(new MessageImpl());
    } else {
        headers.put("Content-Type", Collections.singletonList(contentType));
    }
    message.put("Content-Type", contentType);
    message.setExchange(exchange);
    return message;
}
 
Example 5
Source File: AbstractJMSTester.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static void sendoutMessage(Conduit conduit,
                              Message message,
                              boolean isOneWay,
                              boolean synchronous) throws IOException {
    final Exchange exchange = new ExchangeImpl();
    exchange.setOneWay(isOneWay);
    exchange.setSynchronous(synchronous);
    message.setExchange(exchange);
    exchange.setOutMessage(message);
    conduit.prepare(message);
    try (OutputStream os = message.getContent(OutputStream.class)) {
        if (os != null) {
            os.write(MESSAGE_CONTENT.getBytes()); // TODO encoding
            return;
        }
    }
    try (Writer writer = message.getContent(Writer.class)) {
        if (writer != null) {
            writer.write(MESSAGE_CONTENT);
            return;
        }
    }
    fail("The OutputStream and Writer should not both be null");
}
 
Example 6
Source File: ServiceInvokerInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInterceptor() throws Exception {
    ServiceInvokerInterceptor intc = new ServiceInvokerInterceptor();

    MessageImpl m = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    m.setExchange(exchange);
    exchange.setInMessage(m);

    exchange.setOutMessage(new MessageImpl());

    TestInvoker i = new TestInvoker();
    Endpoint endpoint = createEndpoint(i);
    exchange.put(Endpoint.class, endpoint);
    Object input = new Object();
    List<Object> lst = new ArrayList<>();
    lst.add(input);
    m.setContent(List.class, lst);

    intc.handleMessage(m);

    assertTrue(i.invoked);

    List<?> list = exchange.getOutMessage().getContent(List.class);
    assertEquals(input, list.get(0));
}
 
Example 7
Source File: MAPAggregatorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test(expected = SoapFault.class)
public void testTwoWayRequestWithReplyToNone() throws Exception {
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    exchange.setOutMessage(message);
    message.setExchange(exchange);
    setUpMessageProperty(message,
                         REQUESTOR_ROLE,
                         Boolean.FALSE);
    AddressingProperties maps = new AddressingProperties();
    EndpointReferenceType replyTo = new EndpointReferenceType();
    replyTo.setAddress(ContextUtils.getAttributedURI(Names.WSA_NONE_ADDRESS));
    maps.setReplyTo(replyTo);
    AttributedURIType id =
        ContextUtils.getAttributedURI("urn:uuid:12345");
    maps.setMessageID(id);
    maps.setAction(ContextUtils.getAttributedURI(""));
    setUpMessageProperty(message,
                         ADDRESSING_PROPERTIES_OUTBOUND,
                         maps);
    setUpMessageProperty(message,
                         "org.apache.cxf.ws.addressing.map.fault.name",
                         "NoneAddress");

    aggregator.mediate(message, false);
}
 
Example 8
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 9
Source File: BasicAuthenticationInterceptorTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
private Message createEmptyMessage() {
    Exchange exchange = new ExchangeImpl();
    exchange.setInMessage(inMessageMock);
    exchange.setOutMessage(outMessageMock);
    exchange.setDestination(destinationMock);

    Message message = new MessageImpl();
    message.setExchange(exchange);
    message.setInterceptorChain(interceptorChainMock);
    return message;
}
 
Example 10
Source File: ContextUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsOutbound() throws Exception {
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    exchange.setOutMessage(message);
    message.setExchange(exchange);

    assertTrue(ContextUtils.isOutbound(message));
}
 
Example 11
Source File: OutgoingChainInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterceptor() throws Exception {
    OutgoingChainInterceptor intc = new OutgoingChainInterceptor();

    MessageImpl m = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    m.setExchange(exchange);
    exchange.put(Bus.class, bus);
    exchange.put(Endpoint.class, endpoint);
    exchange.put(Binding.class, binding);
    exchange.put(BindingOperationInfo.class, bopInfo);
    exchange.setOutMessage(m);
    intc.handleMessage(m);
}
 
Example 12
Source File: RMCaptureOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setTerminateSequence(Message msg, Identifier identifier, ProtocolVariation protocol)
    throws RMException {
    TerminateSequenceType ts = new TerminateSequenceType();
    ts.setIdentifier(identifier);
    MessageContentsList contents =
        new MessageContentsList(new Object[]{protocol.getCodec().convertToSend(ts)});
    msg.setContent(List.class, contents);

    // create a new exchange for this output-only exchange
    Exchange newex = new ExchangeImpl();
    Exchange oldex = msg.getExchange();

    newex.put(Bus.class, oldex.getBus());
    newex.put(Endpoint.class, oldex.getEndpoint());
    newex.put(Service.class, oldex.getEndpoint().getService());
    newex.put(Binding.class, oldex.getEndpoint().getBinding());
    newex.setConduit(oldex.getConduit(msg));
    newex.setDestination(oldex.getDestination());

    //Setup the BindingOperationInfo
    RMEndpoint rmep = getManager().getReliableEndpoint(msg);
    OperationInfo oi = rmep.getEndpoint(protocol).getEndpointInfo().getService().getInterface()
        .getOperation(protocol.getConstants().getTerminateSequenceAnonymousOperationName());
    BindingInfo bi = rmep.getBindingInfo(protocol);
    BindingOperationInfo boi = bi.getOperation(oi);

    newex.put(BindingInfo.class, bi);
    newex.put(BindingOperationInfo.class, boi);

    msg.setExchange(newex);
    newex.setOutMessage(msg);
}
 
Example 13
Source File: MessageContextImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPropertyFromOtherMessage() {
    Message m1 = new MessageImpl();
    Message m2 = new MessageImpl();
    m2.put("a", "b");

    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m1);
    ex.setOutMessage(m2);
    MessageContext mc = new MessageContextImpl(m1);
    assertEquals("b", mc.get("a"));
    assertNull(mc.get("b"));
}
 
Example 14
Source File: ContextPropertiesMappingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWebServiceContextWithInAttachments() {
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();

    Collection<Attachment> attachments = new LinkedList<>();

    DataSource source = new ByteDataSource(new byte[0], "text/xml");

    DataHandler handler1 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part1", handler1));
    DataHandler handler2 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part2", handler2));
    inMessage.setAttachments(attachments);

    inMessage.putAll(message);
    exchange.setInMessage(inMessage);
    exchange.setOutMessage(new MessageImpl());

    MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);

    Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
    assertNotNull("inbound attachments object must be initialized", inAttachments);
    assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
    Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>)inAttachments);
    assertEquals("two inbound attachments expected", 2, dataHandlers.size());

    assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
    // should do as it's the same instance
    assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
    assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
    assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
 
Example 15
Source File: GZIPAcceptEncodingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    interceptor = new GZIPOutInterceptor();
    inMessage = new MessageImpl();
    outMessage = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    exchange.setInMessage(inMessage);
    inMessage.setExchange(exchange);
    inMessage.setContent(InputStream.class, new ByteArrayInputStream(new byte[0]));
    exchange.setOutMessage(outMessage);
    outMessage.setExchange(exchange);
    outMessage.setContent(OutputStream.class, new ByteArrayOutputStream());
    outInterceptors = EasyMock.createMock(InterceptorChain.class);
    outMessage.setInterceptorChain(outInterceptors);
}
 
Example 16
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 17
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 18
Source File: MAPCodecTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void setUpOutbound(Message message, boolean outbound) {
    Exchange exchange = new ExchangeImpl();
    exchange.setOutMessage(outbound ? message : new MessageImpl());
    message.setExchange(exchange);
}
 
Example 19
Source File: MAPAggregatorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetReplyToUsingBaseAddress() throws Exception {
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);

    final String localReplyTo = "/SoapContext/decoupled";
    final String decoupledEndpointBase = "http://localhost:8181/cxf";
    final String replyTo = decoupledEndpointBase + localReplyTo;

    ServiceInfo s = new ServiceInfo();
    Service svc = new ServiceImpl(s);
    EndpointInfo ei = new EndpointInfo();
    InterfaceInfo ii = s.createInterface(new QName("FooInterface"));
    s.setInterface(ii);
    ii.addOperation(new QName("fooOp"));
    SoapBindingInfo b = new SoapBindingInfo(s, "http://schemas.xmlsoap.org/soap/", Soap11.getInstance());
    b.setTransportURI("http://schemas.xmlsoap.org/soap/http");
    ei.setBinding(b);

    ei.setAddress("http://nowhere.com/bar/foo");
    ei.setName(new QName("http://nowhere.com/port", "foo"));
    Bus bus = new ExtensionManagerBus();
    DestinationFactoryManager dfm = control.createMock(DestinationFactoryManager.class);
    DestinationFactory df = control.createMock(DestinationFactory.class);
    Destination d = control.createMock(Destination.class);
    bus.setExtension(dfm, DestinationFactoryManager.class);
    EasyMock.expect(dfm.getDestinationFactoryForUri(localReplyTo)).andReturn(df);
    EasyMock.expect(df.getDestination(
        EasyMock.anyObject(EndpointInfo.class), EasyMock.anyObject(Bus.class))).andReturn(d);
    EasyMock.expect(d.getAddress()).andReturn(EndpointReferenceUtils.getEndpointReference(localReplyTo));

    Endpoint ep = new EndpointImpl(bus, svc, ei);
    exchange.put(Endpoint.class, ep);
    exchange.put(Bus.class, bus);
    exchange.setOutMessage(message);
    setUpMessageProperty(message,
                         REQUESTOR_ROLE,
                         Boolean.TRUE);
    message.getContextualProperty(WSAContextUtils.REPLYTO_PROPERTY);
    message.put(WSAContextUtils.REPLYTO_PROPERTY, localReplyTo);
    message.put(WSAContextUtils.DECOUPLED_ENDPOINT_BASE_PROPERTY, decoupledEndpointBase);

    AddressingProperties maps = new AddressingProperties();
    AttributedURIType id =
        ContextUtils.getAttributedURI("urn:uuid:12345");
    maps.setMessageID(id);
    maps.setAction(ContextUtils.getAttributedURI(""));
    setUpMessageProperty(message,
                         CLIENT_ADDRESSING_PROPERTIES,
                         maps);
    control.replay();
    aggregator.mediate(message, false);
    AddressingProperties props =
        (AddressingProperties)message.get(JAXWSAConstants.ADDRESSING_PROPERTIES_OUTBOUND);

    assertEquals(replyTo, props.getReplyTo().getAddress().getValue());
    control.verify();
}
 
Example 20
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);
        }
    }
}