Looks like the attachment didn't make it through the first time...adding a txt 
extension...

-----Original Message-----
From: Scott Came [mailto:[email protected]] 
Sent: Friday, March 04, 2011 5:42 PM
To: [email protected]
Subject: Generic file -> CXF Payload type converter issues

Per Willem's advice [1] I have attempted to bring in the CxfPayloadConverter 
from 2.7-SNAPSHOT [2] so that I can make the following Camel route work:

<osgi:camelContext xmlns="http://camel.apache.org/schema/spring";>
    <route>
      <from uri="file:/tmp/tsc-input"/>
      <to uri="cxf:bean:outEndpoint?dataFormat=PAYLOAD"/>
    </route>
  </osgi:camelContext>

I am able to build and deploy the converter just fine within my osgi bundle.  
However, I've run into a problem whereby the body on the converted message is 
empty.  I know this because I am routing the outbound message through tcpmon, 
and it just has a <soap:Body/> element where the body should be:

Content-Type: text/xml; charset=UTF-8
SOAPAction: "http://it.ojp.gov/GlobalJRA/TSCEI/0.9.4/ws/SendEncounter";
Accept: */*
User-Agent: Apache CXF 2.3.2-fuse-00-00
Cache-Control: no-cache
Pragma: no-cache
Host: 127.0.0.1:18081
Connection: keep-alive
Content-Length: 98

<soap:Envelope 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";><soap:Body/></soap:Envelope>

To try to identify the problem, I have reduced the converter down to a single 
method (see attached, method genericFileToCxfPayload(...)).  The code is 
running, and when I set the log level to debug, I see the full body in the 
debug messages.  So I am confident the payload object being returned has a 
proper body...it's just getting lost somewhere before the message hits the 
wire.  How could that happen?

Oddly enough, I discovered that not only was the GenericFile -> CxfPayload 
converter called, but it is also wanting to convert from CxfPayload -> String.  
I can't imagine why this is the case.  I do have another OSGi bundle, with a 
camel route, deployed in this same instance of servicemix, and that bundle's 
route does go from CxfPayload -> String.  But it is in a separate bundle, so my 
assumption would be that the type converter class would not be accessible.  Is 
that assumption incorrect?  In any case, the debug message in the 
CxfPayloadToString method in the attached does get called, and the 
payload.getBody() call there returns null.

Thanks for any advice.
--Scott

[1] http://www.mail-archive.com/[email protected]/msg13115.html
[2] 
https://svn.apache.org/repos/asf/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/converter/CxfPayloadConverter.java

package gov.ojp.it.global;

import java.io.*;
import java.util.*;

import javax.xml.parsers.*;

import org.apache.camel.*;
import org.apache.camel.component.cxf.*;
import org.apache.camel.component.file.*;
import org.apache.commons.logging.*;
import org.w3c.dom.*;
import org.xml.sax.*;

@Converter
public final class GenericFileToCxfPayloadConverter
{

        private static final Log LOG = 
LogFactory.getLog(GenericFileToCxfPayloadConverter.class);

        private GenericFileToCxfPayloadConverter()
        {
                // Helper class
        }

        @Converter
        public static <T> CxfPayload<T> genericFileToCxfPayload(GenericFile<T> 
f, Exchange exchange) throws SAXException,
                        IOException, ParserConfigurationException
        {

                List<T> headers = new ArrayList<T>();
                List<Element> body = new ArrayList<Element>();
                Object fileBody = f.getBody();
                File file = (File) fileBody;
                String fileURI = file.toURI().toString();
                LOG.debug("Converting file at fileURI=" + fileURI);
                Document doc = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fileURI);
                LOG.debug("document element=" + 
doc.getDocumentElement().getNodeName());
                CxfPayload<T> payload = new CxfPayload<T>(headers, body);
                body.add(doc.getDocumentElement());
                if (LOG.isDebugEnabled())
                {
                        List<Element> payloadBody = payload.getBody();
                        Element e = payloadBody.get(0);
                        dumpElement(e);
                        LOG.debug(payloadBody);
                }
                return payload;
        }
        
        @Converter
        public static String CxfPayloadToString(CxfPayload<?> payload, Exchange 
exchange)
        {
                LOG.debug("Converting CxfPayload to String: " + 
payload.getBody());
                return null;
        }


        private static void dumpElement(Node e)
        {
                LOG.debug(e);
                NodeList children = e.getChildNodes();
                for (int i = 0; i < children.getLength(); i++)
                {
                        Node n = children.item(i);
                        dumpElement(n);
                }
        }

}

Reply via email to