use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class MessageModeOutInterceptor method doSoap.
private void doSoap(Message message) {
MessageContentsList list = (MessageContentsList) message.getContent(List.class);
if (list == null || list.isEmpty()) {
return;
}
Object o = list.get(0);
if (o instanceof SOAPMessage) {
SOAPMessage soapMessage = (SOAPMessage) o;
if (soapMessage.countAttachments() > 0) {
message.put("write.attachments", Boolean.TRUE);
}
try {
if (message instanceof org.apache.cxf.binding.soap.SoapMessage) {
org.apache.cxf.binding.soap.SoapMessage cxfSoapMessage = (org.apache.cxf.binding.soap.SoapMessage) message;
String cxfNamespace = cxfSoapMessage.getVersion().getNamespace();
SOAPHeader soapHeader = soapMessage.getSOAPHeader();
String namespace = soapHeader == null ? null : soapHeader.getNamespaceURI();
if (Soap12.SOAP_NAMESPACE.equals(namespace) && !namespace.equals(cxfNamespace)) {
cxfSoapMessage.setVersion(Soap12.getInstance());
cxfSoapMessage.put(Message.CONTENT_TYPE, cxfSoapMessage.getVersion().getContentType());
}
}
} catch (SOAPException e) {
// ignore
}
try {
Object enc = soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
if (enc instanceof String) {
message.put(Message.ENCODING, enc);
}
} catch (SOAPException e) {
// ignore
}
try {
Object xmlDec = soapMessage.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
if (xmlDec != null) {
boolean b = PropertyUtils.isTrue(xmlDec);
message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, b);
}
} catch (SOAPException e) {
// ignore
}
}
message.getInterceptorChain().add(internal);
}
use of org.apache.cxf.message.MessageContentsList 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.message.MessageContentsList in project cxf by apache.
the class JAXWSMethodInvokerTest method testFaultAvoidHeadersCopy.
@Test
public void testFaultAvoidHeadersCopy() throws Throwable {
ExceptionService serviceObject = new ExceptionService();
Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
Exchange ex = new ExchangeImpl();
prepareInMessage(ex, false);
JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
try {
jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
fail("Expected fault");
} catch (Fault fault) {
Message outMsg = ex.getOutMessage();
assertNull(outMsg);
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class HolderInInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
MessageContentsList inObjects = MessageContentsList.getContentsList(message);
Exchange exchange = message.getExchange();
BindingOperationInfo bop = exchange.getBindingOperationInfo();
if (bop == null) {
return;
}
OperationInfo op = bop.getOperationInfo();
if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
return;
}
List<MessagePartInfo> parts = op.getOutput().getMessageParts();
boolean client = Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE));
if (client) {
List<Holder<?>> outHolders = CastUtils.cast((List<?>) message.getExchange().getOutMessage().get(CLIENT_HOLDERS));
for (MessagePartInfo part : parts) {
if (part.getIndex() != 0 && part.getTypeClass() != null) {
@SuppressWarnings("unchecked") Holder<Object> holder = (Holder<Object>) outHolders.get(part.getIndex() - 1);
if (holder != null) {
holder.value = inObjects.get(part);
inObjects.put(part, holder);
}
}
}
} else {
for (MessagePartInfo part : parts) {
int idx = part.getIndex() - 1;
if (idx >= 0 && part.getTypeClass() != null) {
if (inObjects == null) {
// if soap:body is empty, the contents may not exist
// so we need to create a contents list to store
// the holders for the outgoing parts (CXF-4031)
inObjects = new MessageContentsList();
message.setContent(List.class, inObjects);
}
if (idx >= inObjects.size()) {
inObjects.set(idx, new Holder<Object>());
} else {
Object o = inObjects.get(idx);
inObjects.set(idx, new Holder<Object>(o));
}
}
}
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class RMCaptureOutInterceptor method setTerminateSequence.
private void setTerminateSequence(Message msg, Identifier identifier, ProtocolVariation protocol) throws RMException {
TerminateSequenceType ts = new TerminateSequenceType();
ts.setIdentifier(identifier);
MessageContentsList contents = new MessageContentsList(new Object[] { protocol.getCodec().convertToSend(ts) });
msg.setContent(List.class, contents);
// create a new exchange for this output-only exchange
Exchange newex = new ExchangeImpl();
Exchange oldex = msg.getExchange();
newex.put(Bus.class, oldex.getBus());
newex.put(Endpoint.class, oldex.getEndpoint());
newex.put(Service.class, oldex.getEndpoint().getService());
newex.put(Binding.class, oldex.getEndpoint().getBinding());
newex.setConduit(oldex.getConduit(msg));
newex.setDestination(oldex.getDestination());
// Setup the BindingOperationInfo
RMEndpoint rmep = getManager().getReliableEndpoint(msg);
OperationInfo oi = rmep.getEndpoint(protocol).getEndpointInfo().getService().getInterface().getOperation(protocol.getConstants().getTerminateSequenceAnonymousOperationName());
BindingInfo bi = rmep.getBindingInfo(protocol);
BindingOperationInfo boi = bi.getOperation(oi);
newex.put(BindingInfo.class, bi);
newex.put(BindingOperationInfo.class, boi);
msg.setExchange(newex);
newex.setOutMessage(msg);
}
Aggregations