You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/10/22 15:49:19 UTC

svn commit: r1187717 - /camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java

Author: davsclaus
Date: Sat Oct 22 13:49:18 2011
New Revision: 1187717

URL: http://svn.apache.org/viewvc?rev=1187717&view=rev
Log:
CAMEL-4278: Added additional type converters to camel-quickfix. Thanks to Pablo for the patch.

Modified:
    camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java

Modified: camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java?rev=1187717&r1=1187716&r2=1187717&view=diff
==============================================================================
--- camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java (original)
+++ camel/trunk/components/camel-quickfix/src/main/java/org/apache/camel/component/quickfixj/converter/QuickfixjConverters.java Sat Oct 22 13:49:18 2011
@@ -16,12 +16,17 @@
  */
 package org.apache.camel.component.quickfixj.converter;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
 import org.apache.camel.Converter;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.component.quickfixj.QuickfixjEndpoint;
 import org.apache.camel.component.quickfixj.QuickfixjEventCategory;
+import org.apache.camel.util.IOHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,19 +55,54 @@ public final class QuickfixjConverters {
     public static SessionID toSessionID(String sessionID) {
         return new SessionID(sessionID);
     }
-    
+
     @Converter
     public static Message toMessage(String value, Exchange exchange) throws InvalidMessage, ConfigError {
         DataDictionary dataDictionary = getDataDictionary(exchange);
         return new Message(value, dataDictionary);
     }
 
+    @Converter
+    public static Message toMessage(byte[] value, Exchange exchange) throws InvalidMessage, ConfigError, UnsupportedEncodingException {
+        DataDictionary dataDictionary = getDataDictionary(exchange);
+        String charsetName = IOHelper.getCharsetName(exchange);
+
+        String message;
+        if (charsetName != null) {
+            message = new String(value, charsetName);
+        } else {
+            message = new String(value);
+        }
+
+        // if message ends with any sort of newline trim it so QuickfixJ's doesn't fail while parsing the string
+        if (message.endsWith("\r\n")) {
+            message = message.substring(0, message.length() - 2);
+        } else if (message.endsWith("\r") || message.endsWith("\n")) {
+            message = message.substring(0, message.length() - 1);
+        }
+
+        return new Message(message, dataDictionary, false);
+    }
+
+    @Converter
+    public static InputStream toInputStream(Message value, Exchange exchange) throws InvalidMessage, ConfigError, UnsupportedEncodingException {
+        if (exchange != null) {
+            String charsetName = IOHelper.getCharsetName(exchange);
+            if (charsetName != null) {
+                return new ByteArrayInputStream(value.toString().getBytes(charsetName));
+            } else {
+                return new ByteArrayInputStream(value.toString().getBytes());
+            }
+        }
+        return null;
+    }
+
     private static DataDictionary getDataDictionary(Exchange exchange) throws ConfigError {
         Object dictionaryValue = exchange.getProperties().get(QuickfixjEndpoint.DATA_DICTIONARY_KEY);
-        
+
         DataDictionary dataDictionary;
         if (dictionaryValue instanceof DataDictionary) {
-            dataDictionary = (DataDictionary)dictionaryValue;
+            dataDictionary = (DataDictionary) dictionaryValue;
         } else if (dictionaryValue instanceof String) {
             dataDictionary = new DataDictionary((String) dictionaryValue);
         } else {
@@ -70,26 +110,26 @@ public final class QuickfixjConverters {
             Session session = Session.lookupSession(sessionID);
             dataDictionary = session != null ? session.getDataDictionary() : null;
         }
-        
+
         return dataDictionary;
     }
-    
+
     public static Exchange toExchange(Endpoint endpoint, SessionID sessionID, Message message, QuickfixjEventCategory eventCategory) {
         Exchange exchange = endpoint.createExchange(ExchangePattern.InOnly);
-        
+
         org.apache.camel.Message camelMessage = exchange.getIn();
         camelMessage.setHeader(EVENT_CATEGORY_KEY, eventCategory);
         camelMessage.setHeader(SESSION_ID_KEY, sessionID);
-        
+
         if (message != null) {
             try {
                 camelMessage.setHeader(MESSAGE_TYPE_KEY, message.getHeader().getString(MsgType.FIELD));
-            } catch (FieldNotFound fieldNotFoundEx) {
-                LOG.error("Message type field not found in QFJ message, continuing...");
+            } catch (FieldNotFound e) {
+                LOG.warn("Message type field not found in QFJ message: {}, continuing...", message);
             }
         }
         camelMessage.setBody(message);
-        
+
         return exchange;
     }