151e2646dSJens Baumgart /* 251e2646dSJens Baumgart * Copyright (C) 2009-2010, Google Inc. 3*5c5f7c6bSMatthias Sohn * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> and others 451e2646dSJens Baumgart * 5*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 6*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 7*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 851e2646dSJens Baumgart * 9*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 1051e2646dSJens Baumgart */ 1151e2646dSJens Baumgart package org.eclipse.jgit.junit.http; 1251e2646dSJens Baumgart 1351e2646dSJens Baumgart import java.net.URI; 1451e2646dSJens Baumgart import java.net.URISyntaxException; 1551e2646dSJens Baumgart 1651e2646dSJens Baumgart import javax.servlet.http.HttpServletRequest; 1751e2646dSJens Baumgart 1851e2646dSJens Baumgart import org.eclipse.jetty.servlet.ServletContextHandler; 1951e2646dSJens Baumgart import org.eclipse.jetty.servlet.ServletHolder; 2051e2646dSJens Baumgart import org.eclipse.jgit.errors.RepositoryNotFoundException; 2151e2646dSJens Baumgart import org.eclipse.jgit.http.server.GitServlet; 2251e2646dSJens Baumgart import org.eclipse.jgit.lib.Repository; 2351e2646dSJens Baumgart import org.eclipse.jgit.transport.URIish; 2451e2646dSJens Baumgart 2551e2646dSJens Baumgart /** 2651e2646dSJens Baumgart * Simple http server for testing http access to Git repositories. 2751e2646dSJens Baumgart * Authentication with hardcoded credentials user:agitter password:letmein. 2851e2646dSJens Baumgart */ 2951e2646dSJens Baumgart public class SimpleHttpServer { 3051e2646dSJens Baumgart 3151e2646dSJens Baumgart AppServer server; 3251e2646dSJens Baumgart 339b63f324SMatthias Sohn private final Repository db; 3451e2646dSJens Baumgart 3551e2646dSJens Baumgart private URIish uri; 3651e2646dSJens Baumgart 37d946f95cSThomas Wolf private URIish secureUri; 38d946f95cSThomas Wolf 390b131b73SMatthias Sohn /** 400b131b73SMatthias Sohn * Constructor for <code>SimpleHttpServer</code>. 410b131b73SMatthias Sohn * 420b131b73SMatthias Sohn * @param repository 430b131b73SMatthias Sohn */ SimpleHttpServer(Repository repository)449b63f324SMatthias Sohn public SimpleHttpServer(Repository repository) { 45d946f95cSThomas Wolf this(repository, false); 46d946f95cSThomas Wolf } 47d946f95cSThomas Wolf 480b131b73SMatthias Sohn /** 490b131b73SMatthias Sohn * Constructor for <code>SimpleHttpServer</code>. 500b131b73SMatthias Sohn * 510b131b73SMatthias Sohn * @param repository 520b131b73SMatthias Sohn * @param withSsl 530b131b73SMatthias Sohn */ SimpleHttpServer(Repository repository, boolean withSsl)54d946f95cSThomas Wolf public SimpleHttpServer(Repository repository, boolean withSsl) { 5551e2646dSJens Baumgart this.db = repository; 56d946f95cSThomas Wolf server = new AppServer(0, withSsl ? 0 : -1); 5751e2646dSJens Baumgart } 5851e2646dSJens Baumgart 590b131b73SMatthias Sohn /** 600b131b73SMatthias Sohn * Start the server 610b131b73SMatthias Sohn * 620b131b73SMatthias Sohn * @throws Exception 630b131b73SMatthias Sohn */ start()6451e2646dSJens Baumgart public void start() throws Exception { 6551e2646dSJens Baumgart ServletContextHandler sBasic = server.authBasic(smart("/sbasic")); 6651e2646dSJens Baumgart server.setUp(); 6751e2646dSJens Baumgart final String srcName = db.getDirectory().getName(); 6851e2646dSJens Baumgart uri = toURIish(sBasic, srcName); 69d946f95cSThomas Wolf int sslPort = server.getSecurePort(); 70d946f95cSThomas Wolf if (sslPort > 0) { 71d946f95cSThomas Wolf secureUri = uri.setPort(sslPort).setScheme("https"); 72d946f95cSThomas Wolf } 7351e2646dSJens Baumgart } 7451e2646dSJens Baumgart 750b131b73SMatthias Sohn /** 760b131b73SMatthias Sohn * Stop the server. 770b131b73SMatthias Sohn * 780b131b73SMatthias Sohn * @throws Exception 790b131b73SMatthias Sohn */ stop()8051e2646dSJens Baumgart public void stop() throws Exception { 8151e2646dSJens Baumgart server.tearDown(); 8251e2646dSJens Baumgart } 8351e2646dSJens Baumgart 840b131b73SMatthias Sohn /** 850b131b73SMatthias Sohn * Get the <code>uri</code>. 860b131b73SMatthias Sohn * 870b131b73SMatthias Sohn * @return the uri 880b131b73SMatthias Sohn */ getUri()8951e2646dSJens Baumgart public URIish getUri() { 9051e2646dSJens Baumgart return uri; 9151e2646dSJens Baumgart } 9251e2646dSJens Baumgart 930b131b73SMatthias Sohn /** 940b131b73SMatthias Sohn * Get the <code>secureUri</code>. 950b131b73SMatthias Sohn * 960b131b73SMatthias Sohn * @return the secure uri 970b131b73SMatthias Sohn */ getSecureUri()98d946f95cSThomas Wolf public URIish getSecureUri() { 99d946f95cSThomas Wolf return secureUri; 100d946f95cSThomas Wolf } 101d946f95cSThomas Wolf smart(String path)1026d370d83SHan-Wen Nienhuys private ServletContextHandler smart(String path) { 10351e2646dSJens Baumgart GitServlet gs = new GitServlet(); 10484fc5c90SCarsten Hammer gs.setRepositoryResolver((HttpServletRequest req, String name) -> { 10584fc5c90SCarsten Hammer if (!name.equals(nameOf(db))) { 10651e2646dSJens Baumgart throw new RepositoryNotFoundException(name); 10784fc5c90SCarsten Hammer } 10851e2646dSJens Baumgart db.incrementOpen(); 10951e2646dSJens Baumgart return db; 11051e2646dSJens Baumgart }); 11151e2646dSJens Baumgart 11251e2646dSJens Baumgart ServletContextHandler ctx = server.addContext(path); 11351e2646dSJens Baumgart ctx.addServlet(new ServletHolder(gs), "/*"); 11451e2646dSJens Baumgart return ctx; 11551e2646dSJens Baumgart } 11651e2646dSJens Baumgart nameOf(Repository db)1176d370d83SHan-Wen Nienhuys private static String nameOf(Repository db) { 11851e2646dSJens Baumgart return db.getDirectory().getName(); 11951e2646dSJens Baumgart } 12051e2646dSJens Baumgart toURIish(String path)12151e2646dSJens Baumgart private URIish toURIish(String path) throws URISyntaxException { 12251e2646dSJens Baumgart URI u = server.getURI().resolve(path); 12351e2646dSJens Baumgart return new URIish(u.toString()); 12451e2646dSJens Baumgart } 12551e2646dSJens Baumgart toURIish(ServletContextHandler app, String name)12651e2646dSJens Baumgart private URIish toURIish(ServletContextHandler app, String name) 12751e2646dSJens Baumgart throws URISyntaxException { 12851e2646dSJens Baumgart String p = app.getContextPath(); 12951e2646dSJens Baumgart if (!p.endsWith("/") && !name.startsWith("/")) 13051e2646dSJens Baumgart p += "/"; 13151e2646dSJens Baumgart p += name; 13251e2646dSJens Baumgart return toURIish(p); 13351e2646dSJens Baumgart } 13451e2646dSJens Baumgart } 135