Skip to content

Commit c23a581

Browse files
committed
[rest] Remove faulty caching from add-on resource
Fixes #4834. Partly reverts #4107. The caching of the add-on resource is faulty, e.g. changes to the community marketplace settings don't invalidate the cache. As Main UI now better handles add-on store loading, caching is no more needed there. Signed-off-by: Florian Hotze <dev@florianhotze.com>
1 parent 88d05e5 commit c23a581

1 file changed

Lines changed: 6 additions & 52 deletions

File tree

  • bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/addons

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/addons/AddonResource.java

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import java.net.URI;
1717
import java.net.URISyntaxException;
1818
import java.text.Collator;
19-
import java.time.Instant;
20-
import java.time.temporal.ChronoUnit;
2119
import java.util.Collection;
22-
import java.util.Date;
2320
import java.util.List;
2421
import java.util.Locale;
2522
import java.util.Map;
@@ -40,7 +37,6 @@
4037
import javax.ws.rs.QueryParam;
4138
import javax.ws.rs.core.Context;
4239
import javax.ws.rs.core.MediaType;
43-
import javax.ws.rs.core.Request;
4440
import javax.ws.rs.core.Response;
4541
import javax.ws.rs.core.Response.Status;
4642
import javax.ws.rs.core.UriInfo;
@@ -49,7 +45,6 @@
4945
import org.eclipse.jdt.annotation.Nullable;
5046
import org.eclipse.jetty.http.HttpStatus;
5147
import org.openhab.core.addon.Addon;
52-
import org.openhab.core.addon.AddonEvent;
5348
import org.openhab.core.addon.AddonEventFactory;
5449
import org.openhab.core.addon.AddonInfo;
5550
import org.openhab.core.addon.AddonInfoRegistry;
@@ -64,7 +59,6 @@
6459
import org.openhab.core.config.discovery.addon.AddonSuggestionService;
6560
import org.openhab.core.events.Event;
6661
import org.openhab.core.events.EventPublisher;
67-
import org.openhab.core.events.EventSubscriber;
6862
import org.openhab.core.io.rest.JSONResponse;
6963
import org.openhab.core.io.rest.LocaleService;
7064
import org.openhab.core.io.rest.RESTConstants;
@@ -112,14 +106,13 @@
112106
@SecurityRequirement(name = "oauth2", scopes = { "admin" })
113107
@Tag(name = AddonResource.PATH_ADDONS)
114108
@NonNullByDefault
115-
public class AddonResource implements RESTResource, EventSubscriber {
109+
public class AddonResource implements RESTResource {
116110

117111
private static final String THREAD_POOL_NAME = "addonService";
118112

119113
public static final String PATH_ADDONS = "addons";
120114

121115
public static final String DEFAULT_ADDON_SERVICE = "karaf";
122-
private static final Set<String> SUBSCRIBED_EVENT_TYPES = Set.of(AddonEvent.TYPE);
123116

124117
private final Logger logger = LoggerFactory.getLogger(AddonResource.class);
125118
private final Set<AddonService> addonServices = new CopyOnWriteArraySet<>();
@@ -130,8 +123,6 @@ public class AddonResource implements RESTResource, EventSubscriber {
130123
private final ConfigDescriptionRegistry configDescriptionRegistry;
131124
private final AddonSuggestionService addonSuggestionService;
132125

133-
private @Nullable Date lastModified = null;
134-
135126
private @Context @NonNullByDefault({}) UriInfo uriInfo;
136127

137128
@Activate
@@ -151,57 +142,31 @@ public AddonResource(final @Reference EventPublisher eventPublisher, final @Refe
151142
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
152143
protected void addAddonService(AddonService featureService) {
153144
this.addonServices.add(featureService);
154-
lastModified = null;
155145
}
156146

157147
protected void removeAddonService(AddonService featureService) {
158148
this.addonServices.remove(featureService);
159149
}
160150

161-
@Override
162-
public Set<String> getSubscribedEventTypes() {
163-
return SUBSCRIBED_EVENT_TYPES;
164-
}
165-
166-
@Override
167-
public void receive(Event event) {
168-
lastModified = null;
169-
}
170-
171-
private boolean lastModifiedIsValid() {
172-
return (lastModified != null) && ((new Date().getTime() - lastModified.getTime()) <= 450 * 1000);
173-
}
174-
175151
@GET
176152
@Produces(MediaType.APPLICATION_JSON)
177153
@Operation(operationId = "getAddons", summary = "Get all add-ons.", responses = {
178154
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Addon.class)))),
179155
@ApiResponse(responseCode = "404", description = "Service not found") })
180-
public Response getAddon(final @Context Request request,
156+
public Response getAddon(
181157
@HeaderParam("Accept-Language") @Parameter(description = "language") @Nullable String language,
182158
@QueryParam("serviceId") @Parameter(description = "service ID") @Nullable String serviceId) {
183159
logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());
184-
if (lastModifiedIsValid()) {
185-
Response.ResponseBuilder responseBuilder = request.evaluatePreconditions(lastModified);
186-
if (responseBuilder != null) {
187-
// send 304 Not Modified
188-
return responseBuilder.build();
189-
}
190-
} else {
191-
lastModified = Date.from(Instant.now().truncatedTo(ChronoUnit.SECONDS));
192-
}
193160

194161
final Locale locale = localeService.getLocale(language);
195162
if ("all".equals(serviceId)) {
196-
return Response.ok(new Stream2JSONInputStream(getAllAddons(locale))).lastModified(lastModified)
197-
.cacheControl(RESTConstants.CACHE_CONTROL).build();
163+
return Response.ok(new Stream2JSONInputStream(getAllAddons(locale))).build();
198164
} else {
199165
AddonService addonService = (serviceId != null) ? getServiceById(serviceId) : getDefaultService();
200166
if (addonService == null) {
201167
return Response.status(HttpStatus.NOT_FOUND_404).build();
202168
}
203-
return Response.ok(new Stream2JSONInputStream(addonService.getAddons(locale).stream()))
204-
.lastModified(lastModified).cacheControl(RESTConstants.CACHE_CONTROL).build();
169+
return Response.ok(new Stream2JSONInputStream(addonService.getAddons(locale).stream())).build();
205170
}
206171
}
207172

@@ -210,23 +175,12 @@ public Response getAddon(final @Context Request request,
210175
@Produces(MediaType.APPLICATION_JSON)
211176
@Operation(operationId = "getAddonTypes", summary = "Get all add-on types.", responses = {
212177
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = AddonType.class)))) })
213-
public Response getServices(final @Context Request request,
178+
public Response getServices(
214179
@HeaderParam("Accept-Language") @Parameter(description = "language") @Nullable String language) {
215180
logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());
216-
if (lastModifiedIsValid()) {
217-
Response.ResponseBuilder responseBuilder = request.evaluatePreconditions(lastModified);
218-
if (responseBuilder != null) {
219-
// send 304 Not Modified
220-
return responseBuilder.build();
221-
}
222-
} else {
223-
lastModified = Date.from(Instant.now().truncatedTo(ChronoUnit.SECONDS));
224-
}
225-
226181
final Locale locale = localeService.getLocale(language);
227182
Stream<AddonServiceDTO> addonTypeStream = addonServices.stream().map(s -> convertToAddonServiceDTO(s, locale));
228-
return Response.ok(new Stream2JSONInputStream(addonTypeStream)).lastModified(lastModified)
229-
.cacheControl(RESTConstants.CACHE_CONTROL).build();
183+
return Response.ok(new Stream2JSONInputStream(addonTypeStream)).build();
230184
}
231185

232186
@GET

0 commit comments

Comments
 (0)