Created
May 1, 2014 23:32
-
-
Save philwebb/3c11ca42c2bdad8395b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.util.Arrays; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
public abstract class Response<T> { | |
public abstract ResponseEntity<T> asResponseEntity(); | |
public static DefaultResponse<Void> created() { | |
return withStatus(HttpStatus.CREATED); | |
} | |
public static <T> DefaultResponse<T> created(T value) { | |
return withStatus(HttpStatus.CREATED, value); | |
} | |
public static DefaultResponse<Void> withStatus(HttpStatus status) { | |
return withStatus(status, (Void) null); | |
} | |
public static <T> DefaultResponse<T> withStatus(HttpStatus status, T body) { | |
return new DefaultResponse<T>(status, body); | |
} | |
public static class DefaultResponse<T> extends Response<T> { | |
private final HeadersResponse<T> headers = new HeadersResponse<T>(this); | |
private final HttpStatus status; | |
private final T body; | |
public DefaultResponse(HttpStatus status, T body) { | |
this.status = status; | |
this.body = body; | |
} | |
@Override | |
public ResponseEntity<T> asResponseEntity() { | |
return new ResponseEntity<T>(this.body, this.headers.headers(), this.status); | |
} | |
public HeadersResponse<T> withHeaders() { | |
return this.headers; | |
} | |
} | |
public static class HeadersResponse<T> extends Response<T> { | |
private DefaultResponse<T> response; | |
private HttpHeaders headers; | |
public HeadersResponse(DefaultResponse<T> response) { | |
this.response = response; | |
this.headers = new HttpHeaders(); | |
} | |
@Override | |
public ResponseEntity<T> asResponseEntity() { | |
return and().asResponseEntity(); | |
} | |
protected HttpHeaders headers() { | |
return this.headers; | |
} | |
public DefaultResponse<T> and() { | |
return this.response; | |
} | |
public HeadersResponse<T> accepts(MediaType... mediaTypes) { | |
this.headers.setAccept(Arrays.asList(mediaTypes)); | |
return this; | |
} | |
public <C> C locationFromController(Class<C> controllerClass) { | |
// return a proxy here | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment