151e2646dSJens Baumgart /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2010, Google Inc. and others 351e2646dSJens Baumgart * 4*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 5*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 751e2646dSJens Baumgart * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 951e2646dSJens Baumgart */ 1051e2646dSJens Baumgart 1151e2646dSJens Baumgart package org.eclipse.jgit.junit.http; 1251e2646dSJens Baumgart 1351e2646dSJens Baumgart import java.util.Collections; 1451e2646dSJens Baumgart import java.util.Enumeration; 1551e2646dSJens Baumgart import java.util.Map; 1651e2646dSJens Baumgart import java.util.TreeMap; 1751e2646dSJens Baumgart 1851e2646dSJens Baumgart import org.eclipse.jetty.server.Request; 1951e2646dSJens Baumgart import org.eclipse.jetty.server.Response; 2051e2646dSJens Baumgart 210b131b73SMatthias Sohn /** 220b131b73SMatthias Sohn * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}. 230b131b73SMatthias Sohn */ 2451e2646dSJens Baumgart public class AccessEvent { 2551e2646dSJens Baumgart private final String method; 2651e2646dSJens Baumgart 2751e2646dSJens Baumgart private final String uri; 2851e2646dSJens Baumgart 2951e2646dSJens Baumgart private final Map<String, String> requestHeaders; 3051e2646dSJens Baumgart 3151e2646dSJens Baumgart private final Map<String, String[]> parameters; 3251e2646dSJens Baumgart 3351e2646dSJens Baumgart private final int status; 3451e2646dSJens Baumgart 3551e2646dSJens Baumgart private final Map<String, String> responseHeaders; 3651e2646dSJens Baumgart AccessEvent(Request req, Response rsp)37f3ec7cf3SHan-Wen Nienhuys AccessEvent(Request req, Response rsp) { 3851e2646dSJens Baumgart method = req.getMethod(); 3951e2646dSJens Baumgart uri = req.getRequestURI(); 4051e2646dSJens Baumgart requestHeaders = cloneHeaders(req); 4151e2646dSJens Baumgart parameters = clone(req.getParameterMap()); 4251e2646dSJens Baumgart 4351e2646dSJens Baumgart status = rsp.getStatus(); 4451e2646dSJens Baumgart responseHeaders = cloneHeaders(rsp); 4551e2646dSJens Baumgart } 4651e2646dSJens Baumgart cloneHeaders(Request req)476d370d83SHan-Wen Nienhuys private static Map<String, String> cloneHeaders(Request req) { 483b444863SDavid Pursehouse Map<String, String> r = new TreeMap<>(); 4951e2646dSJens Baumgart Enumeration hn = req.getHeaderNames(); 5051e2646dSJens Baumgart while (hn.hasMoreElements()) { 5151e2646dSJens Baumgart String key = (String) hn.nextElement(); 5251e2646dSJens Baumgart if (!r.containsKey(key)) { 5351e2646dSJens Baumgart r.put(key, req.getHeader(key)); 5451e2646dSJens Baumgart } 5551e2646dSJens Baumgart } 5651e2646dSJens Baumgart return Collections.unmodifiableMap(r); 5751e2646dSJens Baumgart } 5851e2646dSJens Baumgart cloneHeaders(Response rsp)596d370d83SHan-Wen Nienhuys private static Map<String, String> cloneHeaders(Response rsp) { 603b444863SDavid Pursehouse Map<String, String> r = new TreeMap<>(); 6151e2646dSJens Baumgart Enumeration<String> hn = rsp.getHttpFields().getFieldNames(); 6251e2646dSJens Baumgart while (hn.hasMoreElements()) { 6351e2646dSJens Baumgart String key = hn.nextElement(); 6451e2646dSJens Baumgart if (!r.containsKey(key)) { 6551e2646dSJens Baumgart Enumeration<String> v = rsp.getHttpFields().getValues(key); 6651e2646dSJens Baumgart r.put(key, v.nextElement()); 6751e2646dSJens Baumgart } 6851e2646dSJens Baumgart } 6951e2646dSJens Baumgart return Collections.unmodifiableMap(r); 7051e2646dSJens Baumgart } 7151e2646dSJens Baumgart 7251e2646dSJens Baumgart @SuppressWarnings("unchecked") clone(Map parameterMap)7351e2646dSJens Baumgart private static Map<String, String[]> clone(Map parameterMap) { 7476e86f4eSShawn Pearce return new TreeMap<>(parameterMap); 7551e2646dSJens Baumgart } 7651e2646dSJens Baumgart 770b131b73SMatthias Sohn /** 780b131b73SMatthias Sohn * Get the <code>method</code>. 790b131b73SMatthias Sohn * 800b131b73SMatthias Sohn * @return {@code "GET"} or {@code "POST"} 810b131b73SMatthias Sohn */ getMethod()8251e2646dSJens Baumgart public String getMethod() { 8351e2646dSJens Baumgart return method; 8451e2646dSJens Baumgart } 8551e2646dSJens Baumgart 860b131b73SMatthias Sohn /** 870b131b73SMatthias Sohn * Get <code>path</code>. 880b131b73SMatthias Sohn * 890b131b73SMatthias Sohn * @return path of the file on the server, e.g. {@code /git/HEAD}. 900b131b73SMatthias Sohn */ getPath()9151e2646dSJens Baumgart public String getPath() { 9251e2646dSJens Baumgart return uri; 9351e2646dSJens Baumgart } 9451e2646dSJens Baumgart 9551e2646dSJens Baumgart /** 960b131b73SMatthias Sohn * Get request header 970b131b73SMatthias Sohn * 9851e2646dSJens Baumgart * @param name 9951e2646dSJens Baumgart * name of the request header to read. 10051e2646dSJens Baumgart * @return first value of the request header; null if not sent. 10151e2646dSJens Baumgart */ getRequestHeader(String name)10251e2646dSJens Baumgart public String getRequestHeader(String name) { 10351e2646dSJens Baumgart return requestHeaders.get(name); 10451e2646dSJens Baumgart } 10551e2646dSJens Baumgart 10651e2646dSJens Baumgart /** 1070b131b73SMatthias Sohn * Get parameter 1080b131b73SMatthias Sohn * 10951e2646dSJens Baumgart * @param name 11051e2646dSJens Baumgart * name of the request parameter to read. 11151e2646dSJens Baumgart * @return first value of the request parameter; null if not sent. 11251e2646dSJens Baumgart */ getParameter(String name)11351e2646dSJens Baumgart public String getParameter(String name) { 11451e2646dSJens Baumgart String[] r = parameters.get(name); 11551e2646dSJens Baumgart return r != null && 1 <= r.length ? r[0] : null; 11651e2646dSJens Baumgart } 11751e2646dSJens Baumgart 1180b131b73SMatthias Sohn /** 1190b131b73SMatthias Sohn * Get <code>parameters</code> 1200b131b73SMatthias Sohn * 1210b131b73SMatthias Sohn * @return all parameters in the request. 1220b131b73SMatthias Sohn */ getParameters()12351e2646dSJens Baumgart public Map<String, String[]> getParameters() { 12451e2646dSJens Baumgart return parameters; 12551e2646dSJens Baumgart } 12651e2646dSJens Baumgart 1270b131b73SMatthias Sohn /** 1280b131b73SMatthias Sohn * Get the <code>status</code>. 1290b131b73SMatthias Sohn * 1300b131b73SMatthias Sohn * @return HTTP status code of the response, e.g. 200, 403, 500. 1310b131b73SMatthias Sohn */ getStatus()13251e2646dSJens Baumgart public int getStatus() { 13351e2646dSJens Baumgart return status; 13451e2646dSJens Baumgart } 13551e2646dSJens Baumgart 13651e2646dSJens Baumgart /** 1370b131b73SMatthias Sohn * Get response header. 1380b131b73SMatthias Sohn * 13951e2646dSJens Baumgart * @param name 14051e2646dSJens Baumgart * name of the response header to read. 14151e2646dSJens Baumgart * @return first value of the response header; null if not sent. 14251e2646dSJens Baumgart */ getResponseHeader(String name)14351e2646dSJens Baumgart public String getResponseHeader(String name) { 14451e2646dSJens Baumgart return responseHeaders.get(name); 14551e2646dSJens Baumgart } 14651e2646dSJens Baumgart 1470b131b73SMatthias Sohn /** {@inheritDoc} */ 1487ac182f4SDavid Pursehouse @Override toString()14951e2646dSJens Baumgart public String toString() { 15051e2646dSJens Baumgart StringBuilder b = new StringBuilder(); 15151e2646dSJens Baumgart b.append(method); 15251e2646dSJens Baumgart b.append(' '); 15351e2646dSJens Baumgart b.append(uri); 15451e2646dSJens Baumgart if (!parameters.isEmpty()) { 15551e2646dSJens Baumgart b.append('?'); 15651e2646dSJens Baumgart boolean first = true; 15751e2646dSJens Baumgart for (Map.Entry<String, String[]> e : parameters.entrySet()) { 15851e2646dSJens Baumgart for (String val : e.getValue()) { 15951e2646dSJens Baumgart if (!first) { 16051e2646dSJens Baumgart b.append('&'); 16151e2646dSJens Baumgart } 16251e2646dSJens Baumgart first = false; 16351e2646dSJens Baumgart 16451e2646dSJens Baumgart b.append(e.getKey()); 16551e2646dSJens Baumgart b.append('='); 16651e2646dSJens Baumgart b.append(val); 16751e2646dSJens Baumgart } 16851e2646dSJens Baumgart } 16951e2646dSJens Baumgart } 17051e2646dSJens Baumgart b.append(' '); 17151e2646dSJens Baumgart b.append(status); 17251e2646dSJens Baumgart return b.toString(); 17351e2646dSJens Baumgart } 17451e2646dSJens Baumgart } 175