camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject git commit: CAMEL-7078 camel-cxf-transport component should propagate SecurityContext with thanks to Sergey
Date Wed, 18 Dec 2013 14:40:10 GMT
Updated Branches:
  refs/heads/master 20b5756cc -> 00077a0aa


CAMEL-7078 camel-cxf-transport component should propagate SecurityContext with thanks to Sergey


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/00077a0a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/00077a0a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/00077a0a

Branch: refs/heads/master
Commit: 00077a0aa1eff2ca38f457d8ecc75cbe8918db0f
Parents: 20b5756
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Wed Dec 18 22:39:51 2013 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Wed Dec 18 22:39:51 2013 +0800

----------------------------------------------------------------------
 components/camel-cxf-transport/pom.xml          |  1 +
 .../common/message/DefaultCxfMessageMapper.java | 40 +++++++++++++++++++-
 .../message/DefaultCxfMessageMapperTest.java    | 33 ++++++++++++++--
 3 files changed, 70 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/00077a0a/components/camel-cxf-transport/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-cxf-transport/pom.xml b/components/camel-cxf-transport/pom.xml
index 750ec8d..138a582 100644
--- a/components/camel-cxf-transport/pom.xml
+++ b/components/camel-cxf-transport/pom.xml
@@ -39,6 +39,7 @@
       javax.ws.rs*;version="[0.0,2)",
       javax.xml.stream*;version="[0.0,2)",
       javax.xml.ws*;version="[0.0,3.0)",
+      javax.servlet*;resolution="optional";version="[0,4)",
       org.apache.camel.*;${camel.osgi.import.camel.version},
       org.springframework.beans*;resolution:=optional,
       ${camel.osgi.import.defaults},

http://git-wip-us.apache.org/repos/asf/camel/blob/00077a0a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapper.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapper.java
b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapper.java
index 4d4e266..d2510be 100644
--- a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapper.java
+++ b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapper.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.cxf.common.message;
 
+import java.lang.reflect.Method;
+import java.security.Principal;
 import java.util.List;
 import java.util.Map;
 
@@ -23,6 +25,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.security.SecurityContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -67,15 +70,50 @@ public class DefaultCxfMessageMapper implements CxfMessageMapper {
         
         Object request = camelMessage.getHeader(Exchange.HTTP_SERVLET_REQUEST);
         answer.put(CXF_HTTP_REQUEST, request);
+        
+        if (request != null) {
+            setSecurityContext(answer, request);
+        }
+        
         Object response = camelMessage.getHeader(Exchange.HTTP_SERVLET_RESPONSE);
         answer.put(CXF_HTTP_RESPONSE, response);
-        // TODO propagate security context
         
         LOG.trace("Processing {}, requestContentType = {}, acceptContentTypes = {}, encoding
= {}, path = {}, basePath = {}, verb = {}",
             new Object[]{camelExchange, requestContentType, acceptContentTypes, enc, path,
basePath, verb});
 
         return answer;
     }
+    
+    protected void setSecurityContext(Message cxfMessage, final Object request) {
+        try {
+            final Method getPrincipalMethod = request.getClass()
+                .getMethod("getUserPrincipal", new Class[] {});
+            final Method userInRoleMethod = request.getClass()
+                .getMethod("isUserInRole", new Class[] {String.class});
+            cxfMessage.put(SecurityContext.class, new SecurityContext() {
+
+                public Principal getUserPrincipal() {
+                    try {
+                        return (Principal)getPrincipalMethod.invoke(request, new Object[]
{});
+                    } catch (Throwable t) {
+                        return null;
+                    }
+                }
+
+                @Override
+                public boolean isUserInRole(String role) {
+                    try {
+                        return (Boolean)userInRoleMethod.invoke(request, new Object[] {role});
+                    } catch (Throwable t) {
+                        return false;
+                    }
+                }
+
+            });
+        } catch (Throwable t) {
+            // not expected
+        }
+    }
 
     public void propagateResponseHeadersToCamel(Message cxfMessage, Exchange exchange,
                                                 HeaderFilterStrategy strategy) {

http://git-wip-us.apache.org/repos/asf/camel/blob/00077a0a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapperTest.java
b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapperTest.java
index f08860a..e136e4b 100644
--- a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapperTest.java
+++ b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/common/message/DefaultCxfMessageMapperTest.java
@@ -20,11 +20,15 @@ import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.Collections;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.component.cxf.transport.CamelTransportConstants;
 import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.cxf.common.security.SimplePrincipal;
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.security.SecurityContext;
 import org.easymock.EasyMock;
 import org.junit.Assert;
 import org.junit.Test;
@@ -38,15 +42,38 @@ public class DefaultCxfMessageMapperTest extends Assert {
 
         DefaultCxfMessageMapper mapper = new DefaultCxfMessageMapper();
 
-        Exchange camelExchange = setupCamelExchange(requestURI, requestPath);
+        Exchange camelExchange = setupCamelExchange(requestURI, requestPath, null);
         Message cxfMessage = mapper.createCxfMessageFromCamelExchange(
             camelExchange, EasyMock.createMock(HeaderFilterStrategy.class));
 
         assertEquals(requestURI, cxfMessage.get(Message.REQUEST_URI).toString());
         assertEquals(requestPath, cxfMessage.get(Message.BASE_PATH).toString());
     }
+    
+    @Test
+    public void testSecurityContext() {
+        DefaultCxfMessageMapper mapper = new DefaultCxfMessageMapper();
 
-    private Exchange setupCamelExchange(String requestURI, String requestPath) {
+        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
+        request.getUserPrincipal();
+        EasyMock.expectLastCall().andReturn(new SimplePrincipal("barry"));
+        request.isUserInRole("role1");
+        EasyMock.expectLastCall().andReturn(true);
+        request.isUserInRole("role2");
+        EasyMock.expectLastCall().andReturn(false);
+        EasyMock.replay(request);
+        Exchange camelExchange = setupCamelExchange("/", "/", request);
+        
+        Message cxfMessage = mapper.createCxfMessageFromCamelExchange(
+            camelExchange, EasyMock.createMock(HeaderFilterStrategy.class));
+        SecurityContext sc = cxfMessage.get(SecurityContext.class);
+        assertNotNull(sc);
+        assertEquals("barry", sc.getUserPrincipal().getName());
+        assertTrue(sc.isUserInRole("role1"));
+        assertFalse(sc.isUserInRole("role2"));
+    }
+
+    private Exchange setupCamelExchange(String requestURI, String requestPath, HttpServletRequest
request) {
         org.apache.camel.Message camelMessage = EasyMock
             .createMock(org.apache.camel.Message.class);
         Exchange camelExchange = EasyMock.createMock(Exchange.class);
@@ -78,7 +105,7 @@ public class DefaultCxfMessageMapperTest extends Assert {
         camelMessage.getHeader(Exchange.HTTP_QUERY, String.class);
         EasyMock.expectLastCall().andReturn("");
         camelMessage.getHeader(Exchange.HTTP_SERVLET_REQUEST);
-        EasyMock.expectLastCall().andReturn(null);
+        EasyMock.expectLastCall().andReturn(request);
         camelMessage.getHeader(Exchange.HTTP_SERVLET_RESPONSE);
         EasyMock.expectLastCall().andReturn(null);
 


Mime
View raw message