cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r709357 - in /cxf/branches/2.1.x-fixes: ./ rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/ rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/
Date Fri, 31 Oct 2008 06:10:08 GMT
Author: ningjiang
Date: Thu Oct 30 23:10:08 2008
New Revision: 709357

URL: http://svn.apache.org/viewvc?rev=709357&view=rev
Log:
Merged revisions 709353-709354 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r709353 | ningjiang | 2008-10-31 13:35:50 +0800 (Fri, 31 Oct 2008) | 1 line
  
  CXF-1749 quick fix for JMS Encoding issue
........
  r709354 | ningjiang | 2008-10-31 13:49:18 +0800 (Fri, 31 Oct 2008) | 1 line
  
  CXF-1749 update the messages.properties
........

Modified:
    cxf/branches/2.1.x-fixes/   (props changed)
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSDestination.java
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/Messages.properties
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
    cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSUtilsTest.java

Propchange: cxf/branches/2.1.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct 30 23:10:08 2008
@@ -1 +1 @@
-/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100
+/cxf/trunk:686333-686363,686764,686820,687096,687194,687363,687387,687463,687543,687722,687798,687814,687817,687891,687910,687914,688086,688102,688133,688596,688735,688870,689572,689596,689855,689924,690067,690289,691246,691271,691295,691338,691355,691488,691602,691646,691706,691728,692116,692157,692310,692466,692499,693653,693819,694179,694263,694417,694716,694744,694747,694795,694869,694981,694987,694993,695041,695096,695396,695484,695537,695552,695561,695619,695684,695835,695840,695868,695935,695977,696016,696094,696433,696720,697085,697868,698128,699289,700261,700507,700602,700981,701316,701783,701830,701862,702187,702205-702248,702267,702547,702561,702580,702602,702609,702616,702653,702656,702957,703191,703239,703309,703501,703513,703548,704584,704937,704997,705150,705235,705274,705340,705446,705548,705614,705692,705708,706482,706631,706675,706900,706909,707034,707089,707100,709353-709354

Propchange: cxf/branches/2.1.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
Thu Oct 30 23:10:08 2008
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Level;
@@ -174,21 +175,26 @@
         Message inMessage = new MessageImpl();
         exchange.setInMessage(inMessage);
         LOG.log(Level.FINE, "client received reply: ", jmsMessage);
-        JMSUtils.populateIncomingContext(jmsMessage, inMessage, JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
-        byte[] response = JMSUtils.retrievePayload(jmsMessage);
-        LOG.log(Level.FINE, "The Response Message payload is : [" + response + "]");
-        inMessage.setContent(InputStream.class, new ByteArrayInputStream(response));
-
-        if (exchange.isSynchronous()) {
-            synchronized (exchange) {
-                exchange.put(CORRELATED, Boolean.TRUE);
-                exchange.notifyAll();
+        try {
+            JMSUtils.populateIncomingContext(jmsMessage, inMessage, JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
+        
+            byte[] response = JMSUtils.retrievePayload(jmsMessage, (String)inMessage.get(Message.ENCODING));
+            LOG.log(Level.FINE, "The Response Message payload is : [" + response + "]");
+            inMessage.setContent(InputStream.class, new ByteArrayInputStream(response));
+
+            if (exchange.isSynchronous()) {
+                synchronized (exchange) {
+                    exchange.put(CORRELATED, Boolean.TRUE);
+                    exchange.notifyAll();
+                }
             }
-        }
         
-        //REVISIT: put on a workqueue?
-        if (incomingObserver != null) {
-            incomingObserver.onMessage(exchange.getInMessage());
+            //REVISIT: put on a workqueue?
+            if (incomingObserver != null) {
+                incomingObserver.onMessage(exchange.getInMessage());
+            }
+        } catch (UnsupportedEncodingException ex) {
+            getLogger().log(Level.WARNING, "can't get the right encoding information " +
ex);
         }
     }
 

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSDestination.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSDestination.java?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSDestination.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSDestination.java
Thu Oct 30 23:10:08 2008
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.List;
@@ -158,14 +159,13 @@
     public void onMessage(javax.jms.Message message) {
         try {
             getLogger().log(Level.FINE, "server received request: ", message);
-
-            byte[] request = JMSUtils.retrievePayload(message);
+             // Build CXF message from JMS message
+            MessageImpl inMessage = new MessageImpl();            
+            JMSUtils.populateIncomingContext(message, inMessage, JMSConstants.JMS_SERVER_REQUEST_HEADERS);
+            
+            byte[] request = JMSUtils.retrievePayload(message, (String)inMessage.get(Message.ENCODING));
             getLogger().log(Level.FINE, "The Request Message is [ " + request + "]");
-
-            // Build CXF message from JMS message
-            MessageImpl inMessage = new MessageImpl();
             inMessage.setContent(InputStream.class, new ByteArrayInputStream(request));
-            JMSUtils.populateIncomingContext(message, inMessage, JMSConstants.JMS_SERVER_REQUEST_HEADERS);
             inMessage.put(JMSConstants.JMS_SERVER_RESPONSE_HEADERS, new JMSMessageHeadersType());
             inMessage.put(JMSConstants.JMS_REQUEST_MESSAGE, message);
             inMessage.setDestination(this);
@@ -174,6 +174,8 @@
 
             // handle the incoming message
             incomingObserver.onMessage(inMessage);
+        } catch (UnsupportedEncodingException ex) {
+            getLogger().log(Level.WARNING, "can't get the right encoding information. " +
ex);
         } finally {
             BusFactory.setThreadDefaultBus(null);
         }

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
Thu Oct 30 23:10:08 2008
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.transport.jms;
 
+import java.io.UnsupportedEncodingException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
@@ -28,6 +29,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.jms.BytesMessage;
@@ -39,6 +41,7 @@
 
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.helpers.HttpHeaderHelper;
 import org.springframework.jms.support.JmsUtils;
 import org.springframework.jms.support.converter.MessageConversionException;
 import org.springframework.jms.support.converter.SimpleMessageConverter102;
@@ -97,9 +100,12 @@
      * Extract the payload of an incoming message.
      * 
      * @param message the incoming message
+     * @param encoding the message encoding
      * @return the message payload as byte[]
+     * @throws UnsupportedEncodingException 
      */
-    public static byte[] retrievePayload(Message message) {
+    public static byte[] retrievePayload(Message message, String encoding) 
+        throws UnsupportedEncodingException {
         Object converted;
         try {
             converted = new SimpleMessageConverter102().fromMessage(message);
@@ -109,7 +115,12 @@
             throw JmsUtils.convertJmsAccessException(e);
         }
         if (converted instanceof String) {
-            return ((String)converted).getBytes(); // TODO encoding
+            if (encoding != null) {
+                return ((String)converted).getBytes(encoding); 
+            } else { 
+                // Using the UTF-8 encoding as default
+                return ((String)converted).getBytes("UTF-8");
+            }
         } else if (converted instanceof byte[]) {
             return (byte[])converted;
         } else {
@@ -118,7 +129,8 @@
     }
 
     public static void populateIncomingContext(javax.jms.Message message,
-                                               org.apache.cxf.message.Message inMessage,
String headerType) {
+                                               org.apache.cxf.message.Message inMessage,
String headerType) 
+        throws UnsupportedEncodingException {
         try {
             JMSMessageHeadersType headers = null;
             headers = (JMSMessageHeadersType)inMessage.get(headerType);
@@ -150,13 +162,10 @@
                 if (name.equals(org.apache.cxf.message.Message.CONTENT_TYPE)
                     || name.equals(JMSConstants.JMS_CONTENT_TYPE) && val != null)
{
                     inMessage.put(org.apache.cxf.message.Message.CONTENT_TYPE, val);
+                    // set the message encoding
+                    inMessage.put(org.apache.cxf.message.Message.ENCODING, getEncoding(val));
                 }
-
-                protHeaders.put(name, Collections.singletonList(val));
-                if (name.equals(org.apache.cxf.message.Message.CONTENT_TYPE)
-                    || name.equals(JMSConstants.JMS_CONTENT_TYPE) && val != null)
{
-                    inMessage.put(org.apache.cxf.message.Message.CONTENT_TYPE, val);
-                }
+                
             }
             inMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, protHeaders);
         } catch (JMSException ex) {
@@ -164,6 +173,29 @@
         }
     }
 
+    static String getEncoding(String ct) throws UnsupportedEncodingException {
+        String contentType = ct.toLowerCase();
+        String enc = null;
+        
+        String[] tokens = contentType.split(";");
+        for (String token : tokens) {
+            int index = token.indexOf("charset=");
+            if (index >= 0) {
+                enc = token.substring(index + 8);                
+            }            
+        }
+        
+        String normalizedEncoding = HttpHeaderHelper.mapCharset(enc);
+        if (normalizedEncoding == null) {
+            String m = new org.apache.cxf.common.i18n.Message("INVALID_ENCODING_MSG",
+                                                              LOG, new Object[] {enc}).toString();
+            LOG.log(Level.WARNING, m);
+            throw new UnsupportedEncodingException(m);   
+        }
+        
+        return normalizedEncoding;
+    }
+
     protected static void addProtocolHeaders(Message message, Map<String, List<String>>
headers)
         throws JMSException {
         if (headers == null) {
@@ -191,7 +223,18 @@
     }
 
     public static void addContentTypeToProtocolHeader(org.apache.cxf.message.Message message)
{
-        String contentType = (String)message.get(org.apache.cxf.message.Message.CONTENT_TYPE);
+        String contentType = (String)message.get(org.apache.cxf.message.Message.CONTENT_TYPE);
       
+        String enc = (String) message.get(org.apache.cxf.message.Message.ENCODING);
+        // add the encoding information
+        if (null != contentType) {
+            if (enc != null && contentType.indexOf("charset=") == -1) {
+                contentType = contentType + "; charset=" + enc;
+            }
+        } else if (enc != null) {
+            contentType = "text/xml; charset=" + enc;
+        } else {
+            contentType = "text/xml";
+        }
 
         // Retrieve or create protocol headers
         Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>)message

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/Messages.properties
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/Messages.properties?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/Messages.properties
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/Messages.properties
Thu Oct 30 23:10:08 2008
@@ -22,4 +22,5 @@
 INSUFFICIENT_CONFIGURATION_CONDUIT = Insufficient configuration for Conduit. Did you configure
a <jms:conduit name=\"{0}\"> and set the jndiConnectionFactoryName ?
 INSUFFICIENT_CONFIGURATION_DESTINATION = Insufficient configuration for Destination. Did
you configure a <jms:destination name=\"{0}\"> and set the jndiConnectionFactoryName
?
 JMSCONFIGFEATURE_ONLY_JMS = You can only use JMSConfigFeature with the jms transport
-JMSCONFIG_REQUIRED = You have to set the property jmsConfig
\ No newline at end of file
+JMSCONFIG_REQUIRED = You have to set the property jmsConfig
+INVALID_ENCODING_MSG = Invalid character set {0} in the message.
\ No newline at end of file

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
Thu Oct 30 23:10:08 2008
@@ -198,6 +198,7 @@
         header.setJMSPriority(1);
         header.setTimeToLive(1000);
         outMessage.put(JMSConstants.JMS_CLIENT_REQUEST_HEADERS, header);
+        outMessage.put(Message.ENCODING, "US-ASCII");
     }
 
     private void verifyReceivedMessage(Message inMessage) {
@@ -210,12 +211,16 @@
             ex.printStackTrace();
         }
         String reponse = IOUtils.newStringFromBytes(bytes);
-        assertEquals("The reponse date should be equals", reponse, "HelloWorld");
+        assertEquals("The reponse date should be equal", reponse, "HelloWorld");
     }
 
     private void verifyRequestResponseHeaders(Message inMessage, Message outMessage) {
         JMSMessageHeadersType outHeader = (JMSMessageHeadersType)outMessage
             .get(JMSConstants.JMS_CLIENT_REQUEST_HEADERS);
+        String inEncoding = (String) inMessage.get(Message.ENCODING);
+        String outEncoding = (String) outMessage.get(Message.ENCODING);
+        
+        assertEquals("The message encoding should be equal", inEncoding, outEncoding);
 
         JMSMessageHeadersType inHeader = (JMSMessageHeadersType)inMessage
             .get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
@@ -336,6 +341,8 @@
                     backConduit = destination.getBackChannel(m, null, null);
                     // wait for the message to be got from the conduit
                     Message replyMessage = new MessageImpl();
+                    // copy the message encoding
+                    replyMessage.put(Message.ENCODING, m.get(Message.ENCODING));
                     sendoutMessage(backConduit, replyMessage, true);
                 } catch (Exception e) {
                     throw new RuntimeException(e);

Modified: cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSUtilsTest.java?rev=709357&r1=709356&r2=709357&view=diff
==============================================================================
--- cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSUtilsTest.java
(original)
+++ cxf/branches/2.1.x-fixes/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSUtilsTest.java
Thu Oct 30 23:10:08 2008
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.transport.jms;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.util.Properties;
 
 import javax.naming.Context;
@@ -46,5 +48,22 @@
         assertTrue("Environment should not be empty", env.size() > 0);
         assertTrue("Environemnt should contain NamingBatchSize property", env.get(Context.BATCHSIZE)
!= null);
     }
+    
+    @Test
+    public void testGetEncoding() throws IOException {                
+        assertEquals("Get the wrong encoding", JMSUtils.getEncoding("text/xml; charset=utf-8"),
"UTF-8");
+        assertEquals("Get the wrong encoding", JMSUtils.getEncoding("text/xml"), "UTF-8");
+        assertEquals("Get the wrong encoding", JMSUtils.getEncoding("text/xml; charset=GBK"),
"GBK");
+        try {
+            JMSUtils.getEncoding("text/xml; charset=asci");
+            fail("Expect the exception here");
+        } catch (Exception ex) {
+            assertTrue("we should get the UnsupportedEncodingException here",
+                       ex instanceof UnsupportedEncodingException);
+        }
+        
+        
+        
+    }
 
 }



Mime
View raw message