Java Code Examples for org.apache.cxf.transport.ConduitInitiator#getConduit()

The following examples show how to use org.apache.cxf.transport.ConduitInitiator#getConduit() . 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: TestUtilities.java    From cxf with Apache License 2.0 6 votes vote down vote up
public byte[] invokeBytes(String address, String transport, byte[] message) throws Exception {
    EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/soap/http");
    ei.setAddress(address);

    ConduitInitiatorManager conduitMgr = getBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator(transport);
    Conduit conduit = conduitInit.getConduit(ei, getBus());

    TestMessageObserver obs = new TestMessageObserver();
    conduit.setMessageObserver(obs);

    Message m = new MessageImpl();
    conduit.prepare(m);

    OutputStream os = m.getContent(OutputStream.class);
    os.write(message);

    // TODO: shouldn't have to do this. IO caching needs cleaning
    // up or possibly removal...
    os.flush();
    os.close();

    return obs.getResponseStream().toByteArray();
}
 
Example 2
Source File: InternalContextUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Conduit getBackChannel(Message inMessage) throws IOException {
    if (ContextUtils.isNoneAddress(reference)) {
        return null;
    }
    Bus bus = inMessage.getExchange().getBus();
    //this is a response targeting a decoupled endpoint.   Treat it as a oneway so
    //we don't wait for a response.
    inMessage.getExchange().setOneWay(true);
    ConduitInitiator conduitInitiator
        = bus.getExtension(ConduitInitiatorManager.class)
            .getConduitInitiatorForUri(reference.getAddress().getValue());
    if (conduitInitiator != null) {
        Conduit c = conduitInitiator.getConduit(ei, reference, bus);
        // ensure decoupled back channel input stream is closed
        c.setMessageObserver(new MessageObserver() {
            public void onMessage(Message m) {
                InputStream is = m.getContent(InputStream.class);
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        });
        return c;
    }
    return null;
}
 
Example 3
Source File: InternalContextUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Conduit getBackChannel(Message inMessage) throws IOException {
    if (ContextUtils.isNoneAddress(reference)) {
        return null;
    }
    Bus bus = inMessage.getExchange().getBus();
    //this is a response targeting a decoupled endpoint.   Treat it as a oneway so
    //we don't wait for a response.
    inMessage.getExchange().setOneWay(true);
    ConduitInitiator conduitInitiator
        = bus.getExtension(ConduitInitiatorManager.class)
            .getConduitInitiatorForUri(reference.getAddress().getValue());
    if (conduitInitiator != null) {
        Conduit c = conduitInitiator.getConduit(ei, reference, bus);
        // ensure decoupled back channel input stream is closed
        c.setMessageObserver(new MessageObserver() {
            public void onMessage(Message m) {
                InputStream is = m.getContent(InputStream.class);
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        });
        return c;
    }
    return null;
}
 
Example 4
Source File: SoapTransportFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Conduit getConduit(EndpointInfo ei, EndpointReferenceType target, Bus bus) throws IOException {
    String address = target == null ? ei.getAddress() : target.getAddress().getValue();
    BindingInfo bi = ei.getBinding();
    String transId = ei.getTransportId();
    if (bi instanceof SoapBindingInfo) {
        transId = ((SoapBindingInfo)bi).getTransportURI();
        if (transId == null) {
            transId = ei.getTransportId();
        }
    }
    ConduitInitiator conduitInit;
    try {
        ConduitInitiatorManager mgr = bus.getExtension(ConduitInitiatorManager.class);
        if (StringUtils.isEmpty(address)
            || address.startsWith("http")
            || address.startsWith("jms")
            || address.startsWith("soap.udp")) {
            conduitInit = mgr.getConduitInitiator(mapTransportURI(transId, address));
        } else {
            conduitInit = mgr.getConduitInitiatorForUri(address);
        }
        if (conduitInit == null) {
            throw new RuntimeException(String.format(CANNOT_GET_CONDUIT_ERROR, address, transId));
        }
        return conduitInit.getConduit(ei, target, bus);
    } catch (BusException e) {
        throw new RuntimeException(String.format(CANNOT_GET_CONDUIT_ERROR, address, transId));
    }
}
 
Example 5
Source File: TestUtilities.java    From cxf with Apache License 2.0 5 votes vote down vote up
public byte[] invokeBytes(String address, String transport, String message) throws Exception {
    EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/soap/http");
    ei.setAddress(address);

    ConduitInitiatorManager conduitMgr = getBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator(transport);
    Conduit conduit = conduitInit.getConduit(ei, getBus());

    TestMessageObserver obs = new TestMessageObserver();
    conduit.setMessageObserver(obs);

    Message m = new MessageImpl();
    conduit.prepare(m);

    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = getResourceAsStream(message);
    if (is == null) {
        throw new RuntimeException("Could not find resource " + message);
    }

    IOUtils.copy(is, os);

    // TODO: shouldn't have to do this. IO caching needs cleaning
    // up or possibly removal...
    os.flush();
    is.close();
    os.close();

    return obs.getResponseStream().toByteArray();
}
 
Example 6
Source File: AbstractConduitSelector.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Conduit createConduit(Message message, Exchange exchange, ConduitInitiator conduitInitiator)
    throws IOException {
    Conduit c;
    synchronized (endpoint) {
        if (!conduits.isEmpty()) {
            c = findCompatibleConduit(message);
            if (c != null) {
                return c;
            }
        }
        EndpointInfo ei = endpoint.getEndpointInfo();
        String add = (String)message.get(Message.ENDPOINT_ADDRESS);
        String basePath = (String)message.get(Message.BASE_PATH);
        if (StringUtils.isEmpty(add)
            || add.equals(ei.getAddress())) {
            c = conduitInitiator.getConduit(ei, exchange.getBus());
            replaceEndpointAddressPropertyIfNeeded(message, add, c);
        } else {
            EndpointReferenceType epr = new EndpointReferenceType();
            AttributedURIType ad = new AttributedURIType();
            ad.setValue(StringUtils.isEmpty(basePath) ? add : basePath);
            epr.setAddress(ad);
            c = conduitInitiator.getConduit(ei, epr, exchange.getBus());
        }
        MessageObserver observer =
            exchange.get(MessageObserver.class);
        if (observer != null) {
            c.setMessageObserver(observer);
        } else {
            getLogger().warning("MessageObserver not found");
        }
        conduits.add(c);
    }
    return c;
}
 
Example 7
Source File: MtomServerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMtomRequest() throws Exception {
    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceBean(new EchoService());
    sf.setBus(getStaticBus());
    String address = "http://localhost:" + PORT1 + "/EchoService";
    sf.setAddress(address);
    Map<String, Object> props = new HashMap<>();
    props.put(Message.MTOM_ENABLED, "true");
    sf.setProperties(props);
    sf.create();

    EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
    ei.setAddress(address);

    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());

    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);

    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; "
                + "start=\"<[email protected]>\"; "
                + "start-info=\"text/xml\"; "
                + "boundary=\"----=_Part_4_701508.1145579811786\"";

    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);

    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }

    IOUtils.copy(is, os);

    os.flush();
    is.close();
    os.close();

    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();

    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());

    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertEquals(27364, out.size());
    }
}
 
Example 8
Source File: MtomServerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testURLBasedAttachment() throws Exception {
    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceBean(new EchoService());
    sf.setBus(getStaticBus());
    String address = "http://localhost:" + PORT2 + "/EchoService";
    sf.setAddress(address);
    Map<String, Object> props = new HashMap<>();
    props.put(Message.MTOM_ENABLED, "true");
    sf.setProperties(props);
    Server server = sf.create();
    server.getEndpoint().getService().getDataBinding().setMtomThreshold(0);

    servStatic(getClass().getResource("mtom-policy.xml"),
               "http://localhost:" + PORT2 + "/policy.xsd");

    EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
    ei.setAddress(address);

    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());

    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);

    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; "
                + "start=\"<[email protected]>\"; "
                + "start-info=\"text/xml; charset=utf-8\"; "
                + "boundary=\"----=_Part_4_701508.1145579811786\"";

    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);

    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request-url-attachment");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }
    try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        IOUtils.copy(is, bout);
        String s = bout.toString(StandardCharsets.UTF_8.name());
        s = s.replaceAll(":9036/", ":" + PORT2 + "/");

        os.write(s.getBytes(StandardCharsets.UTF_8));
    }
    os.flush();
    is.close();
    os.close();

    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();

    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());

    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertTrue("Wrong size: " + out.size()
                + "\n" + out.toString(),
                out.size() > 970 && out.size() < 1020);
    }
    unregisterServStatic("http://localhost:" + PORT2 + "/policy.xsd");

}
 
Example 9
Source File: MtomPolicyTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void sendMtomMessage(String a) throws Exception {
    EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/wsdl/http");
    ei.setAddress(a);

    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());

    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);

    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; "
                + "start=\"<[email protected]>\"; "
                + "start-info=\"text/xml; charset=utf-8\"; "
                + "boundary=\"----=_Part_4_701508.1145579811786\"";

    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);

    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }

    IOUtils.copy(is, os);

    os.flush();
    is.close();
    os.close();

    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();

    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());

    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertEquals(27364, out.size());
    }
}
 
Example 10
Source File: ContextUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static Destination createDecoupledDestination(Exchange exchange,
                                                     final EndpointReferenceType reference) {
    final EndpointInfo ei = exchange.getEndpoint().getEndpointInfo();
    return new Destination() {
        public EndpointReferenceType getAddress() {
            return reference;
        }
        public Conduit getBackChannel(Message inMessage) throws IOException {
            Bus bus = inMessage.getExchange().getBus();
            //this is a response targeting a decoupled endpoint.   Treat it as a oneway so
            //we don't wait for a response.
            inMessage.getExchange().setOneWay(true);
            ConduitInitiator conduitInitiator
                = bus.getExtension(ConduitInitiatorManager.class)
                    .getConduitInitiatorForUri(reference.getAddress().getValue());
            if (conduitInitiator != null) {
                Conduit c = conduitInitiator.getConduit(ei, reference, bus);
                //ensure decoupled back channel input stream is closed
                c.setMessageObserver(new MessageObserver() {
                    public void onMessage(Message m) {
                        InputStream is = m.getContent(InputStream.class);
                        if (is != null) {
                            try {
                                is.close();
                            } catch (Exception e) {
                                //ignore
                            }
                        }
                    }
                });
                return c;
            }
            return null;
        }
        public MessageObserver getMessageObserver() {
            return null;
        }
        public void shutdown() {
        }
        public void setMessageObserver(MessageObserver observer) {
        }
    };
}