View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.geronimo.jetty.cluster;
19  
20  import java.io.IOException;
21  
22  import org.apache.geronimo.clustering.ClusteredInvocation;
23  import org.apache.geronimo.clustering.ClusteredInvocationException;
24  import org.apache.geronimo.jetty.GeronimoServletHttpRequest;
25  import org.apache.geronimo.jetty.HandleInterceptor;
26  import org.mortbay.http.HttpException;
27  import org.mortbay.http.HttpRequest;
28  import org.mortbay.http.HttpResponse;
29  
30  /**
31   *
32   * @version $Rev$ $Date$
33   */
34  public abstract class AbstractClusteredHandleInterceptor implements HandleInterceptor {
35  
36      public void handle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse,
37              HandleInterceptor end) throws HttpException, IOException {
38          ClusteredInvocation invocation = 
39              newClusteredInvocation(pathInContext, pathParams, httpRequest, httpResponse, end);
40          try {
41              invocation.invoke();
42          } catch (ClusteredInvocationException e) {
43              Throwable cause = e.getCause();
44              if (cause instanceof HttpException) {
45                  throw (HttpException) cause;
46              } else if (cause instanceof IOException) {
47                  throw (IOException) cause;
48              } else {
49                  throw (IOException) new IOException().initCause(cause);
50              }
51          }
52      }
53      
54      protected abstract ClusteredInvocation newClusteredInvocation(String pathInContext, String pathParams,
55              HttpRequest request, HttpResponse response, HandleInterceptor end);
56      
57      protected abstract class WebClusteredInvocation implements ClusteredInvocation {
58          protected final String pathInContext; 
59          protected final String pathParams;
60          protected final HttpRequest request;
61          protected final HttpResponse response;
62          protected final HandleInterceptor end;
63          
64          public WebClusteredInvocation(String pathInContext, String pathParams, HttpRequest request,
65                  HttpResponse response, HandleInterceptor end) {
66              this.pathInContext = pathInContext;
67              this.pathParams = pathParams;
68              this.request = request;
69              this.response = response;
70              this.end = end;
71  
72              GeronimoServletHttpRequest servletHttpRequest = (GeronimoServletHttpRequest) request.getWrapper();
73              servletHttpRequest.setRequestedSessionId(pathParams);
74          }
75  
76          protected void invokeLocally() throws ClusteredInvocationException {
77              try {
78                  end.handle(pathInContext, pathParams, request, response, null);
79              } catch (IOException e) {
80                  throw new ClusteredInvocationException(e);
81              }
82          }
83  
84          public String getRequestedSessionId() {
85              GeronimoServletHttpRequest servletHttpRequest = (GeronimoServletHttpRequest) request.getWrapper();
86              return servletHttpRequest.getRequestedSessionId();
87          }
88      }
89  }