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