001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.jhc;
018
019 import java.io.IOException;
020
021 import org.apache.camel.AsyncCallback;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Processor;
024 import org.apache.camel.impl.DefaultConsumer;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.apache.http.HttpEntity;
028 import org.apache.http.HttpEntityEnclosingRequest;
029 import org.apache.http.HttpException;
030 import org.apache.http.HttpRequest;
031 import org.apache.http.HttpResponse;
032 import org.apache.http.HttpResponseFactory;
033 import org.apache.http.HttpStatus;
034 import org.apache.http.HttpVersion;
035 import org.apache.http.ProtocolVersion;
036 import org.apache.http.impl.DefaultHttpResponseFactory;
037 import org.apache.http.nio.NHttpConnection;
038 import org.apache.http.nio.protocol.EventListener;
039 import org.apache.http.params.HttpParams;
040 import org.apache.http.protocol.HttpContext;
041 import org.apache.http.protocol.HttpRequestHandler;
042
043 /**
044 * Created by IntelliJ IDEA. User: gnodet Date: Sep 7, 2007 Time: 8:15:54 PM To
045 * change this template use File | Settings | File Templates.
046 */
047 public class JhcConsumer extends DefaultConsumer<JhcExchange> {
048
049 private static final Log LOG = LogFactory.getLog(JhcConsumer.class);
050 private JhcServerEngine engine;
051 private MyHandler handler;
052
053 public JhcConsumer(JhcEndpoint endpoint, Processor processor) {
054 super(endpoint, processor);
055 engine = JhcServerEngineFactory.getJhcServerEngine(endpoint.getParams(), endpoint.getPort(), endpoint
056 .getProtocol());
057 handler = new MyHandler(endpoint.getParams(), endpoint.getPath());
058
059 }
060
061 public JhcEndpoint getEndpoint() {
062 return (JhcEndpoint)super.getEndpoint();
063 }
064
065 protected void doStart() throws Exception {
066 super.doStart();
067 engine.register(handler.getPath() + "*", handler);
068 if (!engine.isStarted()) {
069 engine.start();
070 }
071 }
072
073 protected void doStop() throws Exception {
074 engine.unregister(handler.getPath() + "*");
075 if (engine.getReferenceCounter() == 0) {
076 engine.stop();
077 }
078 super.doStop();
079 }
080
081 class MyHttpRequestHandler implements HttpRequestHandler {
082
083 public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext)
084 throws HttpException, IOException {
085 LOG.debug("handle");
086 }
087 }
088
089 static class EventLogger implements EventListener {
090
091 public void connectionOpen(final NHttpConnection conn) {
092 if (LOG.isDebugEnabled()) {
093 LOG.debug("Connection open: " + conn);
094 }
095 }
096
097 public void connectionTimeout(final NHttpConnection conn) {
098 if (LOG.isDebugEnabled()) {
099 LOG.debug("Connection timed out: " + conn);
100 }
101 }
102
103 public void connectionClosed(final NHttpConnection conn) {
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("Connection closed: " + conn);
106 }
107 }
108
109 public void fatalIOException(final IOException ex, final NHttpConnection conn) {
110 if (LOG.isDebugEnabled()) {
111 LOG.debug("I/O error: " + ex.getMessage());
112 }
113 }
114
115 public void fatalProtocolException(final HttpException ex, final NHttpConnection conn) {
116 if (LOG.isDebugEnabled()) {
117 LOG.debug("HTTP error: " + ex.getMessage());
118 }
119 }
120
121 }
122
123 class MyHandler implements AsyncHttpRequestHandler {
124 private final HttpParams params;
125 private final HttpResponseFactory responseFactory;
126 private final String path;
127
128 public MyHandler(HttpParams params, String path) {
129 this(params, path, new DefaultHttpResponseFactory());
130 }
131
132 public MyHandler(HttpParams params, String path, HttpResponseFactory responseFactory) {
133 this.params = params;
134 this.path = path;
135 this.responseFactory = responseFactory;
136 }
137
138 public String getPath() {
139 return path;
140 }
141
142 public void handle(final HttpRequest request, final HttpContext context,
143 final AsyncResponseHandler handler) throws HttpException, IOException {
144 final Exchange exchange = getEndpoint().createExchange();
145 exchange.getIn().setHeader("http.uri", request.getRequestLine().getUri());
146 if (request instanceof HttpEntityEnclosingRequest) {
147 exchange.getIn().setBody(((HttpEntityEnclosingRequest)request).getEntity());
148 }
149 getAsyncProcessor().process(exchange, new AsyncCallback() {
150 public void done(boolean doneSynchronously) {
151 LOG.debug("handleExchange");
152 // create the default response to this request
153 ProtocolVersion httpVersion = (HttpVersion)request.getRequestLine().getProtocolVersion();
154
155 HttpResponse response = responseFactory.newHttpResponse(
156 httpVersion, HttpStatus.SC_OK, context);
157 response.setParams(params);
158 HttpEntity entity = exchange.getOut().getBody(HttpEntity.class);
159 response.setEntity(entity);
160 response.setParams(getEndpoint().getParams());
161 try {
162 handler.sendResponse(response);
163 } catch (Exception e) {
164 LOG.info(e);
165 }
166 }
167 });
168 }
169
170 public void handle(HttpRequest request, HttpResponse response, HttpContext context)
171 throws HttpException, IOException {
172 // now we just handler the requset async, do nothing here
173 }
174 }
175 }