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