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/01/01 03:35:51 UTC

svn commit: r730504 - in /activemq/camel/trunk: apache-camel/ apache-camel/bundle/ components/camel-restlet/src/main/java/org/apache/camel/component/restlet/

Author: wtam
Date: Wed Dec 31 18:35:50 2008
New Revision: 730504

URL: http://svn.apache.org/viewvc?rev=730504&view=rev
Log:
[CAMEL-1203] Add HeaderFilterStrategy mechanism in Restlet component

Added:
    activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletHeaderFilterStrategy.java   (with props)
Modified:
    activemq/camel/trunk/apache-camel/bundle/pom.xml
    activemq/camel/trunk/apache-camel/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/RestletBinding.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/RestletConsumer.java

Modified: activemq/camel/trunk/apache-camel/bundle/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/apache-camel/bundle/pom.xml?rev=730504&r1=730503&r2=730504&view=diff
==============================================================================
--- activemq/camel/trunk/apache-camel/bundle/pom.xml (original)
+++ activemq/camel/trunk/apache-camel/bundle/pom.xml Wed Dec 31 18:35:50 2008
@@ -251,6 +251,10 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-xstream</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-restlet</artifactId>
+    </dependency>
         
   </dependencies>
 

Modified: activemq/camel/trunk/apache-camel/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/apache-camel/pom.xml?rev=730504&r1=730503&r2=730504&view=diff
==============================================================================
--- activemq/camel/trunk/apache-camel/pom.xml (original)
+++ activemq/camel/trunk/apache-camel/pom.xml Wed Dec 31 18:35:50 2008
@@ -261,6 +261,10 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-xstream</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-restlet</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>org.apache.camel</groupId>

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=730504&r1=730503&r2=730504&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 Wed Dec 31 18:35:50 2008
@@ -22,7 +22,10 @@
 import javax.xml.transform.dom.DOMSource;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.HeaderFilterStrategyAware;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.restlet.data.ChallengeResponse;
@@ -37,80 +40,79 @@
  *
  * @version $Revision$
  */
-public class DefaultRestletBinding implements RestletBinding {
+public class DefaultRestletBinding implements RestletBinding, HeaderFilterStrategyAware {
     private static final Log LOG = LogFactory.getLog(DefaultRestletBinding.class);
+    private static final String CAMEL_REQUEST = "camel.request";
+    private HeaderFilterStrategy headerFilterStrategy;
 
     /**
-     * populateExchangeFromRestletRequest
+     * Populate Camel message from Restlet request
+     * 
+     * @param request message to be copied from
+     * @param exchange to be populated
+     * @throws Exception 
      */
     public void populateExchangeFromRestletRequest(Request request,
             Exchange exchange) throws Exception {
 
+        // extract headers from restlet 
         for (Map.Entry<String, Object> entry : request.getAttributes().entrySet()) {
-            if (!entry.getKey().startsWith("org.restlet.")) {
+            if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), 
+                    entry.getValue())) {
+                
                 exchange.getIn().setHeader(entry.getKey(), entry.getValue());
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Populate exchange from Restlet request header: " 
                             + entry.getKey() + " value: " + entry.getValue());
                 }
+
             }
         }
-        
-        Form headers = (Form) request.getAttributes().get("org.restlet.http.headers");
-        if (headers != null) {
-            for (Map.Entry<String, String> entry : headers.getValuesMap().entrySet()) {
-                if (!entry.getKey().startsWith("org.restlet.")
-                        && !entry.getKey().equals("Host")
-                        && !entry.getKey().equals("User-Agent")
-                        && !entry.getKey().equals("Content-Length")
-                        && !entry.getKey().equals("Content-Type")
-                        && !entry.getKey().equals("Connection")
-                        && !entry.getKey().equals("Accept")) {
-                    exchange.getIn().setHeader(entry.getKey(), entry.getValue());
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Populate exchange from Restlet request header: " 
-                                + entry.getKey() + " value: " + entry.getValue());
-                    }
-                }
-            }
-        }
+
+        // extract our header and body
         Form form = new Form(request.getEntity());
         if (form != null) {
-            for (Map.Entry<String, String> entry : form.getValuesMap()
-                    .entrySet()) {
-                exchange.getIn().setHeader(entry.getKey(), entry.getValue());
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Populate exchange from Restlet request header: " 
-                            + entry.getKey() + " value: " + entry.getValue());
+            for (Map.Entry<String, String> entry : form.getValuesMap().entrySet()) {
+                if (CAMEL_REQUEST.equals(entry.getKey())) {
+                    exchange.getIn().setBody(entry.getValue());
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Populate exchange from Restlet request body: " + entry.getValue());
+                    }
+                } else {
+                    if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), 
+                            entry.getValue())) {
+
+                        exchange.getIn().setHeader(entry.getKey(), entry.getValue());
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("Populate exchange from Restlet request user header: " 
+                                    + entry.getKey() + " value: " + entry.getValue());
+                        }
+                    }
                 }
             }
         }
-        
-        Object body = form.getValuesMap().get("camel.body");
-        exchange.getIn().setBody(body);
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Populate exchange from Restlet request body: " + body);
-        }
-    }
+    }   
 
     /**
-     * populateRestletRequestFromExchange
+     * Populate Restlet Request from Camel message
+     * 
+     * @param request to be populated
+     * @param exchange message to be copied from
      */
     public void populateRestletRequestFromExchange(Request request,
             Exchange exchange) {
         request.setReferrerRef("camel-restlet");
         String body = exchange.getIn().getBody(String.class);
         Form form = new Form();
-        form.add("camel.body", body);
+        form.add(CAMEL_REQUEST, body);
         
         if (LOG.isDebugEnabled()) {
             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);
+        // login and password are filtered by header filter strategy
+        String login = (String) exchange.getIn().getHeader(RestletConstants.LOGIN);
+        String password = (String) exchange.getIn().getHeader(RestletConstants.PASSWORD);
           
         if (login != null && password != null) {
             ChallengeResponse authentication = new ChallengeResponse(
@@ -122,25 +124,30 @@
         }
         
         for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
-            form.add(entry.getKey(), entry.getValue().toString());
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Populate Restlet request from exchange header: " 
-                        + entry.getKey() + " value: " + entry.getValue());
+            if (!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), 
+                    entry.getValue())) {
+                if (entry.getKey().startsWith("org.restlet.")) {
+                    // put the org.restlet headers in attributes
+                    request.getAttributes().put(entry.getKey(), entry.getValue());
+                } else {
+                    // put the user stuff in the form
+                    form.add(entry.getKey(), entry.getValue().toString());   
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Populate Restlet request from exchange header: " 
+                            + entry.getKey() + " value: " + entry.getValue());
+                }
             }
         }
         
-        for (Map.Entry<String, Object> entry : exchange.getProperties().entrySet()) {
-            form.add(entry.getKey(), entry.getValue().toString());
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Populate Restlet request from exchange header: " 
-                        + entry.getKey() + " value: " + entry.getValue());
-            }
-        }
         request.setEntity(form.getWebRepresentation());
     }
 
     /**
-     * populateRestletResponseFromExchange
+     * Populate Restlet request from Camel message
+     *  
+     * @param exchange message to be copied from 
+     * @param response to be populated
      */
     public void populateRestletResponseFromExchange(Exchange exchange,
             Response response) {
@@ -151,20 +158,46 @@
         } else if (body instanceof StringSource || body instanceof DOMSource) {
             mediaType = MediaType.TEXT_XML;
         }
+                
+        for (Map.Entry<String, Object> entry : exchange.getOut().getHeaders().entrySet()) {
+            if (!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), 
+                    entry.getValue())) {
+                response.getAttributes().put(entry.getKey(), entry.getValue());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Populate Restlet response from exchange header: " 
+                            + entry.getKey() + " value: " + entry.getValue());
+                }
+            }
+        }
         
         String text = exchange.getOut().getBody(String.class);
         if (LOG.isDebugEnabled()) {
             LOG.debug("Populate Restlet response from exchange body: " + text);
         }
         response.setEntity(text, mediaType);
-        exchange.getIn().setBody(body);
     }
 
     /**
-     * populateExchangeFromRestletResponse
+     * Populate Camel message from Restlet response
+     * 
+     * @param exchange to be populated
+     * @param response message to be copied from
+     * @throws IOException 
      */
     public void populateExchangeFromRestletResponse(Exchange exchange,
             Response response) throws IOException {
+        
+        for (Map.Entry<String, Object> entry : response.getAttributes().entrySet()) {
+            if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), 
+                    entry.getValue())) {
+                exchange.getOut().setHeader(entry.getKey(), entry.getValue());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Populate exchange from Restlet response header: " 
+                            + entry.getKey() + " value: " + entry.getValue());
+                }
+            }
+        }
+
         String text = response.getEntity().getText();
         if (LOG.isDebugEnabled()) {
             LOG.debug("Populate exchange from Restlet response: " + text);
@@ -173,7 +206,16 @@
         if (exchange.getPattern().isOutCapable()) {
             exchange.getOut().setBody(text);
         } else {
-            LOG.warn("Exchange is incapable of receiving response");
+            throw new RuntimeCamelException("Exchange is incapable of receiving response: " 
+                    + exchange);
         }
     }
+
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
+        headerFilterStrategy = strategy;
+    }
 }

Modified: activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java?rev=730504&r1=730503&r2=730504&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java (original)
+++ activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java Wed Dec 31 18:35:50 2008
@@ -32,8 +32,8 @@
     /**
      * Populate Restlet request from Camel message
      *  
-     * @param exchange
-     * @param response
+     * @param exchange message to be copied from 
+     * @param response to be populated
      */
     void populateRestletResponseFromExchange(Exchange exchange,
             Response response);
@@ -41,25 +41,26 @@
     /**
      * Populate Camel message from Restlet request
      * 
-     * @param request
-     * @param exchange
+     * @param request message to be copied from
+     * @param exchange to be populated
      * @throws Exception 
      */
-    void populateExchangeFromRestletRequest(Request request, Exchange exchange) throws Exception;
+    void populateExchangeFromRestletRequest(Request request, 
+            Exchange exchange) throws Exception;
 
     /**
      * Populate Restlet Request from Camel message
      * 
-     * @param request
-     * @param exchange
+     * @param request to be populated
+     * @param exchange message to be copied from
      */
     void populateRestletRequestFromExchange(Request request, Exchange exchange);
 
     /**
      * Populate Camel message from Restlet response
      * 
-     * @param exchange
-     * @param response
+     * @param exchange to be populated
+     * @param response message to be copied from
      * @throws IOException 
      */
     void populateExchangeFromRestletResponse(Exchange exchange,

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=730504&r1=730503&r2=730504&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 Wed Dec 31 18:35:50 2008
@@ -20,7 +20,9 @@
 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.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -37,13 +39,13 @@
  *
  * @version $Revision$
  */
-public class RestletComponent extends DefaultComponent {
+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 HeaderFilterStrategy headerFilterStrategy = new RestletHeaderFilterStrategy();
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining,
@@ -55,22 +57,20 @@
         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();
         }
         
+        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);
-            if (realm == null) {
-                LOG.warn("Realm '" + ref + "' cannot be found in the context");
-            }
         }
         
         Method method = getAndRemoveParameter(parameters, "restletMethod", Method.class);
@@ -89,21 +89,13 @@
     
     @Override
     protected void doStart() throws Exception {
-        try {
-            super.doStart();
-        } catch (Exception e) {
-            LOG.warn("Failed to stop", e);
-        }
+        super.doStart();
         component.start();
     }
     
     @Override
     protected void doStop() throws Exception {
-        try {
-            component.stop();
-        } catch (Exception e) {
-            LOG.warn("Failed to stop", e);
-        }
+        component.stop();
         super.doStop();
     }
     
@@ -149,6 +141,14 @@
                 + endpoint.getRestletMethod());
     }    
     
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
+        this.headerFilterStrategy = strategy;
+    }
+    
     private MethodBasedRouter getMethodRouter(String uriPattern) {
         synchronized (routers) {
             MethodBasedRouter result = routers.get(uriPattern);

Modified: activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java?rev=730504&r1=730503&r2=730504&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java (original)
+++ activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java Wed Dec 31 18:35:50 2008
@@ -19,8 +19,8 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultConsumer;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.restlet.Restlet;
@@ -54,8 +54,7 @@
                     getProcessor().process(exchange);
                     binding.populateRestletResponseFromExchange(exchange, response);
                 } catch (Exception e) {
-                    LOG.error(e);
-                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                    throw new RuntimeCamelException(e);
                 }
             }
         };
@@ -68,9 +67,9 @@
     }
 
     @Override
-    public void stop() throws Exception {
+    public void doStop() throws Exception {
         ((RestletEndpoint)getEndpoint()).disconnect(this);
-        super.stop();
+        super.doStop();
     }
 
     public Restlet getRestlet() {

Added: activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletHeaderFilterStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletHeaderFilterStrategy.java?rev=730504&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletHeaderFilterStrategy.java (added)
+++ activemq/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletHeaderFilterStrategy.java Wed Dec 31 18:35:50 2008
@@ -0,0 +1,39 @@
+/**
+ * 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 org.apache.camel.impl.DefaultHeaderFilterStrategy;
+
+/**
+ * Default header filtering strategy for Restlet
+ * 
+ * @version $Revision$
+ */
+public class RestletHeaderFilterStrategy extends DefaultHeaderFilterStrategy {
+
+    public RestletHeaderFilterStrategy() {
+        // No IN filters and copy all headers from Restlet to Camel
+        
+        // OUT filters (from Camel headers to Restlet headers)
+        // filter headers start with the org.restlet.
+        setOutFilterPattern("(org\\.restlet)[\\.|a-z|A-z|0-9]*");
+        
+        // filter headers used internally by this component
+        getOutFilter().add(RestletConstants.LOGIN);
+        getOutFilter().add(RestletConstants.PASSWORD);
+    }
+}

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

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