Skip to content

Commit

Permalink
[rest] Add caching for UoM info (openhab#3838)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Oct 9, 2023
1 parent 176e23f commit 2794c97
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,33 @@
*/
package org.openhab.core.io.rest.internal.resources;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Objects;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.auth.Role;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
import org.openhab.core.io.rest.internal.resources.beans.SystemInfoBean;
import org.openhab.core.io.rest.internal.resources.beans.UoMInfoBean;
import org.openhab.core.service.StartLevelService;
import org.osgi.service.cm.ConfigurationEvent;
import org.osgi.service.cm.ConfigurationListener;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -60,20 +70,28 @@
@SecurityRequirement(name = "oauth2", scopes = { "admin" })
@Tag(name = SystemInfoResource.PATH_SYSTEMINFO)
@NonNullByDefault
public class SystemInfoResource implements RESTResource {
public class SystemInfoResource implements RESTResource, ConfigurationListener {

/** The URI path to this resource */
public static final String PATH_SYSTEMINFO = "systeminfo";

private final StartLevelService startLevelService;
private final UnitProvider unitProvider;
private @Nullable Date lastModified = null;

@Activate
public SystemInfoResource(@Reference StartLevelService startLevelService, @Reference UnitProvider unitProvider) {
this.startLevelService = startLevelService;
this.unitProvider = unitProvider;
}

@Override
public void configurationEvent(@Nullable ConfigurationEvent event) {
if (Objects.equals(event.getPid(), "org.openhab.i18n")) {
lastModified = null;
}
}

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -89,8 +107,22 @@ public Response getSystemInfo(@Context UriInfo uriInfo) {
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getUoMInformation", summary = "Get all supported dimensions and their system units.", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = UoMInfoBean.class))) })
public Response getUoMInfo(@Context UriInfo uriInfo) {
public Response getUoMInfo(final @Context Request request, final @Context UriInfo uriInfo) {
if (lastModified != null) {
Response.ResponseBuilder responseBuilder = request.evaluatePreconditions(lastModified);
if (responseBuilder != null) {
// send 304 Not Modified
return responseBuilder.build();
}
} else {
lastModified = Date.from(Instant.now().truncatedTo(ChronoUnit.SECONDS));
}

CacheControl cc = new CacheControl();
cc.setMustRevalidate(true);
cc.setPrivate(true);

final UoMInfoBean bean = new UoMInfoBean(unitProvider);
return Response.ok(bean).build();
return Response.ok(bean).lastModified(lastModified).cacheControl(cc).build();
}
}

0 comments on commit 2794c97

Please sign in to comment.