View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}.
56   */
57  public class AccessEvent {
58  	private final String method;
59  
60  	private final String uri;
61  
62  	private final Map<String, String> requestHeaders;
63  
64  	private final Map<String, String[]> parameters;
65  
66  	private final int status;
67  
68  	private final Map<String, String> responseHeaders;
69  
70  	AccessEvent(Request req, Response rsp) {
71  		method = req.getMethod();
72  		uri = req.getRequestURI();
73  		requestHeaders = cloneHeaders(req);
74  		parameters = clone(req.getParameterMap());
75  
76  		status = rsp.getStatus();
77  		responseHeaders = cloneHeaders(rsp);
78  	}
79  
80  	private static Map<String, String> cloneHeaders(Request req) {
81  		Map<String, String> r = new TreeMap<>();
82  		Enumeration hn = req.getHeaderNames();
83  		while (hn.hasMoreElements()) {
84  			String key = (String) hn.nextElement();
85  			if (!r.containsKey(key)) {
86  				r.put(key, req.getHeader(key));
87  			}
88  		}
89  		return Collections.unmodifiableMap(r);
90  	}
91  
92  	private static Map<String, String> cloneHeaders(Response rsp) {
93  		Map<String, String> r = new TreeMap<>();
94  		Enumeration<String> hn = rsp.getHttpFields().getFieldNames();
95  		while (hn.hasMoreElements()) {
96  			String key = hn.nextElement();
97  			if (!r.containsKey(key)) {
98  				Enumeration<String> v = rsp.getHttpFields().getValues(key);
99  				r.put(key, v.nextElement());
100 			}
101 		}
102 		return Collections.unmodifiableMap(r);
103 	}
104 
105 	@SuppressWarnings("unchecked")
106 	private static Map<String, String[]> clone(Map parameterMap) {
107 		return new TreeMap<>(parameterMap);
108 	}
109 
110 	/**
111 	 * Get the <code>method</code>.
112 	 *
113 	 * @return {@code "GET"} or {@code "POST"}
114 	 */
115 	public String getMethod() {
116 		return method;
117 	}
118 
119 	/**
120 	 * Get <code>path</code>.
121 	 *
122 	 * @return path of the file on the server, e.g. {@code /git/HEAD}.
123 	 */
124 	public String getPath() {
125 		return uri;
126 	}
127 
128 	/**
129 	 * Get request header
130 	 *
131 	 * @param name
132 	 *            name of the request header to read.
133 	 * @return first value of the request header; null if not sent.
134 	 */
135 	public String getRequestHeader(String name) {
136 		return requestHeaders.get(name);
137 	}
138 
139 	/**
140 	 * Get parameter
141 	 *
142 	 * @param name
143 	 *            name of the request parameter to read.
144 	 * @return first value of the request parameter; null if not sent.
145 	 */
146 	public String getParameter(String name) {
147 		String[] r = parameters.get(name);
148 		return r != null && 1 <= r.length ? r[0] : null;
149 	}
150 
151 	/**
152 	 * Get <code>parameters</code>
153 	 *
154 	 * @return all parameters in the request.
155 	 */
156 	public Map<String, String[]> getParameters() {
157 		return parameters;
158 	}
159 
160 	/**
161 	 * Get the <code>status</code>.
162 	 *
163 	 * @return HTTP status code of the response, e.g. 200, 403, 500.
164 	 */
165 	public int getStatus() {
166 		return status;
167 	}
168 
169 	/**
170 	 * Get response header.
171 	 *
172 	 * @param name
173 	 *            name of the response header to read.
174 	 * @return first value of the response header; null if not sent.
175 	 */
176 	public String getResponseHeader(String name) {
177 		return responseHeaders.get(name);
178 	}
179 
180 	/** {@inheritDoc} */
181 	@Override
182 	public String toString() {
183 		StringBuilder b = new StringBuilder();
184 		b.append(method);
185 		b.append(' ');
186 		b.append(uri);
187 		if (!parameters.isEmpty()) {
188 			b.append('?');
189 			boolean first = true;
190 			for (Map.Entry<String, String[]> e : parameters.entrySet()) {
191 				for (String val : e.getValue()) {
192 					if (!first) {
193 						b.append('&');
194 					}
195 					first = false;
196 
197 					b.append(e.getKey());
198 					b.append('=');
199 					b.append(val);
200 				}
201 			}
202 		}
203 		b.append(' ');
204 		b.append(status);
205 		return b.toString();
206 	}
207 }