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 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   * 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          super();
93          this.remotePort = SimpleHttpRequest.nextRemotePort();
94      }
95  
96      /**
97       * Adds the header.
98       *
99       * @param headerName
100      *            the header name
101      * @param headerValue
102      *            the header value
103      */
104     public void addHeader(final String headerName, final String headerValue) {
105         this.headers.put(headerName, headerValue);
106     }
107 
108     /**
109      * Adds the parameter.
110      *
111      * @param parameterName
112      *            the parameter name
113      * @param parameterValue
114      *            the parameter value
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      * Sets the content length.
187      *
188      * @param length
189      *            the new content length
190      */
191     public void setContentLength(final int length) {
192         this.content = new byte[length];
193     }
194 
195     /**
196      * Sets the method.
197      *
198      * @param value
199      *            the new method
200      */
201     public void setMethod(final String value) {
202         this.method = value;
203     }
204 
205     /**
206      * Sets the query string.
207      *
208      * @param queryValue
209      *            the new query string
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      * Sets the remote user.
234      *
235      * @param value
236      *            the new remote user
237      */
238     public void setRemoteUser(final String value) {
239         this.remoteUser = value;
240     }
241 
242     /**
243      * Sets the request uri.
244      *
245      * @param value
246      *            the new request uri
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 }