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:
Shawn Pearce
2013-01-16 19:37:40 -08:00
parent 0a8969410f
commit c375647875
2 changed files with 38 additions and 1 deletions

View File

@@ -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);
}
}
}