use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.
the class MtomServerTest method testURLBasedAttachment.
@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=\"<soap.xml@xfire.codehaus.org>\"; " + "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");
}
use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.
the class MtomPolicyTest method sendMtomMessage.
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=\"<soap.xml@xfire.codehaus.org>\"; " + "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());
}
}
use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.
the class MessageModeOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
if (bop != null && !bindingName.equals(bop.getBinding().getName())) {
return;
}
if (saajOut != null) {
doSoap(message);
} else if (DataSource.class.isAssignableFrom(type)) {
// datasource stuff, must check if multi-source
MessageContentsList list = (MessageContentsList) message.getContent(List.class);
DataSource ds = (DataSource) list.get(0);
String ct = ds.getContentType();
if (ct.toLowerCase().contains("multipart/related")) {
Message msg = new MessageImpl();
msg.setExchange(message.getExchange());
msg.put(Message.CONTENT_TYPE, ct);
try {
msg.setContent(InputStream.class, ds.getInputStream());
AttachmentDeserializer deser = new AttachmentDeserializer(msg);
deser.initializeAttachments();
} catch (IOException ex) {
throw new Fault(ex);
}
message.setAttachments(msg.getAttachments());
final InputStream in = msg.getContent(InputStream.class);
final String ct2 = (String) msg.get(Message.CONTENT_TYPE);
list.set(0, new DataSource() {
public String getContentType() {
return ct2;
}
public InputStream getInputStream() throws IOException {
return in;
}
public String getName() {
return ct2;
}
public OutputStream getOutputStream() throws IOException {
return null;
}
});
} else if (!ct.toLowerCase().contains("xml")) {
// not XML based, need to stream out directly. This is a bit tricky as
// we don't want the stax stuff triggering and such
OutputStream out = message.getContent(OutputStream.class);
message.put(Message.CONTENT_TYPE, ct);
try {
InputStream in = ds.getInputStream();
IOUtils.copy(in, out);
in.close();
out.flush();
out.close();
} catch (IOException e) {
throw new Fault(e);
}
list.remove(0);
out = new CachedOutputStream();
message.setContent(OutputStream.class, out);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
message.setContent(XMLStreamWriter.class, writer);
}
} else if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && Source.class.isAssignableFrom(type)) {
// if schema validation is on, we'll end up converting to a DOMSource anyway,
// let's convert and check for a fault
MessageContentsList list = (MessageContentsList) message.getContent(List.class);
Source ds = (Source) list.get(0);
if (!(ds instanceof DOMSource)) {
try {
ds = new DOMSource(StaxUtils.read(ds));
} catch (XMLStreamException e) {
throw new Fault(e);
}
list.set(0, ds);
validatePossibleFault(message, bop, ((DOMSource) ds).getNode());
}
}
}
use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.
the class PersistenceUtils method decodeRMContent.
public static void decodeRMContent(RMMessage rmmsg, Message msg) throws IOException {
String contentType = rmmsg.getContentType();
final CachedOutputStream cos = rmmsg.getContent();
if ((null != contentType) && contentType.startsWith("multipart/related")) {
final InputStream is = cos.getInputStream();
msg.put(Message.CONTENT_TYPE, contentType);
msg.setContent(InputStream.class, is);
AttachmentDeserializer ad = new AttachmentDeserializer(msg);
ad.initializeAttachments();
// create new cos with soap envelope only
CachedOutputStream cosSoap = new CachedOutputStream();
IOUtils.copy(msg.getContent(InputStream.class), cosSoap);
cosSoap.flush();
msg.put(RMMessageConstants.SAVED_CONTENT, cosSoap);
// REVISIT -- At the moment references must be hold for retransmission
// and the final cleanup of the CachedOutputStream.
msg.put(RMMessageConstants.ATTACHMENTS_CLOSEABLE, new Closeable() {
@Override
public void close() throws IOException {
try {
is.close();
} catch (IOException e) {
// Ignore
}
try {
cos.close();
} catch (IOException e) {
// Ignore
}
}
});
} else {
msg.put(RMMessageConstants.SAVED_CONTENT, cos);
}
}
use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.
the class MtomServerTest method testMtomRequest.
@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=\"<soap.xml@xfire.codehaus.org>\"; " + "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());
}
}
Aggregations