Author: davsclaus
Date: Thu Feb 12 14:27:53 2009
New Revision: 743762

URL: http://svn.apache.org/viewvc?rev=743762&view=rev
Log:
CAMEL-1289: a little cleanup in the restlet component. the endpoint should NOT 
know about URI parameters from the component.

Modified:
    
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/RestletQueryTest.java

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=743762&r1=743761&r2=743762&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 14:27:53 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.restlet;
 
+import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -24,6 +25,8 @@
 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;
 import org.apache.commons.logging.LogFactory;
 import org.restlet.Component;
@@ -42,21 +45,20 @@
 public class RestletComponent extends DefaultComponent implements 
HeaderFilterStrategyAware {
     private static final Log LOG = LogFactory.getLog(RestletComponent.class);
 
-    private Map<String, Server> servers = new HashMap<String, Server>();
-    private Map<String, MethodBasedRouter> routers = new HashMap<String, 
MethodBasedRouter>();
-    private Component component = new Component();
+    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
-    protected Endpoint createEndpoint(String uri, String remaining,
-            Map parameters) throws Exception {
+    @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);
+            restletBinding = 
CamelContextHelper.mandatoryLookup(getCamelContext(), ref, 
RestletBinding.class);
         }
         
         if (restletBinding == null) {
@@ -74,16 +76,36 @@
         }
         
         Method method = getAndRemoveParameter(parameters, "restletMethod", 
Method.class);
-        RestletEndpoint result = new RestletEndpoint(this, remaining, 
parameters, restletBinding);
-        
+
+        // construct URI so we can use it to get the splitted information
+        URI u = new URI(UnsafeUriCharactersEncoder.encode(remaining));
+        String protocol = u.getScheme();
+
+        String uriPattern = u.getPath();
+        if (parameters.size() > 0) {
+            uriPattern = uriPattern + "?" + 
URISupport.createQueryString(parameters);
+        }
+
+        int port = 0;
+        String host = u.getHost();
+        if (u.getPort() > 0) {
+            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;
     }
     
@@ -96,9 +118,20 @@
     @Override
     protected void doStop() throws Exception {
         component.stop();
+        // just clear maps, component will stop the servers and routes
+        servers.clear();
+        routers.clear();
         super.doStop();
     }
     
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
+        this.headerFilterStrategy = strategy;
+    }
+
     public void connect(RestletConsumer consumer) throws Exception {
         RestletEndpoint endpoint = (RestletEndpoint)consumer.getEndpoint();
         addServerIfNeccessary(endpoint);
@@ -127,33 +160,30 @@
                 LOG.debug("Attached methodRouter uriPattern: " + 
endpoint.getUriPattern());
             }
         }
-        
-        LOG.debug("Attached restlet uriPattern: " + endpoint.getUriPattern() + 
" method: " 
-                + endpoint.getRestletMethod());
 
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Attached restlet uriPattern: " + 
endpoint.getUriPattern() + " method: " + endpoint.getRestletMethod());
+        }
     }
 
     public void disconnect(RestletConsumer consumer) throws Exception {
         RestletEndpoint endpoint = (RestletEndpoint)consumer.getEndpoint();
         MethodBasedRouter router = getMethodRouter(endpoint.getUriPattern());
         router.removeRoute(endpoint.getRestletMethod());
-        LOG.debug("Detached restlet uriPattern: " + endpoint.getUriPattern() + 
" method: " 
-                + endpoint.getRestletMethod());
-    }    
-    
-    public HeaderFilterStrategy getHeaderFilterStrategy() {
-        return headerFilterStrategy;
-    }
 
-    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
-        this.headerFilterStrategy = strategy;
-    }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Detached restlet uriPattern: " + 
endpoint.getUriPattern() + " method: " + endpoint.getRestletMethod());
+        }
+    }    
     
     private MethodBasedRouter getMethodRouter(String uriPattern) {
         synchronized (routers) {
             MethodBasedRouter result = routers.get(uriPattern);
             if (result == null) {
                 result = new MethodBasedRouter(uriPattern);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Added method based router: " + result);
+                }
                 routers.put(uriPattern, result);
             }
             return result;
@@ -162,14 +192,15 @@
     
     private void addServerIfNeccessary(RestletEndpoint endpoint) throws 
Exception {
         String key = buildKey(endpoint);
-        Server server = null;
+        Server server;
         synchronized (servers) {
             server = servers.get(key);
             if (server == null) {
-                server = 
component.getServers().add(Protocol.valueOf(endpoint.getProtocol()), 
-                        endpoint.getPort());
+                server = 
component.getServers().add(Protocol.valueOf(endpoint.getProtocol()), 
endpoint.getPort());
                 servers.put(key, server);
-                LOG.info("Add server: " + key);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Added server: " + key);
+                }
                 server.start();
             }
         }

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=743762&r1=743761&r2=743762&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 14:27:53 2009
@@ -14,18 +14,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.component.restlet;
 
-import java.net.URI;
 import java.util.Map;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
-import org.apache.camel.util.URISupport;
-import org.apache.camel.util.UnsafeUriCharactersEncoder;
 import org.restlet.data.Method;
 
 /**
@@ -46,24 +42,10 @@
     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 {
+
+    public RestletEndpoint(RestletComponent component, String remaining, 
RestletBinding restletBinding) throws Exception {
         super(remaining, component);
         this.restletBinding = restletBinding;
-        
-        URI u = new URI(UnsafeUriCharactersEncoder.encode(remaining));
-        protocol = u.getScheme();
-        
-        uriPattern = u.getPath();
-        if (parameters.size() > 0) {
-            uriPattern = uriPattern + "?" + 
URISupport.createQueryString(parameters);
-        }
-        
-        host = u.getHost();
-        if (u.getPort() > 0) {
-            port = u.getPort();
-        }
     }
 
     public boolean isSingleton() {
@@ -75,7 +57,7 @@
         // true to allow dynamic URI options to be configured and passed to 
external system.
         return true;
     }
-    
+
     public Consumer createConsumer(Processor processor) throws Exception {
         return new RestletConsumer(this, processor);
     }
@@ -84,82 +66,67 @@
         return new RestletProducer(this);
     }
 
-    /**
-     * @param restletConsumer
-     */
     public void connect(RestletConsumer restletConsumer) throws Exception {
         ((RestletComponent)getComponent()).connect(restletConsumer);
     }
 
-    /**
-     * @param restletConsumer
-     */
     public void disconnect(RestletConsumer restletConsumer) throws Exception {
         ((RestletComponent)getComponent()).disconnect(restletConsumer);        
     }
 
-    /**
-     * @return the protocol
-     */
+    public Method getRestletMethod() {
+        return restletMethod;
+    }
+
+    public void setRestletMethod(Method restletMethod) {
+        this.restletMethod = restletMethod;
+    }
+
     public String getProtocol() {
         return protocol;
     }
 
-    /**
-     * @return the host
-     */
+    public void setProtocol(String protocol) {
+        this.protocol = protocol;
+    }
+
     public String getHost() {
         return host;
     }
 
-    /**
-     * @return the port
-     */
+    public void setHost(String host) {
+        this.host = host;
+    }
+
     public int getPort() {
         return port;
     }
-    
-    /**
-     * @return the uriPattern
-     */
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
     public String getUriPattern() {
         return uriPattern;
     }
 
-    /**
-     * @return the restletBinding
-     */
+    public void setUriPattern(String uriPattern) {
+        this.uriPattern = uriPattern;
+    }
+
     public RestletBinding getRestletBinding() {
         return restletBinding;
     }
 
-    /**
-     * @param restletMethod the restletMethod to set
-     */
-    public void setRestletMethod(Method restletMethod) {
-        this.restletMethod = restletMethod;
+    public void setRestletBinding(RestletBinding restletBinding) {
+        this.restletBinding = restletBinding;
     }
 
-    /**
-     * @return the restletMethod
-     */
-    public Method getRestletMethod() {
-        return restletMethod;
+    public Map<String, String> getRealm() {
+        return realm;
     }
 
-    /**
-     * @param realm
-     */
     public void setRealm(Map<String, String> realm) {
         this.realm = realm;
     }
-    
-    /**
-     * @return the realm
-     */
-    public Map<String, String> getRealm() {
-        return realm;
-    }
-
-
 }

Modified: 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java?rev=743762&r1=743761&r2=743762&view=diff
==============================================================================
--- 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
 (original)
+++ 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
 Thu Feb 12 14:27:53 2009
@@ -31,26 +31,21 @@
 public class RestletQueryTest extends ContextTestSupport {
     private static final String QUERY_STRING = "foo=bar&test=123";
 
-    
     @Override
     protected RouteBuilder createRouteBuilder() {
-
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
                 from("restlet:http://localhost:9080/users/{username}";)
                     .process(new SetUserProcessor());
-                
             }
-            
         };
     }
     
     class SetUserProcessor implements Processor {
 
         public void process(Exchange exchange) throws Exception {   
-            assertEquals(QUERY_STRING, 
exchange.getIn().getHeader(RestletConstants.QUERY_STRING, 
-                    String.class));
+            assertEquals(QUERY_STRING, 
exchange.getIn().getHeader(RestletConstants.QUERY_STRING, String.class));
         }
         
     }


Reply via email to