Search in sources :

Example 1 with CollectorListResponse

use of org.graylog.plugins.sidecar.rest.responses.CollectorListResponse 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)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)1 ApiOperation (io.swagger.annotations.ApiOperation)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 CacheControl (javax.ws.rs.core.CacheControl)1 EntityTag (javax.ws.rs.core.EntityTag)1 Response (javax.ws.rs.core.Response)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 Collector (org.graylog.plugins.sidecar.rest.models.Collector)1 CollectorListResponse (org.graylog.plugins.sidecar.rest.responses.CollectorListResponse)1 CollectorSummaryResponse (org.graylog.plugins.sidecar.rest.responses.CollectorSummaryResponse)1