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

The following examples show how to use org.apache.cxf.message.MessageImpl#get() . 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: CdiResourceProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void assertNotSingleton() {
    final ResourceProvider provider = new PerRequestResourceProvider(
    () -> new Lifecycle(beanManager, bean), Object.class);
    assertFalse(new JAXRSCdiResourceExtension().isCxfSingleton(beanManager, bean));
    assertFalse(provider.isSingleton());

    final MessageImpl message1 = new MessageImpl();
    final MessageImpl message2 = new MessageImpl();
    final Object instance = provider.getInstance(message1);
    assertNotNull(instance);
    assertNotEquals(provider.getInstance(message1), provider.getInstance(message2));

    // ensure we can close the lifecycle
    final Lifecycle lifecycle1 = message1.get(Lifecycle.class);
    assertNotNull(lifecycle1);
    assertNotNull(message2.get(Lifecycle.class));
}
 
Example 2
Source File: LocalDestinationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the status code is available after closing the destination so that it can be logged.
 * Note that this test verifies the current approach of setting the status code if it is not set earlier.
 *
 * @throws Exception
 */
@Test
public void testStatusCodeSetAfterClose() throws Exception {
    Bus bus = BusFactory.getDefaultBus();
    LocalTransportFactory factory = new LocalTransportFactory();

    EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/soap/http");
    ei.setAddress("http://localhost/test");

    LocalDestination d = (LocalDestination) factory.getDestination(ei, bus);
    MessageImpl m = new MessageImpl();

    Conduit conduit = factory.getConduit(ei, bus);
    m.put(LocalConduit.IN_CONDUIT, conduit);
    Exchange ex = new ExchangeImpl();
    ex.put(Bus.class, bus);
    m.setExchange(ex);

    Integer code = (Integer)m.get(Message.RESPONSE_CODE);
    assertNull(code);

    Conduit backChannel = d.getBackChannel(m);

    backChannel.close(m);

    code = (Integer)m.get(Message.RESPONSE_CODE);
    assertNotNull(code);
    assertEquals(200, code.intValue());
}
 
Example 3
Source File: AttachmentSerializerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageMTOM() throws Exception {
    MessageImpl msg = new MessageImpl();

    Collection<Attachment> atts = new ArrayList<>();
    AttachmentImpl a = new AttachmentImpl("test.xml");

    InputStream is = getClass().getResourceAsStream("my.wav");
    ByteArrayDataSource ds = new ByteArrayDataSource(is, "application/octet-stream");
    a.setDataHandler(new DataHandler(ds));

    atts.add(a);

    msg.setAttachments(atts);

    // Set the SOAP content type
    msg.put(Message.CONTENT_TYPE, "application/soap+xml");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.setContent(OutputStream.class, out);

    AttachmentSerializer serializer = new AttachmentSerializer(msg);

    serializer.writeProlog();

    String ct = (String) msg.get(Message.CONTENT_TYPE);
    assertTrue(ct.indexOf("multipart/related;") == 0);
    assertTrue(ct.indexOf("start=\"<[email protected]>\"") > -1);
    assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1);

    out.write("<soap:Body/>".getBytes());

    serializer.writeAttachments();

    out.flush();
    DataSource source = new ByteArrayDataSource(new ByteArrayInputStream(out.toByteArray()), ct);
    MimeMultipart mpart = new MimeMultipart(source);
    Session session = Session.getDefaultInstance(new Properties());
    MimeMessage inMsg = new MimeMessage(session);
    inMsg.setContent(mpart);

    inMsg.addHeaderLine("Content-Type: " + ct);

    MimeMultipart multipart = (MimeMultipart) inMsg.getContent();

    MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
    assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\"",
                 part.getHeader("Content-Type")[0]);
    assertEquals("binary", part.getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("<[email protected]>", part.getHeader("Content-ID")[0]);

    InputStream in = part.getDataHandler().getInputStream();
    ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
    IOUtils.copy(in, bodyOut);
    out.close();
    in.close();

    assertEquals("<soap:Body/>", bodyOut.toString());

    MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1);
    assertEquals("application/octet-stream", part2.getHeader("Content-Type")[0]);
    assertEquals("binary", part2.getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);

}