You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/04/30 15:49:24 UTC
svn commit: r1477632 - in /camel/trunk/components/camel-netty-http/src:
main/java/org/apache/camel/component/netty/http/
main/resources/META-INF/services/org/apache/camel/
test/java/org/apache/camel/component/netty/http/
Author: davsclaus
Date: Tue Apr 30 13:49:24 2013
New Revision: 1477632
URL: http://svn.apache.org/r1477632
Log:
CAMEL-6327: More work on new camel-netty-http component.
Added:
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java
camel/trunk/components/camel-netty-http/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestBeanTest.java
- copied, changed from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java
- copied, changed from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java
- copied, changed from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyUseRawHttpResponseTest.java
- copied, changed from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java
Modified:
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java?rev=1477632&r1=1477631&r2=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java Tue Apr 30 13:49:24 2013
@@ -100,6 +100,11 @@ public class DefaultNettyHttpBinding imp
public HttpResponse fromCamelMessage(Message message) throws Exception {
LOG.trace("fromCamelMessage: {}", message);
+ // the message body may already be a Netty HTTP response
+ if (message.getBody() instanceof HttpResponse) {
+ return (HttpResponse) message.getBody();
+ }
+
// the status code is default 200, but a header can override that
Integer code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 200, Integer.class);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code));
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java?rev=1477632&r1=1477631&r2=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java Tue Apr 30 13:49:24 2013
@@ -36,6 +36,8 @@ import org.slf4j.LoggerFactory;
import static org.jboss.netty.handler.codec.http.HttpHeaders.is100ContinueExpected;
import static org.jboss.netty.handler.codec.http.HttpHeaders.isKeepAlive;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
+import static org.jboss.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
+import static org.jboss.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
@@ -60,17 +62,37 @@ public class HttpServerChannelHandler ex
request = (HttpRequest) messageEvent.getMessage();
if (LOG.isDebugEnabled()) {
- LOG.debug("Message received: keep-alive {}", isKeepAlive(request));
+ LOG.debug("Message received: {} keep-alive: {}", request, isKeepAlive(request));
}
if (is100ContinueExpected(request)) {
// send back http 100 response to continue
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CONTINUE);
messageEvent.getChannel().write(response);
- } else {
- // let Camel process this message
- super.messageReceived(ctx, messageEvent);
+ return;
}
+
+ if (consumer.isSuspended()) {
+ // are we suspended?
+ LOG.debug("Consumer suspended, cannot service request {}", request);
+ HttpResponse response = new DefaultHttpResponse(HTTP_1_1, SERVICE_UNAVAILABLE);
+ messageEvent.getChannel().write(response);
+ return;
+ }
+ if (consumer.getEndpoint().getHttpMethodRestrict() != null
+ && !consumer.getEndpoint().getHttpMethodRestrict().contains(request.getMethod().getName())) {
+ HttpResponse response = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
+ messageEvent.getChannel().write(response);
+ return;
+ }
+ if ("TRACE".equals(request.getMethod().getName()) && !consumer.getEndpoint().isTraceEnabled()) {
+ HttpResponse response = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
+ messageEvent.getChannel().write(response);
+ return;
+ }
+
+ // let Camel process this message
+ super.messageReceived(ctx, messageEvent);
}
@Override
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java?rev=1477632&r1=1477631&r2=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java Tue Apr 30 13:49:24 2013
@@ -52,11 +52,12 @@ public interface NettyHttpBinding {
/**
* Binds from Camel {@link Message} to Netty {@link HttpResponse}.
*
+ *
* @param message the Camel message
* @return the http response
* @throws Exception is thrown if error during binding
*/
- HttpResponse fromCamelMessage(Message message) throws Exception;
+ Object fromCamelMessage(Message message) throws Exception;
/**
* Gets the header filter strategy
Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java?rev=1477632&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java Tue Apr 30 13:49:24 2013
@@ -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.netty.http;
+
+import org.apache.camel.Converter;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.jboss.netty.handler.codec.http.HttpRequest;
+
+@Converter
+public final class NettyHttpConverter {
+
+ private NettyHttpConverter() {
+ }
+
+ /**
+ * A fallback converter that allows us to easily call Java beans and use the raw Netty {@link HttpRequest} as parameter types.
+ */
+ @FallbackConverter
+ public static Object convertToHttpRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+ // if we want to covert to HttpRequest
+ if (value != null && HttpRequest.class.isAssignableFrom(type)) {
+
+ // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
+ // so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
+ // HttpRequest from the NettyHttpMessage
+ NettyHttpMessage msg = exchange.getIn(NettyHttpMessage.class);
+ if (msg != null && msg.getBody() == value) {
+ return msg.getHttpRequest();
+ }
+ }
+
+ return null;
+ }
+
+}
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java?rev=1477632&r1=1477631&r2=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java Tue Apr 30 13:49:24 2013
@@ -37,6 +37,8 @@ public class NettyHttpEndpoint extends N
private NettyHttpBinding nettyHttpBinding;
private HeaderFilterStrategy headerFilterStrategy;
+ private boolean traceEnabled;
+ private String httpMethodRestrict;
public NettyHttpEndpoint(String endpointUri, NettyHttpComponent component, NettyConfiguration configuration) {
super(endpointUri, component, configuration);
@@ -92,6 +94,22 @@ public class NettyHttpEndpoint extends N
this.headerFilterStrategy = headerFilterStrategy;
}
+ public boolean isTraceEnabled() {
+ return traceEnabled;
+ }
+
+ public void setTraceEnabled(boolean traceEnabled) {
+ this.traceEnabled = traceEnabled;
+ }
+
+ public String getHttpMethodRestrict() {
+ return httpMethodRestrict;
+ }
+
+ public void setHttpMethodRestrict(String httpMethodRestrict) {
+ this.httpMethodRestrict = httpMethodRestrict;
+ }
+
@Override
protected void doStart() throws Exception {
super.doStart();
Added: camel/trunk/components/camel-netty-http/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/resources/META-INF/services/org/apache/camel/TypeConverter?rev=1477632&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/resources/META-INF/services/org/apache/camel/TypeConverter (added)
+++ camel/trunk/components/camel-netty-http/src/main/resources/META-INF/services/org/apache/camel/TypeConverter Tue Apr 30 13:49:24 2013
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.camel.component.netty.http.NettyHttpConverter
\ No newline at end of file
Added: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java?rev=1477632&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java (added)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java Tue Apr 30 13:49:24 2013
@@ -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.netty.http;
+
+import java.nio.charset.Charset;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
+import org.jboss.netty.handler.codec.http.HttpRequest;
+import org.jboss.netty.handler.codec.http.HttpResponse;
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;
+import org.jboss.netty.handler.codec.http.HttpVersion;
+import org.junit.Test;
+
+public class NettyHttpAccessHttpRequestAndResponseBeanTest extends BaseNettyTest {
+
+ @Test
+ public void testRawHttpRequestAndResponseInBean() throws Exception {
+ getMockEndpoint("mock:input").expectedBodiesReceived("World", "Camel");
+
+ String out = template.requestBody("http://localhost:{{port}}/foo", "World", String.class);
+ assertEquals("Bye World", out);
+
+ String out2 = template.requestBody("http://localhost:{{port}}/foo", "Camel", String.class);
+ assertEquals("Bye Camel", out2);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("netty-http:http://0.0.0.0:{{port}}/foo")
+ .to("mock:input")
+ .transform().method(NettyHttpAccessHttpRequestAndResponseBeanTest.class, "myTransformer");
+ }
+ };
+ }
+
+ /**
+ * We can use both a netty http request and response type for transformation
+ */
+ public static HttpResponse myTransformer(HttpRequest request) {
+ String in = request.getContent().toString(Charset.forName("UTF-8"));
+ String reply = "Bye " + in;
+
+ HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
+ response.setContent(ChannelBuffers.copiedBuffer(reply.getBytes()));
+ response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, reply.length());
+
+ return response;
+ }
+
+}
Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestBeanTest.java (from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestBeanTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestBeanTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java&r1=1477564&r2=1477632&rev=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestBeanTest.java Tue Apr 30 13:49:24 2013
@@ -16,19 +16,19 @@
*/
package org.apache.camel.component.netty.http;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
+import java.nio.charset.Charset;
+
import org.apache.camel.builder.RouteBuilder;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.junit.Test;
-public class NettyHttpAccessHttpRequestTest extends BaseNettyTest {
+public class NettyHttpAccessHttpRequestBeanTest extends BaseNettyTest {
@Test
public void testAccessHttpRequest() throws Exception {
- getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:input").expectedBodiesReceived("World");
- String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
+ String out = template.requestBody("http://localhost:{{port}}/foo", "World", String.class);
assertEquals("Bye World", out);
assertMockEndpointsSatisfied();
@@ -41,17 +41,14 @@ public class NettyHttpAccessHttpRequestT
public void configure() throws Exception {
from("netty-http:http://0.0.0.0:{{port}}/foo")
.to("mock:input")
- .process(new Processor() {
- @Override
- public void process(Exchange exchange) throws Exception {
- // we can get the original http request
- HttpRequest request = exchange.getIn(NettyHttpMessage.class).getHttpRequest();
- assertNotNull(request);
- }
- })
- .transform().constant("Bye World");
+ .transform().method(NettyHttpAccessHttpRequestBeanTest.class, "myTransformer");
}
};
}
+ public static String myTransformer(HttpRequest request) {
+ String in = request.getContent().toString(Charset.forName("UTF-8"));
+ return "Bye " + in;
+ }
+
}
Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java (from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java&r1=1477564&r2=1477632&rev=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java Tue Apr 30 13:49:24 2013
@@ -16,29 +16,57 @@
*/
package org.apache.camel.component.netty.http;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.httpclient.HttpClient;
+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;
-public class NettyHttpSimpleTest extends BaseNettyTest {
+public class NettyHttpMethodRestrictTest extends BaseNettyTest {
+
+ private String getUrl() {
+ return "http://localhost:" + getPort() + "/methodRestrict";
+ }
@Test
- public void testHttpSimple() throws Exception {
- getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+ 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);
- String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
- assertEquals("Bye World", out);
+ 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);
- assertMockEndpointsSatisfied();
+ assertEquals("Get a wrong response status", 405, status);
}
- @Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
- @Override
public void configure() throws Exception {
- from("netty-http:http://0.0.0.0:{{port}}/foo")
- .to("mock:input")
- .transform().constant("Bye World");
+ from("netty-http://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");
+ }
+ });
}
};
}
Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java (from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java&r1=1477564&r2=1477632&rev=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java Tue Apr 30 13:49:24 2013
@@ -17,18 +17,35 @@
package org.apache.camel.component.netty.http;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.TraceMethod;
import org.junit.Test;
-public class NettyHttpSimpleTest extends BaseNettyTest {
+public class NettyHttpTraceDisabledTest extends BaseNettyTest {
- @Test
- public void testHttpSimple() throws Exception {
- getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+ private int portTraceOn = getNextPort();
+ private int portTraceOff = getNextPort();
- String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
- assertEquals("Bye World", out);
+ @Test
+ public void testTraceDisabled() throws Exception {
+ HttpClient httpclient = new HttpClient();
+ TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOff + "/myservice");
+ httpclient.executeMethod(trace);
+
+ // TRACE shouldn't be allowed by default
+ assertTrue(trace.getStatusCode() == 405);
+ trace.releaseConnection();
+ }
- assertMockEndpointsSatisfied();
+ @Test
+ public void testTraceEnabled() throws Exception {
+ HttpClient httpclient = new HttpClient();
+ TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOn + "/myservice");
+ httpclient.executeMethod(trace);
+
+ // TRACE is now allowed
+ assertTrue(trace.getStatusCode() == 200);
+ trace.releaseConnection();
}
@Override
@@ -36,9 +53,8 @@ public class NettyHttpSimpleTest extends
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("netty-http:http://0.0.0.0:{{port}}/foo")
- .to("mock:input")
- .transform().constant("Bye World");
+ from("netty-http:http://localhost:" + portTraceOff + "/myservice").to("log:foo");
+ from("netty-http:http://localhost:" + portTraceOn + "/myservice?traceEnabled=true").to("log:bar");
}
};
}
Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyUseRawHttpResponseTest.java (from r1477564, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyUseRawHttpResponseTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyUseRawHttpResponseTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java&r1=1477564&r2=1477632&rev=1477632&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyUseRawHttpResponseTest.java Tue Apr 30 13:49:24 2013
@@ -19,10 +19,15 @@ package org.apache.camel.component.netty
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
-import org.jboss.netty.handler.codec.http.HttpRequest;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
+import org.jboss.netty.handler.codec.http.HttpResponse;
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;
+import org.jboss.netty.handler.codec.http.HttpVersion;
import org.junit.Test;
-public class NettyHttpAccessHttpRequestTest extends BaseNettyTest {
+public class NettyUseRawHttpResponseTest extends BaseNettyTest {
@Test
public void testAccessHttpRequest() throws Exception {
@@ -44,12 +49,13 @@ public class NettyHttpAccessHttpRequestT
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
- // we can get the original http request
- HttpRequest request = exchange.getIn(NettyHttpMessage.class).getHttpRequest();
- assertNotNull(request);
+ HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
+ response.setContent(ChannelBuffers.copiedBuffer("Bye World".getBytes()));
+ response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, 9);
+
+ exchange.getOut().setBody(response);
}
- })
- .transform().constant("Bye World");
+ });
}
};
}