1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.apache.catalina;
25
26 import java.security.Principal;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import javax.servlet.http.HttpSession;
31
32 import mockit.Mocked;
33
34 import org.apache.catalina.connector.Request;
35
36
37
38
39 public class SimpleHttpRequest extends Request {
40
41
42 private static int remotePortS;
43
44
45
46
47
48
49 public synchronized static int nextRemotePort() {
50 return ++SimpleHttpRequest.remotePortS;
51 }
52
53
54
55
56 public synchronized static void resetRemotePort() {
57 SimpleHttpRequest.remotePortS = 0;
58 }
59
60
61 private String requestURI;
62
63
64 private String queryString;
65
66
67 private String remoteUser;
68
69
70 private String method = "GET";
71
72
73 private final Map<String, String> headers = new HashMap<>();
74
75
76 private final Map<String, String> parameters = new HashMap<>();
77
78
79 private byte[] content;
80
81
82 @Mocked
83 private HttpSession httpSession;
84
85
86 private Principal principal;
87
88
89
90
91 public SimpleHttpRequest() {
92 super();
93 this.remotePort = SimpleHttpRequest.nextRemotePort();
94 }
95
96
97
98
99
100
101
102
103
104 public void addHeader(final String headerName, final String headerValue) {
105 this.headers.put(headerName, headerValue);
106 }
107
108
109
110
111
112
113
114
115
116 public void addParameter(final String parameterName, final String parameterValue) {
117 this.parameters.put(parameterName, parameterValue);
118 }
119
120 @Override
121 public int getContentLength() {
122 return this.content == null ? -1 : this.content.length;
123 }
124
125 @Override
126 public String getHeader(final String headerName) {
127 return this.headers.get(headerName);
128 }
129
130 @Override
131 public String getMethod() {
132 return this.method;
133 }
134
135 @Override
136 public String getParameter(final String parameterName) {
137 return this.parameters.get(parameterName);
138 }
139
140 @Override
141 public String getQueryString() {
142 return this.queryString;
143 }
144
145 @Override
146 public String getRemoteAddr() {
147 return this.remoteAddr;
148 }
149
150 @Override
151 public String getRemoteHost() {
152 return this.remoteHost;
153 }
154
155 @Override
156 public int getRemotePort() {
157 return this.remotePort;
158 }
159
160 @Override
161 public String getRemoteUser() {
162 return this.remoteUser;
163 }
164
165 @Override
166 public String getRequestURI() {
167 return this.requestURI;
168 }
169
170 @Override
171 public HttpSession getSession() {
172 return this.httpSession;
173 }
174
175 @Override
176 public HttpSession getSession(final boolean create) {
177 return this.httpSession;
178 }
179
180 @Override
181 public Principal getUserPrincipal() {
182 return this.principal;
183 }
184
185
186
187
188
189
190
191 public void setContentLength(final int length) {
192 this.content = new byte[length];
193 }
194
195
196
197
198
199
200
201 public void setMethod(final String value) {
202 this.method = value;
203 }
204
205
206
207
208
209
210
211 public void setQueryString(final String queryValue) {
212 this.queryString = queryValue;
213 if (this.queryString != null) {
214 for (final String eachParameter : this.queryString.split("[&]", -1)) {
215 final String[] pair = eachParameter.split("=", -1);
216 final String value = pair.length == 2 ? pair[1] : "";
217 this.addParameter(pair[0], value);
218 }
219 }
220 }
221
222 @Override
223 public void setRemoteAddr(final String value) {
224 this.remoteAddr = value;
225 }
226
227 @Override
228 public void setRemoteHost(final String value) {
229 this.remoteHost = value;
230 }
231
232
233
234
235
236
237
238 public void setRemoteUser(final String value) {
239 this.remoteUser = value;
240 }
241
242
243
244
245
246
247
248 public void setRequestURI(final String value) {
249 this.requestURI = value;
250 }
251
252 @Override
253 public void setUserPrincipal(final Principal value) {
254 this.principal = value;
255 }
256
257 }