xref: /JGit/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/SimpleHttpServer.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2009-2010, Google Inc.
3  * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> and others
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Distribution License v. 1.0 which is available at
7  * https://www.eclipse.org/org/documents/edl-v10.php.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  */
11 package org.eclipse.jgit.junit.http;
12 
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 
16 import javax.servlet.http.HttpServletRequest;
17 
18 import org.eclipse.jetty.servlet.ServletContextHandler;
19 import org.eclipse.jetty.servlet.ServletHolder;
20 import org.eclipse.jgit.errors.RepositoryNotFoundException;
21 import org.eclipse.jgit.http.server.GitServlet;
22 import org.eclipse.jgit.lib.Repository;
23 import org.eclipse.jgit.transport.URIish;
24 
25 /**
26  * Simple http server for testing http access to Git repositories.
27  * Authentication with hardcoded credentials user:agitter password:letmein.
28  */
29 public class SimpleHttpServer {
30 
31 	AppServer server;
32 
33 	private final Repository db;
34 
35 	private URIish uri;
36 
37 	private URIish secureUri;
38 
39 	/**
40 	 * Constructor for <code>SimpleHttpServer</code>.
41 	 *
42 	 * @param repository
43 	 */
SimpleHttpServer(Repository repository)44 	public SimpleHttpServer(Repository repository) {
45 		this(repository, false);
46 	}
47 
48 	/**
49 	 * Constructor for <code>SimpleHttpServer</code>.
50 	 *
51 	 * @param repository
52 	 * @param withSsl
53 	 */
SimpleHttpServer(Repository repository, boolean withSsl)54 	public SimpleHttpServer(Repository repository, boolean withSsl) {
55 		this.db = repository;
56 		server = new AppServer(0, withSsl ? 0 : -1);
57 	}
58 
59 	/**
60 	 * Start the server
61 	 *
62 	 * @throws Exception
63 	 */
start()64 	public void start() throws Exception {
65 		ServletContextHandler sBasic = server.authBasic(smart("/sbasic"));
66 		server.setUp();
67 		final String srcName = db.getDirectory().getName();
68 		uri = toURIish(sBasic, srcName);
69 		int sslPort = server.getSecurePort();
70 		if (sslPort > 0) {
71 			secureUri = uri.setPort(sslPort).setScheme("https");
72 		}
73 	}
74 
75 	/**
76 	 * Stop the server.
77 	 *
78 	 * @throws Exception
79 	 */
stop()80 	public void stop() throws Exception {
81 		server.tearDown();
82 	}
83 
84 	/**
85 	 * Get the <code>uri</code>.
86 	 *
87 	 * @return the uri
88 	 */
getUri()89 	public URIish getUri() {
90 		return uri;
91 	}
92 
93 	/**
94 	 * Get the <code>secureUri</code>.
95 	 *
96 	 * @return the secure uri
97 	 */
getSecureUri()98 	public URIish getSecureUri() {
99 		return secureUri;
100 	}
101 
smart(String path)102 	private ServletContextHandler smart(String path) {
103 		GitServlet gs = new GitServlet();
104 		gs.setRepositoryResolver((HttpServletRequest req, String name) -> {
105 			if (!name.equals(nameOf(db))) {
106 				throw new RepositoryNotFoundException(name);
107 			}
108 			db.incrementOpen();
109 			return db;
110 		});
111 
112 		ServletContextHandler ctx = server.addContext(path);
113 		ctx.addServlet(new ServletHolder(gs), "/*");
114 		return ctx;
115 	}
116 
nameOf(Repository db)117 	private static String nameOf(Repository db) {
118 		return db.getDirectory().getName();
119 	}
120 
toURIish(String path)121 	private URIish toURIish(String path) throws URISyntaxException {
122 		URI u = server.getURI().resolve(path);
123 		return new URIish(u.toString());
124 	}
125 
toURIish(ServletContextHandler app, String name)126 	private URIish toURIish(ServletContextHandler app, String name)
127 			throws URISyntaxException {
128 		String p = app.getContextPath();
129 		if (!p.endsWith("/") && !name.startsWith("/"))
130 			p += "/";
131 		p += name;
132 		return toURIish(p);
133 	}
134 }
135