1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.junit.http;
12
13 import java.util.Collections;
14 import java.util.Enumeration;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 import org.eclipse.jetty.server.Request;
19 import org.eclipse.jetty.server.Response;
20
21
22
23
24 public class AccessEvent {
25 private final String method;
26
27 private final String uri;
28
29 private final Map<String, String> requestHeaders;
30
31 private final Map<String, String[]> parameters;
32
33 private final int status;
34
35 private final Map<String, String> responseHeaders;
36
37 AccessEvent(Request req, Response rsp) {
38 method = req.getMethod();
39 uri = req.getRequestURI();
40 requestHeaders = cloneHeaders(req);
41 parameters = clone(req.getParameterMap());
42
43 status = rsp.getStatus();
44 responseHeaders = cloneHeaders(rsp);
45 }
46
47 private static Map<String, String> cloneHeaders(Request req) {
48 Map<String, String> r = new TreeMap<>();
49 Enumeration hn = req.getHeaderNames();
50 while (hn.hasMoreElements()) {
51 String key = (String) hn.nextElement();
52 if (!r.containsKey(key)) {
53 r.put(key, req.getHeader(key));
54 }
55 }
56 return Collections.unmodifiableMap(r);
57 }
58
59 private static Map<String, String> cloneHeaders(Response rsp) {
60 Map<String, String> r = new TreeMap<>();
61 Enumeration<String> hn = rsp.getHttpFields().getFieldNames();
62 while (hn.hasMoreElements()) {
63 String key = hn.nextElement();
64 if (!r.containsKey(key)) {
65 Enumeration<String> v = rsp.getHttpFields().getValues(key);
66 r.put(key, v.nextElement());
67 }
68 }
69 return Collections.unmodifiableMap(r);
70 }
71
72 @SuppressWarnings("unchecked")
73 private static Map<String, String[]> clone(Map parameterMap) {
74 return new TreeMap<>(parameterMap);
75 }
76
77
78
79
80
81
82 public String getMethod() {
83 return method;
84 }
85
86
87
88
89
90
91 public String getPath() {
92 return uri;
93 }
94
95
96
97
98
99
100
101
102 public String getRequestHeader(String name) {
103 return requestHeaders.get(name);
104 }
105
106
107
108
109
110
111
112
113 public String getParameter(String name) {
114 String[] r = parameters.get(name);
115 return r != null && 1 <= r.length ? r[0] : null;
116 }
117
118
119
120
121
122
123 public Map<String, String[]> getParameters() {
124 return parameters;
125 }
126
127
128
129
130
131
132 public int getStatus() {
133 return status;
134 }
135
136
137
138
139
140
141
142
143 public String getResponseHeader(String name) {
144 return responseHeaders.get(name);
145 }
146
147
148 @Override
149 public String toString() {
150 StringBuilder b = new StringBuilder();
151 b.append(method);
152 b.append(' ');
153 b.append(uri);
154 if (!parameters.isEmpty()) {
155 b.append('?');
156 boolean first = true;
157 for (Map.Entry<String, String[]> e : parameters.entrySet()) {
158 for (String val : e.getValue()) {
159 if (!first) {
160 b.append('&');
161 }
162 first = false;
163
164 b.append(e.getKey());
165 b.append('=');
166 b.append(val);
167 }
168 }
169 }
170 b.append(' ');
171 b.append(status);
172 return b.toString();
173 }
174 }