Created
June 4, 2014 21:55
-
-
Save ggtools/4d47e1e0874eca16d258 to your computer and use it in GitHub Desktop.
Restx Router to handle GET on a video stream
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
public class GetVideoRoute extends StdEntityRoute<Void, InputStream> { | |
private final VideoResource videoResource; | |
public GetVideoRoute(EntityRequestBodyReaderRegistry readerRegistry, VideoResource videoResource) { | |
super("Get Video Route", | |
readerRegistry.<Void>build(Void.class, Optional.<String>absent()), | |
new AbstractEntityResponseWriter<InputStream>(OutputStream.class, "video/mp4") { | |
@Override | |
protected void write(InputStream value, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException { | |
try { | |
ByteStreams.copy(value, resp.getOutputStream()); | |
} finally { | |
resp.getOutputStream().flush(); | |
Closeables.closeQuietly(value); | |
} | |
} | |
}, | |
new StdRestxRequestMatcher("GET", "/videos/{videoId}"), | |
HttpStatus.OK, RestxLogLevel.DEFAULT | |
); | |
this.videoResource = videoResource; | |
} | |
@Override | |
protected Optional<InputStream> doRoute(RestxRequest restxRequest, RestxRequestMatch match, Void aVoid) throws IOException { | |
return Optional.of(videoResource.getVideo(match.getPathParam("videoId"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment