1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.service;
19   
20   import java.io.IOException;
21   
22   import javax.servlet.Filter;
23   import javax.servlet.ServletException;
24   import javax.servlet.ServletRequest;
25   import javax.servlet.http.HttpServletRequest;
26   import javax.servlet.http.HttpServletResponse;
27   import javax.servlet.http.HttpServletResponseWrapper;
28   
29   /**
30    * is a common base for {@code Filter}s in <em>pulse</em>.
31    * 
32    * @author Thomas Weber
33    * @version $Revision: 1392 $
34    */
35   public abstract class AbstractPulseFilter implements Filter {
36   
37       /**
38        * default constructor.
39        */
40       public AbstractPulseFilter() {
41           super();
42       }
43   
44       /**
45        * get the base path (HTTP) to the web app.
46        * 
47        * @param req
48        *            the current request
49        * @return the web apps base URI as a string (HTTP)
50        * @throws ServletException
51        *             never
52        */
53       public final String getWebAppBasePath(final ServletRequest req)
54               throws ServletException {
55           HttpServletRequest request = (HttpServletRequest) req;
56           return request.getContextPath();
57       }
58   
59       /**
60        * a wrapped response, giving access to the status.
61        */
62       public static class WrappedResponse extends HttpServletResponseWrapper {
63   
64           /**
65            * the status.
66            */
67           private int status = 0;
68   
69           /**
70            * creates the wrapped response.
71            * 
72            * @param response
73            *            the response to be wrapped
74            */
75           public WrappedResponse(final HttpServletResponse response) {
76               super(response);
77           }
78   
79           /**
80            * returns the status of the response.
81            * 
82            * @return the status
83            */
84           public final int getStatus() {
85               return this.status;
86           }
87   
88           /**
89            * @param sc
90            *            the status-code
91            * @throws IOException
92            *             on errors
93            * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int)
94            */
95           @Override
96           public final void sendError(final int sc) throws IOException {
97               this.status = sc;
98               super.sendError(sc);
99           }
100  
101          /**
102           * @param sc
103           *            the status code
104           * @param msg
105           *            the message
106           * @throws IOException
107           *             on errors
108           * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int,
109           *      java.lang.String)
110           */
111          @Override
112          public final void sendError(final int sc, final String msg)
113                  throws IOException {
114              this.status = sc;
115              super.sendError(sc, msg);
116          }
117  
118          /**
119           * @param sc
120           *            the status code
121           * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
122           */
123          @Override
124          public final void setStatus(final int sc) {
125              this.status = sc;
126              super.setStatus(sc);
127          }
128  
129          /**
130           * @param sc
131           *            the status-code
132           * @param sm
133           *            the message
134           * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int,
135           *      java.lang.String)
136           */
137          @Override
138          @Deprecated
139          public final void setStatus(final int sc, final String sm) {
140              this.status = sc;
141              super.setStatus(sc, sm);
142          }
143  
144          /**
145           * @param url
146           *            the string to be encoded as an URL
147           * @return the encoded string
148           * @see javax.servlet.http.HttpServletResponseWrapper#encodeRedirectUrl(java.lang.String)
149           */
150          @Override
151          @Deprecated
152          public String encodeRedirectUrl(final String url) {
153              return super.encodeRedirectUrl(url); // NOPMD
154          }
155  
156          /**
157           * @param url
158           *            the string to be encoded as an URL
159           * @return the encoded string
160           * @see javax.servlet.http.HttpServletResponseWrapper#encodeUrl(java.lang.String)
161           */
162          @Override
163          @Deprecated
164          public String encodeUrl(final String url) {
165              return super.encodeUrl(url); // NOPMD
166          }
167      }
168  
169  }
170