You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/08/13 11:17:38 UTC
svn commit: r1372336 - in /camel/trunk/components:
camel-http/src/main/java/org/apache/camel/component/http/
camel-http4/src/main/java/org/apache/camel/component/http4/
camel-jetty/src/main/java/org/apache/camel/component/jetty/
camel-jetty/src/test/ja...
Author: ningjiang
Date: Mon Aug 13 09:17:37 2012
New Revision: 1372336
URL: http://svn.apache.org/viewvc?rev=1372336&view=rev
Log:
CAMEL-5492 Add option to restrict HTTP method in camel-jetty
Added:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java
Modified:
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java Mon Aug 13 09:17:37 2012
@@ -72,9 +72,16 @@ public class CamelServlet extends HttpSe
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
}
+
+ if (consumer.getEndpoint().getHttpMethodRestrict() != null
+ && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
+ response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
+ }
if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
}
// create exchange and set data on it
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Mon Aug 13 09:17:37 2012
@@ -60,6 +60,7 @@ public class HttpEndpoint extends Defaul
private String authMethodPriority;
private boolean transferException;
private boolean traceEnabled;
+ private String httpMethodRestrict;
public HttpEndpoint() {
}
@@ -328,4 +329,12 @@ public class HttpEndpoint extends Defaul
public void setTraceEnabled(boolean traceEnabled) {
this.traceEnabled = traceEnabled;
}
+
+ public String getHttpMethodRestrict() {
+ return httpMethodRestrict;
+ }
+
+ public void setHttpMethodRestrict(String httpMethodRestrict) {
+ this.httpMethodRestrict = httpMethodRestrict;
+ }
}
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java Mon Aug 13 09:17:37 2012
@@ -49,9 +49,16 @@ public class CamelServlet extends HttpSe
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
+
+ if (consumer.getEndpoint().getHttpMethodRestrict() != null
+ && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
+ response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
+ }
if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
}
// Have the camel process the HTTP exchange.
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpComponent.java Mon Aug 13 09:17:37 2012
@@ -193,6 +193,7 @@ public class HttpComponent extends Heade
if (sslContextParameters == null) {
sslContextParameters = getSslContextParameters();
}
+ String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
boolean secure = HttpHelper.isSecureConnection(uri);
@@ -233,6 +234,9 @@ public class HttpComponent extends Heade
if (httpClientConfigurer != null) {
endpoint.setHttpClientConfigurer(httpClientConfigurer);
}
+ if (httpMethodRestrict != null) {
+ endpoint.setHttpMethodRestrict(httpMethodRestrict);
+ }
endpoint.setHttpContext(getHttpContext());
if (httpContext != null) {
endpoint.setHttpContext(httpContext);
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java Mon Aug 13 09:17:37 2012
@@ -61,6 +61,8 @@ public class HttpEndpoint extends Defaul
private boolean disableStreamCache;
private boolean transferException;
private boolean traceEnabled;
+ private String httpMethodRestrict;
+
public HttpEndpoint() {
}
@@ -321,4 +323,12 @@ public class HttpEndpoint extends Defaul
public void setTraceEnabled(boolean traceEnabled) {
this.traceEnabled = traceEnabled;
}
+
+ public String getHttpMethodRestrict() {
+ return httpMethodRestrict;
+ }
+
+ public void setHttpMethodRestrict(String httpMethodRestrict) {
+ this.httpMethodRestrict = httpMethodRestrict;
+ }
}
Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java Mon Aug 13 09:17:37 2012
@@ -63,8 +63,15 @@ public class CamelContinuationServlet ex
return;
}
+ if (consumer.getEndpoint().getHttpMethodRestrict() != null
+ && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
+ response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
+ }
+
if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ return;
}
final Exchange result = (Exchange) request.getAttribute(EXCHANGE_ATTRIBUTE_NAME);
Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Mon Aug 13 09:17:37 2012
@@ -148,6 +148,7 @@ public class JettyHttpComponent extends
List<Filter> filters = resolveAndRemoveReferenceListParameter(parameters, "filtersRef", Filter.class);
Long continuationTimeout = getAndRemoveParameter(parameters, "continuationTimeout", Long.class);
Boolean useContinuation = getAndRemoveParameter(parameters, "useContinuation", Boolean.class);
+ String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters, "sslContextParametersRef", SSLContextParameters.class);
@@ -263,6 +264,10 @@ public class JettyHttpComponent extends
if (useContinuation != null) {
endpoint.setUseContinuation(useContinuation);
}
+
+ if (httpMethodRestrict != null) {
+ endpoint.setHttpMethodRestrict(httpMethodRestrict);
+ }
if (sslContextParameters == null) {
sslContextParameters = this.sslContextParameters;
Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java?rev=1372336&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java Mon Aug 13 09:17:37 2012
@@ -0,0 +1,82 @@
+/**
+ * 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;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.junit.Test;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HttpMethodRestrictTest extends BaseJettyTest {
+
+ private String getUrl() {
+ return "http://localhost:" + getPort() + "/methodRestrict";
+ }
+
+ @Test
+ public void testProperHttpMethod() throws Exception {
+ HttpClient httpClient = new HttpClient();
+ PostMethod httpPost = new PostMethod(getUrl());
+
+ StringRequestEntity reqEntity = new StringRequestEntity("This is a test", null, null);
+ httpPost.setRequestEntity(reqEntity);
+
+ int status = httpClient.executeMethod(httpPost);
+
+ assertEquals("Get a wrong response status", 200, status);
+
+ String result = httpPost.getResponseBodyAsString();
+ assertEquals("Get a wrong result", "This is a test response", result);
+ }
+
+ @Test
+ public void testImproperHttpMethod() throws Exception {
+ HttpClient httpClient = new HttpClient();
+ GetMethod httpGet = new GetMethod(getUrl());
+ int status = httpClient.executeMethod(httpGet);
+
+ assertEquals("Get a wrong response status", 405, status);
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
+ from("jetty://http://localhost:{{port}}/methodRestrict?httpMethodRestrict=POST").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ Message in = exchange.getIn();
+ String request = in.getBody(String.class);
+ exchange.getOut().setBody(request + " response");
+ }
+ });
+ }
+ };
+ }
+}
Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java (original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java Mon Aug 13 09:17:37 2012
@@ -70,6 +70,7 @@ public class ServletComponent extends Ht
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class);
String servletName = getAndRemoveParameter(parameters, "servletName", String.class, getServletName());
+ String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encode(uri)), parameters);
@@ -100,6 +101,9 @@ public class ServletComponent extends Ht
if (matchOnUriPrefix != null) {
endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
}
+ if (httpMethodRestrict != null) {
+ endpoint.setHttpMethodRestrict(httpMethodRestrict);
+ }
setProperties(endpoint, parameters);
return endpoint;
Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java?rev=1372336&r1=1372335&r2=1372336&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java (original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java Mon Aug 13 09:17:37 2012
@@ -20,6 +20,8 @@ import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.HttpException;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
@@ -55,6 +57,25 @@ public class HttpClientRouteTest extends
assertEquals("The response message is wrong ", "OK", response.getResponseMessage());
client.setExceptionsThrownOnErrorStatus(false);
}
+
+ @Test
+ public void testHttpRestricMethod() throws Exception {
+ WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict", new ByteArrayInputStream(POST_DATA.getBytes()), "text/xml; charset=UTF-8");
+ ServletUnitClient client = newClient();
+ WebResponse response = client.getResponse(req);
+ assertEquals("The response message is wrong ", "OK", response.getResponseMessage());
+ assertEquals("The response body is wrong", POST_DATA, response.getText());
+
+ // Send other web method request
+ req = new GetMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict");
+ try {
+ response = client.getResponse(req);
+ fail("Expect the exception here");
+ } catch (Exception ex) {
+ HttpException httpException = (HttpException)ex;
+ assertEquals("Get a wrong response code", 405, httpException.getResponseCode());
+ }
+ }
@Test
public void testHttpConverter() throws Exception {
@@ -129,6 +150,13 @@ public class HttpClientRouteTest extends
}
});
// END SNIPPET: route
+
+ from("servlet:///testHttpMethodRestrict?httpMethodRestrict=POST").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String request = exchange.getIn().getBody(String.class);
+ exchange.getOut().setBody(request);
+ }
+ });
from("servlet:///testConverter?matchOnUriPrefix=true")
.convertBodyTo(String.class)