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.clustering.ClusteredInvocation;
026    import org.apache.geronimo.clustering.ClusteredInvocationException;
027    import org.apache.geronimo.jetty6.AbstractPreHandler;
028    import org.mortbay.jetty.HttpException;
029    
030    /**
031     * @version $Rev$ $Date$
032     */
033    public abstract class AbstractClusteredPreHandler extends AbstractPreHandler {
034    
035        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
036                throws IOException, ServletException {
037            ClusteredInvocation invocation = newClusteredInvocation(target, request, response, dispatch);
038            try {
039                invocation.invoke();
040            } catch (ClusteredInvocationException e) {
041                Throwable cause = e.getCause();
042                if (cause instanceof HttpException) {
043                    throw (HttpException) cause;
044                } else if (cause instanceof IOException) {
045                    throw (IOException) cause;
046                } else {
047                    throw (IOException) new IOException().initCause(cause);
048                }
049            }
050        }
051    
052        protected abstract ClusteredInvocation newClusteredInvocation(String target,
053                HttpServletRequest request, HttpServletResponse response, int dispatch);
054    
055    
056        protected abstract class WebClusteredInvocation implements ClusteredInvocation {
057            protected final String target;
058            protected final HttpServletRequest request;
059            protected final HttpServletResponse response;
060            protected final int dispatch;
061    
062            protected WebClusteredInvocation(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) {
063                this.target = target;
064                this.request = request;
065                this.response = response;
066                this.dispatch = dispatch;
067            }
068    
069            protected void invokeLocally() throws ClusteredInvocationException {
070                try {
071                    next.handle(target, request, response, dispatch);
072                } catch (IOException e) {
073                    throw new ClusteredInvocationException(e);
074                } catch (ServletException e) {
075                    //WHAT DO I DO HERE???
076                    throw new ClusteredInvocationException(e);
077                }
078            }
079    
080            public String getRequestedSessionId() {
081                return request.getRequestedSessionId();
082            }
083        }
084        
085    }