use of org.apache.cxf.message.MessageContentsList in project camel by apache.
the class CxfConverter method convertTo.
/**
* Use a fallback type converter so we can convert the embedded list element
* if the value is MessageContentsList. The algorithm of this converter
* finds the first non-null list element from the list and applies conversion
* to the list element.
*
* @param type the desired type to be converted to
* @param exchange optional exchange which can be null
* @param value the object to be converted
* @param registry type converter registry
* @return the converted value of the desired type or null if no suitable converter found
*/
@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// CXF-WS MessageContentsList class
if (MessageContentsList.class.isAssignableFrom(value.getClass())) {
MessageContentsList list = (MessageContentsList) value;
// try to turn the first array element into the object that we want
for (Object embedded : list) {
if (embedded != null) {
if (type.isInstance(embedded)) {
return type.cast(embedded);
} else {
TypeConverter tc = registry.lookup(type, embedded.getClass());
if (tc != null) {
Object result = tc.convertTo(type, exchange, embedded);
if (result != null) {
return (T) result;
}
// there is no suitable result will be return
break;
}
}
}
}
// return void to indicate its not possible to convert at this time
return (T) Void.TYPE;
}
// CXF-RS Response class
if (Response.class.isAssignableFrom(value.getClass())) {
Response response = (Response) value;
Object entity = response.getEntity();
TypeConverter tc = registry.lookup(type, entity.getClass());
if (tc != null) {
return tc.convertTo(type, exchange, entity);
}
// return void to indicate its not possible to convert at this time
return (T) Void.TYPE;
}
return null;
}
use of org.apache.cxf.message.MessageContentsList in project camel by apache.
the class DefaultCxfBinding method getContentFromCxf.
protected static Object getContentFromCxf(Message message, DataFormat dataFormat, String encoding) {
Set<Class<?>> contentFormats = message.getContentFormats();
Object answer = null;
if (contentFormats != null) {
if (LOG.isTraceEnabled()) {
for (Class<?> contentFormat : contentFormats) {
LOG.trace("Content format={} value={}", contentFormat, message.getContent(contentFormat));
}
}
if (dataFormat == DataFormat.POJO) {
answer = message.getContent(List.class);
if (answer == null) {
answer = message.getContent(Object.class);
if (answer != null) {
answer = new MessageContentsList(answer);
}
}
} else if (dataFormat == DataFormat.PAYLOAD) {
List<SoapHeader> headers = CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
Map<String, String> nsMap = new HashMap<String, String>();
answer = new CxfPayload<SoapHeader>(headers, getPayloadBodyElements(message, nsMap), nsMap);
} else if (dataFormat.dealias() == DataFormat.RAW) {
answer = message.getContent(InputStream.class);
if (answer == null) {
answer = message.getContent(Reader.class);
if (answer != null) {
if (encoding == null) {
encoding = "UTF-8";
}
LOG.trace("file encoding is = {}", encoding);
answer = new ReaderInputStream((Reader) answer, Charset.forName(encoding));
}
}
} else if (dataFormat.dealias() == DataFormat.CXF_MESSAGE && message.getContent(List.class) != null) {
// CAMEL-6404 added check point of message content
// The message content of list could be null if there is a fault message is received
answer = message.getContent(List.class).get(0);
}
LOG.trace("Extracted body from CXF message = {}", answer);
}
return answer;
}
use of org.apache.cxf.message.MessageContentsList in project camel by apache.
the class DefaultCxfBinding method getPayloadBodyElements.
protected static List<Source> getPayloadBodyElements(Message message, Map<String, String> nsMap) {
// take the namespace attribute from soap envelop
Map<String, String> bodyNC = CastUtils.cast((Map<?, ?>) message.get("soap.body.ns.context"));
if (bodyNC != null) {
// if there is no Node and the addNamespaceContext option is enabled, this map is available
nsMap.putAll(bodyNC);
} else {
Document soapEnv = (Document) message.getContent(Node.class);
if (soapEnv != null) {
NamedNodeMap attrs = soapEnv.getFirstChild().getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node node = attrs.item(i);
if (!node.getNodeValue().equals(Soap11.SOAP_NAMESPACE) && !node.getNodeValue().equals(Soap12.SOAP_NAMESPACE)) {
nsMap.put(node.getLocalName(), node.getNodeValue());
}
}
}
}
MessageContentsList inObjects = MessageContentsList.getContentsList(message);
if (inObjects == null) {
return new ArrayList<Source>(0);
}
org.apache.cxf.message.Exchange exchange = message.getExchange();
BindingOperationInfo boi = exchange.getBindingOperationInfo();
OperationInfo op = boi.getOperationInfo();
if (boi.isUnwrapped()) {
op = boi.getWrappedOperation().getOperationInfo();
}
List<MessagePartInfo> partInfos = null;
boolean client = Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE));
if (client) {
// it is a response
partInfos = op.getOutput().getMessageParts();
} else {
// it is a request
partInfos = op.getInput().getMessageParts();
}
List<Source> answer = new ArrayList<Source>();
for (MessagePartInfo partInfo : partInfos) {
if (!inObjects.hasValue(partInfo)) {
continue;
}
Object part = inObjects.get(partInfo);
if (part instanceof Holder) {
part = ((Holder<?>) part).value;
}
if (part instanceof Source) {
Element element = null;
if (part instanceof DOMSource) {
element = getFirstElement(((DOMSource) part).getNode());
}
if (element != null) {
addNamespace(element, nsMap);
answer.add(new DOMSource(element));
} else {
answer.add((Source) part);
}
if (LOG.isTraceEnabled()) {
LOG.trace("Extract body element {}", element == null ? "null" : getXMLString(element));
}
} else if (part instanceof Element) {
addNamespace((Element) part, nsMap);
answer.add(new DOMSource((Element) part));
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Unhandled part type '{}'", part.getClass());
}
}
}
return answer;
}
use of org.apache.cxf.message.MessageContentsList in project camel by apache.
the class ConverterTest method testFallbackConverter.
@Test
public void testFallbackConverter() throws Exception {
CamelContext context = new DefaultCamelContext();
Exchange exchange = new DefaultExchange(context);
MessageContentsList list = new MessageContentsList();
NodeListWrapper nl = new NodeListWrapper(new ArrayList<Element>());
list.add(nl);
exchange.getIn().setBody(list);
Node node = exchange.getIn().getBody(Node.class);
assertNull(node);
File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
document.getDocumentElement().normalize();
List<Element> elements = new ArrayList<Element>();
elements.add(document.getDocumentElement());
nl = new NodeListWrapper(elements);
list.clear();
list.add(nl);
exchange.getIn().setBody(list);
node = exchange.getIn().getBody(Node.class);
assertNotNull(node);
}
use of org.apache.cxf.message.MessageContentsList in project tomee by apache.
the class EjbMethodInvoker method preEjbInvoke.
private Object preEjbInvoke(Exchange exchange, Method method, List<Object> params) {
EjbMessageContext ctx = new EjbMessageContext(exchange.getInMessage(), Scope.APPLICATION);
WebServiceContextImpl.setMessageContext(ctx);
Map<String, Object> handlerProperties = removeHandlerProperties(ctx);
exchange.put(HANDLER_PROPERTIES, handlerProperties);
try {
EjbInterceptor interceptor = new EjbInterceptor(params, method, this.bus, exchange);
Object[] arguments = { ctx, interceptor };
RpcContainer container = (RpcContainer) this.beanContext.getContainer();
Class callInterface = this.beanContext.getServiceEndpointInterface();
method = getMostSpecificMethod(beanContext, method, callInterface);
Object res = container.invoke(this.beanContext.getDeploymentID(), InterfaceType.SERVICE_ENDPOINT, callInterface, method, arguments, null);
if (exchange.isOneWay()) {
return null;
}
return new MessageContentsList(res);
} catch (ApplicationException e) {
// when no handler is defined, EjbInterceptor will directly delegate
// to #directEjbInvoke. So if an application exception is thrown by
// the end user, when must consider the ApplicationException as a
// web fault if it contains the @WebFault exception
Throwable t = e.getCause();
if (t != null) {
if (RuntimeException.class.isAssignableFrom(t.getClass()) && t.getClass().isAnnotationPresent(javax.ejb.ApplicationException.class)) {
// it's not a checked exception so it can not be a WebFault
throw (RuntimeException) t;
} else if (!t.getClass().isAnnotationPresent(WebFault.class)) {
// not a web fault even if it's an EJB ApplicationException
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(t, method, params, false);
}
} else {
// may not occurs ...
t = e;
}
// TODO may be we can change to FaultMode.CHECKED_APPLICATION_FAULT
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(t, method, params, false);
} catch (Exception e) {
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(e, method, params, false);
} finally {
WebServiceContextImpl.clear();
}
}
Aggregations