use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class DocLiteralInInterceptor method handleMessage.
public void handleMessage(Message message) {
if (isGET(message) && message.getContent(List.class) != null) {
LOG.fine("DocLiteralInInterceptor skipped in HTTP GET method");
return;
}
DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
MessageContentsList parameters = new MessageContentsList();
Exchange exchange = message.getExchange();
BindingOperationInfo bop = exchange.getBindingOperationInfo();
boolean client = isRequestor(message);
// operation anymore, just return
if (bop != null && !StaxUtils.toNextElement(xmlReader)) {
// body may be empty for partial response to decoupled request
return;
}
Service service = ServiceModelUtil.getService(message.getExchange());
bop = getBindingOperationInfo(xmlReader, exchange, bop, client);
boolean forceDocLitBare = false;
if (bop != null && bop.getBinding() != null) {
forceDocLitBare = Boolean.TRUE.equals(bop.getBinding().getService().getProperty("soap.force.doclit.bare"));
}
DataReader<XMLStreamReader> dr = getDataReader(message);
try {
if (!forceDocLitBare && bop != null && bop.isUnwrappedCapable()) {
ServiceInfo si = bop.getBinding().getService();
// Wrapped case
MessageInfo msgInfo = setMessage(message, bop, client, si);
setDataReaderValidation(service, message, dr);
// Determine if we should keep the parameters wrapper
if (shouldWrapParameters(msgInfo, message)) {
QName startQName = xmlReader.getName();
MessagePartInfo mpi = msgInfo.getFirstMessagePart();
if (!mpi.getConcreteName().equals(startQName)) {
throw new Fault("UNEXPECTED_WRAPPER_ELEMENT", LOG, null, startQName, mpi.getConcreteName());
}
Object wrappedObject = dr.read(mpi, xmlReader);
parameters.put(mpi, wrappedObject);
} else {
// Unwrap each part individually if we don't have a wrapper
bop = bop.getUnwrappedOperation();
msgInfo = setMessage(message, bop, client, si);
List<MessagePartInfo> messageParts = msgInfo.getMessageParts();
Iterator<MessagePartInfo> itr = messageParts.iterator();
// stuck
if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
StaxUtils.nextEvent(xmlReader);
}
// loop through each child element
getPara(xmlReader, dr, parameters, itr, message);
}
} else {
// Bare style
BindingMessageInfo msgInfo = null;
Endpoint ep = exchange.getEndpoint();
ServiceInfo si = ep.getEndpointInfo().getService();
if (bop != null) {
// for xml binding or client side
if (client) {
msgInfo = bop.getOutput();
} else {
msgInfo = bop.getInput();
if (bop.getOutput() == null) {
exchange.setOneWay(true);
}
}
if (msgInfo == null) {
return;
}
setMessage(message, bop, client, si, msgInfo.getMessageInfo());
}
Collection<OperationInfo> operations = null;
operations = new ArrayList<>();
operations.addAll(si.getInterface().getOperations());
if (xmlReader == null || !StaxUtils.toNextElement(xmlReader)) {
// empty input
getBindingOperationForEmptyBody(operations, ep, exchange);
return;
}
setDataReaderValidation(service, message, dr);
int paramNum = 0;
do {
QName elName = xmlReader.getName();
Object o = null;
MessagePartInfo p;
if (!client && msgInfo != null && msgInfo.getMessageParts() != null && msgInfo.getMessageParts().isEmpty()) {
// no input messagePartInfo
return;
}
if (msgInfo != null && msgInfo.getMessageParts() != null && msgInfo.getMessageParts().size() > 0) {
if (msgInfo.getMessageParts().size() > paramNum) {
p = msgInfo.getMessageParts().get(paramNum);
} else {
p = null;
}
} else {
p = findMessagePart(exchange, operations, elName, client, paramNum, message);
}
if (!forceDocLitBare) {
// Make sure the elName found on the wire is actually OK for
// the purpose we need it
validatePart(p, elName, message);
}
o = dr.read(p, xmlReader);
if (forceDocLitBare && parameters.isEmpty()) {
// webservice provider does not need to ensure size
parameters.add(o);
} else {
parameters.put(p, o);
}
paramNum++;
if (message.getContent(XMLStreamReader.class) == null || o == xmlReader) {
xmlReader = null;
}
} while (xmlReader != null && StaxUtils.toNextElement(xmlReader));
}
message.setContent(List.class, parameters);
} catch (Fault f) {
if (!isRequestor(message)) {
f.setFaultCode(Fault.FAULT_CODE_CLIENT);
}
throw f;
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class ColocUtil method convertSourceToObject.
public static void convertSourceToObject(Message message) {
List<Object> content = CastUtils.cast(message.getContent(List.class));
if (content == null || content.size() < 1) {
// nothing to convert
return;
}
// only supporting the wrapped style for now (one pojo <-> one source)
Source source = (Source) content.get(0);
DataReader<XMLStreamReader> reader = message.getExchange().getService().getDataBinding().createReader(XMLStreamReader.class);
MessagePartInfo mpi = getMessageInfo(message).getMessagePart(0);
XMLStreamReader streamReader = null;
Object wrappedObject = null;
try {
streamReader = StaxUtils.createXMLStreamReader(source);
wrappedObject = reader.read(mpi, streamReader);
} finally {
try {
StaxUtils.close(streamReader);
} catch (XMLStreamException e) {
// Ignore
}
}
MessageContentsList parameters = new MessageContentsList();
parameters.put(mpi, wrappedObject);
message.setContent(List.class, parameters);
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class HolderOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
MessageContentsList outObjects = MessageContentsList.getContentsList(message);
Exchange exchange = message.getExchange();
OperationInfo op = exchange.getBindingOperationInfo() == null ? null : exchange.getBindingOperationInfo().getOperationInfo();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("op: " + op);
if (null != op) {
LOG.fine("op.hasOutput(): " + op.hasOutput());
if (op.hasOutput()) {
LOG.fine("op.getOutput().size(): " + op.getOutput().size());
}
}
}
if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
LOG.fine("Returning.");
return;
}
if (!Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
List<MessagePartInfo> parts = op.getOutput().getMessageParts();
MessageContentsList inObjects = MessageContentsList.getContentsList(exchange.getInMessage());
if (inObjects != null) {
if (!(inObjects == outObjects)) {
for (int x = 0; x < inObjects.size(); x++) {
Object o = inObjects.get(x);
if (o instanceof Holder) {
outObjects.set(x + 1, o);
}
}
} else {
LOG.severe("CANNOT_SET_HOLDER_OBJECTS");
throw new Fault(new org.apache.cxf.common.i18n.Message("CANNOT_SET_HOLDER_OBJECTS", LOG));
}
}
for (MessagePartInfo part : parts) {
if (part.getIndex() > 0 && part.getTypeClass() != null) {
Holder<?> holder = (Holder<?>) outObjects.get(part);
outObjects.put(part, holder.value);
}
}
} else {
List<Object> holders = new ArrayList<>(outObjects);
for (int x = 0; x < outObjects.size(); x++) {
Object o = outObjects.get(x);
if (o instanceof Holder) {
outObjects.set(x, ((Holder<?>) o).value);
} else {
holders.set(x, null);
}
}
message.put(HolderInInterceptor.CLIENT_HOLDERS, holders);
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class MessageModeInInterceptor method doFromSoapMessage.
private void doFromSoapMessage(Message message, Object sm) {
SOAPMessage m = (SOAPMessage) sm;
MessageContentsList list = (MessageContentsList) message.getContent(List.class);
if (list == null) {
list = new MessageContentsList();
message.setContent(List.class, list);
}
Object o = m;
if (StreamSource.class.isAssignableFrom(type)) {
try {
try (CachedOutputStream out = new CachedOutputStream()) {
XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
StaxUtils.copy(new DOMSource(m.getSOAPPart()), xsw);
xsw.close();
o = new StreamSource(out.getInputStream());
}
} catch (Exception e) {
throw new Fault(e);
}
} else if (SAXSource.class.isAssignableFrom(type)) {
o = new StaxSource(new W3CDOMStreamReader(m.getSOAPPart()));
} else if (Source.class.isAssignableFrom(type)) {
o = new DOMSource(m.getSOAPPart());
}
list.set(0, o);
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class SwAInInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
if (bop == null) {
return;
}
if (bop.isUnwrapped()) {
bop = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
if (bmi == null) {
return;
}
SoapBodyInfo sbi = bmi.getExtensor(SoapBodyInfo.class);
if (sbi == null || sbi.getAttachments() == null || sbi.getAttachments().isEmpty()) {
return;
}
Set<Integer> foundAtts = new HashSet<>();
MessageContentsList inObjects = MessageContentsList.getContentsList(message);
for (MessagePartInfo mpi : sbi.getAttachments()) {
String partName = mpi.getConcreteName().getLocalPart();
String start = partName + "=";
boolean found = false;
if (foundAtts.contains(mpi.getIndex())) {
continue;
}
foundAtts.add(mpi.getIndex());
for (Attachment a : message.getAttachments()) {
if (a.getId().startsWith(start)) {
DataHandler dh = a.getDataHandler();
String ct = dh.getContentType();
Object o = null;
Class<?> typeClass = mpi.getTypeClass();
if (DataHandler.class.isAssignableFrom(typeClass)) {
o = dh;
} else if (String.class.isAssignableFrom(typeClass)) {
try {
// o = IOUtils.readBytesFromStream(dh.getInputStream());
o = dh.getContent();
} catch (IOException e) {
throw new Fault(e);
}
} else if (byte[].class.isAssignableFrom(typeClass)) {
try {
o = IOUtils.readBytesFromStream(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else if (ct.startsWith("image/")) {
try {
o = ImageIO.read(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else if (ct.startsWith("text/xml") || ct.startsWith("application/xml")) {
try {
o = new StreamSource(dh.getInputStream());
} catch (IOException e) {
throw new Fault(e);
}
} else {
o = dh;
}
inObjects.put(mpi, o);
found = true;
break;
}
}
if (!found) {
inObjects.put(mpi, null);
}
}
}
Aggregations