From 3e728166296c08079b3b4b159c4b9fd3f07687e0 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Sun, 20 Nov 2022 21:13:50 +0100 Subject: [PATCH] UploadPackServlet#doPost use try-with-resource to ensure up is closed Change-Id: Iadbf81f183bb94f3b00b9940f065586b13e85c95 --- .../jgit/http/server/UploadPackServlet.java | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java index b0a07f1d5..f16e56d94 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java @@ -168,42 +168,43 @@ public void doPost(HttpServletRequest req, HttpServletResponse rsp) } UploadPackRunnable r = () -> { - UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER); - // to be explicitly closed by caller - @SuppressWarnings("resource") - SmartOutputStream out = new SmartOutputStream(req, rsp, false) { - @Override - public void flush() throws IOException { - doFlush(); - } - }; - - up.setBiDirectionalPipe(false); - rsp.setContentType(UPLOAD_PACK_RESULT_TYPE); - - try { - up.uploadWithExceptionPropagation(getInputStream(req), out, - null); - out.close(); - } catch (ServiceMayNotContinueException e) { - if (e.isOutput()) { - consumeRequestBody(req); - out.close(); - } - throw e; - } catch (UploadPackInternalServerErrorException e) { - // Special case exception, error message was sent to client. - log(up.getRepository(), e.getCause()); - consumeRequestBody(req); - out.close(); - } finally { - up.close(); - } + upload(req, rsp); }; handler.upload(req, rsp, r); } + private void upload(HttpServletRequest req, HttpServletResponse rsp) + throws IOException, ServiceMayNotContinueException { + // to be explicitly closed by caller + @SuppressWarnings("resource") + SmartOutputStream out = new SmartOutputStream(req, rsp, false) { + @Override + public void flush() throws IOException { + doFlush(); + } + }; + Repository repo = null; + try (UploadPack up = (UploadPack) req.getAttribute(ATTRIBUTE_HANDLER)) { + up.setBiDirectionalPipe(false); + rsp.setContentType(UPLOAD_PACK_RESULT_TYPE); + repo = up.getRepository(); + up.uploadWithExceptionPropagation(getInputStream(req), out, null); + out.close(); + } catch (ServiceMayNotContinueException e) { + if (e.isOutput()) { + consumeRequestBody(req); + out.close(); + } + throw e; + } catch (UploadPackInternalServerErrorException e) { + // Special case exception, error message was sent to client. + log(repo, e.getCause()); + consumeRequestBody(req); + out.close(); + } + } + private void defaultUploadPackHandler(HttpServletRequest req, HttpServletResponse rsp, UploadPackRunnable r) throws IOException { try {