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.jetty;
018    
019    import java.io.IOException;
020    
021    import javax.servlet.ServletException;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.component.http.CamelServlet;
027    import org.apache.camel.component.http.HttpConsumer;
028    import org.mortbay.util.ajax.Continuation;
029    import org.mortbay.util.ajax.ContinuationSupport;
030    
031    /**
032     * @version $Revision: 816070 $
033     */
034    public class CamelContinuationServlet extends CamelServlet {
035    
036        // TODO: should use the new Async API and allow end users to define if they want Jetty continuation support or not
037    
038        private static final long serialVersionUID = 1L;
039            
040        @Override
041        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
042            try {
043                // Is there a consumer registered for the request.
044                HttpConsumer consumer = resolve(request);
045                if (consumer == null) {
046                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
047                    return;
048                }
049    
050                final Continuation continuation = ContinuationSupport.getContinuation(request, null);
051                if (continuation.isNew()) {
052                    // Have the camel process the HTTP exchange.
053                    // final DefaultExchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
054                    // exchange.setProperty(HttpConstants.SERVLET_REQUEST, request);
055                    // exchange.setProperty(HttpConstants.SERVLET_RESPONSE, response);
056                    // exchange.setIn(new HttpMessage(exchange, request));
057                    // boolean sync = consumer.getAsyncProcessor().process(exchange, new AsyncCallback() {
058                    //     public void done(boolean sync) {
059                    //        if (sync) {
060                    //            return;
061                    //        }
062                    //        continuation.setObject(exchange);
063                    //        continuation.resume();
064                    //    }
065                    //});
066    
067                    //if (!sync) {
068                        // Wait for the exchange to get processed.
069                        // This might block until it completes or it might return via an exception and
070                        // then this method is re-invoked once the the exchange has finished processing
071                    //    continuation.suspend(0);
072                    //}
073    
074                    // HC: The getBinding() is interesting because it illustrates the
075                    // impedance miss-match between HTTP's stream oriented protocol, and
076                    // Camels more message oriented protocol exchanges.
077    
078                    // now lets output to the response
079                    //consumer.getBinding().writeResponse(exchange, response);
080                    return;
081                }
082    
083                if (continuation.isResumed()) {
084                    Exchange exchange = (Exchange)continuation.getObject();
085                    // now lets output to the response
086                    consumer.getBinding().writeResponse(exchange, response);
087                    return;
088                }
089            } catch (Exception e) {
090                throw new ServletException(e);
091            }
092        }
093    }