[rest] Add caching for UoM info (#3838)

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2023-10-09 09:18:45 +02:00 committed by GitHub
parent 176e23f296
commit 2794c973d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,16 +12,24 @@
*/
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;
@ -29,6 +37,8 @@ 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;
@ -60,13 +70,14 @@ import io.swagger.v3.oas.annotations.tags.Tag;
@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) {
@ -74,6 +85,13 @@ public class SystemInfoResource implements RESTResource {
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)
@ -89,8 +107,22 @@ public class SystemInfoResource implements RESTResource {
@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();
}
}