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/05/01 17:39:33 UTC
svn commit: r1478054 - in /camel/trunk/components:
camel-netty-http/src/main/java/org/apache/camel/component/netty/http/
camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/
camel-netty-http/src/test/java/org/apache/camel/comp...
Author: davsclaus
Date: Wed May 1 15:39:33 2013
New Revision: 1478054
URL: http://svn.apache.org/r1478054
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/HttpClientPipelineFactory.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
- copied, changed from r1478011, camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java
camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerSimpleTest.java
- copied, changed from r1478011, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
Removed:
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.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/HttpServerPipelineFactory.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/NettyHttpConfiguration.java
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/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.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=1478054&r1=1478053&r2=1478054&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 Wed May 1 15:39:33 2013
@@ -28,8 +28,10 @@ import org.apache.camel.util.MessageHelp
import org.apache.camel.util.ObjectHelper;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
+import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
@@ -97,8 +99,8 @@ public class DefaultNettyHttpBinding imp
}
@Override
- public HttpResponse fromCamelMessage(Message message) throws Exception {
- LOG.trace("fromCamelMessage: {}", message);
+ public HttpResponse toNettyResponse(Message message) throws Exception {
+ LOG.trace("toNettyResponse: {}", message);
// the message body may already be a Netty HTTP response
if (message.getBody() instanceof HttpResponse) {
@@ -170,6 +172,77 @@ public class DefaultNettyHttpBinding imp
}
@Override
+ public HttpRequest toNettyRequest(Message message, String uri) throws Exception {
+ LOG.trace("toNettyRequest: {}", message);
+
+ // the message body may already be a Netty HTTP response
+ if (message.getBody() instanceof HttpRequest) {
+ return (HttpRequest) message.getBody();
+ }
+
+ // TODO: Compute the method to use
+ HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri);
+
+ TypeConverter tc = message.getExchange().getContext().getTypeConverter();
+
+ // append headers
+ // must use entrySet to ensure case of keys is preserved
+ for (Map.Entry<String, Object> entry : message.getHeaders().entrySet()) {
+ String key = entry.getKey();
+ Object value = entry.getValue();
+ // use an iterator as there can be multiple values. (must not use a delimiter)
+ final Iterator<?> it = ObjectHelper.createIterator(value, null);
+ while (it.hasNext()) {
+ String headerValue = tc.convertTo(String.class, it.next());
+ if (headerValue != null && headerFilterStrategy != null
+ && !headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue, message.getExchange())) {
+ LOG.trace("HTTP-Header: {}={}", key, headerValue);
+ request.addHeader(key, headerValue);
+ }
+ }
+ }
+
+ Object body = message.getBody();
+ if (body != null) {
+ // support bodies as native Netty
+ ChannelBuffer buffer;
+ if (body instanceof ChannelBuffer) {
+ buffer = (ChannelBuffer) body;
+ } else {
+ // try to convert to buffer first
+ buffer = message.getBody(ChannelBuffer.class);
+ if (buffer == null) {
+ // fallback to byte array as last resort
+ byte[] data = message.getMandatoryBody(byte[].class);
+ buffer = ChannelBuffers.copiedBuffer(data);
+ }
+ }
+ if (buffer != null) {
+ request.setContent(buffer);
+ int len = buffer.readableBytes();
+ // set content-length
+ request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, len);
+ LOG.trace("Content-Length: {}", len);
+ }
+ }
+
+ // set the content type in the response.
+ String contentType = MessageHelper.getContentType(message);
+ if (contentType != null) {
+ // set content-type
+ request.setHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
+ LOG.trace("Content-Type: {}", contentType);
+ }
+
+ // TODO: keep alive should be something we can control
+ String keepAlive = HttpHeaders.Values.CLOSE;
+ request.setHeader(HttpHeaders.Names.CONNECTION, keepAlive);
+ LOG.trace("Connection: {}", keepAlive);
+
+ return request;
+ }
+
+ @Override
public HeaderFilterStrategy getHeaderFilterStrategy() {
return headerFilterStrategy;
}
Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientPipelineFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientPipelineFactory.java?rev=1478054&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientPipelineFactory.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientPipelineFactory.java Wed May 1 15:39:33 2013
@@ -0,0 +1,138 @@
+/**
+ * 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 javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+
+import org.apache.camel.component.netty.ClientPipelineFactory;
+import org.apache.camel.component.netty.NettyProducer;
+import org.apache.camel.component.netty.http.handlers.HttpClientChannelHandler;
+import org.apache.camel.component.netty.ssl.SSLEngineFactory;
+import org.apache.camel.util.ObjectHelper;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.handler.codec.http.HttpClientCodec;
+import org.jboss.netty.handler.ssl.SslHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link org.apache.camel.component.netty.ClientPipelineFactory} for the Netty HTTP client.
+ */
+public class HttpClientPipelineFactory extends ClientPipelineFactory {
+
+ private static final Logger LOG = LoggerFactory.getLogger(HttpClientPipelineFactory.class);
+ private NettyProducer producer;
+ private SSLContext sslContext;
+
+ public HttpClientPipelineFactory() {
+ // default constructor needed
+ }
+
+ public HttpClientPipelineFactory(NettyProducer nettyProducer) {
+ this.producer = nettyProducer;
+ try {
+ this.sslContext = createSSLContext(producer);
+ } catch (Exception e) {
+ throw ObjectHelper.wrapRuntimeCamelException(e);
+ }
+
+ LOG.info("Created SslContext {}", sslContext);
+ }
+
+ @Override
+ public ClientPipelineFactory createPipelineFactory(NettyProducer nettyProducer) {
+ return new HttpClientPipelineFactory(nettyProducer);
+ }
+
+ @Override
+ public ChannelPipeline getPipeline() throws Exception {
+ ChannelPipeline pipeline = Channels.pipeline();
+
+ SslHandler sslHandler = configureClientSSLOnDemand();
+ if (sslHandler != null) {
+ LOG.debug("Client SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
+ pipeline.addLast("ssl", sslHandler);
+ }
+
+ pipeline.addLast("http", new HttpClientCodec());
+
+ // handler to route Camel messages
+ pipeline.addLast("handler", new HttpClientChannelHandler(producer));
+
+ return pipeline;
+ }
+
+ private SSLContext createSSLContext(NettyProducer producer) throws Exception {
+ if (!producer.getConfiguration().isSsl()) {
+ return null;
+ }
+
+ // create ssl context once
+ if (producer.getConfiguration().getSslContextParameters() != null) {
+ SSLContext context = producer.getConfiguration().getSslContextParameters().createSSLContext();
+ return context;
+ }
+
+ return null;
+ }
+
+ private SslHandler configureClientSSLOnDemand() throws Exception {
+ if (!producer.getConfiguration().isSsl()) {
+ return null;
+ }
+
+ if (producer.getConfiguration().getSslHandler() != null) {
+ return producer.getConfiguration().getSslHandler();
+ } else if (sslContext != null) {
+ SSLEngine engine = sslContext.createSSLEngine();
+ engine.setUseClientMode(true);
+ return new SslHandler(engine);
+ } else {
+ if (producer.getConfiguration().getKeyStoreFile() == null && producer.getConfiguration().getKeyStoreResource() == null) {
+ LOG.debug("keystorefile is null");
+ }
+ if (producer.getConfiguration().getTrustStoreFile() == null && producer.getConfiguration().getTrustStoreResource() == null) {
+ LOG.debug("truststorefile is null");
+ }
+ if (producer.getConfiguration().getPassphrase().toCharArray() == null) {
+ LOG.debug("passphrase is null");
+ }
+ SSLEngineFactory sslEngineFactory;
+ if (producer.getConfiguration().getKeyStoreFile() != null || producer.getConfiguration().getTrustStoreFile() != null) {
+ sslEngineFactory = new SSLEngineFactory(
+ producer.getConfiguration().getKeyStoreFormat(),
+ producer.getConfiguration().getSecurityProvider(),
+ producer.getConfiguration().getKeyStoreFile(),
+ producer.getConfiguration().getTrustStoreFile(),
+ producer.getConfiguration().getPassphrase().toCharArray());
+ } else {
+ sslEngineFactory = new SSLEngineFactory(producer.getContext().getClassResolver(),
+ producer.getConfiguration().getKeyStoreFormat(),
+ producer.getConfiguration().getSecurityProvider(),
+ producer.getConfiguration().getKeyStoreResource(),
+ producer.getConfiguration().getTrustStoreResource(),
+ producer.getConfiguration().getPassphrase().toCharArray());
+ }
+ SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine();
+ sslEngine.setUseClientMode(true);
+ return new SslHandler(sslEngine);
+ }
+ }
+
+}
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java?rev=1478054&r1=1478053&r2=1478054&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java Wed May 1 15:39:33 2013
@@ -21,6 +21,7 @@ import javax.net.ssl.SSLEngine;
import org.apache.camel.component.netty.NettyConsumer;
import org.apache.camel.component.netty.ServerPipelineFactory;
+import org.apache.camel.component.netty.http.handlers.HttpServerChannelHandler;
import org.apache.camel.component.netty.ssl.SSLEngineFactory;
import org.apache.camel.util.ObjectHelper;
import org.jboss.netty.channel.ChannelPipeline;
@@ -43,7 +44,7 @@ public class HttpServerPipelineFactory e
private SSLContext sslContext;
public HttpServerPipelineFactory() {
- // needed for blueprint IoC
+ // default constructor needed
}
public HttpServerPipelineFactory(NettyHttpConsumer nettyConsumer) {
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=1478054&r1=1478053&r2=1478054&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 Wed May 1 15:39:33 2013
@@ -50,14 +50,23 @@ public interface NettyHttpBinding {
void populateCamelHeaders(HttpRequest request, Map<String, Object> headers, Exchange exchange) throws Exception;
/**
- * Binds from Camel {@link Message} to Netty {@link HttpResponse}.
- *
+ * Binds from Camel {@link Message} to Netty {@link org.jboss.netty.handler.codec.http.HttpResponse}.
*
* @param message the Camel message
* @return the http response
* @throws Exception is thrown if error during binding
*/
- Object fromCamelMessage(Message message) throws Exception;
+ HttpResponse toNettyResponse(Message message) throws Exception;
+
+ /**
+ * Binds from Camel {@link Message} to Netty {@link org.jboss.netty.handler.codec.http.HttpRequest}.
+ *
+ * @param message the Camel message
+ * @param uri the uri which is the intended uri to call, though the message may override the uri
+ * @return the http request
+ * @throws Exception is thrown if error during binding
+ */
+ HttpRequest toNettyRequest(Message message, String uri) throws Exception;
/**
* Gets the header filter strategy
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java?rev=1478054&r1=1478053&r2=1478054&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java Wed May 1 15:39:33 2013
@@ -36,6 +36,7 @@ public class NettyHttpConfiguration exte
setSync(true);
setReuseAddress(true);
setServerPipelineFactory(new HttpServerPipelineFactory());
+ setClientPipelineFactory(new HttpClientPipelineFactory());
}
@Override
Modified: 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=1478054&r1=1478053&r2=1478054&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java Wed May 1 15:39:33 2013
@@ -16,11 +16,14 @@
*/
package org.apache.camel.component.netty.http;
+import java.nio.charset.Charset;
+
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;
+import org.jboss.netty.handler.codec.http.HttpResponse;
@Converter
public final class NettyHttpConverter {
@@ -48,4 +51,23 @@ public final class NettyHttpConverter {
return null;
}
+ @Converter
+ public static String toString(HttpResponse response, Exchange exchange) {
+ String contentType = response.getHeader(Exchange.CONTENT_TYPE);
+ String charset = NettyHttpHelper.getCharsetFromContentType(contentType);
+ if (charset == null && exchange != null) {
+ charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
+ }
+ if (charset != null) {
+ return response.getContent().toString(Charset.forName(charset));
+ } else {
+ return response.getContent().toString(Charset.defaultCharset());
+ }
+ }
+
+ @Converter
+ public static byte[] toBytes(HttpResponse response, Exchange exchange) {
+ return response.getContent().toByteBuffer().array();
+ }
+
}
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=1478054&r1=1478053&r2=1478054&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 Wed May 1 15:39:33 2013
@@ -20,9 +20,11 @@ import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
+import org.apache.camel.Producer;
import org.apache.camel.component.netty.NettyConfiguration;
import org.apache.camel.component.netty.NettyConstants;
import org.apache.camel.component.netty.NettyEndpoint;
+import org.apache.camel.impl.SynchronousDelegateProducer;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
import org.apache.camel.util.ObjectHelper;
@@ -52,6 +54,16 @@ public class NettyHttpEndpoint extends N
}
@Override
+ public Producer createProducer() throws Exception {
+ Producer answer = new NettyHttpProducer(this, getConfiguration());
+ if (isSynchronous()) {
+ return new SynchronousDelegateProducer(answer);
+ } else {
+ return answer;
+ }
+ }
+
+ @Override
public Exchange createExchange(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
Exchange exchange = createExchange();
Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java?rev=1478054&r1=1478053&r2=1478054&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java Wed May 1 15:39:33 2013
@@ -22,6 +22,7 @@ import java.util.Map;
import org.apache.camel.Exchange;
import org.apache.camel.converter.IOConverter;
+import org.apache.camel.util.IOHelper;
/**
* Helpers.
@@ -33,14 +34,22 @@ public final class NettyHttpHelper {
@SuppressWarnings("deprecation")
public static void setCharsetFromContentType(String contentType, Exchange exchange) {
+ String charset = getCharsetFromContentType(contentType);
+ if (charset != null) {
+ exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(charset));
+ }
+ }
+
+ public static String getCharsetFromContentType(String contentType) {
if (contentType != null) {
// find the charset and set it to the Exchange
int index = contentType.indexOf("charset=");
if (index > 0) {
String charset = contentType.substring(index + 8);
- exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(charset));
+ return IOHelper.normalizeCharset(charset);
}
}
+ return null;
}
/**
Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java?rev=1478054&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpProducer.java Wed May 1 15:39:33 2013
@@ -0,0 +1,30 @@
+package org.apache.camel.component.netty.http;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.netty.NettyConfiguration;
+import org.apache.camel.component.netty.NettyProducer;
+
+/**
+ * HTTP based {@link NettyProducer}.
+ */
+public class NettyHttpProducer extends NettyProducer {
+
+ public NettyHttpProducer(NettyHttpEndpoint nettyEndpoint, NettyConfiguration configuration) {
+ super(nettyEndpoint, configuration);
+ }
+
+ @Override
+ public NettyHttpEndpoint getEndpoint() {
+ return (NettyHttpEndpoint) super.getEndpoint();
+ }
+
+ @Override
+ protected Object getRequestBody(Exchange exchange) throws Exception {
+ String uri = getEndpoint().getEndpointUri();
+ if (exchange.hasOut()) {
+ return getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getOut(), uri);
+ } else {
+ return getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getIn(), uri);
+ }
+ }
+}
Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java?rev=1478054&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java Wed May 1 15:39:33 2013
@@ -0,0 +1,41 @@
+/**
+ * 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.handlers;
+
+import org.apache.camel.component.netty.NettyProducer;
+import org.apache.camel.component.netty.handlers.ClientChannelHandler;
+import org.apache.camel.component.netty.http.NettyHttpProducer;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.MessageEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpClientChannelHandler extends ClientChannelHandler {
+
+ // use NettyHttpConsumer as logger to make it easier to read the logs as this is part of the producer
+ private static final transient Logger LOG = LoggerFactory.getLogger(NettyHttpProducer.class);
+
+ public HttpClientChannelHandler(NettyProducer producer) {
+ super(producer);
+ }
+
+ @Override
+ public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
+ super.messageReceived(ctx, messageEvent); //To change body of overridden methods use File | Settings | File Templates.
+ }
+
+}
Copied: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java (from r1478011, 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/handlers/HttpServerChannelHandler.java?p2=camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java&p1=camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java&r1=1478011&r2=1478054&rev=1478054&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/handlers/HttpServerChannelHandler.java Wed May 1 15:39:33 2013
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.component.netty.http;
+package org.apache.camel.component.netty.http.handlers;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
@@ -23,6 +23,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.component.netty.NettyConsumer;
import org.apache.camel.component.netty.NettyHelper;
import org.apache.camel.component.netty.handlers.ServerChannelHandler;
+import org.apache.camel.component.netty.http.NettyHttpConsumer;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
@@ -125,9 +126,9 @@ public class HttpServerChannelHandler ex
protected Object getResponseBody(Exchange exchange) throws Exception {
// use the binding
if (exchange.hasOut()) {
- return consumer.getEndpoint().getNettyHttpBinding().fromCamelMessage(exchange.getOut());
+ return consumer.getEndpoint().getNettyHttpBinding().toNettyResponse(exchange.getOut());
} else {
- return consumer.getEndpoint().getNettyHttpBinding().fromCamelMessage(exchange.getIn());
+ return consumer.getEndpoint().getNettyHttpBinding().toNettyResponse(exchange.getIn());
}
}
}
Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerSimpleTest.java (from r1478011, 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/NettyHttpProducerSimpleTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpProducerSimpleTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java&r1=1478011&r2=1478054&rev=1478054&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/NettyHttpProducerSimpleTest.java Wed May 1 15:39:33 2013
@@ -19,13 +19,13 @@ package org.apache.camel.component.netty
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
-public class NettyHttpSimpleTest extends BaseNettyTest {
+public class NettyHttpProducerSimpleTest extends BaseNettyTest {
@Test
public void testHttpSimple() throws Exception {
getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
- String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
+ String out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class);
assertEquals("Bye World", out);
assertMockEndpointsSatisfied();
Modified: camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java?rev=1478054&r1=1478053&r2=1478054&view=diff
==============================================================================
--- camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java (original)
+++ camel/trunk/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java Wed May 1 15:39:33 2013
@@ -26,7 +26,6 @@ import org.apache.camel.CamelContext;
import org.apache.camel.CamelException;
import org.apache.camel.CamelExchangeException;
import org.apache.camel.Exchange;
-import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.impl.DefaultAsyncProducer;
import org.apache.camel.util.CamelLogger;
import org.apache.camel.util.ExchangeHelper;
@@ -182,22 +181,18 @@ public class NettyProducer extends Defau
return true;
}
- Object body = NettyPayloadHelper.getIn(getEndpoint(), exchange);
- if (body == null) {
- noReplyLogger.log("No payload to send for exchange: " + exchange);
- callback.done(true);
- return true;
- }
-
- // if textline enabled then covert to a String which must be used for textline
- if (getConfiguration().isTextline()) {
- try {
- body = NettyHelper.getTextlineBody(body, exchange, getConfiguration().getDelimiter(), getConfiguration().isAutoAppendDelimiter());
- } catch (NoTypeConversionAvailableException e) {
- exchange.setException(e);
+ Object body;
+ try {
+ body = getRequestBody(exchange);
+ if (body == null) {
+ noReplyLogger.log("No payload to send for exchange: " + exchange);
callback.done(true);
return true;
}
+ } catch (Exception e) {
+ exchange.setException(e);
+ callback.done(true);
+ return true;
}
// set the exchange encoding property
@@ -282,6 +277,27 @@ public class NettyProducer extends Defau
}
/**
+ * Gets the object we want to use as the request object for sending to netty.
+ *
+ * @param exchange the exchange
+ * @return the object to use as request
+ * @throws Exception is thrown if error getting the request body
+ */
+ protected Object getRequestBody(Exchange exchange) throws Exception {
+ Object body = NettyPayloadHelper.getIn(getEndpoint(), exchange);
+ if (body == null) {
+ return null;
+ }
+
+ // if textline enabled then covert to a String which must be used for textline
+ if (getConfiguration().isTextline()) {
+ body = NettyHelper.getTextlineBody(body, exchange, getConfiguration().getDelimiter(), getConfiguration().isAutoAppendDelimiter());
+ }
+
+ return body;
+ }
+
+ /**
* To get the {@link NettyCamelState} from the given channel.
*/
public NettyCamelState getState(Channel channel) {