Search in sources :

Example 76 with EntityTag

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();
}
Also used : CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Response(javax.ws.rs.core.Response) CollectorSummaryResponse(org.graylog.plugins.sidecar.rest.responses.CollectorSummaryResponse) CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Collector(org.graylog.plugins.sidecar.rest.models.Collector) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 77 with EntityTag

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();
}
Also used : Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ApiResponse(io.swagger.annotations.ApiResponse) EntityTag(javax.ws.rs.core.EntityTag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)77 Response (javax.ws.rs.core.Response)28 GET (javax.ws.rs.GET)27 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)25 Path (javax.ws.rs.Path)24 CacheControl (javax.ws.rs.core.CacheControl)20 Test (org.junit.Test)18 Produces (javax.ws.rs.Produces)16 Date (java.util.Date)10 Test (org.testng.annotations.Test)9 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)8 Timestamp (com.yahoo.rdl.Timestamp)5 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)5 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Principal (com.yahoo.athenz.auth.Principal)4