View Javadoc
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  
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   * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}.
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  	 * Get the <code>method</code>.
79  	 *
80  	 * @return {@code "GET"} or {@code "POST"}
81  	 */
82  	public String getMethod() {
83  		return method;
84  	}
85  
86  	/**
87  	 * Get <code>path</code>.
88  	 *
89  	 * @return path of the file on the server, e.g. {@code /git/HEAD}.
90  	 */
91  	public String getPath() {
92  		return uri;
93  	}
94  
95  	/**
96  	 * Get request header
97  	 *
98  	 * @param name
99  	 *            name of the request header to read.
100 	 * @return first value of the request header; null if not sent.
101 	 */
102 	public String getRequestHeader(String name) {
103 		return requestHeaders.get(name);
104 	}
105 
106 	/**
107 	 * Get parameter
108 	 *
109 	 * @param name
110 	 *            name of the request parameter to read.
111 	 * @return first value of the request parameter; null if not sent.
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 	 * Get <code>parameters</code>
120 	 *
121 	 * @return all parameters in the request.
122 	 */
123 	public Map<String, String[]> getParameters() {
124 		return parameters;
125 	}
126 
127 	/**
128 	 * Get the <code>status</code>.
129 	 *
130 	 * @return HTTP status code of the response, e.g. 200, 403, 500.
131 	 */
132 	public int getStatus() {
133 		return status;
134 	}
135 
136 	/**
137 	 * Get response header.
138 	 *
139 	 * @param name
140 	 *            name of the response header to read.
141 	 * @return first value of the response header; null if not sent.
142 	 */
143 	public String getResponseHeader(String name) {
144 		return responseHeaders.get(name);
145 	}
146 
147 	/** {@inheritDoc} */
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 }