Search in sources :

Example 1 with MessageContentsList

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) Response(javax.ws.rs.core.Response) MessageContentsList(org.apache.cxf.message.MessageContentsList) FallbackConverter(org.apache.camel.FallbackConverter)

Example 2 with MessageContentsList

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;
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) ReaderInputStream(org.apache.camel.component.cxf.util.ReaderInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) ReaderInputStream(org.apache.camel.component.cxf.util.ReaderInputStream) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap)

Example 3 with MessageContentsList

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;
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) DOMSource(javax.xml.transform.dom.DOMSource) NamedNodeMap(org.w3c.dom.NamedNodeMap) MessageContentsList(org.apache.cxf.message.MessageContentsList) Node(org.w3c.dom.Node) Holder(javax.xml.ws.Holder) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source)

Example 4 with MessageContentsList

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MessageContentsList(org.apache.cxf.message.MessageContentsList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) Test(org.junit.Test)

Example 5 with MessageContentsList

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();
    }
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) ApplicationException(org.apache.openejb.ApplicationException) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) RpcContainer(org.apache.openejb.RpcContainer) ApplicationException(org.apache.openejb.ApplicationException)

Aggregations

MessageContentsList (org.apache.cxf.message.MessageContentsList)63 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)24 Exchange (org.apache.cxf.message.Exchange)22 Fault (org.apache.cxf.interceptor.Fault)19 Message (org.apache.cxf.message.Message)19 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)18 ArrayList (java.util.ArrayList)13 List (java.util.List)13 MessageImpl (org.apache.cxf.message.MessageImpl)11 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 Method (java.lang.reflect.Method)9 OperationInfo (org.apache.cxf.service.model.OperationInfo)9 Response (javax.ws.rs.core.Response)8 XMLStreamException (javax.xml.stream.XMLStreamException)8 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)8 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)8 MessageInfo (org.apache.cxf.service.model.MessageInfo)8 Test (org.junit.Test)8 Service (org.apache.cxf.service.Service)7