KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > StrutsRequestWrapper


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
//
15
// You may view the full text here: http://www.apache.org/LICENSE.txt
16

17 package servletunit.struts;
18
19 import org.apache.cactus.ServletURL;
20 import org.apache.cactus.server.HttpServletRequestWrapper;
21
22 import java.util.*;
23
24 /**
25  * A wrapper for the HttpServletRequest class. This is used in
26  * CactusStrutsTestCase so that we can add our own request parameters
27  * outside of the beginXXX and endXXX methods. This allows us to
28  * to use the ActionServlet as a black box, rather than mimic its
29  * behavior as was previously the case.
30  */

31 public class StrutsRequestWrapper extends HttpServletRequestWrapper {
32
33     private String JavaDoc pathInfo;
34     private String JavaDoc servletPath;
35     private Map parameters;
36
37     public StrutsRequestWrapper(HttpServletRequestWrapper request) {
38         super(request,new ServletURL(request.getServerName(),request.getContextPath(),request.getServletPath(),request.getPathInfo(),request.getQueryString()));
39         parameters = new HashMap();
40     }
41
42     public void setPathInfo(String JavaDoc pathInfo) {
43         this.pathInfo = pathInfo;
44     }
45
46     public String JavaDoc getPathInfo() {
47         if (this.pathInfo == null)
48             return super.getPathInfo();
49         else
50             return this.pathInfo;
51     }
52
53     public void setServletPath(String JavaDoc servletPath) {
54         this.servletPath = servletPath;
55     }
56
57     public String JavaDoc getServletPath() {
58         if (this.servletPath == null)
59             return super.getServletPath();
60         else
61             return this.servletPath;
62     }
63
64
65     public String JavaDoc getParameter(String JavaDoc name) {
66         String JavaDoc[] result = getParameterValues(name);
67         if ((result != null) && (result.length > 0)) {
68             return result[0];
69         } else
70             return null;
71     }
72
73     public String JavaDoc[] getParameterValues(String JavaDoc name) {
74         Object JavaDoc result = super.getParameterValues(name);
75         if ((result == null) && (parameters.containsKey(name))) {
76             result = parameters.get(name);
77             if (!(result instanceof String JavaDoc[])) {
78                 String JavaDoc[] resultArray = { result.toString() };
79                 result = resultArray;
80             }
81         }
82         return (String JavaDoc[]) result;
83     }
84
85     public Enumeration getParameterNames() {
86         Enumeration superNames = super.getParameterNames();
87         List nameList = new ArrayList(parameters.keySet());
88         while (superNames.hasMoreElements()) {
89             nameList.add(superNames.nextElement());
90         }
91         return Collections.enumeration(nameList);
92     }
93
94     public void addParameter(String JavaDoc name, String JavaDoc value) {
95         if ((super.getParameter(name) == null) && (name != null) && (value != null))
96             parameters.put(name,value);
97     }
98
99     public void addParameter(String JavaDoc name, String JavaDoc[] values) {
100         if ((super.getParameter(name) == null) && (name != null) && (values != null))
101             parameters.put(name,values);
102     }
103
104     public Map getParameterMap() {
105         Map result = new HashMap();
106         result.putAll(super.getParameterMap());
107         result.putAll(parameters);
108         return result;
109     }
110
111     public void clearRequestParameters() {
112         this.parameters.clear();
113 // super.request.getParameterMap().clear();
114
}
115
116 }
117
118
119
Popular Tags