Browse Source

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>
stable-4.5
Masaya Suzuki 8 years ago
parent
commit
c4e209b20f
  1. 2
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/RepositoryFilter.java
  2. 2
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java
  3. 2
      org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java
  4. 1
      org.eclipse.jgit/BUCK
  5. 1
      org.eclipse.jgit/META-INF/MANIFEST.MF
  6. 6
      org.eclipse.jgit/pom.xml
  7. 45
      org.eclipse.jgit/src/org/eclipse/jgit/transport/ServiceMayNotContinueException.java

2
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 {

2
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());
}
}

2
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;

1
org.eclipse.jgit/BUCK

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

1
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

6
org.eclipse.jgit/pom.xml

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

45
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;
}
}

Loading…
Cancel
Save