Allow REST APIs to return HTTP redirects
Eventually we can expose avatar links under /accounts/{id}/avatar
instead of /avatar/{id}. This better fits with the path space of
the server.
Change-Id: I461ea540310622b5d769bf1951a72cf34ad60a0e
This commit is contained in:
@@ -35,6 +35,11 @@ public abstract class Response<T> {
|
||||
return NONE;
|
||||
}
|
||||
|
||||
/** HTTP 302 Found: temporary redirect to another URL. */
|
||||
public static Redirect redirect(String location) {
|
||||
return new Redirect(location);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <T> T unwrap(T obj) {
|
||||
while (obj instanceof Response) {
|
||||
@@ -90,4 +95,33 @@ public abstract class Response<T> {
|
||||
return "[204 No Content] None";
|
||||
}
|
||||
}
|
||||
|
||||
/** An HTTP redirect to another location. */
|
||||
public static final class Redirect {
|
||||
private final String location;
|
||||
|
||||
private Redirect(String url) {
|
||||
this.location = url;
|
||||
}
|
||||
|
||||
public String location() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return location.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof Redirect
|
||||
&& ((Redirect) o).location.equals(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("[302 Redirect] %s", location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,12 +43,12 @@ import com.google.gerrit.extensions.restapi.AcceptsCreate;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||
import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.DefaultInput;
|
||||
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
|
||||
import com.google.gerrit.extensions.restapi.PutInput;
|
||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestCollection;
|
||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
@@ -258,6 +258,9 @@ public class RestApiServlet extends HttpServlet {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Response r = (Response) result;
|
||||
status = r.statusCode();
|
||||
} else if (result instanceof Response.Redirect) {
|
||||
res.sendRedirect(((Response.Redirect) result).location());
|
||||
return;
|
||||
}
|
||||
res.setStatus(status);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user