You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/09/06 05:47:40 UTC

[camel] 01/02: CAMEL-12320: camel-restlet - Should match better on uri pattern and return 404 for invalid urls

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0e7ba6ab63e9466711fa6c758db5d73021c16223
Author: Ramu <kk...@redhat.com>
AuthorDate: Wed Sep 5 22:13:22 2018 +0530

    CAMEL-12320: camel-restlet - Should match better on uri pattern and return 404 for invalid urls
---
 .../camel/component/restlet/MethodBasedRouter.java | 29 +++++++-----
 .../component/restlet/RestletInvalidURLTest.java   | 51 ++++++++++++++++++++++
 .../restlet/RestletProducerSynchronouslyTest.java  |  2 +-
 .../restlet/SpringRestRestletPojoInOutTest.java    |  1 -
 4 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
index d11ceae..d631249 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
@@ -20,6 +20,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.camel.support.RestConsumerContextPathMatcher;
 import org.restlet.Request;
 import org.restlet.Response;
 import org.restlet.Restlet;
@@ -48,18 +49,26 @@ class MethodBasedRouter extends Restlet {
     public void handle(Request request, Response response) {
         Method method = request.getMethod();
         LOG.debug("MethodRouter ({}) received request method: {}", uriPattern, method);
-        
-        Restlet target = routes.get(method);
-        if (target == null || org.restlet.data.Method.OPTIONS.equals(method)) {
-            // must include list of allowed methods
-            response.setAllowedMethods(routes.keySet());
-        }
-        if (target != null) {
-            target.handle(request, response);
+        String requestPath = request.getResourceRef().getPath();
+        String consumerPath = request.getRootRef().getPath();
+        boolean matchOnUriPrefix = false;
+        boolean  result = RestConsumerContextPathMatcher.matchPath(requestPath, consumerPath, matchOnUriPrefix);
+        if (result) {
+            Restlet target = routes.get(method);
+            if (target == null || org.restlet.data.Method.OPTIONS.equals(method)) {
+                // must include list of allowed methods
+                response.setAllowedMethods(routes.keySet());
+            }
+            if (target != null) {
+                target.handle(request, response);
+            } else {
+                LOG.debug("MethodRouter ({}) method not allowed: {}", uriPattern, method);
+                response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
+            }
         } else {
-            LOG.debug("MethodRouter ({}) method not allowed: {}", uriPattern, method);
-            response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
+            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         }
+    
     }
 
     void addRoute(Method method, Restlet target) {
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletInvalidURLTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletInvalidURLTest.java
new file mode 100644
index 0000000..a40597e
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletInvalidURLTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.HttpPost;
+import org.junit.Test;
+
+public class RestletInvalidURLTest extends RestletTestSupport {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("restlet:http://localhost:" + portNum + "/users/{username}?restletMethod=POST").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        
+                    }
+                });
+                from("direct:start").to("restlet:http://localhost:" + portNum + "/users/tester?restletMethod=POST");
+            }
+        };
+    }
+    
+    @Test
+    public void testExceptionResponse() throws Exception {
+        HttpResponse response = doExecute(new HttpPost("http://localhost:" + portNum + "/users/123/45"));
+        System.out.println(response.getStatusLine().getStatusCode());
+        assertEquals(404, response.getStatusLine().getStatusCode());
+    }
+   
+}
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
index 96e781b..39793cc 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
@@ -49,7 +49,7 @@ public class RestletProducerSynchronouslyTest extends RestletTestSupport {
 
                 from("direct:start").to("restlet:http://localhost:" + portNum + "/users/123/basic").to("log:reply");
                 
-                from("direct:delete").to("restlet:http://localhost:" + portNum + "/users/123/basicrestletMethod=DELETE");
+                from("direct:delete").to("restlet:http://localhost:" + portNum + "/users/123/basic");
 
                 from("restlet:http://localhost:" + portNum + "/users/{id}/basic?restletMethods=GET,DELETE")
                     .process(new Processor() {
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/SpringRestRestletPojoInOutTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/SpringRestRestletPojoInOutTest.java
index 6b11b97..73e1cf1 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/SpringRestRestletPojoInOutTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/SpringRestRestletPojoInOutTest.java
@@ -15,7 +15,6 @@
  * limitations under the License.
  */
 package org.apache.camel.component.restlet;
-import org.junit.Before;
 
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.spring.CamelSpringTestSupport;