View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.view.facelets.mock;
21  
22  import java.io.BufferedReader;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.io.UnsupportedEncodingException;
26  import java.net.URI;
27  import java.util.Arrays;
28  import java.util.Enumeration;
29  import java.util.Locale;
30  import java.util.Properties;
31  import java.util.Vector;
32  
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.ServletContext;
35  import javax.servlet.ServletInputStream;
36  import javax.servlet.http.Cookie;
37  import javax.servlet.http.HttpSession;
38  
39  import org.apache.myfaces.test.mock.MockHttpSession;
40  
41  /**
42   * 
43   * @author Jacob Hookom
44   * @version $Id: MockHttpServletRequest.java 883907 2009-11-24 22:37:53Z lu4242 $
45   */
46  public class MockHttpServletRequest extends org.apache.myfaces.test.mock.MockHttpServletRequest {
47  
48      private final ServletContext servletContext;
49  
50      private final URI uri;
51  
52      private final String method;
53  
54      private Cookie[] cookies = new Cookie[0];
55  
56      private String servletPath;
57  
58      private HttpSession session;
59  
60      private final Properties param = new Properties();
61  
62      private String characterEncoding = "ISO-8859-1";
63  
64      private String contentType = "text/html";
65  
66      private int contentLength = 0;
67  
68      private String protocol = "HTTP/1.1";
69  
70      private String localName = "localhost";
71  
72      private int localPort = 80;
73  
74      private String remoteAddr = "127.0.0.1";
75  
76      private String remoteHost = "localhost";
77  
78      private Locale locale = Locale.getDefault();
79  
80      private Vector locales = new Vector(Arrays.asList(Locale
81              .getAvailableLocales()));
82      
83      private boolean secure = false;
84      
85      private int remotePort = 1024;
86      
87      private String localAddr = "127.0.0.1";
88  
89      private ServletInputStream inputStream = new MockServletInputStream();
90  
91      public MockHttpServletRequest(ServletContext servletContext, URI uri) {
92          this(servletContext, "GET", uri);
93      }
94  
95      public MockHttpServletRequest(ServletContext servletContext, String uri) {
96          this(servletContext, "GET", uri);
97      }
98  
99      public MockHttpServletRequest(ServletContext servletContext, String method,
100             String uri) {
101         this(servletContext, method, URI.create(uri));
102     }
103 
104     public MockHttpServletRequest(ServletContext servletContext, String method,
105             URI uri) {
106         this.servletContext = servletContext;
107         this.uri = uri;
108         this.method = method;
109 
110         String q = this.uri.getRawQuery();
111         if (q != null) {
112             String[] p = q.split("(&|=)");
113             for (int i = 0; i < p.length; i += 2) {
114                 this.param.put(p[i], p[i + 1]);
115             }
116         }
117     }
118 
119     @Override
120     public String getAuthType() {
121         return BASIC_AUTH;
122     }
123 
124     @Override
125     public Cookie[] getCookies() {
126         return this.cookies;
127     }
128 
129     @Override
130     public String getMethod() {
131         return this.method;
132     }
133 
134     @Override
135     public String getPathInfo() {
136         return this.uri.getPath();
137     }
138 
139     @Override
140     public String getPathTranslated() {
141         return this.servletContext.getRealPath(this.uri.getPath());
142     }
143 
144     @Override
145     public String getContextPath() {
146         return this.uri.getPath();
147     }
148 
149     @Override
150     public String getQueryString() {
151         return this.uri.getQuery();
152     }
153 
154     @Override
155     public String getRequestedSessionId() {
156         return this.getParameter("jsessionid");
157     }
158 
159     @Override
160     public String getRequestURI() {
161         return this.uri.getPath();
162     }
163 
164     @Override
165     public StringBuffer getRequestURL() {
166         return new StringBuffer(this.uri.toString());
167     }
168 
169     @Override
170     public String getServletPath() {
171         return this.servletPath;
172     }
173 
174     @Override
175     public HttpSession getSession(boolean create) {
176         if (this.session == null && create) {
177             this.session = new MockHttpSession(this.servletContext);
178         }
179         return this.session;
180     }
181 
182     @Override
183     public HttpSession getSession() {
184         return this.getSession(true);
185     }
186 
187     @Override
188     public String getCharacterEncoding() {
189         return this.characterEncoding;
190     }
191 
192     @Override
193     public void setCharacterEncoding(String characterEncoding){
194         this.characterEncoding = characterEncoding;
195     }
196     
197     @Override
198     public int getContentLength() {
199         return this.contentLength;
200     }
201     
202     @Override
203     public String getContentType() {
204         return this.contentType;
205     }
206 
207     @Override
208     public ServletInputStream getInputStream(){
209         return this.inputStream;
210     }
211     
212     public void setParameter(String name, String value) {
213     	this.getParameterMap().put(name, value);
214     }
215 
216     @Override
217     public String getProtocol() {
218         return this.protocol;
219     }
220 
221     @Override
222     public String getScheme() {
223         return this.uri.getScheme();
224     }
225 
226     @Override
227     public String getServerName() {
228         return this.localName;
229     }
230 
231     @Override
232     public int getServerPort() {
233         return this.localPort;
234     }
235 
236     @Override
237     public BufferedReader getReader(){
238         if (this.inputStream != null) {
239             try{
240                 Reader sourceReader = (this.characterEncoding != null) ? new InputStreamReader(
241                         this.inputStream, this.characterEncoding)
242                         : new InputStreamReader(this.inputStream);
243                 return new BufferedReader(sourceReader);
244             }
245             catch(UnsupportedEncodingException e)
246             {
247                 throw new RuntimeException(e);
248             }
249         } else {
250             return null;
251         }
252     }
253 
254     @Override
255     public String getRemoteAddr() {
256         return this.remoteAddr;
257     }
258 
259     @Override
260     public String getRemoteHost() {
261         return this.remoteHost;
262     }
263 
264     @Override
265     public Locale getLocale() {
266         return this.locale;
267     }
268 
269     @Override
270     public Enumeration getLocales() {
271         return this.locales.elements();
272     }
273 
274     @Override
275     public boolean isSecure() {
276         return this.secure;
277     }
278 
279     @Override
280     public RequestDispatcher getRequestDispatcher(String path) {
281         return this.servletContext.getRequestDispatcher(path);
282     }
283 
284     @Override
285     public String getRealPath(String path) {
286         return this.servletContext.getRealPath(path);
287     }
288 
289     @Override
290     public int getRemotePort() {
291         return this.remotePort;
292     }
293 
294     @Override
295     public String getLocalName() {
296         return this.localName;
297     }
298 
299     @Override
300     public String getLocalAddr() {
301         return this.localAddr;
302     }
303 
304     @Override
305     public int getLocalPort() {
306         return this.localPort;
307     }
308 
309 }