servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r984437 - in /servicemix/smx4/features/trunk: ./ camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ camel/servicemix-camel/src/test/resources/
Date Wed, 11 Aug 2010 15:17:34 GMT
Author: gertv
Date: Wed Aug 11 15:17:27 2010
New Revision: 984437

URL: http://svn.apache.org/viewvc?rev=984437&view=rev
Log:
SMX4-576: Ensure that Subject is properly conveyed by the camel-nmr component

Added:
    servicemix/smx4/features/trunk/.gitignore
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java
Modified:
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/resources/log4j.properties

Added: servicemix/smx4/features/trunk/.gitignore
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/.gitignore?rev=984437&view=auto
==============================================================================
--- servicemix/smx4/features/trunk/.gitignore (added)
+++ servicemix/smx4/features/trunk/.gitignore Wed Aug 11 15:17:27 2010
@@ -0,0 +1,5 @@
+.settings
+.classpath
+.project
+target
+*.i??
\ No newline at end of file

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java?rev=984437&r1=984436&r2=984437&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
(original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixBinding.java
Wed Aug 11 15:17:27 2010
@@ -22,6 +22,7 @@ import java.util.Map;
 import java.util.Set;
 
 import javax.activation.DataHandler;
+import javax.security.auth.Subject;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
@@ -47,7 +48,10 @@ public class ServiceMixBinding {
             addNmrHeaders(nmrMessage, camelMessage);
             nmrMessage.getAttachments().clear();
             nmrMessage.getAttachments().putAll(camelMessage.getAttachments());
-            addSecuritySubject(nmrMessage, camelMessage);
+            //addSecuritySubject(nmrMessage, camelMessage);
+
+            // propagate the security subject
+            nmrMessage.setSecuritySubject(camelMessage.getHeader(Exchange.AUTHENTICATION,
Subject.class));
         }
     }
 
@@ -55,7 +59,10 @@ public class ServiceMixBinding {
         camelMessage.setBody(nmrMessage.getBody());
         camelMessage.setHeader(NMR_MESSAGE, nmrMessage);
         camelMessage.getHeaders().putAll(nmrMessage.getHeaders());
-        addCamelAttachments(nmrMessage, camelMessage);        
+        addCamelAttachments(nmrMessage, camelMessage);
+
+        // copy the security subject
+        camelMessage.setHeader(Exchange.AUTHENTICATION, nmrMessage.getSecuritySubject());
     }
     
     public org.apache.servicemix.nmr.api.Exchange populateNmrExchangeFromCamelExchange(Exchange
camelExchange, Channel client)  {
@@ -75,7 +82,7 @@ public class ServiceMixBinding {
         
         // copy the nmrExchange's properties
         answer.getProperties().putAll(nmrExchange.getProperties());
-        
+
         org.apache.servicemix.nmr.api.Message inMessage = nmrExchange.getIn();
         if (inMessage != null) {
             Message message = new DefaultMessage();
@@ -122,24 +129,20 @@ public class ServiceMixBinding {
         }
         
     }
-    
-    protected void addSecuritySubject(org.apache.servicemix.nmr.api.Message nmrMessage, Message
camelMessage) {
-        org.apache.servicemix.nmr.api.Message from = getNmrMessage(camelMessage);
-        if (from != null) {
-            // copy the security subject
-            nmrMessage.setSecuritySubject(from.getSecuritySubject());
-        }       
-                
-   }
-        
-    protected org.apache.servicemix.nmr.api.Message getNmrMessage(Message message) {
+
+    /**
+     * Extract the underlying NMR {@link org.apache.servicemix.nmr.api.Message} for a Camel
message
+     *
+     * @param message the Camel Message
+     * @return the corresponding NMR message
+     */
+    public org.apache.servicemix.nmr.api.Message getNmrMessage(Message message) {
         if (message.getHeader(NMR_MESSAGE) != null) {
-            return (org.apache.servicemix.nmr.api.Message)message.getHeader(NMR_MESSAGE);
+            return message.getHeader(NMR_MESSAGE, org.apache.servicemix.nmr.api.Message.class);
         }
         return null;
     }
 
-
     /**
      * Extract the NMR Exchange from the Camel Exchange
      *

Added: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java?rev=984437&view=auto
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java
(added)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java
Wed Aug 11 15:17:27 2010
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.camel.nmr;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+import javax.security.auth.Subject;
+import java.security.Principal;
+
+/**
+ * Test case to ensure the Camel NMR component is capable of conveying security information
provided by Camel
+ */
+public class SecuritySubjectTest extends AbstractComponentTest {
+
+    private static final String REQUEST_MESSAGE = "Simple message body";
+    private static final String RESPONSE_MESSAGE = "Simple message reply";
+
+    private static final Principal CLARK_KENT = new PrincipalImpl("Clark Kent");
+    private static final Principal SUPERMAN = new PrincipalImpl("Superman");
+
+    /*
+     * Test case for conveying security subject information in an InOnly MEP
+     */
+    public void testInOnlyWithSecuritySubject() throws Exception {
+        Subject subject = createSubject(CLARK_KENT);
+
+        MockEndpoint mock = getMockEndpoint("mock:simple");
+        mock.expectedBodiesReceived(REQUEST_MESSAGE);
+
+        template.sendBodyAndHeader("direct:simple", REQUEST_MESSAGE,
+                Exchange.AUTHENTICATION, subject);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    /*
+     * Test case for conveying security subject information in an InOut MEP
+     */
+    public void testInOutWithSecuritySubject() throws Exception {
+        final Subject subject = createSubject(CLARK_KENT);
+
+        Exchange result = template.request("direct:simple", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(REQUEST_MESSAGE);
+                exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject);
+            }
+        });
+
+        assertSecuritySubject(SUPERMAN, result.getOut().getHeader(Exchange.AUTHENTICATION,
Subject.class));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                from("direct:simple").to("nmr:simple");
+                from("nmr:simple").process(assertSecuritySubject()).to("mock:simple");
+            }
+        };
+    }
+
+    /*
+     * Build a simple {@link Processor} to ensure our exchange contains the correct security
information
+     */
+    private Processor assertSecuritySubject() {
+        return new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                Subject subject = exchange.getIn().getHeader(Exchange.AUTHENTICATION, Subject.class);
+                assertSecuritySubject(CLARK_KENT, subject);
+
+                if (ExchangePattern.InOut.equals(exchange.getPattern())) {
+                    // wow, Clark Kent is Superman, who would have thought that?
+                    exchange.getOut().copyFrom(exchange.getIn());
+                    exchange.getOut().setBody(RESPONSE_MESSAGE);
+                    exchange.getOut().setHeader(Exchange.AUTHENTICATION, createSubject(SUPERMAN));
+                }
+            }
+        };
+    }
+
+    @Override
+    public void exchangeSent(org.apache.servicemix.nmr.api.Exchange exchange) {
+        super.exchangeSent(exchange);
+
+        // let's check the subject inside the NMR as well
+        if (exchange.getIn(false) != null) {
+            assertSecuritySubject(CLARK_KENT, exchange.getIn().getSecuritySubject());
+        }
+        if (exchange.getOut(false) != null) {
+            assertSecuritySubject(SUPERMAN, exchange.getOut().getSecuritySubject());
+        }
+    }
+
+    /*
+    * Ensure that the Subject is valid and matches the principal
+    */
+    private void assertSecuritySubject(Principal expected, Subject subject) {
+        assertNotNull(subject);
+        assertTrue("Subject should have contained " + expected, subject.getPrincipals().contains(expected));
+    }
+
+    /*
+     * Create a new Subject, containing the provided principal information
+     */
+    private Subject createSubject(Principal principal) {
+        final Subject subject = new Subject();
+        subject.getPrincipals().add(principal);
+        return subject;
+    }
+
+    /*
+     * Simple {@link Principal} implementation used for testing
+     */
+    private static final class PrincipalImpl implements Principal {
+
+        private final String name;
+
+        public PrincipalImpl(String name) {
+            super();
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public String toString() {
+            return String.format("Principal [%s]", name);
+        }
+    }
+}

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/resources/log4j.properties?rev=984437&r1=984436&r2=984437&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/resources/log4j.properties
(original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/resources/log4j.properties
Wed Aug 11 15:17:27 2010
@@ -18,7 +18,7 @@
 #
 # The logging properties used during tests..
 #
-log4j.rootLogger=DEBUG, out
+log4j.rootLogger=DEBUG, out, stdout
 
 # Separate loggers for Camel and ServiceMix to reduce lock contention
 log4j.org.apache.camel=DEBUG, out



Mime
View raw message