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.apache.catalina;
15  
16  import java.security.Principal;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import javax.servlet.http.HttpSession;
21  
22  import org.apache.catalina.connector.Request;
23  import org.mockito.Mockito;
24  
25  /**
26   * Simple HTTP Request.
27   * 
28   * @author dblock[at]dblock[dot]org
29   */
30  public class SimpleHttpRequest extends Request {
31  
32      private static int remotePortS;
33  
34      public synchronized static int nextRemotePort() {
35          return ++SimpleHttpRequest.remotePortS;
36      }
37  
38      public synchronized static void resetRemotePort() {
39          SimpleHttpRequest.remotePortS = 0;
40      }
41  
42      private String                    requestURI;
43      private String                    queryString;
44      private String                    remoteUser;
45      private String                    method     = "GET";
46      private final Map<String, String> headers    = new HashMap<String, String>();
47      private final Map<String, String> parameters = new HashMap<String, String>();
48      private byte[]                    content;
49  
50      private SimpleHttpSession         httpSession;
51  
52      private Principal                 principal;
53  
54      public SimpleHttpRequest() {
55          super();
56          this.httpSession = Mockito.mock(SimpleHttpSession.class, Mockito.CALLS_REAL_METHODS);
57          this.httpSession.setAttributes(new HashMap<String, Object>());
58          this.remotePort = SimpleHttpRequest.nextRemotePort();
59      }
60  
61      public void addHeader(final String headerName, final String headerValue) {
62          this.headers.put(headerName, headerValue);
63      }
64  
65      public void addParameter(final String parameterName, final String parameterValue) {
66          this.parameters.put(parameterName, parameterValue);
67      }
68  
69      @Override
70      public int getContentLength() {
71          return this.content == null ? -1 : this.content.length;
72      }
73  
74      @Override
75      public String getHeader(final String headerName) {
76          return this.headers.get(headerName);
77      }
78  
79      @Override
80      public String getMethod() {
81          return this.method;
82      }
83  
84      @Override
85      public String getParameter(final String parameterName) {
86          return this.parameters.get(parameterName);
87      }
88  
89      @Override
90      public String getQueryString() {
91          return this.queryString;
92      }
93  
94      @Override
95      public String getRemoteAddr() {
96          return this.remoteAddr;
97      }
98  
99      @Override
100     public String getRemoteHost() {
101         return this.remoteHost;
102     }
103 
104     @Override
105     public int getRemotePort() {
106         return this.remotePort;
107     }
108 
109     @Override
110     public String getRemoteUser() {
111         return this.remoteUser;
112     }
113 
114     @Override
115     public String getRequestURI() {
116         return this.requestURI;
117     }
118 
119     @Override
120     public HttpSession getSession() {
121         return this.httpSession;
122     }
123 
124     @Override
125     public HttpSession getSession(final boolean create) {
126         if (this.httpSession == null && create) {
127             this.httpSession = Mockito.mock(SimpleHttpSession.class, Mockito.CALLS_REAL_METHODS);
128             this.httpSession.setAttributes(new HashMap<String, Object>());
129         }
130         return this.httpSession;
131     }
132 
133     @Override
134     public Principal getUserPrincipal() {
135         return this.principal;
136     }
137 
138     public void setContentLength(final int length) {
139         this.content = new byte[length];
140     }
141 
142     public void setMethod(final String value) {
143         this.method = value;
144     }
145 
146     public void setQueryString(final String queryValue) {
147         this.queryString = queryValue;
148         if (this.queryString != null) {
149             for (final String eachParameter : this.queryString.split("[&]")) {
150                 final String[] pair = eachParameter.split("=");
151                 final String value = pair.length == 2 ? pair[1] : "";
152                 this.addParameter(pair[0], value);
153             }
154         }
155     }
156 
157     @Override
158     public void setRemoteAddr(final String value) {
159         this.remoteAddr = value;
160     }
161 
162     @Override
163     public void setRemoteHost(final String value) {
164         this.remoteHost = value;
165     }
166 
167     public void setRemoteUser(final String value) {
168         this.remoteUser = value;
169     }
170 
171     public void setRequestURI(final String value) {
172         this.requestURI = value;
173     }
174 
175     @Override
176     public void setUserPrincipal(final Principal value) {
177         this.principal = value;
178     }
179 }