use of javax.ws.rs.core.EntityTag in project graylog2-server by Graylog2.
the class CollectorResource method listCollectors.
@GET
@Timed
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List all collectors")
public Response listCollectors(@Context HttpHeaders httpHeaders) {
String ifNoneMatch = httpHeaders.getHeaderString("If-None-Match");
Boolean etagCached = false;
Response.ResponseBuilder builder = Response.noContent();
// check if client is up to date with a known valid etag
if (ifNoneMatch != null) {
EntityTag etag = new EntityTag(ifNoneMatch.replaceAll("\"", ""));
if (etagService.isPresent(etag.toString())) {
etagCached = true;
builder = Response.notModified();
builder.tag(etag);
}
}
// fetch collector list from database if client is outdated
if (!etagCached) {
final List<Collector> result = this.collectorService.all();
CollectorListResponse collectorListResponse = CollectorListResponse.create(result.size(), result);
// add new etag to cache
String etagString = collectorsToEtag(collectorListResponse);
EntityTag collectorsEtag = new EntityTag(etagString);
builder = Response.ok(collectorListResponse);
builder.tag(collectorsEtag);
etagService.put(collectorsEtag.toString());
}
// set cache control
CacheControl cacheControl = new CacheControl();
cacheControl.setNoTransform(true);
cacheControl.setPrivate(true);
builder.cacheControl(cacheControl);
return builder.build();
}
use of javax.ws.rs.core.EntityTag in project gravitee-management-rest-api by gravitee-io.
the class CurrentUserResource method getCurrentUserPicture.
@GET
@Path("avatar")
@ApiOperation(value = "Get user's avatar")
@ApiResponses({ @ApiResponse(code = 200, message = "User's avatar"), @ApiResponse(code = 404, message = "User not found"), @ApiResponse(code = 500, message = "Internal server error") })
public Response getCurrentUserPicture(@Context Request request) {
String userId = userService.findById(getAuthenticatedUser()).getId();
PictureEntity picture = userService.getPicture(userId);
if (picture instanceof UrlPictureEntity) {
return Response.temporaryRedirect(URI.create(((UrlPictureEntity) picture).getUrl())).build();
}
InlinePictureEntity image = (InlinePictureEntity) picture;
if (image == null || image.getContent() == null) {
return Response.ok().build();
}
EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode()));
Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
if (builder != null) {
return builder.build();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(image.getContent(), 0, image.getContent().length);
return ok().entity(baos).tag(etag).type(image.getType()).build();
}
Aggregations