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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /**
  51. * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}.
  52. */
  53. public class AccessEvent {
  54. private final String method;
  55. private final String uri;
  56. private final Map<String, String> requestHeaders;
  57. private final Map<String, String[]> parameters;
  58. private final int status;
  59. private final Map<String, String> responseHeaders;
  60. AccessEvent(final Request req, final Response rsp) {
  61. method = req.getMethod();
  62. uri = req.getRequestURI();
  63. requestHeaders = cloneHeaders(req);
  64. parameters = clone(req.getParameterMap());
  65. status = rsp.getStatus();
  66. responseHeaders = cloneHeaders(rsp);
  67. }
  68. private static Map<String, String> cloneHeaders(final Request req) {
  69. Map<String, String> r = new TreeMap<>();
  70. Enumeration hn = req.getHeaderNames();
  71. while (hn.hasMoreElements()) {
  72. String key = (String) hn.nextElement();
  73. if (!r.containsKey(key)) {
  74. r.put(key, req.getHeader(key));
  75. }
  76. }
  77. return Collections.unmodifiableMap(r);
  78. }
  79. private static Map<String, String> cloneHeaders(final Response rsp) {
  80. Map<String, String> r = new TreeMap<>();
  81. Enumeration<String> hn = rsp.getHttpFields().getFieldNames();
  82. while (hn.hasMoreElements()) {
  83. String key = hn.nextElement();
  84. if (!r.containsKey(key)) {
  85. Enumeration<String> v = rsp.getHttpFields().getValues(key);
  86. r.put(key, v.nextElement());
  87. }
  88. }
  89. return Collections.unmodifiableMap(r);
  90. }
  91. @SuppressWarnings("unchecked")
  92. private static Map<String, String[]> clone(Map parameterMap) {
  93. return new TreeMap<>(parameterMap);
  94. }
  95. /**
  96. * Get the <code>method</code>.
  97. *
  98. * @return {@code "GET"} or {@code "POST"}
  99. */
  100. public String getMethod() {
  101. return method;
  102. }
  103. /**
  104. * Get <code>path</code>.
  105. *
  106. * @return path of the file on the server, e.g. {@code /git/HEAD}.
  107. */
  108. public String getPath() {
  109. return uri;
  110. }
  111. /**
  112. * Get request header
  113. *
  114. * @param name
  115. * name of the request header to read.
  116. * @return first value of the request header; null if not sent.
  117. */
  118. public String getRequestHeader(String name) {
  119. return requestHeaders.get(name);
  120. }
  121. /**
  122. * Get parameter
  123. *
  124. * @param name
  125. * name of the request parameter to read.
  126. * @return first value of the request parameter; null if not sent.
  127. */
  128. public String getParameter(String name) {
  129. String[] r = parameters.get(name);
  130. return r != null && 1 <= r.length ? r[0] : null;
  131. }
  132. /**
  133. * Get <code>parameters</code>
  134. *
  135. * @return all parameters in the request.
  136. */
  137. public Map<String, String[]> getParameters() {
  138. return parameters;
  139. }
  140. /**
  141. * Get the <code>status</code>.
  142. *
  143. * @return HTTP status code of the response, e.g. 200, 403, 500.
  144. */
  145. public int getStatus() {
  146. return status;
  147. }
  148. /**
  149. * Get response header.
  150. *
  151. * @param name
  152. * name of the response header to read.
  153. * @return first value of the response header; null if not sent.
  154. */
  155. public String getResponseHeader(String name) {
  156. return responseHeaders.get(name);
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. public String toString() {
  161. StringBuilder b = new StringBuilder();
  162. b.append(method);
  163. b.append(' ');
  164. b.append(uri);
  165. if (!parameters.isEmpty()) {
  166. b.append('?');
  167. boolean first = true;
  168. for (Map.Entry<String, String[]> e : parameters.entrySet()) {
  169. for (String val : e.getValue()) {
  170. if (!first) {
  171. b.append('&');
  172. }
  173. first = false;
  174. b.append(e.getKey());
  175. b.append('=');
  176. b.append(val);
  177. }
  178. }
  179. }
  180. b.append(' ');
  181. b.append(status);
  182. return b.toString();
  183. }
  184. }