LfsProtocolServlet: Pass request and path to getLargeFileRepository

Passing the request and path to the method will allow implementations
to have more control over determination of the backend, for example:

- return different backends for different requests
- accept or refuse requests based on request characteristics
- etc

Change-Id: I1ec6ec54c91a5f0601b620ed18846eb4a3f46783
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
David Pursehouse 2016-07-25 14:43:33 +09:00
parent e27eab26e2
commit bb9988c236
2 changed files with 31 additions and 14 deletions

View File

@ -82,40 +82,57 @@ public abstract class LfsProtocolServlet extends HttpServlet {
/** /**
* Get the large file repository * Get the large file repository
* *
* @return the large file repository storing large files * @param request
* the request
* @param path
* the path
*
* @return the large file repository storing large files or null if the
* request is not supported.
*/ */
protected abstract LargeFileRepository getLargeFileRepository(); protected abstract LargeFileRepository getLargeFileRepository(
LfsRequest request, String path);
/** LFS request. */
protected static class LfsRequest {
private String operation;
private List<LfsObject> objects;
/**
* Get the LFS operation.
*
* @return the operation
*/
public String getOperation() {
return operation;
}
}
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { throws ServletException, IOException {
res.setStatus(SC_OK);
res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
Writer w = new BufferedWriter( Writer w = new BufferedWriter(
new OutputStreamWriter(res.getOutputStream(), UTF_8)); new OutputStreamWriter(res.getOutputStream(), UTF_8));
Reader r = new BufferedReader(new InputStreamReader(req.getInputStream(), UTF_8)); Reader r = new BufferedReader(new InputStreamReader(req.getInputStream(), UTF_8));
LfsRequest request = gson.fromJson(r, LfsRequest.class); LfsRequest request = gson.fromJson(r, LfsRequest.class);
String path = req.getPathInfo();
LargeFileRepository repo = getLargeFileRepository(); LargeFileRepository repo = getLargeFileRepository(request, path);
if (repo == null) { if (repo == null) {
res.setStatus(SC_SERVICE_UNAVAILABLE); res.setStatus(SC_SERVICE_UNAVAILABLE);
return; return;
} }
res.setStatus(SC_OK);
res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
TransferHandler handler = TransferHandler TransferHandler handler = TransferHandler
.forOperation(request.operation, repo, request.objects); .forOperation(request.operation, repo, request.objects);
gson.toJson(handler.process(), w); gson.toJson(handler.process(), w);
w.flush(); w.flush();
} }
private static class LfsRequest {
String operation;
List<LfsObject> objects;
}
private static Gson createGson() { private static Gson createGson() {
GsonBuilder gb = new GsonBuilder() GsonBuilder gb = new GsonBuilder()
.setFieldNamingPolicy( .setFieldNamingPolicy(

View File

@ -253,10 +253,10 @@ protected void run() throws Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override @Override
protected LargeFileRepository getLargeFileRepository() { protected LargeFileRepository getLargeFileRepository(
LfsRequest request, String path) {
return repository; return repository;
} }
}; };
app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH); app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH);