You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ff...@apache.org on 2018/07/04 02:51:07 UTC

[camel] branch master updated: [CAMEL-12621]Rest DSL with Jetty9 components returns 404 instead of 405, when http method is not supported

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2c3adc2  [CAMEL-12621]Rest DSL with Jetty9 components returns 404 instead of 405, when http method is not supported
2c3adc2 is described below

commit 2c3adc2d229c0450886797faccbe8e24eae12a59
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Wed Jul 4 10:49:47 2018 +0800

    [CAMEL-12621]Rest DSL with Jetty9 components returns 404 instead of 405, when http method is not supported
---
 .../org/apache/camel/http/common/CamelServlet.java |  2 +-
 .../component/jetty/CamelContinuationServlet.java  | 16 +++++-
 .../jetty/rest/RestJettyMethodNotAllowedTest.java  | 67 ++++++++++++++++++++++
 3 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
index 5d9f74c..1e7688d 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java
@@ -44,7 +44,7 @@ public class CamelServlet extends HttpServlet {
     public static final String ASYNC_PARAM = "async";
 
     private static final long serialVersionUID = -7061982839117697829L;
-    private static final List<String> METHODS = Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH");
+    public static final List<String> METHODS = Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH");
 
     protected final Logger log = LoggerFactory.getLogger(getClass());
 
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
index a125488..5e4cdff 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
@@ -62,9 +62,19 @@ public class CamelContinuationServlet extends CamelServlet {
         // is there a consumer registered for the request.
         HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers());
         if (consumer == null) {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND);
-            return;
-        }
+            // okay we cannot process this requires so return either 404 or 405.
+            // to know if its 405 then we need to check if any other HTTP method would have a consumer for the "same" request
+            boolean hasAnyMethod = METHODS.stream().anyMatch(m -> getServletResolveConsumerStrategy().isHttpMethodAllowed(request, m, getConsumers()));
+            if (hasAnyMethod) {
+                log.debug("No consumer to service request {} as method {} is not allowed", request, request.getMethod());
+                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+                return;
+            } else {
+                log.debug("No consumer to service request {} as resource is not found", request);
+                response.sendError(HttpServletResponse.SC_NOT_FOUND);
+                return;
+            }
+        }       
 
         // figure out if continuation is enabled and what timeout to use
         boolean useContinuation = false;
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java
new file mode 100644
index 0000000..21a772b
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java
@@ -0,0 +1,67 @@
+/**
+ * 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.jetty.rest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.http.common.HttpOperationFailedException;
+import org.junit.Test;
+
+public class RestJettyMethodNotAllowedTest extends BaseJettyTest {
+
+    @Test
+    public void testMethodNotAllowed() {
+        try {
+            template.sendBody("http://localhost:" + getPort() + "/users/123/basic", "body");
+            fail("Shall not pass!");
+        } catch (Exception e) {
+            HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+            assertEquals(405, hofe.getStatusCode());
+        }
+    }
+    
+    @Test
+    public void testMethodAllowed() {
+        try {
+            template.sendBodyAndHeader("http://localhost:" + getPort() + "/users/123/basic", "body", Exchange.HTTP_METHOD, "GET");
+        } catch (Exception e) {
+            fail("Shall pass with GET http method!");
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // configure to use jetty on localhost
+                restConfiguration().component("jetty").host("localhost").port(getPort());
+
+                // use the rest DSL to define the rest services
+                rest("/users/")
+                        .get("{id}/basic")
+                        .route()
+                        .to("mock:input")
+                        .process(exchange -> {
+                            String id = exchange.getIn().getHeader("id", String.class);
+                            exchange.getOut().setBody(id + ";Donald Duck");
+                        });
+            }
+        };
+    }
+}