diff options
Diffstat (limited to 'org.eclipse.jgit.junit.http/src/org/eclipse')
7 files changed, 95 insertions, 73 deletions
diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AccessEvent.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AccessEvent.java index 873d430675..36da539e8d 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AccessEvent.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AccessEvent.java @@ -11,12 +11,14 @@ package org.eclipse.jgit.junit.http; import java.util.Collections; -import java.util.Enumeration; import java.util.Map; import java.util.TreeMap; +import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.util.Fields; /** * A single request made through {@link org.eclipse.jgit.junit.http.AppServer}. @@ -24,7 +26,7 @@ import org.eclipse.jetty.server.Response; public class AccessEvent { private final String method; - private final String uri; + private final HttpURI uri; private final Map<String, String> requestHeaders; @@ -36,9 +38,9 @@ public class AccessEvent { AccessEvent(Request req) { method = req.getMethod(); - uri = req.getRequestURI(); + uri = req.getHttpURI(); requestHeaders = cloneHeaders(req); - parameters = clone(req.getParameterMap()); + parameters = cloneParameters(req); } void setResponse(Response rsp) { @@ -48,11 +50,10 @@ public class AccessEvent { private static Map<String, String> cloneHeaders(Request req) { Map<String, String> r = new TreeMap<>(); - Enumeration hn = req.getHeaderNames(); - while (hn.hasMoreElements()) { - String key = (String) hn.nextElement(); + for (HttpField f : req.getHeaders()) { + String key = f.getName(); if (!r.containsKey(key)) { - r.put(key, req.getHeader(key)); + r.put(key, f.getValue()); } } return Collections.unmodifiableMap(r); @@ -60,20 +61,29 @@ public class AccessEvent { private static Map<String, String> cloneHeaders(Response rsp) { Map<String, String> r = new TreeMap<>(); - Enumeration<String> hn = rsp.getHttpFields().getFieldNames(); - while (hn.hasMoreElements()) { - String key = hn.nextElement(); + for (HttpField f : rsp.getHeaders()) { + String key = f.getName(); if (!r.containsKey(key)) { - Enumeration<String> v = rsp.getHttpFields().getValues(key); - r.put(key, v.nextElement()); + r.put(key, f.getValue()); } } return Collections.unmodifiableMap(r); } - @SuppressWarnings("unchecked") - private static Map<String, String[]> clone(Map parameterMap) { - return new TreeMap<>(parameterMap); + private static Map<String, String[]> cloneParameters(Request req) { + Map<String, String[]> r = new TreeMap<>(); + + Fields fields; + try { + fields = Request.getParameters(req); + for (String n : fields.getNames()) { + r.put(n, fields.getValues(n).toArray(new String[0])); + } + } catch (Exception e) { + throw new RuntimeException("Failed to extract request parameters", + e); + } + return r; } /** @@ -91,7 +101,7 @@ public class AccessEvent { * @return path of the file on the server, e.g. {@code /git/HEAD}. */ public String getPath() { - return uri; + return uri.getPath(); } /** @@ -146,13 +156,12 @@ public class AccessEvent { return responseHeaders != null ? responseHeaders.get(name) : null; } - /** {@inheritDoc} */ @Override public String toString() { StringBuilder b = new StringBuilder(); b.append(method); b.append(' '); - b.append(uri); + b.append(uri.getPath()); if (!parameters.isEmpty()) { b.append('?'); boolean first = true; diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java index 36f2f2bc7f..76e437bc3d 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java @@ -27,10 +27,12 @@ import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.security.ConstraintMapping; +import org.eclipse.jetty.ee10.servlet.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.AbstractLoginService; import org.eclipse.jetty.security.Authenticator; -import org.eclipse.jetty.security.ConstraintMapping; -import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.eclipse.jetty.security.Constraint; import org.eclipse.jetty.security.RolePrincipal; import org.eclipse.jetty.security.UserPrincipal; import org.eclipse.jetty.security.authentication.BasicAuthenticator; @@ -42,8 +44,6 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.server.handler.ContextHandlerCollection; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.util.security.Constraint; import org.eclipse.jetty.util.security.Password; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jgit.transport.URIish; @@ -243,6 +243,7 @@ public class AppServer { * path of the context; use "/" for the root context if binding * to the root is desired. * @return the context to add servlets into. + * @since 7.0 */ public ServletContextHandler addContext(String path) { assertNotYetSetUp(); @@ -260,8 +261,11 @@ public class AppServer { * Configure basic authentication. * * @param ctx + * servlet context handler * @param methods + * the methods * @return servlet context handler + * @since 7.0 */ public ServletContextHandler authBasic(ServletContextHandler ctx, String... methods) { @@ -303,10 +307,10 @@ public class AppServer { private ConstraintMapping createConstraintMapping() { ConstraintMapping cm = new ConstraintMapping(); - cm.setConstraint(new Constraint()); - cm.getConstraint().setAuthenticate(true); - cm.getConstraint().setDataConstraint(Constraint.DC_NONE); - cm.getConstraint().setRoles(new String[] { authRole }); + Constraint constraint = new Constraint.Builder() + .authorization(Constraint.Authorization.SPECIFIC_ROLE) + .roles(new String[] { authRole }).build(); + cm.setConstraint(constraint); cm.setPathSpec("/*"); return cm; } diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/HttpTestCase.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/HttpTestCase.java index 877b918695..f399471b39 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/HttpTestCase.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/HttpTestCase.java @@ -21,7 +21,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.junit.TestRepository; @@ -45,14 +45,12 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { /** In-memory application server; subclass must start. */ protected AppServer server; - /** {@inheritDoc} */ @Override public void setUp() throws Exception { super.setUp(); server = createServer(); } - /** {@inheritDoc} */ @Override public void tearDown() throws Exception { server.tearDown(); @@ -78,6 +76,7 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * * @return the TestRepository * @throws IOException + * if an IO error occurred */ protected TestRepository<Repository> createTestRepository() throws IOException { @@ -90,8 +89,10 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Convert path to URIish * * @param path + * the path * @return the URIish * @throws URISyntaxException + * if URI is invalid */ protected URIish toURIish(String path) throws URISyntaxException { URI u = server.getURI().resolve(path); @@ -102,9 +103,13 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Convert a path relative to the app's context path to a URIish * * @param app + * app name * @param name + * context path name * @return the warnings (if any) from the last execution * @throws URISyntaxException + * if URI is invalid + * @since 7.0 */ protected URIish toURIish(ServletContextHandler app, String name) throws URISyntaxException { @@ -128,7 +133,9 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Get requests. * * @param base + * base URI * @param path + * the request path relative to {@code base} * * @return list of events */ @@ -140,6 +147,7 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Get requests. * * @param path + * request path * * @return list of events */ @@ -151,8 +159,11 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Run fsck * * @param db + * the repository * @param tips + * tips to start checking from * @throws Exception + * if an error occurred */ protected static void fsck(Repository db, RevObject... tips) throws Exception { @@ -166,6 +177,7 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Mirror refs * * @param refs + * the refs * @return set of RefSpecs */ protected static Set<RefSpec> mirror(String... refs) { @@ -183,9 +195,12 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Push a commit * * @param from + * repository from which to push * @param q + * commit to push * @return collection of RefUpdates * @throws IOException + * if an IO error occurred */ protected static Collection<RemoteRefUpdate> push(TestRepository from, RevCommit q) throws IOException { @@ -205,7 +220,9 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Create loose object path * * @param base + * base URI * @param id + * objectId * @return path of the loose object */ public static String loose(URIish base, AnyObjectId id) { @@ -219,6 +236,7 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Join a base URIish and a path * * @param base + * base URI * @param path * a relative path * @return the joined path @@ -237,8 +255,11 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Rewrite a url * * @param url + * the URL * @param newProtocol + * new protocol * @param newPort + * new port * @return the rewritten url */ protected static String rewriteUrl(String url, String newProtocol, @@ -263,9 +284,12 @@ public abstract class HttpTestCase extends LocalDiskRepositoryTestCase { * Extend a path * * @param uri + * the URI * @param pathComponents + * path components * @return the extended URIish * @throws URISyntaxException + * if URI is invalid */ protected static URIish extendPath(URIish uri, String pathComponents) throws URISyntaxException { diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/MockServletConfig.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/MockServletConfig.java index 715fd19451..808b076943 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/MockServletConfig.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/MockServletConfig.java @@ -15,8 +15,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; /** * Mock ServletConfig @@ -28,19 +28,19 @@ public class MockServletConfig implements ServletConfig { * Set init parameter. * * @param name + * parameter name * @param value + * parameter value */ public void setInitParameter(String name, String value) { parameters.put(name, value); } - /** {@inheritDoc} */ @Override public String getInitParameter(String name) { return parameters.get(name); } - /** {@inheritDoc} */ @Override public Enumeration<String> getInitParameterNames() { final Iterator<String> i = parameters.keySet().iterator(); @@ -58,13 +58,11 @@ public class MockServletConfig implements ServletConfig { }; } - /** {@inheritDoc} */ @Override public String getServletName() { return "MOCK_SERVLET"; } - /** {@inheritDoc} */ @Override public ServletContext getServletContext() { return null; diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/RecordingLogger.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/RecordingLogger.java index af63084e93..f56e1fa5c4 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/RecordingLogger.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/RecordingLogger.java @@ -70,6 +70,7 @@ public class RecordingLogger extends MarkerIgnoringBase { * Constructor for <code>RecordingLogger</code>. * * @param name + * logger name */ public RecordingLogger(String name) { this.name = name; diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/SimpleHttpServer.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/SimpleHttpServer.java index 2493f999fa..bce41383d1 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/SimpleHttpServer.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/SimpleHttpServer.java @@ -13,10 +13,10 @@ package org.eclipse.jgit.junit.http; import java.net.URI; import java.net.URISyntaxException; -import javax.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequest; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletHolder; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.http.server.GitServlet; import org.eclipse.jgit.lib.Repository; @@ -40,6 +40,7 @@ public class SimpleHttpServer { * Constructor for <code>SimpleHttpServer</code>. * * @param repository + * the repository */ public SimpleHttpServer(Repository repository) { this(repository, false); @@ -49,7 +50,9 @@ public class SimpleHttpServer { * Constructor for <code>SimpleHttpServer</code>. * * @param repository + * the repository * @param withSsl + * whether to encrypt the communication */ public SimpleHttpServer(Repository repository, boolean withSsl) { this.db = repository; @@ -60,6 +63,7 @@ public class SimpleHttpServer { * Start the server * * @throws Exception + * if an error occurred */ public void start() throws Exception { ServletContextHandler sBasic = server.authBasic(smart("/sbasic")); @@ -76,6 +80,7 @@ public class SimpleHttpServer { * Stop the server. * * @throws Exception + * if an error occurred */ public void stop() throws Exception { server.tearDown(); diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/TestRequestLog.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/TestRequestLog.java index 04cb2428a2..7a71951dbd 100644 --- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/TestRequestLog.java +++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/TestRequestLog.java @@ -10,25 +10,22 @@ package org.eclipse.jgit.junit.http; -import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.Semaphore; -import javax.servlet.DispatcherType; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import org.eclipse.jetty.server.Handler.Wrapper; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.server.handler.HandlerWrapper; +import org.eclipse.jetty.util.Callback; /** Logs request made through {@link AppServer}. */ -class TestRequestLog extends HandlerWrapper { +class TestRequestLog extends Wrapper { private static final int MAX = 16; - private final List<AccessEvent> events = new ArrayList<>(); + private final List<AccessEvent> events = Collections + .synchronizedList(new ArrayList<>()); private final Semaphore active = new Semaphore(MAX, true); @@ -43,10 +40,7 @@ class TestRequestLog extends HandlerWrapper { continue; } } - - synchronized (events) { - events.clear(); - } + events.clear(); } finally { active.release(MAX); } @@ -63,20 +57,15 @@ class TestRequestLog extends HandlerWrapper { continue; } } - - synchronized (events) { - return events; - } + return Collections.unmodifiableList(new ArrayList<>(events)); } finally { active.release(MAX); } } - /** {@inheritDoc} */ @Override - public void handle(String target, Request baseRequest, - HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException { + public boolean handle(Request request, Response response, Callback callback) + throws Exception { try { for (;;) { try { @@ -87,21 +76,13 @@ class TestRequestLog extends HandlerWrapper { } } - AccessEvent event = null; - if (DispatcherType.REQUEST - .equals(baseRequest.getDispatcherType())) { - event = new AccessEvent((Request) request); - synchronized (events) { - events.add(event); - } - } + AccessEvent event = new AccessEvent(request); + events.add(event); - super.handle(target, baseRequest, request, response); - - if (event != null) { - event.setResponse((Response) response); - } + boolean result = super.handle(request, response, callback); + event.setResponse(response); + return result; } finally { active.release(); } |