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.geronimo.jetty6.cluster;
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.geronimo.jetty6.AbstractPreHandler;
026    import org.apache.geronimo.jetty6.PreHandler;
027    import org.mortbay.jetty.servlet.SessionHandler;
028    
029    /**
030     *
031     * @version $Rev$ $Date$
032     */
033    public class ClusteredSessionHandler extends SessionHandler {
034        private final PreHandler chainedHandler;
035        
036        public ClusteredSessionHandler(ClusteredSessionManager sessionManager, PreHandler chainedHandler) {
037            if (null == chainedHandler) {
038                throw new IllegalArgumentException("chainedHandler is required");
039            }
040            this.chainedHandler = chainedHandler;
041            chainedHandler.setNextHandler(new ActualHandler());
042    
043            setSessionManager(sessionManager);
044        }
045        
046        @Override
047        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
048                throws IOException, ServletException {
049            setRequestedId(request, dispatch);
050            try {
051                chainedHandler.handle(target, request, response, dispatch);
052            } catch (ServletException e) {
053                throw (IOException) new IOException().initCause(e);
054            }
055        }
056        
057        protected void doHandle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
058                throws IOException, ServletException {
059            super.handle(target, request, response, dispatch);
060        }
061    
062        private class ActualHandler extends AbstractPreHandler {
063    
064            public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
065                    throws IOException, ServletException {
066                doHandle(target, request, response, dispatch);
067            }
068        }
069    
070    }