001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.web.servlet;
018    
019    
020    import org.apache.commons.chain.web.MockEnumeration;
021    import org.apache.commons.chain.web.MockPrincipal;
022    
023    import javax.servlet.RequestDispatcher;
024    import javax.servlet.ServletInputStream;
025    import javax.servlet.http.Cookie;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpSession;
028    import java.io.BufferedReader;
029    import java.security.Principal;
030    import java.util.*;
031    
032    
033    // Mock Object for HttpServletRequest (Version 2.3)
034    public class MockHttpServletRequest implements HttpServletRequest {
035    
036    
037        public MockHttpServletRequest() {
038            super();
039        }
040    
041    
042        public MockHttpServletRequest(HttpSession session) {
043            super();
044            setHttpSession(session);
045        }
046    
047    
048        public MockHttpServletRequest(String contextPath, String servletPath,
049                                      String pathInfo, String queryString) {
050            super();
051            setPathElements(contextPath, servletPath, pathInfo, queryString);
052        }
053    
054    
055    
056        public MockHttpServletRequest(String contextPath, String servletPath,
057                                      String pathInfo, String queryString,
058                                      HttpSession session) {
059            super();
060            setPathElements(contextPath, servletPath, pathInfo, queryString);
061            setHttpSession(session);
062        }
063    
064    
065    
066        protected HashMap attributes = new HashMap();
067        protected String contextPath = null;
068        protected HashMap headers = new HashMap();
069        protected Cookie[] cookies = new Cookie[0];
070        protected Locale locale = null;
071        protected HashMap parameters = new HashMap();
072        protected String pathInfo = null;
073        protected Principal principal = null;
074        protected String queryString = null;
075        protected String servletPath = null;
076        protected HttpSession session = null;
077    
078    
079        // --------------------------------------------------------- Public Methods
080    
081    
082        public void addHeader(String name, String value) {
083            String values[] = (String[]) headers.get(name);
084            if (values == null) {
085                String results[] = new String[] { value };
086                headers.put(name, results);
087                return;
088            }
089            String results[] = new String[values.length + 1];
090            System.arraycopy(values, 0, results, 0, values.length);
091            results[values.length] = value;
092            headers.put(name, results);
093        }
094    
095    
096        public void addParameter(String name, String value) {
097            String values[] = (String[]) parameters.get(name);
098            if (values == null) {
099                String results[] = new String[] { value };
100                parameters.put(name, results);
101                return;
102            }
103            String results[] = new String[values.length + 1];
104            System.arraycopy(values, 0, results, 0, values.length);
105            results[values.length] = value;
106            parameters.put(name, results);
107        }
108    
109        public void addCookie(String name, String value) {
110            addCookie(new Cookie(name, value));
111        }
112    
113        public void addCookie(Cookie cookie) {
114            Cookie[] newValues = new Cookie[cookies.length + 1];
115            System.arraycopy(cookies, 0, newValues, 0, cookies.length);
116            cookies = newValues;
117            cookies[cookies.length - 1] = cookie;
118        }
119    
120    
121        public void setHttpSession(HttpSession session) {
122            this.session = session;
123        }
124    
125    
126        public void setLocale(Locale locale) {
127            this.locale = locale;
128        }
129    
130    
131        public void setPathElements(String contextPath, String servletPath,
132                                    String pathInfo, String queryString) {
133    
134            this.contextPath = contextPath;
135            this.servletPath = servletPath;
136            this.pathInfo = pathInfo;
137            this.queryString = queryString;
138    
139        }
140    
141    
142        public void setUserPrincipal(Principal principal) {
143            this.principal = principal;
144        }
145    
146    
147    
148        // --------------------------------------------- HttpServletRequest Methods
149    
150    
151        public String getAuthType() {
152            throw new UnsupportedOperationException();
153        }
154    
155    
156        public String getContextPath() {
157            return (contextPath);
158        }
159    
160    
161        public Cookie[] getCookies() {
162            return cookies;
163        }
164    
165    
166        public long getDateHeader(String name) {
167            throw new UnsupportedOperationException();
168        }
169    
170    
171        public String getHeader(String name) {
172            String values[] = (String[]) headers.get(name);
173            if (values != null) {
174                return (values[0]);
175            } else {
176                return (null);
177            }
178        }
179    
180    
181        public Enumeration getHeaderNames() {
182            return (new MockEnumeration(headers.keySet().iterator()));
183        }
184    
185    
186        public Enumeration getHeaders(String name) {
187            String headers[] = (String[]) this.headers.get(name);
188            if (headers == null) {
189                headers = new String[0];
190            }
191            List list = new ArrayList();
192            for (int i = 0; i < headers.length; i++) {
193                list.add(headers[i]);
194            }
195            return (new MockEnumeration(list.iterator()));
196        }
197    
198    
199        public int getIntHeader(String name) {
200            throw new UnsupportedOperationException();
201        }
202    
203    
204        public String getMethod() {
205            throw new UnsupportedOperationException();
206        }
207    
208    
209        public String getPathInfo() {
210            return (pathInfo);
211        }
212    
213    
214        public String getPathTranslated() {
215            throw new UnsupportedOperationException();
216        }
217    
218    
219        public String getQueryString() {
220            return (queryString);
221        }
222    
223    
224        public String getRemoteUser() {
225            if (principal != null) {
226                return (principal.getName());
227            } else {
228                return (null);
229            }
230        }
231    
232    
233        public String getRequestedSessionId() {
234            throw new UnsupportedOperationException();
235        }
236    
237    
238        public String getRequestURI() {
239            StringBuffer sb = new StringBuffer();
240            if (contextPath != null) {
241                sb.append(contextPath);
242            }
243            if (servletPath != null) {
244                sb.append(servletPath);
245            }
246            if (pathInfo != null) {
247                sb.append(pathInfo);
248            }
249            if (sb.length() > 0) {
250                return (sb.toString());
251            }
252            throw new UnsupportedOperationException();
253        }
254    
255    
256        public StringBuffer getRequestURL() {
257            throw new UnsupportedOperationException();
258        }
259    
260    
261        public String getServletPath() {
262            return (servletPath);
263        }
264    
265    
266        public HttpSession getSession() {
267            return (getSession(true));
268        }
269    
270    
271        public HttpSession getSession(boolean create) {
272            if (create && (session == null)) {
273                throw new UnsupportedOperationException();
274            }
275            return (session);
276        }
277    
278    
279        public Principal getUserPrincipal() {
280            return (principal);
281        }
282    
283    
284        public boolean isRequestedSessionIdFromCookie() {
285            throw new UnsupportedOperationException();
286        }
287    
288    
289        public boolean isRequestedSessionIdFromUrl() {
290            throw new UnsupportedOperationException();
291        }
292    
293    
294        public boolean isRequestedSessionIdFromURL() {
295            throw new UnsupportedOperationException();
296        }
297    
298    
299        public boolean isRequestedSessionIdValid() {
300            throw new UnsupportedOperationException();
301        }
302    
303    
304        public boolean isUserInRole(String role) {
305            if ((principal != null) && (principal instanceof MockPrincipal)) {
306                return (((MockPrincipal) principal).isUserInRole(role));
307            } else {
308                return (false);
309            }
310        }
311    
312    
313        // ------------------------------------------------- ServletRequest Methods
314    
315    
316        public Object getAttribute(String name) {
317            return (attributes.get(name));
318        }
319    
320    
321        public Enumeration getAttributeNames() {
322            return (new MockEnumeration(attributes.keySet().iterator()));
323        }
324    
325    
326        public String getCharacterEncoding() {
327            throw new UnsupportedOperationException();
328        }
329    
330    
331        public int getContentLength() {
332            throw new UnsupportedOperationException();
333        }
334    
335    
336        public String getContentType() {
337            throw new UnsupportedOperationException();
338        }
339    
340    
341        public ServletInputStream getInputStream() {
342            throw new UnsupportedOperationException();
343        }
344    
345    
346        public Locale getLocale() {
347            return (locale);
348        }
349    
350    
351        public Enumeration getLocales() {
352            throw new UnsupportedOperationException();
353        }
354    
355    
356        public String getLocalAddr() {
357            throw new UnsupportedOperationException();
358        }
359    
360    
361        public String getLocalName() {
362        throw new UnsupportedOperationException();
363        }
364    
365    
366        public int getLocalPort() {
367        throw new UnsupportedOperationException();
368        }
369    
370    
371        public String getParameter(String name) {
372            String values[] = (String[]) parameters.get(name);
373            if (values != null) {
374                return (values[0]);
375            } else {
376                return (null);
377            }
378        }
379    
380    
381        public Map getParameterMap() {
382            return (parameters);
383        }
384    
385    
386        public Enumeration getParameterNames() {
387            return (new MockEnumeration(parameters.keySet().iterator()));
388        }
389    
390    
391        public String[] getParameterValues(String name) {
392            return ((String[]) parameters.get(name));
393        }
394    
395    
396        public String getProtocol() {
397            throw new UnsupportedOperationException();
398        }
399    
400    
401        public BufferedReader getReader() {
402            throw new UnsupportedOperationException();
403        }
404    
405    
406        public String getRealPath(String path) {
407            throw new UnsupportedOperationException();
408        }
409    
410    
411        public String getRemoteAddr() {
412            throw new UnsupportedOperationException();
413        }
414    
415    
416        public String getRemoteHost() {
417            throw new UnsupportedOperationException();
418        }
419    
420    
421        public int getRemotePort() {
422        throw new UnsupportedOperationException();
423        }
424    
425    
426        public RequestDispatcher getRequestDispatcher(String path) {
427            throw new UnsupportedOperationException();
428        }
429    
430    
431        public String getScheme() {
432            return ("http");
433        }
434    
435    
436        public String getServerName() {
437            return ("localhost");
438        }
439    
440    
441        public int getServerPort() {
442            return (8080);
443        }
444    
445    
446        public boolean isSecure() {
447            return (false);
448        }
449    
450    
451        public void removeAttribute(String name) {
452            attributes.remove(name);
453        }
454    
455    
456        public void setAttribute(String name, Object value) {
457            if (value == null) {
458                attributes.remove(name);
459            } else {
460                attributes.put(name, value);
461            }
462        }
463    
464    
465        public void setCharacterEncoding(String name) {
466            throw new UnsupportedOperationException();
467        }
468    
469    
470    }