Skip to content

Instantly share code, notes, and snippets.

@ggtools
Created June 4, 2014 21:55
Show Gist options
  • Save ggtools/4d47e1e0874eca16d258 to your computer and use it in GitHub Desktop.
Save ggtools/4d47e1e0874eca16d258 to your computer and use it in GitHub Desktop.
Restx Router to handle GET on a video stream
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