You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2011/09/14 23:28:13 UTC

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

Author: hadrian
Date: Wed Sep 14 21:28:12 2011
New Revision: 1170854

URL: http://svn.apache.org/viewvc?rev=1170854&view=rev
Log:
CAMEL-4451. Use valid URIs in camel-restlet

Added:
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletValidUriQueryTest.java
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/main/java/org/apache/camel/component/restlet/RestletProducer.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletMultiMethodsEndpointTest.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderTest.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=1170854&r1=1170853&r2=1170854&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 Wed Sep 14 21:28:12 2011
@@ -16,13 +16,16 @@
  */
 package org.apache.camel.component.restlet;
 
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.Endpoint;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.HeaderFilterStrategyComponent;
 import org.apache.camel.util.URISupport;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
@@ -68,7 +71,7 @@ public class RestletComponent extends He
         setProperties(result, parameters);
 
         // construct URI so we can use it to get the splitted information
-        URI u = new URI(UnsafeUriCharactersEncoder.encode(remaining));
+        URI u = new URI(remaining);
         String protocol = u.getScheme();
 
         String uriPattern = u.getPath();
@@ -133,8 +136,9 @@ public class RestletComponent extends He
 
         List<MethodBasedRouter> routers = new ArrayList<MethodBasedRouter>();
 
-        if (endpoint.getUriPattern() != null && endpoint.getUriPattern().length() > 0) {
-            routers.add(getMethodRouter(endpoint.getUriPattern()));
+        String pattern = decodePattern(endpoint.getUriPattern());
+        if (pattern != null && !pattern.isEmpty()) {
+            routers.add(getMethodRouter(pattern));
         }
 
         if (endpoint.getRestletUriPatterns() != null) {
@@ -192,6 +196,7 @@ public class RestletComponent extends He
     }
 
     private void attachUriPatternToRestlet(String uriPattern, RestletEndpoint endpoint, Restlet target) {
+        uriPattern = decodePattern(uriPattern);
         MethodBasedRouter router = getMethodRouter(uriPattern);
 
         Map<String, String> realm = endpoint.getRestletRealm();
@@ -217,8 +222,7 @@ public class RestletComponent extends He
         } else {
             router.addRoute(endpoint.getRestletMethod(), target);
             if (LOG.isDebugEnabled()) {
-                LOG.debug("Attached restlet uriPattern: {} method: {}", uriPattern,
-                          endpoint.getRestletMethod());
+                LOG.debug("Attached restlet uriPattern: {} method: {}", uriPattern, endpoint.getRestletMethod());
             }
         }
 
@@ -228,4 +232,18 @@ public class RestletComponent extends He
         }
     }
 
+    @Deprecated
+    protected String preProcessUri(String uri) {
+        // If the URI was not valid (i.e. contains '{' and '}'
+        // it was most likely encoded by normalizeEndpointUri in DefaultCamelContext.getEndpoint(String)
+        return UnsafeUriCharactersEncoder.encode(uri.replaceAll("%7B", "(").replaceAll("%7D", ")"));
+    }
+    
+    private static String encodePattern(String pattern) {
+        return pattern == null ? null : pattern.replaceAll("\\{", "(").replaceAll("\\}", ")");
+    }
+
+    private static String decodePattern(String pattern) {
+        return pattern == null ? null : pattern.replaceAll("\\(", "{").replaceAll("\\)", "}");
+    }
 }

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=1170854&r1=1170853&r2=1170854&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 Wed Sep 14 21:28:12 2011
@@ -203,5 +203,4 @@ public class RestletEndpoint extends Def
     protected void doStop() throws Exception {
         // noop
     }
-
 }

Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java?rev=1170854&r1=1170853&r2=1170854&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java Wed Sep 14 21:28:12 2011
@@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory;
  */
 public class RestletProducer extends DefaultAsyncProducer {
     private static final Logger LOG = LoggerFactory.getLogger(RestletProducer.class);
-    private static final Pattern PATTERN = Pattern.compile("\\{([\\w\\.]*)\\}");
+    private static final Pattern PATTERN = Pattern.compile("\\(([\\w\\.]*)\\)");
     private Client client;
     private boolean throwException;
 
@@ -109,7 +109,7 @@ public class RestletProducer extends Def
         String uri = endpoint.getProtocol() + "://" + endpoint.getHost() + ":" + endpoint.getPort() + endpoint.getUriPattern();
 
         // substitute { } placeholders in uri and use mandatory headers
-        LOG.trace("Substituting { } placeholders in uri: {}", uri);
+        LOG.trace("Substituting '(value)' placeholders in uri: {}", uri);
         Matcher matcher = PATTERN.matcher(uri);
         while (matcher.find()) {
             String key = matcher.group(1);

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java?rev=1170854&r1=1170853&r2=1170854&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java Wed Sep 14 21:28:12 2011
@@ -31,8 +31,5 @@ public class AddQueryTest extends Assert
         assertEquals("http://a/b/c?a=b&c=b", RestletProducer.addQueryToUri("http://a/b/c", "a=b&c=b"));
         assertEquals("http://a/b/c?a=b&c=b&l=m", RestletProducer.addQueryToUri("http://a/b/c?c=b&l=m", "a=b"));
         assertEquals("http://a/b/c?a=b&i=j&c=b&l=m", RestletProducer.addQueryToUri("http://a/b/c?c=b&l=m", "a=b&i=j"));
-
-
     }
-
 }

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletMultiMethodsEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletMultiMethodsEndpointTest.java?rev=1170854&r1=1170853&r2=1170854&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletMultiMethodsEndpointTest.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletMultiMethodsEndpointTest.java Wed Sep 14 21:28:12 2011
@@ -54,8 +54,7 @@ public class RestletMultiMethodsEndpoint
                     .process(new Processor() {
                         public void process(Exchange exchange) throws Exception {
                             // echo the method
-                            exchange.getOut().setBody(exchange.getIn().getHeader(Exchange.HTTP_METHOD,
-                                                                                 String.class));
+                            exchange.getOut().setBody(exchange.getIn().getHeader(Exchange.HTTP_METHOD, String.class));
                         }
                     });
                 // END SNIPPET: routeDefinition

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=1170854&r1=1170853&r2=1170854&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 Wed Sep 14 21:28:12 2011
@@ -35,11 +35,8 @@ public class RestletQueryTest extends Re
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("restlet:http://localhost:" + portNum + "/users/{username}")
-                    .process(new SetUserProcessor());
-                
+                from("restlet:http://localhost:" + portNum + "/users/{username}").process(new SetUserProcessor());
                 from("direct:start").to("restlet:http://localhost:" + portNum + "/users/{username}");
-
             }
         };
     }

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java?rev=1170854&r1=1170853&r2=1170854&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderAuthTest.java Wed Sep 14 21:28:12 2011
@@ -38,12 +38,11 @@ public class RestletRouteBuilderAuthTest
         headers.put(RestletConstants.RESTLET_PASSWORD, "foo");
         headers.put("id", id);
         
-        String response = (String) template.requestBodyAndHeaders("direct:start-auth", 
-                "<order foo='1'/>", headers);
+        String response = (String)template.requestBodyAndHeaders(
+            "direct:start-auth", "<order foo='1'/>", headers);
         // END SNIPPET: auth_request
 
-        assertEquals("received [<order foo='1'/>] as an order id = " + id,
-                response);
+        assertEquals("received [<order foo='1'/>] as an order id = " + id, response);
     }
 
     @Test(expected = CamelExecutionException.class)
@@ -61,7 +60,7 @@ public class RestletRouteBuilderAuthTest
     @Override
     protected ClassPathXmlApplicationContext createApplicationContext() {
         return new ClassPathXmlApplicationContext(
-                "org/apache/camel/component/restlet/camel-context.xml");
+            "org/apache/camel/component/restlet/camel-context.xml");
     }
 
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderTest.java?rev=1170854&r1=1170853&r2=1170854&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderTest.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRouteBuilderTest.java Wed Sep 14 21:28:12 2011
@@ -49,9 +49,9 @@ public class RestletRouteBuilderTest ext
                 from("restlet:http://localhost:" + portNum + "/orders?restletMethod=post").process(new Processor() {
                     public void process(Exchange exchange) throws Exception {
                         exchange.getOut().setBody(
-                                "received [" + exchange.getIn().getBody(String.class)
-                                + "] as an order id = "
-                                + exchange.getIn().getHeader("id"));
+                            "received [" + exchange.getIn().getBody(String.class)
+                            + "] as an order id = "
+                            + exchange.getIn().getHeader("id"));
                     }
                 });
 
@@ -70,10 +70,10 @@ public class RestletRouteBuilderTest ext
                 from("restlet:http://localhost:" + portNum + "/orders/{id}/{x}").process(new Processor() {
                     public void process(Exchange exchange) throws Exception {
                         exchange.getOut().setBody(
-                                "received GET request with id="
-                                + exchange.getIn().getHeader("id")
-                                + " and x="
-                                + exchange.getIn().getHeader("x"));
+                            "received GET request with id="
+                            + exchange.getIn().getHeader("id")
+                            + " and x="
+                            + exchange.getIn().getHeader("x"));
                     }
                 });
             }
@@ -86,34 +86,32 @@ public class RestletRouteBuilderTest ext
         assertEquals("received [<order foo='1'/>] as an order id = " + ID, response);
         
         response = (String)template.sendBodyAndHeader(
-             "restlet:http://localhost:" + portNum + "/orders?restletMethod=post&foo=bar", 
-             ExchangePattern.InOut,
-             "<order foo='1'/>", "id", "89531");
+            "restlet:http://localhost:" + portNum + "/orders?restletMethod=post&foo=bar", 
+            ExchangePattern.InOut,
+            "<order foo='1'/>", "id", "89531");
         assertEquals("received [<order foo='1'/>] as an order id = " + ID, response);
     }
 
     @Test
     public void testProducerJSON() throws IOException {
         String response = (String)template.sendBodyAndHeader(
-                "restlet:http://localhost:" + portNum + "/ordersJSON?restletMethod=post&foo=bar", 
-                ExchangePattern.InOut,
-                JSON,
-                Exchange.CONTENT_TYPE,
-                MediaType.APPLICATION_JSON);
+            "restlet:http://localhost:" + portNum + "/ordersJSON?restletMethod=post&foo=bar", 
+            ExchangePattern.InOut,
+            JSON,
+            Exchange.CONTENT_TYPE,
+            MediaType.APPLICATION_JSON);
            
         assertEquals(JSON, response);
     }
 
-
     @Test
     public void testProducerJSONFailure() throws IOException {
-        
         String response = (String)template.sendBodyAndHeader(
-                "restlet:http://localhost:" + portNum + "/ordersJSON?restletMethod=post&foo=bar", 
-                ExchangePattern.InOut,
-                "{'JSON'}",
-                Exchange.CONTENT_TYPE,
-                MediaType.APPLICATION_JSON);
+            "restlet:http://localhost:" + portNum + "/ordersJSON?restletMethod=post&foo=bar", 
+            ExchangePattern.InOut,
+            "{'JSON'}",
+            Exchange.CONTENT_TYPE,
+            MediaType.APPLICATION_JSON);
            
         assertEquals("{'JSON'}", response);
     }
@@ -122,19 +120,18 @@ public class RestletRouteBuilderTest ext
     public void testConsumer() throws IOException {
         Client client = new Client(Protocol.HTTP);
         Response response = client.handle(new Request(Method.GET, 
-                "http://localhost:" + portNum + "/orders/99991/6"));
+            "http://localhost:" + portNum + "/orders/99991/6"));
         assertEquals("received GET request with id=99991 and x=6",
-                response.getEntity().getText());
+            response.getEntity().getText());
     }
 
     @Test
     public void testUnhandledConsumer() throws IOException {
         Client client = new Client(Protocol.HTTP);
         Response response = client.handle(new Request(Method.POST, 
-                "http://localhost:" + portNum + "/orders/99991/6"));
+            "http://localhost:" + portNum + "/orders/99991/6"));
         // expect error status as no Restlet consumer to handle POST method
         assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());
         assertNotNull(response.getEntity().getText());
     }
-
 }

Added: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletValidUriQueryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletValidUriQueryTest.java?rev=1170854&view=auto
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletValidUriQueryTest.java (added)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletValidUriQueryTest.java Wed Sep 14 21:28:12 2011
@@ -0,0 +1,72 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.junit.Test;
+
+/**
+ *
+ * @version 
+ */
+public class RestletValidUriQueryTest extends RestletTestSupport {
+    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:" + portNum + "/users/(username)").process(new SetUserProcessor());
+                from("direct:start").to("restlet:http://localhost:" + portNum + "/users/(username)");
+            }
+        };
+    }
+    
+    class SetUserProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {   
+            assertEquals(QUERY_STRING, exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class));
+        }
+        
+    }
+    
+    @Test
+    public void testPostBody() throws Exception {
+        HttpResponse response = doExecute(new HttpGet("http://localhost:" + portNum + "/users/homer?" + QUERY_STRING));
+
+        assertHttpResponse(response, 200, "text/plain");
+    }
+    
+    
+    @Test
+    public void testGetBodyByRestletProducer() throws Exception {        
+        Exchange ex = template.request("direct:start", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_QUERY, QUERY_STRING);
+                exchange.getIn().setHeader("username", "homer");
+                
+            }
+        });
+        assertEquals(200, ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));   
+    }
+}
\ No newline at end of file