View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * Simple HTTP Request.
38   */
39  public class SimpleHttpRequest extends Request {
40  
41      /** The remote port s. */
42      private static int remotePortS;
43  
44      /**
45       * Next remote port.
46       *
47       * @return the int
48       */
49      public synchronized static int nextRemotePort() {
50          return ++SimpleHttpRequest.remotePortS;
51      }
52  
53      /**
54       * Reset remote port.
55       */
56      public synchronized static void resetRemotePort() {
57          SimpleHttpRequest.remotePortS = 0;
58      }
59  
60      /** The request uri. */
61      private String requestURI;
62  
63      /** The query string. */
64      private String queryString;
65  
66      /** The remote user. */
67      private String remoteUser;
68  
69      /** The method. */
70      private String method = "GET";
71  
72      /** The headers. */
73      private final Map<String, String> headers = new HashMap<>();
74  
75      /** The parameters. */
76      private final Map<String, String> parameters = new HashMap<>();
77  
78      /** The content. */
79      private byte[] content;
80  
81      /** The http session. */
82      @Mocked
83      private HttpSession httpSession;
84  
85      /** The principal. */
86      private Principal principal;
87  
88      /**
89       * Instantiates a new simple http request.
90       */
91      public SimpleHttpRequest() {
92          // Tomcat notes that null on connector here may be ok for testing
93          super(null);
94          this.remotePort = SimpleHttpRequest.nextRemotePort();
95      }
96  
97      /**
98       * Adds the header.
99       *
100      * @param headerName
101      *            the header name
102      * @param headerValue
103      *            the header value
104      */
105     public void addHeader(final String headerName, final String headerValue) {
106         this.headers.put(headerName, headerValue);
107     }
108 
109     /**
110      * Adds the parameter.
111      *
112      * @param parameterName
113      *            the parameter name
114      * @param parameterValue
115      *            the parameter value
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      * Sets the content length.
188      *
189      * @param length
190      *            the new content length
191      */
192     public void setContentLength(final int length) {
193         this.content = new byte[length];
194     }
195 
196     /**
197      * Sets the method.
198      *
199      * @param value
200      *            the new method
201      */
202     public void setMethod(final String value) {
203         this.method = value;
204     }
205 
206     /**
207      * Sets the query string.
208      *
209      * @param queryValue
210      *            the new query string
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      * Sets the remote user.
235      *
236      * @param value
237      *            the new remote user
238      */
239     public void setRemoteUser(final String value) {
240         this.remoteUser = value;
241     }
242 
243     /**
244      * Sets the request uri.
245      *
246      * @param value
247      *            the new request uri
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 }