Reorganise postbacks and files under /api

This commit is contained in:
John Lees-Miller 2016-06-03 16:11:02 +01:00
parent 7ed93bc2cc
commit 1ac07526dc
5 changed files with 75 additions and 62 deletions

View file

@ -72,7 +72,7 @@ public class CandidateSnapshot {
}
public JsonElement getJsonRepresentation(String postbackKey) {
String projectURL = Util.getPostbackURL() + projectName;
String projectURL = Util.getPostbackURL() + "api/" + projectName;
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("latestVerId", currentVersion);
jsonObject.add("files", getFilesAsJson(projectURL, postbackKey));

View file

@ -0,0 +1,55 @@
package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InvalidPostbackKeyException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Serve files referenced by the snapshot that we send to the Overleaf API.
*
* Requests must include the postback key.
*/
public class FileHandler extends ResourceHandler {
private static final Logger LOG = LoggerFactory.getLogger(FileHandler.class);
private final BridgeAPI writeLatexDataSource;
private final Pattern DOC_KEY_PATTERN = Pattern.compile("^/(\\w+)/.+$");
public FileHandler(BridgeAPI writeLatexDataSource) {
this.writeLatexDataSource = writeLatexDataSource;
}
@Override
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
if (!"GET".equals(baseRequest.getMethod())) return;
Matcher docKeyMatcher = DOC_KEY_PATTERN.matcher(target);
if (!docKeyMatcher.matches()) return;
String docKey = docKeyMatcher.group(1);
String apiKey = request.getParameter("key");
if (apiKey == null) return;
try {
writeLatexDataSource.checkPostbackKey(docKey, apiKey);
} catch (InvalidPostbackKeyException e) {
LOG.warn("INVALID POST BACK KEY: docKey={} apiKey={}", docKey, apiKey);
return;
}
super.handle(target, baseRequest, request, response);
}
}

View file

@ -1,50 +0,0 @@
package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.MultiMap;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InvalidPostbackKeyException;
import uk.ac.ic.wlgitbridge.util.Log;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Winston on 04/12/14.
*/
public class FileServlet extends ResourceHandler {
private final BridgeAPI writeLatexDataSource;
public FileServlet(BridgeAPI writeLatexDataSource) {
this.writeLatexDataSource = writeLatexDataSource;
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String method = baseRequest.getMethod();
if (method.equals("GET")) {
HttpURI uri = baseRequest.getHttpURI();
Log.info(method + " <- " + uri);
MultiMap<String> multimap = new MultiMap<String>();
uri.decodeQueryTo(multimap);
String[] pathSections = uri.getPath().split("/");
String key = multimap.getString("key");
if (key == null || pathSections.length < 2) {
throw new ServletException();
}
try {
writeLatexDataSource.checkPostbackKey(pathSections[1], key);
} catch (InvalidPostbackKeyException e) {
e.printStackTrace();
throw new ServletException();
}
super.handle(target, baseRequest, request, response);
}
}
}

View file

@ -2,8 +2,7 @@ package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@ -82,15 +81,25 @@ public class GitBridgeServer {
}
private void configureJettyServer(Config config) throws ServletException, InvalidRootDirectoryPathException {
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] {
initResourceHandler(),
new PostbackHandler(bridgeAPI),
initGitHandler(config)
});
HandlerCollection handlers = new HandlerList();
handlers.addHandler(initApiHandler());
handlers.addHandler(initGitHandler(config));
jettyServer.setHandler(handlers);
}
private Handler initApiHandler() {
ContextHandler api = new ContextHandler();
api.setContextPath("/api");
HandlerCollection handlers = new HandlerList();
handlers.addHandler(initResourceHandler());
handlers.addHandler(new PostbackHandler(bridgeAPI));
handlers.addHandler(new DefaultHandler());
api.setHandler(handlers);
return api;
}
private Handler initGitHandler(Config config) throws ServletException, InvalidRootDirectoryPathException {
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
if (config.isUsingOauth2()) {
@ -107,9 +116,8 @@ public class GitBridgeServer {
}
private Handler initResourceHandler() {
ResourceHandler resourceHandler = new FileServlet(bridgeAPI);
ResourceHandler resourceHandler = new FileHandler(bridgeAPI);
resourceHandler.setResourceBase(new File(rootGitDirectoryPath, ".wlgb/atts").getAbsolutePath());
return resourceHandler;
}
}

View file

@ -32,7 +32,7 @@ public class PostbackHandler extends AbstractHandler {
if (request.getMethod().equals("POST") && target.endsWith("postback")) {
response.setContentType("application/json");
String contents = Util.getContentsOfReader(request.getReader());
String[] parts = request.getRequestURI().split("/");
String[] parts = target.split("/");
if (parts.length < 4) {
throw new ServletException();
}