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.mock.http;
25  
26  import java.security.Principal;
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletRequestWrapper;
34  import javax.servlet.http.HttpSession;
35  
36  import org.mockito.Mockito;
37  
38  /**
39   * The Class SimpleHttpRequest.
40   */
41  public class SimpleHttpRequest extends HttpServletRequestWrapper {
42  
43      /** The remote port s. */
44      private static int remotePortS = 0;
45  
46      /** The request uri. */
47      private String requestURI;
48  
49      /** The query string. */
50      private String queryString;
51  
52      /** The remote user. */
53      private String remoteUser;
54  
55      /** The method. */
56      private String method = "GET";
57  
58      /** The remote host. */
59      private String remoteHost;
60  
61      /** The remote addr. */
62      private String remoteAddr;
63  
64      /** The remote port. */
65      private int remotePort = -1;
66  
67      /** The headers. */
68      private final Map<String, String> headers = new HashMap<>();
69  
70      /** The parameters. */
71      private final Map<String, String> parameters = new HashMap<>();
72  
73      /** The content. */
74      private byte[] content;
75  
76      /** The session. */
77      private HttpSession session = new SimpleHttpSession();
78  
79      /** The principal. */
80      private Principal principal;
81  
82      /**
83       * Instantiates a new simple http request.
84       */
85      public SimpleHttpRequest() {
86          super(Mockito.mock(HttpServletRequest.class));
87          this.remotePort = SimpleHttpRequest.nextRemotePort();
88      }
89  
90      /**
91       * Next remote port.
92       *
93       * @return the int
94       */
95      public static synchronized int nextRemotePort() {
96          return ++SimpleHttpRequest.remotePortS;
97      }
98  
99      /**
100      * Reset remote port.
101      */
102     public static synchronized void resetRemotePort() {
103         SimpleHttpRequest.remotePortS = 0;
104     }
105 
106     /**
107      * Adds the header.
108      *
109      * @param headerName
110      *            the header name
111      * @param headerValue
112      *            the header value
113      */
114     public void addHeader(final String headerName, final String headerValue) {
115         this.headers.put(headerName, headerValue);
116     }
117 
118     @Override
119     public String getHeader(final String headerName) {
120         return this.headers.get(headerName);
121     }
122 
123     @Override
124     public Enumeration<String> getHeaderNames() {
125         return Collections.enumeration(this.headers.keySet());
126     }
127 
128     @Override
129     public String getMethod() {
130         return this.method;
131     }
132 
133     @Override
134     public int getContentLength() {
135         return this.content == null ? -1 : this.content.length;
136     }
137 
138     @Override
139     public int getRemotePort() {
140         return this.remotePort;
141     }
142 
143     /**
144      * Sets the method.
145      *
146      * @param methodName
147      *            the new method
148      */
149     public void setMethod(final String methodName) {
150         this.method = methodName;
151     }
152 
153     /**
154      * Sets the content length.
155      *
156      * @param length
157      *            the new content length
158      */
159     public void setContentLength(final int length) {
160         this.content = new byte[length];
161     }
162 
163     /**
164      * Sets the remote user.
165      *
166      * @param username
167      *            the new remote user
168      */
169     public void setRemoteUser(final String username) {
170         this.remoteUser = username;
171     }
172 
173     @Override
174     public String getRemoteUser() {
175         return this.remoteUser;
176     }
177 
178     @Override
179     public HttpSession getSession() {
180         return this.session;
181     }
182 
183     @Override
184     public HttpSession getSession(final boolean create) {
185         if (this.session == null && create) {
186             this.session = new SimpleHttpSession();
187         }
188         return this.session;
189     }
190 
191     @Override
192     public String getQueryString() {
193         return this.queryString;
194     }
195 
196     /**
197      * Sets the query string.
198      *
199      * @param query
200      *            the new query string
201      */
202     public void setQueryString(final String query) {
203         this.queryString = query;
204         if (this.queryString != null) {
205             for (final String eachParameter : this.queryString.split("[&]", -1)) {
206                 final String[] pair = eachParameter.split("=", -1);
207                 final String value = pair.length == 2 ? pair[1] : "";
208                 this.addParameter(pair[0], value);
209             }
210         }
211     }
212 
213     /**
214      * Sets the request uri.
215      *
216      * @param uri
217      *            the new request uri
218      */
219     public void setRequestURI(final String uri) {
220         this.requestURI = uri;
221     }
222 
223     @Override
224     public String getRequestURI() {
225         return this.requestURI;
226     }
227 
228     @Override
229     public String getParameter(final String parameterName) {
230         return this.parameters.get(parameterName);
231     }
232 
233     /**
234      * Adds the parameter.
235      *
236      * @param parameterName
237      *            the parameter name
238      * @param parameterValue
239      *            the parameter value
240      */
241     public void addParameter(final String parameterName, final String parameterValue) {
242         this.parameters.put(parameterName, parameterValue);
243     }
244 
245     @Override
246     public String getRemoteHost() {
247         return this.remoteHost;
248     }
249 
250     /**
251      * Sets the remote host.
252      *
253      * @param value
254      *            the new remote host
255      */
256     public void setRemoteHost(final String value) {
257         this.remoteHost = value;
258     }
259 
260     @Override
261     public String getRemoteAddr() {
262         return this.remoteAddr;
263     }
264 
265     /**
266      * Sets the remote addr.
267      *
268      * @param value
269      *            the new remote addr
270      */
271     public void setRemoteAddr(final String value) {
272         this.remoteAddr = value;
273     }
274 
275     @Override
276     public Principal getUserPrincipal() {
277         return this.principal;
278     }
279 
280     /**
281      * Sets the user principal.
282      *
283      * @param value
284      *            the new user principal
285      */
286     public void setUserPrincipal(final Principal value) {
287         this.principal = value;
288     }
289 }