| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 20 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 22 | import com.google.common.base.Splitter; |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Iterables; |
| 24 | import com.google.common.collect.LinkedListMultimap; |
| 25 | import com.google.common.collect.ListMultimap; |
| 26 | import com.google.common.collect.Maps; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 27 | import java.io.BufferedReader; |
| 28 | import java.io.UnsupportedEncodingException; |
| 29 | import java.net.URLDecoder; |
| 30 | import java.security.Principal; |
| 31 | import java.util.Collection; |
| 32 | import java.util.Collections; |
| 33 | import java.util.Enumeration; |
| 34 | import java.util.List; |
| 35 | import java.util.Locale; |
| 36 | import java.util.Map; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | import javax.servlet.RequestDispatcher; |
| 38 | import javax.servlet.ServletInputStream; |
| 39 | import javax.servlet.http.Cookie; |
| 40 | import javax.servlet.http.HttpServletRequest; |
| 41 | import javax.servlet.http.HttpSession; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 42 | import org.eclipse.jgit.http.server.ServletUtils; |
| 43 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | |
| 45 | /** Simple fake implementation of {@link HttpServletRequest}. */ |
| 46 | public class FakeHttpServletRequest implements HttpServletRequest { |
| 47 | public static final String SERVLET_PATH = "/b"; |
| 48 | |
| 49 | public static FakeHttpServletRequest newRequest() { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 50 | return new FakeHttpServletRequest(URLS.getHostName(null), 80, "", SERVLET_PATH); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public static FakeHttpServletRequest newRequest(DfsRepository repo) { |
| 54 | FakeHttpServletRequest req = newRequest(); |
| 55 | req.setAttribute(ServletUtils.ATTRIBUTE_REPOSITORY, repo); |
| 56 | return req; |
| 57 | } |
| 58 | |
| 59 | private final Map<String, Object> attributes; |
| 60 | private final ListMultimap<String, String> headers; |
| 61 | |
| 62 | private ListMultimap<String, String> parameters; |
| 63 | private String hostName; |
| 64 | private int port; |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 65 | private String method; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | private String contextPath; |
| 67 | private String servletPath; |
| 68 | private String path; |
| 69 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 70 | private FakeHttpServletRequest( |
| 71 | String hostName, int port, String contextPath, String servletPath) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | this.hostName = checkNotNull(hostName, "hostName"); |
| 73 | checkArgument(port > 0); |
| 74 | this.port = port; |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 75 | this.method = "GET"; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 76 | this.contextPath = checkNotNull(contextPath, "contextPath"); |
| 77 | this.servletPath = checkNotNull(servletPath, "servletPath"); |
| 78 | attributes = Maps.newConcurrentMap(); |
| 79 | parameters = LinkedListMultimap.create(); |
| 80 | headers = LinkedListMultimap.create(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Object getAttribute(String name) { |
| 85 | return attributes.get(name); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Enumeration<String> getAttributeNames() { |
| 90 | return Collections.enumeration(attributes.keySet()); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String getCharacterEncoding() { |
| 95 | return UTF_8.name(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int getContentLength() { |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String getContentType() { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public ServletInputStream getInputStream() { |
| 110 | throw new UnsupportedOperationException(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String getLocalAddr() { |
| 115 | return "1.2.3.4"; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public String getLocalName() { |
| 120 | return hostName; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int getLocalPort() { |
| 125 | return port; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Locale getLocale() { |
| 130 | return Locale.US; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Enumeration<Locale> getLocales() { |
| 135 | return Collections.enumeration(Collections.singleton(Locale.US)); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String getParameter(String name) { |
| 140 | return Iterables.getFirst(parameters.get(name), null); |
| 141 | } |
| 142 | |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 143 | private static final String[] stringCollectionToArray(Collection<String> values) { |
| 144 | return values.toArray(new String[0]); |
| 145 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 146 | |
| 147 | @Override |
| 148 | public Map<String, String[]> getParameterMap() { |
| 149 | return Collections.unmodifiableMap( |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 150 | Maps.transformValues(parameters.asMap(), c -> stringCollectionToArray(c))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Enumeration<String> getParameterNames() { |
| 155 | return Collections.enumeration(parameters.keySet()); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public String[] getParameterValues(String name) { |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 160 | return stringCollectionToArray(parameters.get(name)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | public void setQueryString(String qs) { |
| 164 | ListMultimap<String, String> params = LinkedListMultimap.create(); |
| 165 | for (String entry : Splitter.on('&').split(qs)) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 166 | List<String> kv = Splitter.on('=').limit(2).splitToList(entry); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 167 | try { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 168 | params.put( |
| 169 | URLDecoder.decode(kv.get(0), UTF_8.name()), |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 170 | kv.size() == 2 ? URLDecoder.decode(kv.get(1), UTF_8.name()) : ""); |
| 171 | } catch (UnsupportedEncodingException e) { |
| 172 | throw new IllegalArgumentException(e); |
| 173 | } |
| 174 | } |
| 175 | parameters = params; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public String getProtocol() { |
| 180 | return "HTTP/1.1"; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public BufferedReader getReader() { |
| 185 | throw new UnsupportedOperationException(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | @Deprecated |
| 190 | public String getRealPath(String path) { |
| 191 | throw new UnsupportedOperationException(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String getRemoteAddr() { |
| 196 | return "5.6.7.8"; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public String getRemoteHost() { |
| 201 | return "remotehost"; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public int getRemotePort() { |
| 206 | return 1234; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public RequestDispatcher getRequestDispatcher(String path) { |
| 211 | throw new UnsupportedOperationException(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public String getScheme() { |
| 216 | return port == 443 ? "https" : "http"; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public String getServerName() { |
| 221 | return hostName; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public int getServerPort() { |
| 226 | return port; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public boolean isSecure() { |
| 231 | return port == 443; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void removeAttribute(String name) { |
| 236 | attributes.remove(name); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void setAttribute(String name, Object value) { |
| 241 | attributes.put(name, value); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void setCharacterEncoding(String env) throws UnsupportedOperationException { |
| 246 | throw new UnsupportedOperationException(); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String getAuthType() { |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public String getContextPath() { |
| 256 | return contextPath; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public Cookie[] getCookies() { |
| 261 | return new Cookie[0]; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public long getDateHeader(String name) { |
| 266 | throw new UnsupportedOperationException(); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public String getHeader(String name) { |
| 271 | return Iterables.getFirst(headers.get(name), null); |
| 272 | } |
| 273 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 274 | public boolean setHeader(String name, String value) { |
| 275 | return headers.put(name, value); |
| 276 | } |
| 277 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 278 | @Override |
| 279 | public Enumeration<String> getHeaderNames() { |
| 280 | return Collections.enumeration(headers.keySet()); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public Enumeration<String> getHeaders(String name) { |
| 285 | return Collections.enumeration(headers.get(name)); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public int getIntHeader(String name) { |
| 290 | return Integer.parseInt(getHeader(name)); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public String getMethod() { |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 295 | return method; |
| 296 | } |
| 297 | |
| 298 | public void setMethod(String m) { |
| 299 | method = m; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public String getPathInfo() { |
| 304 | return path; |
| 305 | } |
| 306 | |
| 307 | public void setPathInfo(String path) { |
| 308 | this.path = checkNotNull(path); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public String getPathTranslated() { |
| 313 | return path; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public String getQueryString() { |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 318 | return GitilesView.paramsToString(parameters); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public String getRemoteUser() { |
| 323 | return null; |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public String getRequestURI() { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 328 | String uri = contextPath + servletPath + path; |
| 329 | if (!parameters.isEmpty()) { |
| 330 | uri += "?" + GitilesView.paramsToString(parameters); |
| 331 | } |
| 332 | return uri; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public StringBuffer getRequestURL() { |
| 337 | return null; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public String getRequestedSessionId() { |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public String getServletPath() { |
| 347 | return servletPath; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public HttpSession getSession() { |
| 352 | throw new UnsupportedOperationException(); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public HttpSession getSession(boolean create) { |
| 357 | throw new UnsupportedOperationException(); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public Principal getUserPrincipal() { |
| 362 | throw new UnsupportedOperationException(); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public boolean isRequestedSessionIdFromCookie() { |
| 367 | throw new UnsupportedOperationException(); |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public boolean isRequestedSessionIdFromURL() { |
| 372 | throw new UnsupportedOperationException(); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | @Deprecated |
| 377 | public boolean isRequestedSessionIdFromUrl() { |
| 378 | throw new UnsupportedOperationException(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public boolean isRequestedSessionIdValid() { |
| 383 | throw new UnsupportedOperationException(); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public boolean isUserInRole(String role) { |
| 388 | throw new UnsupportedOperationException(); |
| 389 | } |
| 390 | } |