You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleHttpServer.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. import java.net.URI;
  13. import java.net.URISyntaxException;
  14. import javax.servlet.http.HttpServletRequest;
  15. import org.eclipse.jetty.servlet.ServletContextHandler;
  16. import org.eclipse.jetty.servlet.ServletHolder;
  17. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  18. import org.eclipse.jgit.http.server.GitServlet;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.transport.URIish;
  21. /**
  22. * Simple http server for testing http access to Git repositories.
  23. * Authentication with hardcoded credentials user:agitter password:letmein.
  24. */
  25. public class SimpleHttpServer {
  26. AppServer server;
  27. private final Repository db;
  28. private URIish uri;
  29. private URIish secureUri;
  30. /**
  31. * Constructor for <code>SimpleHttpServer</code>.
  32. *
  33. * @param repository
  34. */
  35. public SimpleHttpServer(Repository repository) {
  36. this(repository, false);
  37. }
  38. /**
  39. * Constructor for <code>SimpleHttpServer</code>.
  40. *
  41. * @param repository
  42. * @param withSsl
  43. */
  44. public SimpleHttpServer(Repository repository, boolean withSsl) {
  45. this.db = repository;
  46. server = new AppServer(0, withSsl ? 0 : -1);
  47. }
  48. /**
  49. * Start the server
  50. *
  51. * @throws Exception
  52. */
  53. public void start() throws Exception {
  54. ServletContextHandler sBasic = server.authBasic(smart("/sbasic"));
  55. server.setUp();
  56. final String srcName = db.getDirectory().getName();
  57. uri = toURIish(sBasic, srcName);
  58. int sslPort = server.getSecurePort();
  59. if (sslPort > 0) {
  60. secureUri = uri.setPort(sslPort).setScheme("https");
  61. }
  62. }
  63. /**
  64. * Stop the server.
  65. *
  66. * @throws Exception
  67. */
  68. public void stop() throws Exception {
  69. server.tearDown();
  70. }
  71. /**
  72. * Get the <code>uri</code>.
  73. *
  74. * @return the uri
  75. */
  76. public URIish getUri() {
  77. return uri;
  78. }
  79. /**
  80. * Get the <code>secureUri</code>.
  81. *
  82. * @return the secure uri
  83. */
  84. public URIish getSecureUri() {
  85. return secureUri;
  86. }
  87. private ServletContextHandler smart(String path) {
  88. GitServlet gs = new GitServlet();
  89. gs.setRepositoryResolver((HttpServletRequest req, String name) -> {
  90. if (!name.equals(nameOf(db))) {
  91. throw new RepositoryNotFoundException(name);
  92. }
  93. db.incrementOpen();
  94. return db;
  95. });
  96. ServletContextHandler ctx = server.addContext(path);
  97. ctx.addServlet(new ServletHolder(gs), "/*");
  98. return ctx;
  99. }
  100. private static String nameOf(Repository db) {
  101. return db.getDirectory().getName();
  102. }
  103. private URIish toURIish(String path) throws URISyntaxException {
  104. URI u = server.getURI().resolve(path);
  105. return new URIish(u.toString());
  106. }
  107. private URIish toURIish(ServletContextHandler app, String name)
  108. throws URISyntaxException {
  109. String p = app.getContextPath();
  110. if (!p.endsWith("/") && !name.startsWith("/"))
  111. p += "/";
  112. p += name;
  113. return toURIish(p);
  114. }
  115. }