View Javadoc

1   package net.sourceforge.blogentis.slide;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: MutablePrincipalHttpRequestWrapper.java,v 1.1 2004/10/22 17:34:16 tassos Exp $
23  //
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.security.Principal;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.servlet.ServletInputStream;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletRequestWrapper;
35  
36  class MutablePrincipalHttpRequestWrapper
37          extends HttpServletRequestWrapper {
38  
39      private static class InputStreamWrapper
40              extends ServletInputStream {
41  
42          protected InputStream in = null;
43  
44          public InputStreamWrapper(InputStream in) {
45              this.in = in;
46          }
47          public int available()
48                  throws IOException {
49              return in.available();
50          }
51  
52          public void close()
53                  throws IOException {
54              in.close();
55          }
56  
57          public boolean equals(Object obj) {
58              return in.equals(obj);
59          }
60  
61          public int hashCode() {
62              return in.hashCode();
63          }
64  
65          public void mark(int readlimit) {
66              in.mark(readlimit);
67          }
68  
69          public boolean markSupported() {
70              return in.markSupported();
71          }
72  
73          public int read()
74                  throws IOException {
75              return in.read();
76          }
77  
78          public int read(byte[] b)
79                  throws IOException {
80              return in.read(b);
81          }
82  
83          public int read(byte[] b, int off, int len)
84                  throws IOException {
85              return in.read(b, off, len);
86          }
87  
88          public void reset()
89                  throws IOException {
90              in.reset();
91          }
92  
93          public long skip(long n)
94                  throws IOException {
95              return in.skip(n);
96          }
97  
98      }
99      
100     protected int contentLength = -1;
101     protected String contentType = null;
102     protected String contextPath = null;
103     protected Map headers = new HashMap();
104     protected ServletInputStream inputStream = null;
105     protected String method = null;
106 
107     protected Principal p;
108     protected String pathInfo = null;
109     protected HttpServletRequest req;
110     protected String servletPath = null;
111 
112     public MutablePrincipalHttpRequestWrapper(HttpServletRequest request,
113                                               Principal p) {
114         super(request);
115         this.p = p;
116         this.req = request;
117     }
118 
119     public int getContentLength() {
120         if (contentLength >= 0)
121             return contentLength;
122         return super.getContentLength();
123     }
124 
125     public String getContentType() {
126         if (contentType != null)
127             return contentType;
128         return super.getContentType();
129     }
130 
131     public String getContextPath() {
132         if (contextPath != null)
133             return contextPath;
134         return super.getContextPath();
135     }
136 
137     public String getHeader(String arg0) {
138         String v = (String)headers.get(arg0);
139         if (v != null)
140             return v;
141         return super.getHeader(arg0);
142     }
143 
144     public Enumeration getHeaderNames() {
145         return super.getHeaderNames();
146     }
147 
148     public Enumeration getHeaders(String arg0) {
149         return super.getHeaders(arg0);
150     }
151 
152     public ServletInputStream getInputStream()
153             throws IOException {
154         if (inputStream != null)
155             return inputStream;
156         return super.getInputStream();
157     }
158 
159     public int getIntHeader(String arg0) {
160         return Integer.parseInt(getHeader(arg0));
161     }
162 
163     public String getMethod() {
164         if (method != null)
165             return method;
166         return req.getMethod();
167     }
168 
169     public String getPathInfo() {
170         if (pathInfo != null)
171             return pathInfo;
172         return super.getContextPath();
173     }
174 
175     public String getServletPath() {
176         if (servletPath != null)
177             return servletPath;
178         return super.getServletPath();
179     }
180 
181     public Principal getUserPrincipal() {
182         if (p != null)
183             return p;
184         return req.getUserPrincipal();
185     }
186 
187     public void setContentLength(int contentLength) {
188         this.contentLength = contentLength;
189     }
190 
191     public void setContentType(String contentType) {
192         this.contentType = contentType;
193     }
194 
195     public void setContextPath(String contextPath) {
196         this.contextPath = contextPath;
197     }
198 
199     public void setHeader(String header, String value) {
200         headers.put(header, value);
201     }
202 
203     public void setInputStream(InputStream is) {
204         if (is == null)
205             this.inputStream = null;
206         else
207             this.inputStream = new InputStreamWrapper(is);
208     }
209 
210     public void setMethod(String method) {
211         this.method = method;
212     }
213 
214     public void setPathInfo(String pathInfo) {
215         this.pathInfo = pathInfo;
216     }
217 
218     public void setServletPath(String servletPath) {
219         this.servletPath = servletPath;
220     }
221 
222     public void setUserPrincipal(Principal u) {
223         this.p = u;
224     }
225 }