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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.junit.http;
  11. import java.util.Collections;
  12. import java.util.Enumeration;
  13. import java.util.Map;
  14. import java.util.TreeMap;
  15. import org.eclipse.jetty.server.Request;
  16. import org.eclipse.jetty.server.Response;
  17. /**
  18. * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}.
  19. */
  20. public class AccessEvent {
  21. private final String method;
  22. private final String uri;
  23. private final Map<String, String> requestHeaders;
  24. private final Map<String, String[]> parameters;
  25. private final int status;
  26. private final Map<String, String> responseHeaders;
  27. AccessEvent(Request req, Response rsp) {
  28. method = req.getMethod();
  29. uri = req.getRequestURI();
  30. requestHeaders = cloneHeaders(req);
  31. parameters = clone(req.getParameterMap());
  32. status = rsp.getStatus();
  33. responseHeaders = cloneHeaders(rsp);
  34. }
  35. private static Map<String, String> cloneHeaders(Request req) {
  36. Map<String, String> r = new TreeMap<>();
  37. Enumeration hn = req.getHeaderNames();
  38. while (hn.hasMoreElements()) {
  39. String key = (String) hn.nextElement();
  40. if (!r.containsKey(key)) {
  41. r.put(key, req.getHeader(key));
  42. }
  43. }
  44. return Collections.unmodifiableMap(r);
  45. }
  46. private static Map<String, String> cloneHeaders(Response rsp) {
  47. Map<String, String> r = new TreeMap<>();
  48. Enumeration<String> hn = rsp.getHttpFields().getFieldNames();
  49. while (hn.hasMoreElements()) {
  50. String key = hn.nextElement();
  51. if (!r.containsKey(key)) {
  52. Enumeration<String> v = rsp.getHttpFields().getValues(key);
  53. r.put(key, v.nextElement());
  54. }
  55. }
  56. return Collections.unmodifiableMap(r);
  57. }
  58. @SuppressWarnings("unchecked")
  59. private static Map<String, String[]> clone(Map parameterMap) {
  60. return new TreeMap<>(parameterMap);
  61. }
  62. /**
  63. * Get the <code>method</code>.
  64. *
  65. * @return {@code "GET"} or {@code "POST"}
  66. */
  67. public String getMethod() {
  68. return method;
  69. }
  70. /**
  71. * Get <code>path</code>.
  72. *
  73. * @return path of the file on the server, e.g. {@code /git/HEAD}.
  74. */
  75. public String getPath() {
  76. return uri;
  77. }
  78. /**
  79. * Get request header
  80. *
  81. * @param name
  82. * name of the request header to read.
  83. * @return first value of the request header; null if not sent.
  84. */
  85. public String getRequestHeader(String name) {
  86. return requestHeaders.get(name);
  87. }
  88. /**
  89. * Get parameter
  90. *
  91. * @param name
  92. * name of the request parameter to read.
  93. * @return first value of the request parameter; null if not sent.
  94. */
  95. public String getParameter(String name) {
  96. String[] r = parameters.get(name);
  97. return r != null && 1 <= r.length ? r[0] : null;
  98. }
  99. /**
  100. * Get <code>parameters</code>
  101. *
  102. * @return all parameters in the request.
  103. */
  104. public Map<String, String[]> getParameters() {
  105. return parameters;
  106. }
  107. /**
  108. * Get the <code>status</code>.
  109. *
  110. * @return HTTP status code of the response, e.g. 200, 403, 500.
  111. */
  112. public int getStatus() {
  113. return status;
  114. }
  115. /**
  116. * Get response header.
  117. *
  118. * @param name
  119. * name of the response header to read.
  120. * @return first value of the response header; null if not sent.
  121. */
  122. public String getResponseHeader(String name) {
  123. return responseHeaders.get(name);
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. public String toString() {
  128. StringBuilder b = new StringBuilder();
  129. b.append(method);
  130. b.append(' ');
  131. b.append(uri);
  132. if (!parameters.isEmpty()) {
  133. b.append('?');
  134. boolean first = true;
  135. for (Map.Entry<String, String[]> e : parameters.entrySet()) {
  136. for (String val : e.getValue()) {
  137. if (!first) {
  138. b.append('&');
  139. }
  140. first = false;
  141. b.append(e.getKey());
  142. b.append('=');
  143. b.append(val);
  144. }
  145. }
  146. }
  147. b.append(' ');
  148. b.append(status);
  149. return b.toString();
  150. }
  151. }