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