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  /** A single request made through {@link AppServer}. */
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 	/** @return {@code "GET"} or {@code "POST"} */
109 	public String getMethod() {
110 		return method;
111 	}
112 
113 	/** @return path of the file on the server, e.g. {@code /git/HEAD}. */
114 	public String getPath() {
115 		return uri;
116 	}
117 
118 	/**
119 	 * @param name
120 	 *            name of the request header to read.
121 	 * @return first value of the request header; null if not sent.
122 	 */
123 	public String getRequestHeader(String name) {
124 		return requestHeaders.get(name);
125 	}
126 
127 	/**
128 	 * @param name
129 	 *            name of the request parameter to read.
130 	 * @return first value of the request parameter; null if not sent.
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 	/** @return all parameters in the request. */
138 	public Map<String, String[]> getParameters() {
139 		return parameters;
140 	}
141 
142 	/** @return HTTP status code of the response, e.g. 200, 403, 500. */
143 	public int getStatus() {
144 		return status;
145 	}
146 
147 	/**
148 	 * @param name
149 	 *            name of the response header to read.
150 	 * @return first value of the response header; null if not sent.
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 }