| 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; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 28 | import java.io.IOException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import java.io.UnsupportedEncodingException; |
| 30 | import java.net.URLDecoder; |
| 31 | import java.security.Principal; |
| 32 | import java.util.Collection; |
| 33 | import java.util.Collections; |
| 34 | import java.util.Enumeration; |
| 35 | import java.util.List; |
| 36 | import java.util.Locale; |
| 37 | import java.util.Map; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 38 | import javax.servlet.AsyncContext; |
| 39 | import javax.servlet.DispatcherType; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | import javax.servlet.RequestDispatcher; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 41 | import javax.servlet.ServletContext; |
| 42 | import javax.servlet.ServletException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 43 | import javax.servlet.ServletInputStream; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 44 | import javax.servlet.ServletRequest; |
| 45 | import javax.servlet.ServletResponse; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | import javax.servlet.http.Cookie; |
| 47 | import javax.servlet.http.HttpServletRequest; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 48 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | import javax.servlet.http.HttpSession; |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 50 | import javax.servlet.http.HttpUpgradeHandler; |
| 51 | import javax.servlet.http.Part; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 52 | import org.eclipse.jgit.http.server.ServletUtils; |
| 53 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | |
| 55 | /** Simple fake implementation of {@link HttpServletRequest}. */ |
| 56 | public class FakeHttpServletRequest implements HttpServletRequest { |
| 57 | public static final String SERVLET_PATH = "/b"; |
| 58 | |
| 59 | public static FakeHttpServletRequest newRequest() { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 60 | return new FakeHttpServletRequest(URLS.getHostName(null), 80, "", SERVLET_PATH); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public static FakeHttpServletRequest newRequest(DfsRepository repo) { |
| 64 | FakeHttpServletRequest req = newRequest(); |
| 65 | req.setAttribute(ServletUtils.ATTRIBUTE_REPOSITORY, repo); |
| 66 | return req; |
| 67 | } |
| 68 | |
| 69 | private final Map<String, Object> attributes; |
| 70 | private final ListMultimap<String, String> headers; |
| 71 | |
| 72 | private ListMultimap<String, String> parameters; |
| 73 | private String hostName; |
| 74 | private int port; |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 75 | private String method; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 76 | private String contextPath; |
| 77 | private String servletPath; |
| 78 | private String path; |
| 79 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 80 | private FakeHttpServletRequest( |
| 81 | String hostName, int port, String contextPath, String servletPath) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | this.hostName = checkNotNull(hostName, "hostName"); |
| 83 | checkArgument(port > 0); |
| 84 | this.port = port; |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 85 | this.method = "GET"; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | this.contextPath = checkNotNull(contextPath, "contextPath"); |
| 87 | this.servletPath = checkNotNull(servletPath, "servletPath"); |
| 88 | attributes = Maps.newConcurrentMap(); |
| 89 | parameters = LinkedListMultimap.create(); |
| 90 | headers = LinkedListMultimap.create(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Object getAttribute(String name) { |
| 95 | return attributes.get(name); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Enumeration<String> getAttributeNames() { |
| 100 | return Collections.enumeration(attributes.keySet()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String getCharacterEncoding() { |
| 105 | return UTF_8.name(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int getContentLength() { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | @Override |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 114 | public long getContentLengthLong() { |
| 115 | throw new UnsupportedOperationException(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 119 | public String getContentType() { |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public ServletInputStream getInputStream() { |
| 125 | throw new UnsupportedOperationException(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String getLocalAddr() { |
| 130 | return "1.2.3.4"; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String getLocalName() { |
| 135 | return hostName; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int getLocalPort() { |
| 140 | return port; |
| 141 | } |
| 142 | |
| 143 | @Override |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 144 | public ServletContext getServletContext() { |
| 145 | throw new UnsupportedOperationException(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public AsyncContext startAsync() { |
| 150 | throw new UnsupportedOperationException(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) { |
| 155 | throw new UnsupportedOperationException(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean isAsyncStarted() { |
| 160 | throw new UnsupportedOperationException(); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public boolean isAsyncSupported() { |
| 165 | throw new UnsupportedOperationException(); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public AsyncContext getAsyncContext() { |
| 170 | throw new UnsupportedOperationException(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public DispatcherType getDispatcherType() { |
| 175 | throw new UnsupportedOperationException(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 179 | public Locale getLocale() { |
| 180 | return Locale.US; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public Enumeration<Locale> getLocales() { |
| 185 | return Collections.enumeration(Collections.singleton(Locale.US)); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String getParameter(String name) { |
| 190 | return Iterables.getFirst(parameters.get(name), null); |
| 191 | } |
| 192 | |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 193 | private static final String[] stringCollectionToArray(Collection<String> values) { |
| 194 | return values.toArray(new String[0]); |
| 195 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 196 | |
| 197 | @Override |
| 198 | public Map<String, String[]> getParameterMap() { |
| 199 | return Collections.unmodifiableMap( |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 200 | Maps.transformValues(parameters.asMap(), c -> stringCollectionToArray(c))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Enumeration<String> getParameterNames() { |
| 205 | return Collections.enumeration(parameters.keySet()); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public String[] getParameterValues(String name) { |
| David Pursehouse | dcde0af | 2016-10-04 15:24:59 +0900 | [diff] [blame] | 210 | return stringCollectionToArray(parameters.get(name)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public void setQueryString(String qs) { |
| 214 | ListMultimap<String, String> params = LinkedListMultimap.create(); |
| 215 | for (String entry : Splitter.on('&').split(qs)) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 216 | List<String> kv = Splitter.on('=').limit(2).splitToList(entry); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 217 | try { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 218 | params.put( |
| 219 | URLDecoder.decode(kv.get(0), UTF_8.name()), |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 220 | kv.size() == 2 ? URLDecoder.decode(kv.get(1), UTF_8.name()) : ""); |
| 221 | } catch (UnsupportedEncodingException e) { |
| 222 | throw new IllegalArgumentException(e); |
| 223 | } |
| 224 | } |
| 225 | parameters = params; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public String getProtocol() { |
| 230 | return "HTTP/1.1"; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public BufferedReader getReader() { |
| 235 | throw new UnsupportedOperationException(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | @Deprecated |
| 240 | public String getRealPath(String path) { |
| 241 | throw new UnsupportedOperationException(); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String getRemoteAddr() { |
| 246 | return "5.6.7.8"; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String getRemoteHost() { |
| 251 | return "remotehost"; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public int getRemotePort() { |
| 256 | return 1234; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public RequestDispatcher getRequestDispatcher(String path) { |
| 261 | throw new UnsupportedOperationException(); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public String getScheme() { |
| 266 | return port == 443 ? "https" : "http"; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public String getServerName() { |
| 271 | return hostName; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public int getServerPort() { |
| 276 | return port; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean isSecure() { |
| 281 | return port == 443; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void removeAttribute(String name) { |
| 286 | attributes.remove(name); |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void setAttribute(String name, Object value) { |
| 291 | attributes.put(name, value); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void setCharacterEncoding(String env) throws UnsupportedOperationException { |
| 296 | throw new UnsupportedOperationException(); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public String getAuthType() { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public String getContextPath() { |
| 306 | return contextPath; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public Cookie[] getCookies() { |
| 311 | return new Cookie[0]; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public long getDateHeader(String name) { |
| 316 | throw new UnsupportedOperationException(); |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public String getHeader(String name) { |
| 321 | return Iterables.getFirst(headers.get(name), null); |
| 322 | } |
| 323 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 324 | public boolean setHeader(String name, String value) { |
| 325 | return headers.put(name, value); |
| 326 | } |
| 327 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 328 | @Override |
| 329 | public Enumeration<String> getHeaderNames() { |
| 330 | return Collections.enumeration(headers.keySet()); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public Enumeration<String> getHeaders(String name) { |
| 335 | return Collections.enumeration(headers.get(name)); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public int getIntHeader(String name) { |
| 340 | return Integer.parseInt(getHeader(name)); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public String getMethod() { |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 345 | return method; |
| 346 | } |
| 347 | |
| 348 | public void setMethod(String m) { |
| 349 | method = m; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public String getPathInfo() { |
| 354 | return path; |
| 355 | } |
| 356 | |
| 357 | public void setPathInfo(String path) { |
| 358 | this.path = checkNotNull(path); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public String getPathTranslated() { |
| 363 | return path; |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public String getQueryString() { |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 368 | return GitilesView.paramsToString(parameters); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public String getRemoteUser() { |
| 373 | return null; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public String getRequestURI() { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 378 | String uri = contextPath + servletPath + path; |
| 379 | if (!parameters.isEmpty()) { |
| 380 | uri += "?" + GitilesView.paramsToString(parameters); |
| 381 | } |
| 382 | return uri; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public StringBuffer getRequestURL() { |
| 387 | return null; |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public String getRequestedSessionId() { |
| 392 | return null; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public String getServletPath() { |
| 397 | return servletPath; |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public HttpSession getSession() { |
| 402 | throw new UnsupportedOperationException(); |
| 403 | } |
| 404 | |
| 405 | @Override |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 406 | public String changeSessionId() { |
| 407 | throw new UnsupportedOperationException(); |
| 408 | } |
| 409 | |
| 410 | @Override |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 411 | public HttpSession getSession(boolean create) { |
| 412 | throw new UnsupportedOperationException(); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public Principal getUserPrincipal() { |
| 417 | throw new UnsupportedOperationException(); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public boolean isRequestedSessionIdFromCookie() { |
| 422 | throw new UnsupportedOperationException(); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean isRequestedSessionIdFromURL() { |
| 427 | throw new UnsupportedOperationException(); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | @Deprecated |
| 432 | public boolean isRequestedSessionIdFromUrl() { |
| 433 | throw new UnsupportedOperationException(); |
| 434 | } |
| 435 | |
| 436 | @Override |
| Gerrit Code Review | 5302fc0 | 2021-12-22 13:35:06 -0800 | [diff] [blame] | 437 | public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { |
| 438 | throw new UnsupportedOperationException(); |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void login(String username, String password) throws ServletException { |
| 443 | throw new UnsupportedOperationException(); |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void logout() throws ServletException { |
| 448 | throw new UnsupportedOperationException(); |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public Collection<Part> getParts() throws IOException, ServletException { |
| 453 | throw new UnsupportedOperationException(); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public Part getPart(String name) throws IOException, ServletException { |
| 458 | throw new UnsupportedOperationException(); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass) |
| 463 | throws IOException, ServletException { |
| 464 | throw new UnsupportedOperationException(); |
| 465 | } |
| 466 | |
| 467 | @Override |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 468 | public boolean isRequestedSessionIdValid() { |
| 469 | throw new UnsupportedOperationException(); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public boolean isUserInRole(String role) { |
| 474 | throw new UnsupportedOperationException(); |
| 475 | } |
| 476 | } |