Initial test server.

This commit is contained in:
Winston Li 2014-11-02 17:15:31 +00:00
parent f5143e547a
commit 535c54b5a5
2 changed files with 113 additions and 12 deletions

View file

@ -3,8 +3,6 @@
<component name="ChangeListManager">
<list default="true" id="0c822a5a-f421-4f3c-b941-5c2809c4546f" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/uk/ac/ic/Main.java" afterPath="$PROJECT_DIR$/src/uk/ac/ic/Main.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/writelatex-git-bridge.iml" afterPath="$PROJECT_DIR$/writelatex-git-bridge.iml" />
</list>
<ignored path="writelatex-git-bridge.iws" />
<ignored path=".idea/workspace.xml" />
@ -60,8 +58,8 @@
<file leaf-file-name="Main.java" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/uk/ac/ic/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.38356164" vertical-offset="165" max-vertical-offset="1905">
<caret line="39" column="148" selection-start-line="39" selection-start-column="148" selection-end-line="39" selection-end-column="148" />
<state vertical-scroll-proportion="0.56164384" vertical-offset="0" max-vertical-offset="1905">
<caret line="41" column="0" selection-start-line="41" selection-start-column="0" selection-end-line="41" selection-end-column="0" />
<folding />
</state>
</provider>
@ -387,7 +385,7 @@
</component>
<component name="ToolWindowManager">
<frame x="0" y="23" width="2559" height="1413" extended-state="6" />
<editor active="true" />
<editor active="false" />
<layout>
<window_info id="Documentation" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="FLOATING" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" x="100" y="123" width="1166" height="541" />
<window_info id="Palette&#9;" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
@ -406,7 +404,7 @@
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="SLIDING" type="SLIDING" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Application Servers" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.13325518" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.13325518" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.16443452" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
@ -505,8 +503,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/uk/ac/ic/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.38356164" vertical-offset="165" max-vertical-offset="1905">
<caret line="39" column="148" selection-start-line="39" selection-start-column="148" selection-end-line="39" selection-end-column="148" />
<state vertical-scroll-proportion="0.56164384" vertical-offset="0" max-vertical-offset="1905">
<caret line="41" column="0" selection-start-line="41" selection-start-column="0" selection-end-line="41" selection-end-column="0" />
<folding />
</state>
</provider>

View file

@ -1,18 +1,121 @@
package uk.ac.ic;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.http.server.GitServlet;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.resolver.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
/**
* Created by Roxy on 01/11/14.
* Created by Winston on 01/11/14.
*/
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
GitServlet servlet = new GitServlet();
servlet.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
@Override
public Repository open(HttpServletRequest httpServletRequest, String s) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException {
System.out.println(s);
File workspace = new File("/Users/Roxy/git-test/hello");
workspace.mkdirs();
Repository r = null;
try {
r = new FileRepositoryBuilder().setWorkTree(workspace).build();
} catch (IOException e) {
e.printStackTrace();
}
// if the repository doesn't exist, create it
if (!r.getObjectDatabase().exists()) {
try {
r.create();
} catch (IOException e) {
e.printStackTrace();
}
try {
// import initial content
Git git = new Git(r);
AddCommand cmd = git.add();
cmd.addFilepattern(".");
cmd.call();
CommitCommand co = git.commit();
co.setAuthor("Winston","wl3912@ic.ac.uk");
co.setMessage("Initial import of the existing contents");
co.call();
} catch (GitAPIException e) {
e.printStackTrace();
}
}
return r;
}
});
servlet.setReceivePackFactory(new ReceivePackFactory<HttpServletRequest>() {
@Override
public ReceivePack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException {
return new ReceivePack(repository);
}
});
servlet.setUploadPackFactory(new UploadPackFactory<HttpServletRequest>() {
@Override
public UploadPack create(HttpServletRequest httpServletRequest, Repository repository) throws ServiceNotEnabledException, ServiceNotAuthorizedException {
return new UploadPack(repository);
}
});
servlet.init(new ServletConfig() {
@Override
public String getServletName() {
return "";
}
@Override
public ServletContext getServletContext() {
return servletContextHandler.getServletContext();
}
@Override
public String getInitParameter(String s) {
return "default_init_parameter";
}
@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
});
servletContextHandler.addServlet(new ServletHolder(servlet), "/*");
server.start();
server.join();
}
}