camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r617756 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/converter/ components/camel-cxf/src/main/java/org/apache/camel/component/cxf/ components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/
Date Sat, 02 Feb 2008 06:21:50 GMT
Author: ningjiang
Date: Fri Feb  1 22:21:49 2008
New Revision: 617756

URL: http://svn.apache.org/viewvc?rev=617756&view=rev
Log:
CAMEL-311 using the IOConverter to covert the message body to stream objects for CXF to use

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
    activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSoapBinding.java
    activemq/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/RequestBody.xml

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=617756&r1=617755&r2=617756&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Fri Feb  1 22:21:49 2008
@@ -23,11 +23,20 @@
 
 import java.io.*;
 import java.net.URL;
+import java.util.Properties;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 
 /**
  * Some core java.io based <a
  * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
- * 
+ *
  * @version $Revision$
  */
 @Converter
@@ -37,7 +46,7 @@
     /**
      * Utility classes should not have a public constructor.
      */
-    private IOConverter() {        
+    private IOConverter() {
     }
 
     @Converter
@@ -92,6 +101,17 @@
     }
 
     @Converter
+    public static InputStream toInputStream(BufferedReader buffer) throws IOException {
+        return toInputStream(toString(buffer));
+    }
+
+    @Converter
+    public static InputStream toInputStrean(DOMSource source) throws TransformerException,
IOException {
+        ByteArrayInputStream bais = new ByteArrayInputStream(toString(source).getBytes());
+        return bais;
+    }
+
+    @Converter
     public static byte[] toByteArray(String text) {
         // TODO could we automatically find this?
         return text.getBytes();
@@ -147,6 +167,24 @@
     @Converter
     public static String toString(InputStream in) throws IOException {
         return toString(toReader(in));
+    }
+
+    public static String toString(Source source) throws TransformerException, IOException
{
+        return toString(source, null);
+    }
+
+    public static String toString(Source source, Properties props) throws TransformerException,
IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        StreamResult sr = new StreamResult(bos);
+        Transformer trans = TransformerFactory.newInstance().newTransformer();;
+        if (props == null) {
+            props = new Properties();
+            props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
+        }
+        trans.setOutputProperties(props);
+        trans.transform(source, sr);
+        bos.close();
+        return bos.toString();
     }
 
     @Converter

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java?rev=617756&r1=617755&r2=617756&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java
(original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CamelInvoker.java
Fri Feb  1 22:21:49 2008
@@ -58,8 +58,7 @@
     */
     public Message invoke(Message inMessage) {        
         Exchange exchange = inMessage.getExchange();
-        //InvokingContext invokingContext = exchange.get(InvokingContext.class);
-                               
+                                      
         //Set Request Context into CXF Message
         Map<String, Object> ctxContainer = new HashMap<String, Object>();
         Map<String, Object> requestCtx = new HashMap<String, Object>();

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java?rev=617756&r1=617755&r2=617756&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
(original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfBinding.java
Fri Feb  1 22:21:49 2008
@@ -40,7 +40,7 @@
  */
 public class CxfBinding {
     public static Object extractBodyFromCxf(CxfExchange exchange, Message message) {
-        //  TODO how do we choose a format?
+        // TODO how do we choose a format?
         return getBody(message);
     }
 
@@ -60,7 +60,8 @@
     public Message createCxfMessage(CxfExchange exchange) {
         Message answer = exchange.getInMessage();
 
-        // CXF uses the stax which is based on the stream API to parser the XML, so
+        // CXF uses the stax which is based on the stream API to parser the XML,
+        // so
         // the CXF transport is also based on the stream API.
         // And the interceptors are also based on the stream API,
         // so lets use an InputStream to host the CXF on wire message.
@@ -71,26 +72,16 @@
             body = in.getBody();
         }
         if (body instanceof InputStream) {
-        	answer.setContent(InputStream.class, body);
-                // we need copy context
+            answer.setContent(InputStream.class, body);
+            // we need copy context
         } else if (body instanceof List) {
-        	//just set the operation's parament
-        	answer.setContent(List.class, body);
-                //just set the method name
-                answer.put(CxfConstants.OPERATION_NAME, (String)in.getHeader(CxfConstants.OPERATION_NAME));
-                answer.put(CxfConstants.OPERATION_NAMESPACE, (String)in.getHeader(CxfConstants.OPERATION_NAMESPACE));
-
-        } else if (body instanceof DOMSource) {
-        	DOMSource source = (DOMSource) body;
-        	try {
-				ByteArrayInputStream bais = new ByteArrayInputStream(XMLUtils.toString(source).getBytes());
-				answer.setContent(InputStream.class, bais);
-			} catch (Exception e) {
-				throw new RuntimeCamelException(e);
-			}
-
+            // just set the operation's parament
+            answer.setContent(List.class, body);
+            // just set the method name
+            answer.put(CxfConstants.OPERATION_NAME, (String)in.getHeader(CxfConstants.OPERATION_NAME));
+            answer.put(CxfConstants.OPERATION_NAMESPACE, (String)in
+                .getHeader(CxfConstants.OPERATION_NAMESPACE));
         }
-
 
         return answer;
     }

Modified: activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSoapBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSoapBinding.java?rev=617756&r1=617755&r2=617756&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSoapBinding.java
(original)
+++ activemq/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSoapBinding.java
Fri Feb  1 22:21:49 2008
@@ -36,16 +36,16 @@
 
 public class CxfSoapBinding {
     private CxfSoapBinding() {
-        
+
     }
-    
+    //TODO using the type converter to do this kind of thing
     public static org.apache.cxf.message.Message getCxfInMessage(org.apache.camel.Exchange
exchange, boolean isClient) {
         MessageImpl answer = new MessageImpl();
-        org.apache.cxf.message.Exchange cxfExchange = exchange.getProperty(CxfConstants.CXF_EXCHANGE,

+        org.apache.cxf.message.Exchange cxfExchange = exchange.getProperty(CxfConstants.CXF_EXCHANGE,
                                                                         org.apache.cxf.message.Exchange.class);
         org.apache.camel.Message message = null;
         if (isClient) {
-            message = exchange.getOut();            
+            message = exchange.getOut();
         } else {
             message = exchange.getIn();
         }
@@ -53,37 +53,25 @@
         if (cxfExchange == null) {
             cxfExchange = new ExchangeImpl();
             exchange.setProperty(CxfConstants.CXF_EXCHANGE, cxfExchange);
-        }    
+        }
         Object body = message.getBody(InputStream.class);
+        
         if (body == null) {
             body = message.getBody();
         }
-        if (body instanceof BufferedReader) {
-        	//do transform from BufferedReader to InputStream
-        	
-        	try {
-        		BufferedReader reader = (BufferedReader)body;
-        		String line;
-        		String content = "";
-        		while ((line = reader.readLine()) != null) {
-        			content = content + line;
-        		}
-        		ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
-				answer.setContent(InputStream.class, bais);
-        	} catch (IOException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
-        }
+        // we could do some message transform here
         if (body instanceof InputStream) {
-            answer.setContent(InputStream.class, body);             
+            answer.setContent(InputStream.class, body);
+        } else {
+            // the answer body is null
         }
+
         answer.putAll(message.getHeaders());
         answer.setExchange(cxfExchange);
         cxfExchange.setInMessage(answer);
         return answer;
     }
-    
+
     public static org.apache.cxf.message.Message getCxfOutMessage(org.apache.camel.Exchange
exchange, boolean isClient) {
         org.apache.cxf.message.Exchange cxfExchange = exchange.getProperty(CxfConstants.CXF_EXCHANGE,
org.apache.cxf.message.Exchange.class);
         assert cxfExchange != null;
@@ -97,18 +85,18 @@
         }
         else {
             message = exchange.getOut();
-        }    
+        }
         // send the body back
         Object body = message.getBody(Source.class);
         if (body == null) {
             body = message.getBody();
         }
         if (body instanceof Source) {
-            outMessage.setContent(Source.class, body);             
+            outMessage.setContent(Source.class, body);
         }
         outMessage.putAll(message.getHeaders());
         return outMessage;
     }
-     
+
 
 }

Modified: activemq/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/RequestBody.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/RequestBody.xml?rev=617756&r1=617755&r2=617756&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/RequestBody.xml
(original)
+++ activemq/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/RequestBody.xml
Fri Feb  1 22:21:49 2008
@@ -1 +1 @@
-<testMethod xmlns="http://camel.apache.org/testService" />
\ No newline at end of file
+<testMethod xmlns="http://camel.apache.org/testService"/>
\ No newline at end of file



Mime
View raw message