You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by cs...@apache.org on 2009/02/24 00:40:54 UTC

svn commit: r747217 - in /cxf/trunk: rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/ rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/ testutils/src/main/resources/wsdl/

Author: cschneider
Date: Mon Feb 23 23:40:53 2009
New Revision: 747217

URL: http://svn.apache.org/viewvc?rev=747217&view=rev
Log:
CXF-2055: Support passing username of producer to SecurityContext using JMSXUserID

Modified:
    cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
    cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSBrokerSetup.java
    cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
    cxf/trunk/testutils/src/main/resources/wsdl/jms_test.wsdl

Modified: cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java?rev=747217&r1=747216&r2=747217&view=diff
==============================================================================
--- cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java (original)
+++ cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java Mon Feb 23 23:40:53 2009
@@ -20,6 +20,7 @@
 package org.apache.cxf.transport.jms;
 
 import java.io.UnsupportedEncodingException;
+import java.security.Principal;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -39,6 +40,7 @@
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.HttpHeaderHelper;
+import org.apache.cxf.security.SecurityContext;
 import org.springframework.jms.support.JmsUtils;
 import org.springframework.jms.support.converter.MessageConversionException;
 import org.springframework.jms.support.converter.SimpleMessageConverter102;
@@ -47,8 +49,9 @@
 
     static final Logger LOG = LogUtils.getL7dLogger(JMSUtils.class);
 
-    private static final char[] CORRELATTION_ID_PADDING =  {'0', '0', '0', '0', '0', '0', '0', '0', 
-                                                            '0', '0', '0', '0', '0', '0', '0'};
+    private static final char[] CORRELATTION_ID_PADDING = {
+        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
+    };
 
     private JMSUtils() {
 
@@ -76,7 +79,7 @@
      * Create a JMS of the appropriate type populated with the given payload.
      * 
      * @param payload the message payload, expected to be either of type String or byte[] depending on payload
-     *                type
+     *            type
      * @param session the JMS session
      * @param replyTo the ReplyTo destination if any
      * @return a JMS of the appropriate type populated with the given payload
@@ -102,9 +105,9 @@
      * @param message the incoming message
      * @param encoding the message encoding
      * @return the message payload as byte[]
-     * @throws UnsupportedEncodingException 
+     * @throws UnsupportedEncodingException
      */
-    public static byte[] retrievePayload(Message message, String encoding) 
+    public static byte[] retrievePayload(Message message, String encoding)
         throws UnsupportedEncodingException {
         Object converted;
         try {
@@ -116,8 +119,8 @@
         }
         if (converted instanceof String) {
             if (encoding != null) {
-                return ((String)converted).getBytes(encoding); 
-            } else { 
+                return ((String)converted).getBytes(encoding);
+            } else {
                 // Using the UTF-8 encoding as default
                 return ((String)converted).getBytes("UTF-8");
             }
@@ -129,7 +132,7 @@
     }
 
     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;
@@ -165,35 +168,74 @@
                     // set the message encoding
                     inMessage.put(org.apache.cxf.message.Message.ENCODING, getEncoding(val));
                 }
-                
+
             }
             inMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, protHeaders);
+
+            SecurityContext securityContext = buildSecurityContext(message);
+            inMessage.put(SecurityContext.class, securityContext);
         } catch (JMSException ex) {
             throw JmsUtils.convertJmsAccessException(ex);
         }
     }
 
+    /**
+     * Extract the property JMSXUserID from the jms message and create a SecurityContext from it. 
+     * For more info see Jira Issue CXF-2055
+     * {@link https://issues.apache.org/jira/browse/CXF-2055}
+     * 
+     * @param message jms message to retrieve user information from
+     * @return SecurityContext that contains the user of the producer of the message as the Principal
+     * @throws JMSException if something goes wrong
+     */
+    private static SecurityContext buildSecurityContext(javax.jms.Message message) throws JMSException {
+        final String jmsUserName = message.getStringProperty("JMSXUserID");
+        if (jmsUserName == null) {
+            return null;
+        }
+        final Principal principal = new Principal() {
+            public String getName() {
+                return jmsUserName;
+            }
+
+        };
+
+        SecurityContext securityContext = new SecurityContext() {
+
+            public Principal getUserPrincipal() {
+                return principal;
+            }
+
+            public boolean isUserInRole(String role) {
+                return false;
+            }
+
+        };
+        return securityContext;
+    }
+
     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);
                 break;
-            }            
+            }
         }
-        
+
         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();
+            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);   
+            throw new UnsupportedEncodingException(m);
         }
-        
+
         return normalizedEncoding;
     }
 
@@ -224,8 +266,8 @@
     }
 
     public static void addContentTypeToProtocolHeader(org.apache.cxf.message.Message message) {
-        String contentType = (String)message.get(org.apache.cxf.message.Message.CONTENT_TYPE);        
-        String enc = (String) message.get(org.apache.cxf.message.Message.ENCODING);
+        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) {
@@ -244,7 +286,7 @@
             headers = new HashMap<String, List<String>>();
             message.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
         }
-        
+
         // Add content type to the protocol headers
         List<String> ct;
         if (headers.get(JMSConstants.JMS_CONTENT_TYPE) != null) {
@@ -284,7 +326,7 @@
         jmsMessage.setJMSCorrelationID(correlationId);
         return jmsMessage;
     }
-    
+
     public static String createCorrelationId(final String prefix, long i) {
         String index = Long.toHexString(i);
         StringBuffer id = new StringBuffer(prefix);

Modified: cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSBrokerSetup.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSBrokerSetup.java?rev=747217&r1=747216&r2=747217&view=diff
==============================================================================
--- cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSBrokerSetup.java (original)
+++ cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSBrokerSetup.java Mon Feb 23 23:40:53 2009
@@ -80,6 +80,7 @@
                 synchronized (this) {                                     
                     broker.setPersistenceAdapter(new MemoryPersistenceAdapter());                    
                     broker.setTmpDataDirectory(new File("./target"));
+                    broker.setPopulateJMSXUserID(true);
                     broker.addConnector(brokerUrl);
                     broker.start();
                     Thread.sleep(200);

Modified: cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java?rev=747217&r1=747216&r2=747217&view=diff
==============================================================================
--- cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java (original)
+++ cxf/trunk/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java Mon Feb 23 23:40:53 2009
@@ -33,6 +33,7 @@
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.security.SecurityContext;
 import org.apache.cxf.transport.Conduit;
 import org.apache.cxf.transport.MessageObserver;
 import org.apache.cxf.transport.MultiplexDestination;
@@ -95,7 +96,8 @@
         }
         return jmsDestination;
     }
-
+    
+    
     @Test
     public void testGetConfigurationFromSpring() throws Exception {
         SpringBusFactory bf = new SpringBusFactory();
@@ -406,5 +408,27 @@
                          "HelloWorldService", "HelloWorldPort");
         final JMSDestination destination = setupJMSDestination(true);
         assertTrue("is multiplex", destination instanceof MultiplexDestination);
+        destination.shutdown();
+    }
+    
+    @Test
+    public void testSecurityContext() throws Exception {
+        inMessage = null;
+        setupServiceInfo("http://cxf.apache.org/hello_world_jms", "/wsdl/jms_test.wsdl",
+                         "HelloWorldService", "HelloWorldPort");
+        final JMSDestination destination = setupJMSDestination(true);
+        // set up the conduit send to be true
+        JMSConduit conduit = setupJMSConduit(true, false);
+        final Message outMessage = new MessageImpl();
+        setupMessageHeader(outMessage, null);
+        sendoutMessage(conduit, outMessage, true);
+        waitForReceiveDestMessage();
+        SecurityContext securityContext = destMessage.get(SecurityContext.class);
+        assertNotNull("SecurityContext should be set in message received by JMSDestination", securityContext);
+        assertEquals("Principal in SecurityContext should be", "testUser", 
+                     securityContext.getUserPrincipal().getName());
+        conduit.close();
+        destination.shutdown();
     }
+
 }

Modified: cxf/trunk/testutils/src/main/resources/wsdl/jms_test.wsdl
URL: http://svn.apache.org/viewvc/cxf/trunk/testutils/src/main/resources/wsdl/jms_test.wsdl?rev=747217&r1=747216&r2=747217&view=diff
==============================================================================
--- cxf/trunk/testutils/src/main/resources/wsdl/jms_test.wsdl (original)
+++ cxf/trunk/testutils/src/main/resources/wsdl/jms_test.wsdl Mon Feb 23 23:40:53 2009
@@ -341,7 +341,9 @@
                <jms:clientConfig useConduitIdSelector="false"/>
                <jms:address
                    jndiConnectionFactoryName="ConnectionFactory" 
-                   jndiDestinationName="dynamicQueues/test.jmstransport.text">
+                   jndiDestinationName="dynamicQueues/test.jmstransport.text"
+                   connectionUserName="testUser"
+               	   connectionPassword="testPassword">
                    <jms:JMSNamingProperty name="java.naming.factory.initial" value="org.apache.activemq.jndi.ActiveMQInitialContextFactory"/>
                    <jms:JMSNamingProperty name="java.naming.provider.url" value="tcp://localhost:61500"/>
                </jms:address>