Author: wtam
Date: Tue Dec 30 19:17:58 2008
New Revision: 730299

URL: http://svn.apache.org/viewvc?rev=730299&view=rev
Log:
[CAMEL-1203] patch provided by Przemyslaw Budzik that enables basic 
authentication for Restlet endpoints

Added:
    
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
   (with props)
    
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
   (with props)
    
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/
    
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
   (with props)
    activemq/camel/trunk/components/camel-restlet/src/test/resources/org/
    activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/
    
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/
    
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/
    
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/
    
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
   (with props)
Modified:
    activemq/camel/trunk/components/camel-restlet/pom.xml
    
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
    
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
    
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java

Modified: activemq/camel/trunk/components/camel-restlet/pom.xml
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/pom.xml?rev=730299&r1=730298&r2=730299&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-restlet/pom.xml (original)
+++ activemq/camel/trunk/components/camel-restlet/pom.xml Tue Dec 30 19:17:58 
2008
@@ -62,6 +62,19 @@
     </dependency>
 
     <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-spring</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-spring</artifactId>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <scope>test</scope>

Modified: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=730299&r1=730298&r2=730299&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
 (original)
+++ 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
 Tue Dec 30 19:17:58 2008
@@ -25,6 +25,8 @@
 import org.apache.camel.converter.jaxp.StringSource;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.restlet.data.ChallengeResponse;
+import org.restlet.data.ChallengeScheme;
 import org.restlet.data.Form;
 import org.restlet.data.MediaType;
 import org.restlet.data.Request;
@@ -105,6 +107,20 @@
             LOG.debug("Populate Restlet request from exchange body: " + body);
         }
         
+        String login = (String) exchange.getIn().removeHeader(
+                RestletConstants.LOGIN);
+        String password = (String) exchange.getIn().removeHeader(
+                RestletConstants.PASSWORD);
+          
+        if (login != null && password != null) {
+            ChallengeResponse authentication = new ChallengeResponse(
+                    ChallengeScheme.HTTP_BASIC, login, password);
+            request.setChallengeResponse(authentication);
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Basic HTTP Authentication has been applied");
+            }
+        }
+        
         for (Map.Entry<String, Object> entry : 
exchange.getIn().getHeaders().entrySet()) {
             form.add(entry.getKey(), entry.getValue().toString());
             if (LOG.isDebugEnabled()) {

Modified: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java?rev=730299&r1=730298&r2=730299&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
 (original)
+++ 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
 Tue Dec 30 19:17:58 2008
@@ -25,7 +25,10 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.restlet.Component;
+import org.restlet.Guard;
+import org.restlet.Restlet;
 import org.restlet.Server;
+import org.restlet.data.ChallengeScheme;
 import org.restlet.data.Method;
 import org.restlet.data.Protocol;
 
@@ -39,7 +42,7 @@
 
     private Map<String, Server> servers = new HashMap<String, Server>();
     private Map<String, MethodBasedRouter> routers = new HashMap<String, 
MethodBasedRouter>();
-
+    
     private Component component = new Component();
 
     @Override
@@ -52,18 +55,35 @@
         if (ref != null) {
             restletBinding = 
CamelContextHelper.mandatoryLookup(getCamelContext(), 
                     ref, RestletBinding.class);
+            if (restletBinding == null) {
+                LOG.warn("Binding '" + ref + "' cannot be found in the 
context");
+            }
         }
         
         if (restletBinding == null) {
             restletBinding = new DefaultRestletBinding();
         }
         
+        Map<String, String> realm = null;
+        ref = getAndRemoveParameter(parameters, "restletRealmRef", 
String.class);
+        if (ref != null) {
+            realm = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, 
Map.class);
+            if (realm == null) {
+                LOG.warn("Realm '" + ref + "' cannot be found in the context");
+            }
+        }
+        
         Method method = getAndRemoveParameter(parameters, "restletMethod", 
Method.class);
         RestletEndpoint result = new RestletEndpoint(this, remaining, 
parameters, restletBinding);
+        
         if (method != null) {
             result.setRestletMethod(method);
         }
         
+        if (realm != null) {
+            result.setRealm(realm);
+        }
+                
         return result;
     }
     
@@ -91,7 +111,23 @@
         RestletEndpoint endpoint = (RestletEndpoint)consumer.getEndpoint();
         addServerIfNeccessary(endpoint);
         MethodBasedRouter router = getMethodRouter(endpoint.getUriPattern());
-        router.addRoute(endpoint.getRestletMethod(), consumer.getRestlet());
+        
+        Map<String, String> realm = endpoint.getRealm();
+        Restlet target = consumer.getRestlet();
+        if (realm != null && realm.size() > 0) {
+            Guard guard = new 
Guard(component.getContext().createChildContext(), 
+                    ChallengeScheme.HTTP_BASIC, "Camel-Restlet Endpoint 
Realm");
+            for (Map.Entry<String, String> entry : realm.entrySet()) {
+                guard.getSecrets().put(entry.getKey(), 
entry.getValue().toCharArray());
+            }
+            guard.setNext(target);
+            target = guard;
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Target has been set to guard: " + guard);
+            }
+        }
+        
+        router.addRoute(endpoint.getRestletMethod(), target);
         
         if (!router.hasBeenAttached()) {
             component.getDefaultHost().attach(endpoint.getUriPattern(), 
router);

Added: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java?rev=730299&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
 (added)
+++ 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
 Tue Dec 30 19:17:58 2008
@@ -0,0 +1,31 @@
+/**
+ * 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.camel.component.restlet;
+
+/**
+ * Constants for Restlet component
+ * 
+ * @version $Revision$
+ */
+public final class RestletConstants {
+    
+    public static final String LOGIN = "camel.restlet.auth.login";
+    public static final String PASSWORD = "camel.restlet.auth.password";
+
+    private RestletConstants() {
+    }
+}

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java?rev=730299&r1=730298&r2=730299&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
 (original)
+++ 
activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
 Tue Dec 30 19:17:58 2008
@@ -45,6 +45,7 @@
     private int port = DEFAULT_PORT;
     private String uriPattern;
     private RestletBinding restletBinding;
+    private Map<String, String> realm;
     
     public RestletEndpoint(RestletComponent component, String remaining, 
             Map<String, String> parameters, RestletBinding restletBinding) 
throws Exception {
@@ -146,4 +147,19 @@
         return restletMethod;
     }
 
+    /**
+     * @param realm
+     */
+    public void setRealm(Map<String, String> realm) {
+        this.realm = realm;
+    }
+    
+    /**
+     * @return the realm
+     */
+    public Map<String, String> getRealm() {
+        return realm;
+    }
+
+
 }

Added: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java?rev=730299&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
 (added)
+++ 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
 Tue Dec 30 19:17:58 2008
@@ -0,0 +1,44 @@
+/**
+ * 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.camel.component.restlet;
+
+import java.io.IOException;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class RestletRouteBuilderAuthTest extends SpringTestSupport {
+
+    public void testBasicAuth() throws IOException {
+        String response = (String) template.requestBody("direct:start-auth", 
+                "<order foo='1'/>");
+        assertEquals("received [<order foo='1'/>] as an order id = " + 89531,
+                response);
+    }
+
+    public void testhBasicAuthError() throws IOException {
+        String response = (String) 
template.requestBody("direct:start-bad-auth", 
+                "<order foo='1'/>");
+        assertTrue(response.contains("requires user authentication"));
+    }
+
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext(
+                "org/apache/camel/component/restlet/camel-context.xml");
+    }
+
+}
\ No newline at end of file

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java?rev=730299&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
 (added)
+++ 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
 Tue Dec 30 19:17:58 2008
@@ -0,0 +1,62 @@
+/**
+ * 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.camel.component.restlet.route;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.restlet.RestletConstants;
+
+/**
+ * Route builder for RestletRouteBuilderAuthTest
+ * 
+ * @version $Revision$
+ */
+public class TestRouteBuilder extends RouteBuilder {
+    private static final String ID = "89531";
+
+    @Override
+    public void configure() throws Exception {
+
+        // Note: restletMethod and restletRealmRef are stripped 
+        // from the query before a request is sent as they are 
+        // only processed by Camel.
+        
+        from("direct:start-auth").setHeader("id", constant(ID))
+            .setHeader(RestletConstants.LOGIN, constant("admin"))
+            .setHeader(RestletConstants.PASSWORD, constant("foo"))
+            
.to("restlet:http://localhost:8080/securedOrders?restletMethod=post";);
+
+        from("direct:start-bad-auth").setHeader("id", constant(ID))
+            .setHeader(RestletConstants.LOGIN, constant("admizzzn"))
+            .setHeader(RestletConstants.PASSWORD, constant("fozzzo"))
+            
.to("restlet:http://localhost:8080/securedOrders?restletMethod=post";);
+        
+        
from("restlet:http://localhost:8080/securedOrders?restletMethod=post&restletRealmRef=realm";).process(new
 Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getOut().setBody(
+                        "received [" + exchange.getIn().getBody()
+                        + "] as an order id = "
+                        + exchange.getIn().getHeader("id"));
+            }
+        });
+    }
+
+}
+
+
+

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml?rev=730299&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
 (added)
+++ 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
 Tue Dec 30 19:17:58 2008
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+       <!--
+               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.
+       -->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xmlns:context="http://www.springframework.org/schema/context";
+       xmlns:util="http://www.springframework.org/schema/util";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
+           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+           http://www.springframework.org/schema/context
+           http://www.springframework.org/schema/context/spring-context-2.5.xsd
+       http://activemq.apache.org/camel/schema/spring 
+       http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+       http://www.springframework.org/schema/util 
+       http://www.springframework.org/schema/util/spring-util-2.0.xsd";>
+       
+       <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
+               <package>org.apache.camel.component.restlet.route</package>
+       </camelContext>
+       <util:map id="realm">
+               <entry key="admin" value="foo" />
+               <entry key="bar" value="foo" />
+       </util:map>
+</beans>
\ No newline at end of file

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
activemq/camel/trunk/components/camel-restlet/src/test/resources/org/apache/camel/component/restlet/camel-context.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml


Reply via email to