use of org.apache.cxf.message.XMLMessage in project cxf by apache.
the class AttachmentDeserializerTest method imitateAttachmentInInterceptorForMessageWithMissingBoundary.
@Test
public void imitateAttachmentInInterceptorForMessageWithMissingBoundary() throws Exception {
String contentType = "multipart/mixed;boundary=abc123";
String data = "--abc123\r\n\r\n<Document></Document>\r\n\r\n";
ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes());
Message message = new XMLMessage(new MessageImpl());
message.put(Message.CONTENT_TYPE, contentType);
message.setContent(InputStream.class, inputStream);
message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System.getProperty("java.io.tmpdir"));
message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(AttachmentDeserializer.THRESHOLD));
AttachmentDeserializer ad = new AttachmentDeserializer(message, Collections.singletonList("multipart/mixed"));
ad.initializeAttachments();
assertEquals(0, message.getAttachments().size());
inputStream.close();
}
use of org.apache.cxf.message.XMLMessage in project cxf by apache.
the class XMLBinding method createMessage.
public Message createMessage(Message m) {
if (!m.containsKey(Message.CONTENT_TYPE)) {
String ct = null;
// Should this be done in ServiceInvokerInterceptor to support a case where the
// response content type is detected early on the inbound chain for all the bindings ?
Exchange exchange = m.getExchange();
if (exchange != null) {
ct = (String) exchange.get(Message.CONTENT_TYPE);
}
if (ct == null) {
ct = "text/xml";
}
m.put(Message.CONTENT_TYPE, ct);
}
return new XMLMessage(m);
}
use of org.apache.cxf.message.XMLMessage in project cxf by apache.
the class LogicalMessageImpl method handleDispatchProviderCase.
private Source handleDispatchProviderCase(Service.Mode mode) {
Source source = null;
Message message = msgContext.getWrappedMessage();
Source obj = message.getContent(Source.class);
if (message instanceof SoapMessage) {
// StreamSource may only be used once, need to make a copy
if (obj instanceof StreamSource) {
try (CachedOutputStream cos = new CachedOutputStream()) {
StaxUtils.copy(obj, cos);
obj = new StreamSource(cos.getInputStream());
message.setContent(Source.class, new StreamSource(cos.getInputStream()));
} catch (Exception e) {
throw new Fault(e);
}
}
if (mode == Service.Mode.PAYLOAD) {
source = obj;
} else {
try (CachedOutputStream cos = new CachedOutputStream()) {
StaxUtils.copy(obj, cos);
InputStream in = cos.getInputStream();
SOAPMessage msg = initSOAPMessage(in);
source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild());
in.close();
} catch (Exception e) {
throw new Fault(e);
}
}
} else if (message instanceof XMLMessage) {
if (obj != null) {
source = obj;
} else if (message.getContent(DataSource.class) != null) {
throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
}
}
return source;
}
use of org.apache.cxf.message.XMLMessage in project cxf by apache.
the class LogicalMessageImpl method setPayload.
public void setPayload(Source s) {
Message message = msgContext.getWrappedMessage();
Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
SOAPMessage m = message.getContent(SOAPMessage.class);
if (m != null && !MessageUtils.isOutbound(message)) {
try {
SAAJUtils.getBody(m).removeContents();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
StaxUtils.copy(s, writer);
writer.flush();
writer.close();
if (mode == Service.Mode.MESSAGE) {
s = new DOMSource(m.getSOAPPart());
} else {
s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
}
W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
message.setContent(XMLStreamReader.class, r);
} catch (Exception e) {
throw new Fault(e);
}
} else if (mode != null) {
if (message instanceof SoapMessage) {
if (mode == Service.Mode.MESSAGE) {
try {
// REVISIT: should try to use the original SOAPMessage
// instead of creating a new empty one.
SOAPMessage msg = initSOAPMessage(null);
write(s, SAAJUtils.getBody(msg));
s = new DOMSource(msg.getSOAPPart());
} catch (Exception e) {
throw new Fault(e);
}
}
} else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
}
} else {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
}
msgContext.getWrappedMessage().setContent(Source.class, s);
}
Aggregations