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 jakarta.servlet.http.HttpSession;
27
28 import java.security.Principal;
29 import java.util.HashMap;
30 import java.util.Map;
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 static synchronized int nextRemotePort() {
50 return ++SimpleHttpRequest.remotePortS;
51 }
52
53
54
55
56 public static synchronized 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
93 super(null);
94 this.remotePort = SimpleHttpRequest.nextRemotePort();
95 }
96
97
98
99
100
101
102
103
104
105 public void addHeader(final String headerName, final String headerValue) {
106 this.headers.put(headerName, headerValue);
107 }
108
109
110
111
112
113
114
115
116
117 public void addParameter(final String parameterName, final String parameterValue) {
118 this.parameters.put(parameterName, parameterValue);
119 }
120
121 @Override
122 public int getContentLength() {
123 return this.content == null ? -1 : this.content.length;
124 }
125
126 @Override
127 public String getHeader(final String headerName) {
128 return this.headers.get(headerName);
129 }
130
131 @Override
132 public String getMethod() {
133 return this.method;
134 }
135
136 @Override
137 public String getParameter(final String parameterName) {
138 return this.parameters.get(parameterName);
139 }
140
141 @Override
142 public String getQueryString() {
143 return this.queryString;
144 }
145
146 @Override
147 public String getRemoteAddr() {
148 return this.remoteAddr;
149 }
150
151 @Override
152 public String getRemoteHost() {
153 return this.remoteHost;
154 }
155
156 @Override
157 public int getRemotePort() {
158 return this.remotePort;
159 }
160
161 @Override
162 public String getRemoteUser() {
163 return this.remoteUser;
164 }
165
166 @Override
167 public String getRequestURI() {
168 return this.requestURI;
169 }
170
171 @Override
172 public HttpSession getSession() {
173 return this.httpSession;
174 }
175
176 @Override
177 public HttpSession getSession(final boolean create) {
178 return this.httpSession;
179 }
180
181 @Override
182 public Principal getUserPrincipal() {
183 return this.principal;
184 }
185
186
187
188
189
190
191
192 public void setContentLength(final int length) {
193 this.content = new byte[length];
194 }
195
196
197
198
199
200
201
202 public void setMethod(final String value) {
203 this.method = value;
204 }
205
206
207
208
209
210
211
212 public void setQueryString(final String queryValue) {
213 this.queryString = queryValue;
214 if (this.queryString != null) {
215 for (final String eachParameter : this.queryString.split("[&]", -1)) {
216 final String[] pair = eachParameter.split("=", -1);
217 final String value = pair.length == 2 ? pair[1] : "";
218 this.addParameter(pair[0], value);
219 }
220 }
221 }
222
223 @Override
224 public void setRemoteAddr(final String value) {
225 this.remoteAddr = value;
226 }
227
228 @Override
229 public void setRemoteHost(final String value) {
230 this.remoteHost = value;
231 }
232
233
234
235
236
237
238
239 public void setRemoteUser(final String value) {
240 this.remoteUser = value;
241 }
242
243
244
245
246
247
248
249 public void setRequestURI(final String value) {
250 this.requestURI = value;
251 }
252
253 @Override
254 public void setUserPrincipal(final Principal value) {
255 this.principal = value;
256 }
257
258 }