From c4e209b20fe519f419be0300ed414350649df88f Mon Sep 17 00:00:00 2001 From: Masaya Suzuki Date: Tue, 23 Aug 2016 17:57:59 -0700 Subject: [PATCH] Add HTTP status code to ServiceMayNotContinueException The exception can be thrown in a various reason, and sometimes 403 Forbidden is not appropriate. Make the HTTP status code customizable. Change-Id: If2ef6f454f7479158a4e28a12909837db483521c Signed-off-by: Masaya Suzuki --- .../jgit/http/server/RepositoryFilter.java | 2 +- .../http/server/SmartServiceInfoRefs.java | 2 +- .../jgit/http/server/UploadPackServlet.java | 2 +- org.eclipse.jgit/BUCK | 1 + org.eclipse.jgit/META-INF/MANIFEST.MF | 1 + org.eclipse.jgit/pom.xml | 6 +++ .../ServiceMayNotContinueException.java | 45 ++++++++++++++++++- 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java index a021c1ff5..b3fad3d95 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java @@ -143,7 +143,7 @@ public class RepositoryFilter implements Filter { res.sendError(SC_UNAUTHORIZED, e.getMessage()); return; } catch (ServiceMayNotContinueException e) { - sendError(req, res, SC_FORBIDDEN, e.getMessage()); + sendError(req, res, e.getStatusCode(), e.getMessage()); return; } try { diff --git a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java index 7d4f21b5c..a06bb1e9e 100644 --- a/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java +++ b/org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java @@ -139,7 +139,7 @@ abstract class SmartServiceInfoRefs implements Filter { if (e.isOutput()) buf.close(); else - sendError(req, res, SC_FORBIDDEN, e.getMessage()); + sendError(req, res, e.getStatusCode(), e.getMessage()); } } 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 8c27b712b..a9a0c5b12 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 @@ -197,7 +197,7 @@ class UploadPackServlet extends HttpServlet { out.close(); } else if (!rsp.isCommitted()) { rsp.reset(); - sendError(req, rsp, SC_FORBIDDEN, e.getMessage()); + sendError(req, rsp, e.getStatusCode(), e.getMessage()); } return; diff --git a/org.eclipse.jgit/BUCK b/org.eclipse.jgit/BUCK index 73e208057..2bae6dcf2 100644 --- a/org.eclipse.jgit/BUCK +++ b/org.eclipse.jgit/BUCK @@ -9,6 +9,7 @@ java_library( '//lib:javaewah', '//lib:jsch', '//lib:httpcomponents', + '//lib:servlet-api', '//lib:slf4j-api', ], visibility = ['PUBLIC'], diff --git a/org.eclipse.jgit/META-INF/MANIFEST.MF b/org.eclipse.jgit/META-INF/MANIFEST.MF index 4a9b79466..d87657e8f 100644 --- a/org.eclipse.jgit/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit/META-INF/MANIFEST.MF @@ -142,6 +142,7 @@ Import-Package: com.googlecode.javaewah;version="[0.7.9,0.8.0)", com.jcraft.jsch;version="[0.1.37,0.2.0)", javax.crypto, javax.net.ssl, + javax.servlet.http;version="[2.5.0,3.2.0)", org.slf4j;version="[1.7.0,2.0.0)", org.xml.sax, org.xml.sax.helpers diff --git a/org.eclipse.jgit/pom.xml b/org.eclipse.jgit/pom.xml index 1f8907e4f..e8a3213b8 100644 --- a/org.eclipse.jgit/pom.xml +++ b/org.eclipse.jgit/pom.xml @@ -88,6 +88,12 @@ org.slf4j slf4j-api + + + javax.servlet + javax.servlet-api + provided + diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ServiceMayNotContinueException.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ServiceMayNotContinueException.java index e000cfbe6..ce5ccaa65 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ServiceMayNotContinueException.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ServiceMayNotContinueException.java @@ -44,6 +44,7 @@ package org.eclipse.jgit.transport; import java.io.IOException; +import javax.servlet.http.HttpServletResponse; import org.eclipse.jgit.internal.JGitText; @@ -55,11 +56,13 @@ import org.eclipse.jgit.internal.JGitText; public class ServiceMayNotContinueException extends IOException { private static final long serialVersionUID = 1L; + private final int statusCode; private boolean output; /** Initialize with no message. */ public ServiceMayNotContinueException() { // Do not set a message. + statusCode = HttpServletResponse.SC_FORBIDDEN; } /** @@ -69,6 +72,20 @@ public class ServiceMayNotContinueException extends IOException { */ public ServiceMayNotContinueException(String msg) { super(msg); + statusCode = HttpServletResponse.SC_FORBIDDEN; + } + + /** + * @param msg + * a message explaining why it cannot continue. This message may + * be shown to an end-user. + * @param statusCode + * the HTTP status code. + * @since 4.5 + */ + public ServiceMayNotContinueException(String msg, int statusCode) { + super(msg); + this.statusCode = statusCode; } /** @@ -80,8 +97,24 @@ public class ServiceMayNotContinueException extends IOException { * @since 3.2 */ public ServiceMayNotContinueException(String msg, Throwable cause) { - super(msg); - initCause(cause); + super(msg, cause); + statusCode = HttpServletResponse.SC_FORBIDDEN; + } + + /** + * @param msg + * a message explaining why it cannot continue. This message may + * be shown to an end-user. + * @param cause + * the cause of the exception. + * @param statusCode + * the HTTP status code. + * @since 4.5 + */ + public ServiceMayNotContinueException( + String msg, Throwable cause, int statusCode) { + super(msg, cause); + this.statusCode = statusCode; } /** @@ -104,4 +137,12 @@ public class ServiceMayNotContinueException extends IOException { public void setOutput() { output = true; } + + /** + * @return true if the message was already output to the client. + * @since 4.5 + */ + public int getStatusCode() { + return statusCode; + } }