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

The following examples show how to use org.apache.cxf.message.message#setExchange() . 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: 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 2
Source File: InjectionUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static Message createMessage() {
    ProviderFactory factory = ServerProviderFactory.getInstance();
    Message m = new MessageImpl();
    m.put("org.apache.cxf.http.case_insensitive_queries", false);
    Exchange e = new ExchangeImpl();
    m.setExchange(e);
    e.setInMessage(m);
    Endpoint endpoint = EasyMock.mock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
    EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
    EasyMock.replay(endpoint);
    e.put(Endpoint.class, endpoint);
    return m;
}
 
Example 3
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Message createMessage() {
    ServerProviderFactory factory = ServerProviderFactory.getInstance();
    Message m = new MessageImpl();
    m.put(Message.ENDPOINT_ADDRESS, "http://localhost:8080/bar");
    m.put("org.apache.cxf.http.case_insensitive_queries", false);
    Exchange e = new ExchangeImpl();
    m.setExchange(e);
    e.setInMessage(m);
    Endpoint endpoint = EasyMock.mock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
    EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
    EasyMock.replay(endpoint);
    e.put(Endpoint.class, endpoint);
    return m;
}
 
Example 4
Source File: LoggingOutInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private CachedOutputStream handleAndGetCachedOutputStream(LoggingOutInterceptor interceptor) {
    interceptor.setPrintWriter(new PrintWriter(new ByteArrayOutputStream()));

    Endpoint endpoint = control.createMock(Endpoint.class);
    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    BindingInfo bindingInfo = control.createMock(BindingInfo.class);
    EasyMock.expect(endpointInfo.getBinding()).andReturn(bindingInfo).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(bindingInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    control.replay();

    Message message = new MessageImpl();
    ExchangeImpl exchange = new ExchangeImpl();
    message.setExchange(exchange);
    exchange.put(Endpoint.class, endpoint);

    message.put(Message.CONTENT_TYPE, "application/xml");
    message.setContent(OutputStream.class, new ByteArrayOutputStream());
    interceptor.handleMessage(message);
    OutputStream os = message.getContent(OutputStream.class);
    assertTrue(os instanceof CachedOutputStream);
    return (CachedOutputStream)os;
}
 
Example 5
Source File: PerRequestResourceProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Message createMessage() {
    ProviderFactory factory = ServerProviderFactory.getInstance();
    Message m = new MessageImpl();
    m.put("org.apache.cxf.http.case_insensitive_queries", false);
    Exchange e = new ExchangeImpl();
    m.setExchange(e);
    e.setInMessage(m);
    Endpoint endpoint = EasyMock.mock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
    EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
    EasyMock.replay(endpoint);
    e.put(Endpoint.class, endpoint);
    return m;
}
 
Example 6
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 7
Source File: LogicalMessageImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetPayloadOfJAXB() throws Exception {
    //using Dispatch
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
    Message message = new MessageImpl();
    Exchange e = new ExchangeImpl();
    message.setExchange(e);
    LogicalMessageContextImpl lmci = new LogicalMessageContextImpl(message);

    JAXBElement<AddNumbers> el = new ObjectFactory().createAddNumbers(req);

    LogicalMessageImpl lmi = new LogicalMessageImpl(lmci);
    lmi.setPayload(el, ctx);

    Object obj = lmi.getPayload(ctx);
    assertTrue(obj instanceof JAXBElement);
    JAXBElement<?> el2 = (JAXBElement<?>)obj;
    assertTrue(el2.getValue() instanceof AddNumbers);
    AddNumbers resp = (AddNumbers)el2.getValue();
    assertEquals(req.getArg0(), resp.getArg0());
    assertEquals(req.getArg1(), resp.getArg1());
}
 
Example 8
Source File: JsonpInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonWithPaddingCustomCallbackParam() throws Exception {
    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    message.setExchange(new ExchangeImpl());
    message.put(Message.QUERY_STRING, "_customjsonp=myCallback");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    message.setContent(OutputStream.class, bos);

    // Process the message
    try {
        in.setCallbackParam("_customjsonp");
        in.handleMessage(message);
        preStream.handleMessage(message);
        postStream.handleMessage(message);
        assertEquals("myCallback();", bos.toString());
    } finally {
        in.setCallbackParam("_jsonp");
    }

}
 
Example 9
Source File: TruncatedTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void truncatedInboundInterceptorInputStream() throws IOException {

    Message message = new MessageImpl();
    ByteArrayInputStream inputStream = new ByteArrayInputStream("TestMessage".getBytes(StandardCharsets.UTF_8));
    message.setContent(InputStream.class, inputStream);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    LogEventSenderMock logEventSender = new LogEventSenderMock();
    LoggingInInterceptor interceptor = new LoggingInInterceptor(logEventSender);
    interceptor.setLimit(1); // set limit to 1 byte in order to get a truncated message!

    Collection<PhaseInterceptor<? extends Message>> interceptors = interceptor.getAdditionalInterceptors();
    for (PhaseInterceptor intercept : interceptors) {
        intercept.handleMessage(message);
    }

    interceptor.handleMessage(message);

    LogEvent event = logEventSender.getLogEvent();
    assertNotNull(event);
    assertEquals("T", event.getPayload()); // only the first byte is read!
    assertTrue(event.isTruncated());
}
 
Example 10
Source File: NettyHttpDestinationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message setUpOutMessage() {
    Message outMsg = new MessageImpl();
    outMsg.putAll(inMessage);
    outMsg.setExchange(new ExchangeImpl());
    outMsg.put(Message.PROTOCOL_HEADERS,
               new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER));
    return outMsg;
}
 
Example 11
Source File: RequestPreprocessorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message mockMessage(String baseAddress,
                            String pathInfo,
                            String query,
                            String method,
                            String methodHeader) {
    Message m = new MessageImpl();
    m.put("org.apache.cxf.http.case_insensitive_queries", false);
    m.put("org.apache.cxf.endpoint.private", false);
    Exchange e = new ExchangeImpl();
    m.setExchange(e);
    control.reset();
    Endpoint endp = control.mock(Endpoint.class);
    e.put(Endpoint.class, endp);
    EasyMock.expect(endp.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endp.get(ServerProviderFactory.class.getName())).andReturn(ServerProviderFactory.getInstance())
            .anyTimes();
    ServletDestination d = control.createMock(ServletDestination.class);
    e.setDestination(d);
    EndpointInfo epr = new EndpointInfo();
    epr.setAddress(baseAddress);
    EasyMock.expect(d.getEndpointInfo()).andReturn(epr).anyTimes();
    EasyMock.expect(endp.getEndpointInfo()).andReturn(epr).anyTimes();
    m.put(Message.REQUEST_URI, pathInfo);
    m.put(Message.QUERY_STRING, query);
    m.put(Message.HTTP_REQUEST_METHOD, method);
    Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    if (methodHeader != null) {
        headers.put("X-HTTP-Method-Override", Collections.singletonList(methodHeader));
    }
    m.put(Message.PROTOCOL_HEADERS, headers);
    BindingInfo bi = control.createMock(BindingInfo.class);
    epr.setBinding(bi);
    EasyMock.expect(bi.getProperties()).andReturn(Collections.emptyMap()).anyTimes();

    control.replay();
    return m;
}
 
Example 12
Source File: WadlGeneratorJsonTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message mockMessage(String baseAddress, String pathInfo, String query,
                            ClassResourceInfo cri) throws Exception {
    Message m = new MessageImpl();
    Exchange e = new ExchangeImpl();
    e.put(Service.class, new JAXRSServiceImpl(Collections.singletonList(cri)));
    m.setExchange(e);
    control.reset();
    ServletDestination d = control.createMock(ServletDestination.class);
    EndpointInfo epr = new EndpointInfo();
    epr.setAddress(baseAddress);
    d.getEndpointInfo();
    EasyMock.expectLastCall().andReturn(epr).anyTimes();

    Endpoint endpoint = new EndpointImpl(null, null, epr);
    e.put(Endpoint.class, endpoint);

    e.setDestination(d);
    BindingInfo bi = control.createMock(BindingInfo.class);
    epr.setBinding(bi);
    bi.getProperties();
    EasyMock.expectLastCall().andReturn(Collections.emptyMap()).anyTimes();
    m.put(Message.REQUEST_URI, pathInfo);
    m.put(Message.QUERY_STRING, query);
    m.put(Message.HTTP_REQUEST_METHOD, "GET");
    control.replay();
    return m;
}
 
Example 13
Source File: JMSContinuationProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoContinuationForOneWay() {
    Exchange exchange = new ExchangeImpl();
    exchange.setOneWay(true);
    Message m = new MessageImpl();
    m.setExchange(exchange);
    Counter counter = EasyMock.createMock(Counter.class);
    JMSContinuationProvider provider =
        new JMSContinuationProvider(null, m, null, counter);
    assertNull(provider.getContinuation());
}
 
Example 14
Source File: DefaultLogEventMapperTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testRest() {
    DefaultLogEventMapper mapper = new DefaultLogEventMapper();
    Message message = new MessageImpl();
    message.put(Message.HTTP_REQUEST_METHOD, "GET");
    message.put(Message.REQUEST_URI, "test");
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    LogEvent event = mapper.map(message);
    Assert.assertEquals("GET[test]", event.getOperationName());
}
 
Example 15
Source File: RequestResponseTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void sendAndReceiveMessages(EndpointInfo ei, boolean synchronous)
        throws IOException, InterruptedException {
    // set up the conduit send to be true
    JMSConduit conduit = setupJMSConduitWithObserver(ei);
    final Message outMessage = createMessage();
    final JMSDestination destination = setupJMSDestination(ei);

    MessageObserver observer = new MessageObserver() {
        public void onMessage(Message m) {
            Exchange exchange = new ExchangeImpl();
            exchange.setInMessage(m);
            m.setExchange(exchange);
            verifyReceivedMessage(m);
            verifyHeaders(m, outMessage);
            // setup the message for
            try {
                Conduit backConduit = destination.getBackChannel(m);
                // wait for the message to be got from the conduit
                Message replyMessage = new MessageImpl();
                sendOneWayMessage(backConduit, replyMessage);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    destination.setMessageObserver(observer);

    try {
        sendMessage(conduit, outMessage, synchronous);
        // wait for the message to be got from the destination,
        // create the thread to handler the Destination incoming message

        verifyReceivedMessage(waitForReceiveInMessage());
    } finally {
        conduit.close();
        destination.shutdown();
    }
}
 
Example 16
Source File: HttpUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestGetBaseAddress(String baseURI, String expected) {
    Message m = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    m.setExchange(exchange);
    Destination dest = EasyMock.createMock(Destination.class);
    exchange.setDestination(dest);
    m.put(Message.BASE_PATH, baseURI);
    String address = HttpUtils.getBaseAddress(m);
    assertEquals(expected, address);
}
 
Example 17
Source File: JsonpInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonWithDefaultPadding() throws Exception {
    Message message = new MessageImpl();
    message.put(Message.ACCEPT_CONTENT_TYPE, JsonpInInterceptor.JSONP_TYPE);
    message.setExchange(new ExchangeImpl());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    message.setContent(OutputStream.class, bos);

    // Process the message
    in.handleMessage(message);
    preStream.handleMessage(message);
    postStream.handleMessage(message);
    assertEquals("callback();", bos.toString());
}
 
Example 18
Source File: MAPAggregatorImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Called for an incoming message.
 *
 * @param inMessage
 */
public void onMessage(Message inMessage) {
    // disposable exchange, swapped with real Exchange on correlation
    inMessage.setExchange(new ExchangeImpl());
    inMessage.getExchange().put(Bus.class, bus);
    inMessage.put(Message.DECOUPLED_CHANNEL_MESSAGE, Boolean.TRUE);
    inMessage.put(Message.RESPONSE_CODE, HttpURLConnection.HTTP_OK);

    // remove server-specific properties
    //inMessage.remove(AbstractHTTPDestination.HTTP_REQUEST);
    //inMessage.remove(AbstractHTTPDestination.HTTP_RESPONSE);
    inMessage.remove(Message.ASYNC_POST_RESPONSE_DISPATCH);
    updateResponseCode(inMessage);

    //cache this inputstream since it's defer to use in case of async
    try {
        InputStream in = inMessage.getContent(InputStream.class);
        if (in != null) {
            CachedOutputStream cos = new CachedOutputStream();
            IOUtils.copy(in, cos);
            inMessage.setContent(InputStream.class, cos.getInputStream());
        }
        observer.onMessage(inMessage);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: SelectMethodCandidatesTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindFromAbstractGenericClass3() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(BookEntity.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl)sf.getService()).getClassResourceInfos();
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";

    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, "text/xml");
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);

    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m,
                                                        "/books",
                                                        "PUT",
                                                        values, contentTypes,
                                                        sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "putEntity",
                 ori.getMethodToInvoke().getName());

    String value = "<Chapter><title>The Book</title><id>2</id></Chapter>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    Chapter c = (Chapter)params.get(0);
    assertNotNull(c);
    assertEquals(2L, c.getId());
    assertEquals("The Book", c.getTitle());
}
 
Example 20
Source File: JMSDestinationTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
    public void testProperty() throws Exception {
        EndpointInfo ei = setupServiceInfo("HelloWorldService", "HelloWorldPort");
        final String customPropertyName = "THIS_PROPERTY_WILL_NOT_BE_AUTO_COPIED";

        // set up the conduit send to be true
        JMSConduit conduit = setupJMSConduitWithObserver(ei);
        final Message outMessage = createMessage();

        JMSMessageHeadersType headers = (JMSMessageHeadersType)outMessage
            .get(JMSConstants.JMS_CLIENT_REQUEST_HEADERS);
        headers.putProperty(customPropertyName, customPropertyName);

        final JMSDestination destination = setupJMSDestination(ei);

        // set up MessageObserver for handling the conduit message
        MessageObserver observer = new MessageObserver() {
            public void onMessage(Message m) {
                Exchange exchange = new ExchangeImpl();
                exchange.setInMessage(m);
                m.setExchange(exchange);
                verifyReceivedMessage(m);
                verifyHeaders(m, outMessage);
                // setup the message for
                Conduit backConduit;
                try {
                    backConduit = destination.getBackChannel(m);
                    // wait for the message to be got from the conduit
                    Message replyMessage = new MessageImpl();
                    // copy the message encoding
                    replyMessage.put(Message.ENCODING, m.get(Message.ENCODING));
                    sendOneWayMessage(backConduit, replyMessage);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
        destination.setMessageObserver(observer);
        sendMessageSync(conduit, outMessage);
        // wait for the message to be got from the destination,
        // create the thread to handler the Destination incoming message

        Message inMessage = waitForReceiveInMessage();
        verifyReceivedMessage(inMessage);

        verifyRequestResponseHeaders(inMessage, outMessage);

        JMSMessageHeadersType inHeader = (JMSMessageHeadersType)inMessage
            .get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
        assertNotNull("The inHeader should not be null", inHeader);
        // TODO we need to check the SOAP JMS transport properties here

        // wait for a while for the jms session recycling
//        Thread.sleep(1000L);
        conduit.close();
        destination.shutdown();
    }