diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2016-08-08 09:40:34 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@gmail.com> | 2016-08-09 11:10:39 +0900 |
commit | 4651d6e72308a92c6262ff4279796f0c87a48905 (patch) | |
tree | 1b476a5bff465872e1e576b229059b830a4a4932 /org.eclipse.jgit.lfs.server/src | |
parent | a9b87de97090b4f7d25ace0525edc1268cfa737f (diff) | |
download | jgit-4651d6e72308a92c6262ff4279796f0c87a48905.tar.gz jgit-4651d6e72308a92c6262ff4279796f0c87a48905.zip |
LfsProtocolServlet: Reuse existing Writer when sending error response
Trying to open a new writer on the response causes an illegal state
exception and the response is not sent.
Change-Id: Ic718d23cfb3e74f5691cc2aea7283003af7df207
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.lfs.server/src')
-rw-r--r-- | org.eclipse.jgit.lfs.server/src/org/eclipse/jgit/lfs/server/LfsProtocolServlet.java | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/org.eclipse.jgit.lfs.server/src/org/eclipse/jgit/lfs/server/LfsProtocolServlet.java b/org.eclipse.jgit.lfs.server/src/org/eclipse/jgit/lfs/server/LfsProtocolServlet.java index e6a5ef90fc..1aa8c04058 100644 --- a/org.eclipse.jgit.lfs.server/src/org/eclipse/jgit/lfs/server/LfsProtocolServlet.java +++ b/org.eclipse.jgit.lfs.server/src/org/eclipse/jgit/lfs/server/LfsProtocolServlet.java @@ -54,7 +54,6 @@ import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.util.List; @@ -139,37 +138,34 @@ public abstract class LfsProtocolServlet extends HttpServlet { Writer w = new BufferedWriter( 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); String path = req.getPathInfo(); LargeFileRepository repo = null; try { repo = getLargeFileRepository(request, path); + if (repo == null) { + res.setStatus(SC_SERVICE_UNAVAILABLE); + } else { + res.setStatus(SC_OK); + res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON); + TransferHandler handler = TransferHandler + .forOperation(request.operation, repo, request.objects); + gson.toJson(handler.process(), w); + } } catch (LfsValidationError e) { - sendError(res, SC_UNPROCESSABLE_ENTITY, e.getMessage()); - return; + sendError(res, w, SC_UNPROCESSABLE_ENTITY, e.getMessage()); } catch (LfsRepositoryNotFound e) { - sendError(res, SC_NOT_FOUND, e.getMessage()); - return; + sendError(res, w, SC_NOT_FOUND, e.getMessage()); } catch (LfsRepositoryReadOnly e) { - sendError(res, SC_FORBIDDEN, e.getMessage()); - return; + sendError(res, w, SC_FORBIDDEN, e.getMessage()); } catch (LfsException e) { - sendError(res, SC_SERVICE_UNAVAILABLE, e.getMessage()); - return; + sendError(res, w, SC_SERVICE_UNAVAILABLE, e.getMessage()); + } finally { + w.flush(); } - if (repo == null) { - res.setStatus(SC_SERVICE_UNAVAILABLE); - return; - } - - res.setStatus(SC_OK); - res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON); - TransferHandler handler = TransferHandler - .forOperation(request.operation, repo, request.objects); - gson.toJson(handler.process(), w); - w.flush(); } static class Error { @@ -180,14 +176,10 @@ public abstract class LfsProtocolServlet extends HttpServlet { } } - private void sendError(HttpServletResponse rsp, int status, String message) - throws IOException { + private void sendError(HttpServletResponse rsp, Writer writer, int status, + String message) { rsp.setStatus(status); - PrintWriter writer = rsp.getWriter(); gson.toJson(new Error(message), writer); - writer.flush(); - writer.close(); - rsp.flushBuffer(); } private Gson createGson() { |