camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [2/2] git commit: CAMEL-7078 camel-cxf-transport component should propagate SecurityContext with thanks to Sergey
Date Wed, 18 Dec 2013 14:43:05 GMT
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/09b66baa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/09b66baa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/09b66baa

Branch: refs/heads/camel-2.11.x
Commit: 09b66baafbcc40b099fecd87222814281cecfba3
Parents: b775252
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:42:40 2013 +0800

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


http://git-wip-us.apache.org/repos/asf/camel/blob/09b66baa/components/camel-cxf-transport/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-cxf-transport/pom.xml b/components/camel-cxf-transport/pom.xml
index 5c085d8..4af0258 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/09b66baa/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMesssageMapper.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMesssageMapper.java
b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMesssageMapper.java
index 77e5e20..9b122fd 100644
--- a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMesssageMapper.java
+++ b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/common/message/DefaultCxfMesssageMapper.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 DefaultCxfMesssageMapper 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/09b66baa/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 1cc7f58..2d57f4a 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 {
 
         DefaultCxfMesssageMapper mapper = new DefaultCxfMesssageMapper();
 
-        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();
+
+        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) {
+    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