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