View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.intercept.web;
17  
18  import javax.servlet.FilterChain;
19  import javax.servlet.ServletRequest;
20  import javax.servlet.ServletResponse;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  
25  /***
26   * Holds objects associated with a HTTP filter.
27   * 
28   * <P>
29   * Guarantees the request and response are instances of
30   * <code>HttpServletRequest</code> and <code>HttpServletResponse</code>, and
31   * that there are no <code>null</code> objects.
32   * </p>
33   * 
34   * <P>
35   * Required so that security system classes can obtain access to the filter
36   * environment, as well as the request and response.
37   * </p>
38   *
39   * @author Ben Alex
40   * @author colin sampaleanu
41   * @version $Id: FilterInvocation.java,v 1.6 2005/11/17 00:55:50 benalex Exp $
42   */
43  public class FilterInvocation {
44      //~ Instance fields ========================================================
45  
46      private FilterChain chain;
47      private ServletRequest request;
48      private ServletResponse response;
49  
50      //~ Constructors ===========================================================
51  
52      public FilterInvocation(ServletRequest request, ServletResponse response,
53          FilterChain chain) {
54          if ((request == null) || (response == null) || (chain == null)) {
55              throw new IllegalArgumentException(
56                  "Cannot pass null values to constructor");
57          }
58  
59          if (!(request instanceof HttpServletRequest)) {
60              throw new IllegalArgumentException(
61                  "Can only process HttpServletRequest");
62          }
63  
64          if (!(response instanceof HttpServletResponse)) {
65              throw new IllegalArgumentException(
66                  "Can only process HttpServletResponse");
67          }
68  
69          this.request = request;
70          this.response = response;
71          this.chain = chain;
72      }
73  
74      protected FilterInvocation() {
75          throw new IllegalArgumentException("Cannot use default constructor");
76      }
77  
78      //~ Methods ================================================================
79  
80      public FilterChain getChain() {
81          return chain;
82      }
83  
84      /***
85       * Indicates the URL that the user agent used for this request.
86       * 
87       * <P>
88       * The returned URL does <b>not</b> reflect the port number determined from
89       * a {@link org.acegisecurity.util.PortResolver}.
90       * </p>
91       *
92       * @return the full URL of this request
93       */
94      public String getFullRequestUrl() {
95          return getHttpRequest().getScheme() + "://"
96          + getHttpRequest().getServerName() + ":"
97          + getHttpRequest().getServerPort() + getHttpRequest().getContextPath()
98          + getRequestUrl();
99      }
100 
101     public HttpServletRequest getHttpRequest() {
102         return (HttpServletRequest) request;
103     }
104 
105     public HttpServletResponse getHttpResponse() {
106         return (HttpServletResponse) response;
107     }
108 
109     public ServletRequest getRequest() {
110         return request;
111     }
112 
113     public String getRequestUrl() {
114         String pathInfo = getHttpRequest().getPathInfo();
115         String queryString = getHttpRequest().getQueryString();
116 
117         String uri = getHttpRequest().getServletPath();
118 
119         if (uri == null) {
120             uri = getHttpRequest().getRequestURI();
121             uri = uri.substring(getHttpRequest().getContextPath().length());
122         }
123 
124         return uri + ((pathInfo == null) ? "" : pathInfo)
125         + ((queryString == null) ? "" : ("?" + queryString));
126     }
127 
128     public ServletResponse getResponse() {
129         return response;
130     }
131 
132     public String toString() {
133         return "FilterInvocation: URL: " + getRequestUrl();
134     }
135 }