View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.mock.http;
15  
16  import java.security.Principal;
17  import java.util.Enumeration;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletRequestWrapper;
23  import javax.servlet.http.HttpSession;
24  
25  import org.mockito.Mockito;
26  
27  import com.google.common.collect.Iterators;
28  
29  /**
30   * The Class SimpleHttpRequest.
31   *
32   * @author dblock[at]dblock[dot]org
33   */
34  public class SimpleHttpRequest extends HttpServletRequestWrapper {
35  
36      /** The remote port s. */
37      private static int          remotePortS = 0;
38  
39      /** The request uri. */
40      private String              requestURI;
41      
42      /** The query string. */
43      private String              queryString;
44      
45      /** The remote user. */
46      private String              remoteUser;
47      
48      /** The method. */
49      private String              method      = "GET";
50      
51      /** The remote host. */
52      private String              remoteHost;
53      
54      /** The remote addr. */
55      private String              remoteAddr;
56      
57      /** The remote port. */
58      private int                 remotePort  = -1;
59      
60      /** The headers. */
61      private final Map<String, String> headers     = new HashMap<String, String>();
62      
63      /** The parameters. */
64      private final Map<String, String> parameters  = new HashMap<String, String>();
65      
66      /** The content. */
67      private byte[]              content;
68      
69      /** The session. */
70      private HttpSession         session     = new SimpleHttpSession();
71      
72      /** The principal. */
73      private Principal           principal;
74  
75      /**
76       * Instantiates a new simple http request.
77       */
78      public SimpleHttpRequest() {
79          super(Mockito.mock(HttpServletRequest.class));
80          this.remotePort = SimpleHttpRequest.nextRemotePort();
81      }
82  
83      /**
84       * Next remote port.
85       *
86       * @return the int
87       */
88      public static synchronized int nextRemotePort() {
89          return ++SimpleHttpRequest.remotePortS;
90      }
91  
92      /**
93       * Reset remote port.
94       */
95      public static synchronized void resetRemotePort() {
96          SimpleHttpRequest.remotePortS = 0;
97      }
98  
99      /**
100      * Adds the header.
101      *
102      * @param headerName
103      *            the header name
104      * @param headerValue
105      *            the header value
106      */
107     public void addHeader(final String headerName, final String headerValue) {
108         this.headers.put(headerName, headerValue);
109     }
110 
111     /* (non-Javadoc)
112      * @see javax.servlet.http.HttpServletRequestWrapper#getHeader(java.lang.String)
113      */
114     @Override
115     public String getHeader(final String headerName) {
116         return this.headers.get(headerName);
117     }
118 
119     /* (non-Javadoc)
120      * @see javax.servlet.http.HttpServletRequestWrapper#getHeaderNames()
121      */
122     @Override
123     public Enumeration<String> getHeaderNames() {
124         return Iterators.asEnumeration(this.headers.keySet().iterator());
125     }
126 
127     /* (non-Javadoc)
128      * @see javax.servlet.http.HttpServletRequestWrapper#getMethod()
129      */
130     @Override
131     public String getMethod() {
132         return this.method;
133     }
134 
135     /* (non-Javadoc)
136      * @see javax.servlet.ServletRequestWrapper#getContentLength()
137      */
138     @Override
139     public int getContentLength() {
140         return this.content == null ? -1 : this.content.length;
141     }
142 
143     /* (non-Javadoc)
144      * @see javax.servlet.ServletRequestWrapper#getRemotePort()
145      */
146     @Override
147     public int getRemotePort() {
148         return this.remotePort;
149     }
150 
151     /**
152      * Sets the method.
153      *
154      * @param methodName
155      *            the new method
156      */
157     public void setMethod(final String methodName) {
158         this.method = methodName;
159     }
160 
161     /**
162      * Sets the content length.
163      *
164      * @param length
165      *            the new content length
166      */
167     public void setContentLength(final int length) {
168         this.content = new byte[length];
169     }
170 
171     /**
172      * Sets the remote user.
173      *
174      * @param username
175      *            the new remote user
176      */
177     public void setRemoteUser(final String username) {
178         this.remoteUser = username;
179     }
180 
181     /* (non-Javadoc)
182      * @see javax.servlet.http.HttpServletRequestWrapper#getRemoteUser()
183      */
184     @Override
185     public String getRemoteUser() {
186         return this.remoteUser;
187     }
188 
189     /* (non-Javadoc)
190      * @see javax.servlet.http.HttpServletRequestWrapper#getSession()
191      */
192     @Override
193     public HttpSession getSession() {
194         return this.session;
195     }
196 
197     /* (non-Javadoc)
198      * @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)
199      */
200     @Override
201     public HttpSession getSession(final boolean create) {
202         if (this.session == null && create) {
203             this.session = new SimpleHttpSession();
204         }
205         return this.session;
206     }
207 
208     /* (non-Javadoc)
209      * @see javax.servlet.http.HttpServletRequestWrapper#getQueryString()
210      */
211     @Override
212     public String getQueryString() {
213         return this.queryString;
214     }
215 
216     /**
217      * Sets the query string.
218      *
219      * @param query
220      *            the new query string
221      */
222     public void setQueryString(final String query) {
223         this.queryString = query;
224         if (this.queryString != null) {
225             for (final String eachParameter : this.queryString.split("[&]")) {
226                 final String[] pair = eachParameter.split("=");
227                 final String value = (pair.length == 2) ? pair[1] : "";
228                 this.addParameter(pair[0], value);
229             }
230         }
231     }
232 
233     /**
234      * Sets the request uri.
235      *
236      * @param uri
237      *            the new request uri
238      */
239     public void setRequestURI(final String uri) {
240         this.requestURI = uri;
241     }
242 
243     /* (non-Javadoc)
244      * @see javax.servlet.http.HttpServletRequestWrapper#getRequestURI()
245      */
246     @Override
247     public String getRequestURI() {
248         return this.requestURI;
249     }
250 
251     /* (non-Javadoc)
252      * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
253      */
254     @Override
255     public String getParameter(final String parameterName) {
256         return this.parameters.get(parameterName);
257     }
258 
259     /**
260      * Adds the parameter.
261      *
262      * @param parameterName
263      *            the parameter name
264      * @param parameterValue
265      *            the parameter value
266      */
267     public void addParameter(final String parameterName, final String parameterValue) {
268         this.parameters.put(parameterName, parameterValue);
269     }
270 
271     /* (non-Javadoc)
272      * @see javax.servlet.ServletRequestWrapper#getRemoteHost()
273      */
274     @Override
275     public String getRemoteHost() {
276         return this.remoteHost;
277     }
278 
279     /**
280      * Sets the remote host.
281      *
282      * @param value
283      *            the new remote host
284      */
285     public void setRemoteHost(final String value) {
286         this.remoteHost = value;
287     }
288 
289     /* (non-Javadoc)
290      * @see javax.servlet.ServletRequestWrapper#getRemoteAddr()
291      */
292     @Override
293     public String getRemoteAddr() {
294         return this.remoteAddr;
295     }
296 
297     /**
298      * Sets the remote addr.
299      *
300      * @param value
301      *            the new remote addr
302      */
303     public void setRemoteAddr(final String value) {
304         this.remoteAddr = value;
305     }
306 
307     /* (non-Javadoc)
308      * @see javax.servlet.http.HttpServletRequestWrapper#getUserPrincipal()
309      */
310     @Override
311     public Principal getUserPrincipal() {
312         return this.principal;
313     }
314 
315     /**
316      * Sets the user principal.
317      *
318      * @param value
319      *            the new user principal
320      */
321     public void setUserPrincipal(final Principal value) {
322         this.principal = value;
323     }
324 }