001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.jetty.cluster;
019    
020    import java.io.IOException;
021    
022    import org.apache.geronimo.clustering.ClusteredInvocation;
023    import org.apache.geronimo.clustering.ClusteredInvocationException;
024    import org.apache.geronimo.jetty.GeronimoServletHttpRequest;
025    import org.apache.geronimo.jetty.HandleInterceptor;
026    import org.mortbay.http.HttpException;
027    import org.mortbay.http.HttpRequest;
028    import org.mortbay.http.HttpResponse;
029    
030    /**
031     *
032     * @version $Rev$ $Date$
033     */
034    public abstract class AbstractClusteredHandleInterceptor implements HandleInterceptor {
035    
036        public void handle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse,
037                HandleInterceptor end) throws HttpException, IOException {
038            ClusteredInvocation invocation = 
039                newClusteredInvocation(pathInContext, pathParams, httpRequest, httpResponse, end);
040            try {
041                invocation.invoke();
042            } catch (ClusteredInvocationException e) {
043                Throwable cause = e.getCause();
044                if (cause instanceof HttpException) {
045                    throw (HttpException) cause;
046                } else if (cause instanceof IOException) {
047                    throw (IOException) cause;
048                } else {
049                    throw (IOException) new IOException().initCause(cause);
050                }
051            }
052        }
053        
054        protected abstract ClusteredInvocation newClusteredInvocation(String pathInContext, String pathParams,
055                HttpRequest request, HttpResponse response, HandleInterceptor end);
056        
057        protected abstract class WebClusteredInvocation implements ClusteredInvocation {
058            protected final String pathInContext; 
059            protected final String pathParams;
060            protected final HttpRequest request;
061            protected final HttpResponse response;
062            protected final HandleInterceptor end;
063            
064            public WebClusteredInvocation(String pathInContext, String pathParams, HttpRequest request,
065                    HttpResponse response, HandleInterceptor end) {
066                this.pathInContext = pathInContext;
067                this.pathParams = pathParams;
068                this.request = request;
069                this.response = response;
070                this.end = end;
071    
072                GeronimoServletHttpRequest servletHttpRequest = (GeronimoServletHttpRequest) request.getWrapper();
073                servletHttpRequest.setRequestedSessionId(pathParams);
074            }
075    
076            protected void invokeLocally() throws ClusteredInvocationException {
077                try {
078                    end.handle(pathInContext, pathParams, request, response, null);
079                } catch (IOException e) {
080                    throw new ClusteredInvocationException(e);
081                }
082            }
083    
084            public String getRequestedSessionId() {
085                GeronimoServletHttpRequest servletHttpRequest = (GeronimoServletHttpRequest) request.getWrapper();
086                return servletHttpRequest.getRequestedSessionId();
087            }
088        }
089    }