WLGitBridgeServer.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package uk.ac.ic.wlgitbridge.application;
  2. import org.eclipse.jetty.server.Handler;
  3. import org.eclipse.jetty.server.Server;
  4. import org.eclipse.jetty.server.handler.HandlerCollection;
  5. import org.eclipse.jetty.server.handler.ResourceHandler;
  6. import org.eclipse.jetty.servlet.ServletContextHandler;
  7. import org.eclipse.jetty.servlet.ServletHolder;
  8. import org.eclipse.jetty.util.log.Log;
  9. import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
  10. import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
  11. import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
  12. import uk.ac.ic.wlgitbridge.git.servlet.WLGitServlet;
  13. import uk.ac.ic.wlgitbridge.util.Util;
  14. import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI;
  15. import uk.ac.ic.wlgitbridge.writelatex.api.request.base.SnapshotAPIRequest;
  16. import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
  17. import javax.servlet.ServletException;
  18. import java.io.File;
  19. import java.net.BindException;
  20. /**
  21. * Created by Winston on 02/11/14.
  22. */
  23. /**
  24. * Class for the actual server.
  25. */
  26. public class WLGitBridgeServer {
  27. private final Server jettyServer;
  28. private final int port;
  29. private String rootGitDirectoryPath;
  30. private String writeLatexHostname;
  31. /**
  32. * Constructs an instance of the server.
  33. * @param port the port number to listen on
  34. * @param rootGitDirectoryPath the root directory path containing the git repositories
  35. * @throws ServletException if the servlet throws an exception
  36. */
  37. private WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException, InvalidRootDirectoryPathException {
  38. this.port = port;
  39. this.rootGitDirectoryPath = rootGitDirectoryPath;
  40. Log.setLog(new NullLogger());
  41. jettyServer = new Server(port);
  42. configureJettyServer();
  43. }
  44. public WLGitBridgeServer(Config config) throws ServletException, InvalidRootDirectoryPathException {
  45. this(config.getPort(), config.getRootGitDirectory());
  46. SnapshotAPIRequest.setBasicAuth(config.getUsername(), config.getPassword());
  47. writeLatexHostname = config.getAPIBaseURL();
  48. SnapshotAPIRequest.setBaseURL(writeLatexHostname);
  49. Util.setServiceName(config.getServiceName());
  50. Util.setHostname(config.getHostname());
  51. }
  52. /**
  53. * Starts the server on the port given on construction.
  54. */
  55. public void start() {
  56. try {
  57. jettyServer.start();
  58. System.out.println();
  59. System.out.println(Util.getServiceName() + "-Git Bridge server started");
  60. System.out.println("Hostname: " + Util.getHostname());
  61. System.out.println("Listening on port: " + port);
  62. System.out.println("Bridged to: " + writeLatexHostname);
  63. System.out.println("Root git directory path: " + rootGitDirectoryPath);
  64. } catch (BindException e) {
  65. e.printStackTrace();
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. try {
  70. jettyServer.join();
  71. } catch (InterruptedException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. private void configureJettyServer() throws ServletException, InvalidRootDirectoryPathException {
  76. HandlerCollection handlers = new HandlerCollection();
  77. WriteLatexAPI writeLatexDataSource = new WriteLatexAPI(new WLDataModel(rootGitDirectoryPath));
  78. handlers.setHandlers(new Handler[] {
  79. initResourceHandler(writeLatexDataSource),
  80. new SnapshotPushPostbackHandler(writeLatexDataSource),
  81. initGitHandler(writeLatexDataSource)
  82. });
  83. jettyServer.setHandler(handlers);
  84. }
  85. private Handler initGitHandler(WriteLatexDataSource writeLatexDataSource) throws ServletException, InvalidRootDirectoryPathException {
  86. final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
  87. servletContextHandler.setContextPath("/");
  88. servletContextHandler.addServlet(
  89. new ServletHolder(
  90. new WLGitServlet(servletContextHandler, writeLatexDataSource, rootGitDirectoryPath)),
  91. "/*"
  92. );
  93. return servletContextHandler;
  94. }
  95. private Handler initResourceHandler(WriteLatexAPI writeLatexDataSource) {
  96. ResourceHandler resourceHandler = new AttsResourceHandler(writeLatexDataSource);
  97. resourceHandler.setResourceBase(new File(rootGitDirectoryPath, ".wlgb/atts").getAbsolutePath());
  98. return resourceHandler;
  99. }
  100. }