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