Skip to content

Commit

Permalink
[rest] Add caching for transformation resource (#4376)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Sep 13, 2024
1 parent bcda209 commit e027bb5
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
*/
package org.openhab.core.io.rest.transform.internal;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Stream;

Expand All @@ -26,12 +29,14 @@
import javax.ws.rs.Produces;
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.common.registry.RegistryChangedRunnableListener;
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
import org.openhab.core.io.rest.Stream2JSONInputStream;
Expand All @@ -45,6 +50,7 @@
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;
import org.osgi.service.jaxrs.whiteboard.propertytypes.JSONRequired;
Expand Down Expand Up @@ -85,8 +91,11 @@ public class TransformationResource implements RESTResource {
private final TransformationRegistry transformationRegistry;
private final ManagedTransformationProvider managedTransformationProvider;
private final BundleContext bundleContext;
private final RegistryChangedRunnableListener<Transformation> resetLastModifiedChangeListener = new RegistryChangedRunnableListener<>(
() -> lastModified = null);

private @Context @NonNullByDefault({}) UriInfo uriInfo;
private @Nullable Date lastModified = null;

@Activate
public TransformationResource(final @Reference TransformationRegistry transformationRegistry,
Expand All @@ -95,17 +104,35 @@ public TransformationResource(final @Reference TransformationRegistry transforma
this.transformationRegistry = transformationRegistry;
this.managedTransformationProvider = managedTransformationProvider;
this.bundleContext = bundleContext;

this.transformationRegistry.addRegistryChangeListener(resetLastModifiedChangeListener);
}

@Deactivate
void deactivate() {
this.transformationRegistry.removeRegistryChangeListener(resetLastModifiedChangeListener);
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getTransformations", summary = "Get a list of all transformations", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = TransformationDTO.class)))) })
public Response getTransformations() {
public Response getTransformations(@Context Request request) {
logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());

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));
}
Stream<TransformationDTO> stream = transformationRegistry.stream().map(TransformationDTO::new)
.peek(c -> c.editable = isEditable(c.uid));
return Response.ok(new Stream2JSONInputStream(stream)).build();
return Response.ok(new Stream2JSONInputStream(stream)).lastModified(lastModified)
.cacheControl(RESTConstants.CACHE_CONTROL).build();
}

@GET
Expand Down

0 comments on commit e027bb5

Please sign in to comment.