You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessEvent.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.junit.http;
  44. import java.util.Collections;
  45. import java.util.Enumeration;
  46. import java.util.Map;
  47. import java.util.TreeMap;
  48. import org.eclipse.jetty.server.Request;
  49. import org.eclipse.jetty.server.Response;
  50. /** A single request made through {@link AppServer}. */
  51. public class AccessEvent {
  52. private final String method;
  53. private final String uri;
  54. private final Map<String, String> requestHeaders;
  55. private final Map<String, String[]> parameters;
  56. private final int status;
  57. private final Map<String, String> responseHeaders;
  58. AccessEvent(final Request req, final Response rsp) {
  59. method = req.getMethod();
  60. uri = req.getRequestURI();
  61. requestHeaders = cloneHeaders(req);
  62. parameters = clone(req.getParameterMap());
  63. status = rsp.getStatus();
  64. responseHeaders = cloneHeaders(rsp);
  65. }
  66. private static Map<String, String> cloneHeaders(final Request req) {
  67. Map<String, String> r = new TreeMap<>();
  68. Enumeration hn = req.getHeaderNames();
  69. while (hn.hasMoreElements()) {
  70. String key = (String) hn.nextElement();
  71. if (!r.containsKey(key)) {
  72. r.put(key, req.getHeader(key));
  73. }
  74. }
  75. return Collections.unmodifiableMap(r);
  76. }
  77. private static Map<String, String> cloneHeaders(final Response rsp) {
  78. Map<String, String> r = new TreeMap<>();
  79. Enumeration<String> hn = rsp.getHttpFields().getFieldNames();
  80. while (hn.hasMoreElements()) {
  81. String key = hn.nextElement();
  82. if (!r.containsKey(key)) {
  83. Enumeration<String> v = rsp.getHttpFields().getValues(key);
  84. r.put(key, v.nextElement());
  85. }
  86. }
  87. return Collections.unmodifiableMap(r);
  88. }
  89. @SuppressWarnings("unchecked")
  90. private static Map<String, String[]> clone(Map parameterMap) {
  91. return new TreeMap<String, String[]>(parameterMap);
  92. }
  93. /** @return {@code "GET"} or {@code "POST"} */
  94. public String getMethod() {
  95. return method;
  96. }
  97. /** @return path of the file on the server, e.g. {@code /git/HEAD}. */
  98. public String getPath() {
  99. return uri;
  100. }
  101. /**
  102. * @param name
  103. * name of the request header to read.
  104. * @return first value of the request header; null if not sent.
  105. */
  106. public String getRequestHeader(String name) {
  107. return requestHeaders.get(name);
  108. }
  109. /**
  110. * @param name
  111. * name of the request parameter to read.
  112. * @return first value of the request parameter; null if not sent.
  113. */
  114. public String getParameter(String name) {
  115. String[] r = parameters.get(name);
  116. return r != null && 1 <= r.length ? r[0] : null;
  117. }
  118. /** @return all parameters in the request. */
  119. public Map<String, String[]> getParameters() {
  120. return parameters;
  121. }
  122. /** @return HTTP status code of the response, e.g. 200, 403, 500. */
  123. public int getStatus() {
  124. return status;
  125. }
  126. /**
  127. * @param name
  128. * name of the response header to read.
  129. * @return first value of the response header; null if not sent.
  130. */
  131. public String getResponseHeader(String name) {
  132. return responseHeaders.get(name);
  133. }
  134. @Override
  135. public String toString() {
  136. StringBuilder b = new StringBuilder();
  137. b.append(method);
  138. b.append(' ');
  139. b.append(uri);
  140. if (!parameters.isEmpty()) {
  141. b.append('?');
  142. boolean first = true;
  143. for (Map.Entry<String, String[]> e : parameters.entrySet()) {
  144. for (String val : e.getValue()) {
  145. if (!first) {
  146. b.append('&');
  147. }
  148. first = false;
  149. b.append(e.getKey());
  150. b.append('=');
  151. b.append(val);
  152. }
  153. }
  154. }
  155. b.append(' ');
  156. b.append(status);
  157. return b.toString();
  158. }
  159. }