You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by wt...@apache.org on 2009/02/12 21:54:16 UTC

svn commit: r743881 - in /camel/trunk/components/camel-restlet/src: main/java/org/apache/camel/component/restlet/ test/java/org/apache/camel/component/restlet/route/

Author: wtam
Date: Thu Feb 12 20:54:16 2009
New Revision: 743881

URL: http://svn.apache.org/viewvc?rev=743881&view=rev
Log:
[CAMEL-1289] HeaderFilterStrategy - move from Component to Endpoint (for Restlet component)

Modified:
    camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
    camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
    camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java

Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=743881&r1=743880&r2=743881&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java Thu Feb 12 20:54:16 2009
@@ -33,7 +33,6 @@
 import org.restlet.data.ChallengeScheme;
 import org.restlet.data.Form;
 import org.restlet.data.MediaType;
-import org.restlet.data.Parameter;
 import org.restlet.data.Request;
 import org.restlet.data.Response;
 import org.restlet.data.Status;

Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java?rev=743881&r1=743880&r2=743881&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java Thu Feb 12 20:54:16 2009
@@ -21,10 +21,7 @@
 import java.util.Map;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.HeaderFilterStrategyAware;
 import org.apache.camel.impl.DefaultComponent;
-import org.apache.camel.spi.HeaderFilterStrategy;
-import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.URISupport;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
 import org.apache.commons.logging.Log;
@@ -34,7 +31,6 @@
 import org.restlet.Restlet;
 import org.restlet.Server;
 import org.restlet.data.ChallengeScheme;
-import org.restlet.data.Method;
 import org.restlet.data.Protocol;
 
 /**
@@ -42,41 +38,20 @@
  *
  * @version $Revision$
  */
-public class RestletComponent extends DefaultComponent implements HeaderFilterStrategyAware {
+public class RestletComponent extends DefaultComponent {
     private static final Log LOG = LogFactory.getLog(RestletComponent.class);
 
     private final Map<String, Server> servers = new HashMap<String, Server>();
     private final Map<String, MethodBasedRouter> routers = new HashMap<String, MethodBasedRouter>();
     private final Component component = new Component();
-    private HeaderFilterStrategy headerFilterStrategy = new RestletHeaderFilterStrategy();
 
     @Override
     @SuppressWarnings("unchecked")
     protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
         
-        RestletBinding restletBinding = null;
-        // lookup binding in registry if provided
-        String ref = getAndRemoveParameter(parameters, "restletBindingRef", String.class);
-        if (ref != null) {
-            restletBinding = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, RestletBinding.class);
-        }
-        
-        if (restletBinding == null) {
-            restletBinding = new DefaultRestletBinding();
-        }
-        
-        if (restletBinding instanceof HeaderFilterStrategyAware) {
-            ((HeaderFilterStrategyAware)restletBinding).setHeaderFilterStrategy(headerFilterStrategy);
-        }
-        
-        Map<String, String> realm = null;
-        ref = getAndRemoveParameter(parameters, "restletRealmRef", String.class);
-        if (ref != null) {
-            realm = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, Map.class);
-        }
+        RestletEndpoint result = new RestletEndpoint(this, remaining);
+        setProperties(result, parameters);
         
-        Method method = getAndRemoveParameter(parameters, "restletMethod", Method.class);
-
         // construct URI so we can use it to get the splitted information
         URI u = new URI(UnsafeUriCharactersEncoder.encode(remaining));
         String protocol = u.getScheme();
@@ -92,19 +67,12 @@
             port = u.getPort();
         }
 
-        RestletEndpoint result = new RestletEndpoint(this, remaining, restletBinding);
         result.setProtocol(protocol);
         result.setUriPattern(uriPattern);
         result.setHost(host);
         if (port > 0) {
             result.setPort(port);
         }
-        if (method != null) {
-            result.setRestletMethod(method);
-        }
-        if (realm != null) {
-            result.setRealm(realm);
-        }
 
         return result;
     }
@@ -118,26 +86,24 @@
     @Override
     protected void doStop() throws Exception {
         component.stop();
-        // just clear maps, component will stop the servers and routes
-        servers.clear();
-        routers.clear();
+        // routers map entries are removed as consumer stops and servers map 
+        // is not touch so to keep in sync with component's servers
         super.doStop();
     }
     
-    public HeaderFilterStrategy getHeaderFilterStrategy() {
-        return headerFilterStrategy;
-    }
-
-    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
-        this.headerFilterStrategy = strategy;
+    @Override
+    protected boolean useIntrospectionOnEndpoint() {
+        // we invoke setProperties ourselves so we can construct "user" uri on 
+        // on the remaining parameters
+        return false;
     }
-
+    
     public void connect(RestletConsumer consumer) throws Exception {
         RestletEndpoint endpoint = (RestletEndpoint)consumer.getEndpoint();
         addServerIfNeccessary(endpoint);
         MethodBasedRouter router = getMethodRouter(endpoint.getUriPattern());
         
-        Map<String, String> realm = endpoint.getRealm();
+        Map<String, String> realm = endpoint.getRestletRealm();
         Restlet target = consumer.getRestlet();
         if (realm != null && realm.size() > 0) {
             Guard guard = new Guard(component.getContext().createChildContext(), 

Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java?rev=743881&r1=743880&r2=743881&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java Thu Feb 12 20:54:16 2009
@@ -17,11 +17,16 @@
 package org.apache.camel.component.restlet;
 
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.Consumer;
+import org.apache.camel.HeaderFilterStrategyAware;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.restlet.data.Method;
 
 /**
@@ -30,7 +35,8 @@
  * @version $Revision$
  */
 public class RestletEndpoint extends DefaultEndpoint {
-    
+    private static final Log LOG = LogFactory.getLog(RestletEndpoint.class);
+
     private static final int DEFAULT_PORT = 80;
     private static final String DEFAULT_PROTOCOL = "http";
     private static final String DEFAULT_HOST = "localhost";
@@ -40,12 +46,13 @@
     private String host = DEFAULT_HOST;
     private int port = DEFAULT_PORT;
     private String uriPattern;
+    private Map<String, String> restletRealm;
+    private HeaderFilterStrategy headerFilterStrategy;
     private RestletBinding restletBinding;
-    private Map<String, String> realm;
+    private AtomicBoolean bindingInitialized = new AtomicBoolean(false);
 
-    public RestletEndpoint(RestletComponent component, String remaining, RestletBinding restletBinding) throws Exception {
+    public RestletEndpoint(RestletComponent component, String remaining) throws Exception {
         super(remaining, component);
-        this.restletBinding = restletBinding;
     }
 
     public boolean isSingleton() {
@@ -115,6 +122,18 @@
     }
 
     public RestletBinding getRestletBinding() {
+        if (restletBinding == null) {
+            restletBinding = new DefaultRestletBinding();   
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Create default Restlet Binding " + restletBinding);
+            }
+        }
+        
+        if (!bindingInitialized.getAndSet(true) 
+                && restletBinding instanceof HeaderFilterStrategyAware) {
+            ((HeaderFilterStrategyAware)restletBinding)
+                .setHeaderFilterStrategy(getHeaderFilterStrategy());
+        }
         return restletBinding;
     }
 
@@ -122,11 +141,26 @@
         this.restletBinding = restletBinding;
     }
 
-    public Map<String, String> getRealm() {
-        return realm;
+    public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
+        this.headerFilterStrategy = headerFilterStrategy;
+    }
+
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        if (headerFilterStrategy == null) {
+            headerFilterStrategy = new RestletHeaderFilterStrategy();
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Create Restlet default header filter strategy " 
+                        + headerFilterStrategy);
+            }
+        }
+        return headerFilterStrategy;
+    }
+
+    public void setRestletRealm(Map<String, String> restletRealm) {
+        this.restletRealm = restletRealm;
     }
 
-    public void setRealm(Map<String, String> realm) {
-        this.realm = realm;
+    public Map<String, String> getRestletRealm() {
+        return restletRealm;
     }
 }

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java?rev=743881&r1=743880&r2=743881&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/route/TestRouteBuilder.java Thu Feb 12 20:54:16 2009
@@ -31,7 +31,7 @@
     public void configure() throws Exception {
 
         // START SNIPPET: consumer_route
-        from("restlet:http://localhost:9080/securedOrders?restletMethod=post&restletRealmRef=realm").process(new Processor() {
+        from("restlet:http://localhost:9080/securedOrders?restletMethod=post&restletRealm=#realm").process(new Processor() {
             public void process(Exchange exchange) throws Exception {
                 exchange.getOut().setBody(
                         "received [" + exchange.getIn().getBody()