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 <masayasuzuki@google.com>
This commit is contained in:
Masaya Suzuki 2016-08-23 17:57:59 -07:00
parent 1096652e71
commit c4e209b20f
7 changed files with 54 additions and 5 deletions

View File

@ -143,7 +143,7 @@ public void doFilter(final ServletRequest request,
res.sendError(SC_UNAUTHORIZED, e.getMessage()); res.sendError(SC_UNAUTHORIZED, e.getMessage());
return; return;
} catch (ServiceMayNotContinueException e) { } catch (ServiceMayNotContinueException e) {
sendError(req, res, SC_FORBIDDEN, e.getMessage()); sendError(req, res, e.getStatusCode(), e.getMessage());
return; return;
} }
try { try {

View File

@ -139,7 +139,7 @@ private void service(ServletRequest request, ServletResponse response)
if (e.isOutput()) if (e.isOutput())
buf.close(); buf.close();
else else
sendError(req, res, SC_FORBIDDEN, e.getMessage()); sendError(req, res, e.getStatusCode(), e.getMessage());
} }
} }

View File

@ -197,7 +197,7 @@ public void flush() throws IOException {
out.close(); out.close();
} else if (!rsp.isCommitted()) { } else if (!rsp.isCommitted()) {
rsp.reset(); rsp.reset();
sendError(req, rsp, SC_FORBIDDEN, e.getMessage()); sendError(req, rsp, e.getStatusCode(), e.getMessage());
} }
return; return;

View File

@ -9,6 +9,7 @@ java_library(
'//lib:javaewah', '//lib:javaewah',
'//lib:jsch', '//lib:jsch',
'//lib:httpcomponents', '//lib:httpcomponents',
'//lib:servlet-api',
'//lib:slf4j-api', '//lib:slf4j-api',
], ],
visibility = ['PUBLIC'], visibility = ['PUBLIC'],

View File

@ -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)", com.jcraft.jsch;version="[0.1.37,0.2.0)",
javax.crypto, javax.crypto,
javax.net.ssl, javax.net.ssl,
javax.servlet.http;version="[2.5.0,3.2.0)",
org.slf4j;version="[1.7.0,2.0.0)", org.slf4j;version="[1.7.0,2.0.0)",
org.xml.sax, org.xml.sax,
org.xml.sax.helpers org.xml.sax.helpers

View File

@ -88,6 +88,12 @@
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -44,6 +44,7 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
@ -55,11 +56,13 @@
public class ServiceMayNotContinueException extends IOException { public class ServiceMayNotContinueException extends IOException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final int statusCode;
private boolean output; private boolean output;
/** Initialize with no message. */ /** Initialize with no message. */
public ServiceMayNotContinueException() { public ServiceMayNotContinueException() {
// Do not set a message. // Do not set a message.
statusCode = HttpServletResponse.SC_FORBIDDEN;
} }
/** /**
@ -69,6 +72,20 @@ public ServiceMayNotContinueException() {
*/ */
public ServiceMayNotContinueException(String msg) { public ServiceMayNotContinueException(String msg) {
super(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 ServiceMayNotContinueException(String msg) {
* @since 3.2 * @since 3.2
*/ */
public ServiceMayNotContinueException(String msg, Throwable cause) { public ServiceMayNotContinueException(String msg, Throwable cause) {
super(msg); super(msg, cause);
initCause(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 boolean isOutput() {
public void setOutput() { public void setOutput() {
output = true; output = true;
} }
/**
* @return true if the message was already output to the client.
* @since 4.5
*/
public int getStatusCode() {
return statusCode;
}
} }