Author: ningjiang Date: Thu May 14 12:41:03 2009 New Revision: 774748 URL: http://svn.apache.org/viewvc?rev=774748&view=rev Log: CAMEL-1594 Set the content-type in the camel-jhc converter
Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/AsyncBufferingHttpServiceHandler.java camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConsumer.java camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConverter.java camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcEndpoint.java camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcServerEngine.java Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/AsyncBufferingHttpServiceHandler.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/AsyncBufferingHttpServiceHandler.java?rev=774748&r1=774747&r2=774748&view=diff ============================================================================== --- camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/AsyncBufferingHttpServiceHandler.java (original) +++ camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/AsyncBufferingHttpServiceHandler.java Thu May 14 12:41:03 2009 @@ -41,13 +41,6 @@ import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; -/** - * Created by IntelliJ IDEA. - * User: gnodet - * Date: Sep 11, 2007 - * Time: 6:57:34 PM - * To change this template use File | Settings | File Templates. - */ public class AsyncBufferingHttpServiceHandler extends BufferingHttpServiceHandler { Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConsumer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConsumer.java?rev=774748&r1=774747&r2=774748&view=diff ============================================================================== --- camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConsumer.java (original) +++ camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConsumer.java Thu May 14 12:41:03 2009 @@ -21,6 +21,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.impl.DefaultConsumer; +import org.apache.camel.util.MessageHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; @@ -32,6 +33,7 @@ import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.ProtocolVersion; +import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.impl.DefaultHttpResponseFactory; import org.apache.http.nio.NHttpConnection; import org.apache.http.nio.protocol.EventListener; @@ -39,10 +41,6 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; -/** - * Created by IntelliJ IDEA. User: gnodet Date: Sep 7, 2007 Time: 8:15:54 PM To - * change this template use File | Settings | File Templates. - */ public class JhcConsumer extends DefaultConsumer { private static final Log LOG = LogFactory.getLog(JhcConsumer.class); @@ -141,15 +139,16 @@ public void handle(final HttpRequest request, final HttpContext context, final AsyncResponseHandler handler) throws HttpException, IOException { final Exchange exchange = getEndpoint().createExchange(); - exchange.getIn().setHeader("http.uri", request.getRequestLine().getUri()); + exchange.getIn().setHeader(Exchange.HTTP_URI, request.getRequestLine().getUri()); if (request instanceof HttpEntityEnclosingRequest) { + exchange.getIn().setHeader(Exchange.CONTENT_TYPE, ((HttpEntityEnclosingRequest)request).getEntity().getContentType()); exchange.getIn().setBody(((HttpEntityEnclosingRequest)request).getEntity()); } try { getProcessor().process(exchange); } catch (Exception e) { - exchange.setException(e); + throw new HttpException("Get the exception when processing the exchange", e); } LOG.debug("handleExchange"); @@ -159,9 +158,14 @@ if (responseCode == null) { responseCode = HttpStatus.SC_OK; } - HttpResponse response = responseFactory.newHttpResponse(httpVersion, responseCode, context); + HttpResponse response = responseFactory.newHttpResponse(httpVersion, responseCode, context); response.setParams(params); HttpEntity entity = exchange.getOut().getBody(HttpEntity.class); + String contentType = MessageHelper.getContentType(exchange.getOut()); + if (contentType != null && entity instanceof AbstractHttpEntity) { + // To make sure we set the right content-type here + ((AbstractHttpEntity)entity).setContentType(contentType); + } response.setEntity(entity); response.setParams(getEndpoint().getParams()); try { Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConverter.java?rev=774748&r1=774747&r2=774748&view=diff ============================================================================== --- camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConverter.java (original) +++ camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcConverter.java Thu May 14 12:41:03 2009 @@ -22,18 +22,12 @@ import org.apache.camel.Converter; import org.apache.camel.Exchange; +import org.apache.camel.util.ExchangeHelper; import org.apache.http.HttpEntity; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; -/** - * Created by IntelliJ IDEA. - * User: gnodet - * Date: Sep 10, 2007 - * Time: 8:26:44 AM - * To change this template use File | Settings | File Templates. - */ @Converter public final class JhcConverter { @@ -56,13 +50,23 @@ } @Converter - public static HttpEntity toEntity(InputStream is) { - return new InputStreamEntity(is, -1); + public static HttpEntity toEntity(InputStream is, Exchange exchange) { + InputStreamEntity answer = new InputStreamEntity(is, -1); + String contentType = ExchangeHelper.getContentType(exchange); + if (contentType != null) { + answer.setContentType(contentType); + } + return answer; } @Converter public static HttpEntity toEntity(String str, Exchange exchange) throws UnsupportedEncodingException { String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class); - return new StringEntity(str, charset); + StringEntity answer = new StringEntity(str, charset); + String contentType = ExchangeHelper.getContentType(exchange); + if (contentType != null) { + answer.setContentType(contentType); + } + return answer; } } Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcEndpoint.java?rev=774748&r1=774747&r2=774748&view=diff ============================================================================== --- camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcEndpoint.java (original) +++ camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcEndpoint.java Thu May 14 12:41:03 2009 @@ -26,13 +26,6 @@ import org.apache.camel.spi.HeaderFilterStrategyAware; import org.apache.http.params.HttpParams; -/** - * Created by IntelliJ IDEA. - * User: gnodet - * Date: Sep 7, 2007 - * Time: 8:06:42 PM - * To change this template use File | Settings | File Templates. - */ public class JhcEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware { private HttpParams params; Modified: camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcServerEngine.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcServerEngine.java?rev=774748&r1=774747&r2=774748&view=diff ============================================================================== --- camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcServerEngine.java (original) +++ camel/trunk/components/camel-jhc/src/main/java/org/apache/camel/component/jhc/JhcServerEngine.java Thu May 14 12:41:03 2009 @@ -97,31 +97,31 @@ } public void start() throws IOReactorException { - final SocketAddress addr = new InetSocketAddress(port); - ioReactor = new DefaultListeningIOReactor(nbThreads, threadFactory, params); - - final IOEventDispatch ioEventDispatch; - if ("https".equals(protocol)) { - ioEventDispatch = new SSLServerIOEventDispatch(serviceHandler, sslContext, params); - } else { - ioEventDispatch = new DefaultServerIOEventDispatch(serviceHandler, params); - } - runner = new Thread() { - public void run() { - try { - if (!isStarted.getAndSet(true)) { + if (!isStarted.getAndSet(true)) { + final SocketAddress addr = new InetSocketAddress(port); + ioReactor = new DefaultListeningIOReactor(nbThreads, threadFactory, params); + + final IOEventDispatch ioEventDispatch; + if ("https".equals(protocol)) { + ioEventDispatch = new SSLServerIOEventDispatch(serviceHandler, sslContext, params); + } else { + ioEventDispatch = new DefaultServerIOEventDispatch(serviceHandler, params); + } + runner = new Thread() { + public void run() { + try { ioReactor.listen(addr); - ioReactor.execute(ioEventDispatch); + ioReactor.execute(ioEventDispatch); + } catch (InterruptedIOException ex) { + LOG.info("Interrupted"); + } catch (IOException e) { + LOG.warn("I/O error: " + e.getMessage()); } - } catch (InterruptedIOException ex) { - LOG.info("Interrupted"); - } catch (IOException e) { - LOG.warn("I/O error: " + e.getMessage()); + LOG.debug("Shutdown"); } - LOG.debug("Shutdown"); - } - }; - runner.start(); + }; + runner.start(); + } } public void stop() throws IOException {