From 036e84e7c4bda74e2cd6740341b7fbdee134d663 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Fri, 21 Apr 2023 06:20:30 +1000 Subject: [PATCH] [REST] List semantic tags (#3559) Signed-off-by: Jimmy Tanagra GitOrigin-RevId: bc922022c30b40659ae6408ad7f6a83cbc2ebe21 --- .../io/rest/core/internal/tag/TagDTO.java | 38 +++++++ .../rest/core/internal/tag/TagResource.java | 106 ++++++++++++++++++ .../core/io/rest/LocaleServiceImpl.java | 4 +- 3 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagDTO.java create mode 100644 bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagResource.java diff --git a/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagDTO.java b/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagDTO.java new file mode 100644 index 00000000000..37d68f17cec --- /dev/null +++ b/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagDTO.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.io.rest.core.internal.tag; + +import java.util.List; +import java.util.Locale; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.core.semantics.SemanticTags; +import org.openhab.core.semantics.Tag; + +/** + * A DTO representing a Semantic {@link Tag}. + * + * @author Jimmy Tanagra - initial contribution + */ +@NonNullByDefault +public class TagDTO { + String name; + String label; + List synonyms; + + public TagDTO(Class tag, Locale locale) { + this.name = tag.getSimpleName(); + this.label = SemanticTags.getLabel(tag, locale); + this.synonyms = SemanticTags.getSynonyms(tag, locale); + } +} diff --git a/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagResource.java b/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagResource.java new file mode 100644 index 00000000000..ef0b6f95d66 --- /dev/null +++ b/bundles/org.opensmarthouse.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/tag/TagResource.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.io.rest.core.internal.tag; + +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import javax.annotation.security.RolesAllowed; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +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.io.rest.JSONResponse; +import org.openhab.core.io.rest.LocaleService; +import org.openhab.core.io.rest.RESTConstants; +import org.openhab.core.io.rest.RESTResource; +import org.openhab.core.semantics.model.equipment.Equipments; +import org.openhab.core.semantics.model.location.Locations; +import org.openhab.core.semantics.model.point.Points; +import org.openhab.core.semantics.model.property.Properties; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants; +import org.osgi.service.jaxrs.whiteboard.propertytypes.JSONRequired; +import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsApplicationSelect; +import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsName; +import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +/** + * This class acts as a REST resource for retrieving a list of tags. + * + * @author Jimmy Tanagra - Initial contribution + */ +@Component +@JaxrsResource +@JaxrsName(TagResource.PATH_TAGS) +@JaxrsApplicationSelect("(" + JaxrsWhiteboardConstants.JAX_RS_NAME + "=" + RESTConstants.JAX_RS_NAME + ")") +@JSONRequired +@Path(TagResource.PATH_TAGS) +@io.swagger.v3.oas.annotations.tags.Tag(name = TagResource.PATH_TAGS) +@NonNullByDefault +public class TagResource implements RESTResource { + + /** The URI path to this resource */ + public static final String PATH_TAGS = "tags"; + + private final Logger logger = LoggerFactory.getLogger(TagResource.class); + + private final LocaleService localeService; + + @Activate + public TagResource(final @Reference LocaleService localeService) { + this.localeService = localeService; + } + + @GET + @RolesAllowed({ Role.USER, Role.ADMIN }) + @Produces(MediaType.APPLICATION_JSON) + @Operation(operationId = "getTags", summary = "Get all available tags.", responses = { + @ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = TagDTO.class)))) }) + public Response getTags(final @Context UriInfo uriInfo, final @Context HttpHeaders httpHeaders, + @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language) { + + final Locale locale = localeService.getLocale(language); + + Map> tags = Map.of( // + Locations.class.getSimpleName(), Locations.stream().map(tag -> new TagDTO(tag, locale)).toList(), // + Equipments.class.getSimpleName(), Equipments.stream().map(tag -> new TagDTO(tag, locale)).toList(), // + Points.class.getSimpleName(), Points.stream().map(tag -> new TagDTO(tag, locale)).toList(), // + Properties.class.getSimpleName(), Properties.stream().map(tag -> new TagDTO(tag, locale)).toList() // + ); + + return JSONResponse.createResponse(Status.OK, tags, null); + } +} diff --git a/bundles/org.opensmarthouse.core.io.rest/src/main/java/org/openhab/core/io/rest/LocaleServiceImpl.java b/bundles/org.opensmarthouse.core.io.rest/src/main/java/org/openhab/core/io/rest/LocaleServiceImpl.java index 383a3b2fac0..385053eec81 100644 --- a/bundles/org.opensmarthouse.core.io.rest/src/main/java/org/openhab/core/io/rest/LocaleServiceImpl.java +++ b/bundles/org.opensmarthouse.core.io.rest/src/main/java/org/openhab/core/io/rest/LocaleServiceImpl.java @@ -30,7 +30,7 @@ * @author Dennis Nobel - Initial contribution * @author Markus Rathgeb - Use locale provider * @author Martin Herbst - Support of different language definition variants - * @authro Lyubomir Papazov - Add component annotation, rename the class to LocaleService and add method tryGetLocale + * @author Lyubomir Papazov - Add component annotation, rename the class to LocaleService and add method tryGetLocale */ @Component @NonNullByDefault @@ -81,7 +81,7 @@ private Locale tryGetLocale() { if (provider != null) { return provider.getLocale(); } else { - logger.error("There should ALWAYS be a local provider available, as it is provided by the core."); + logger.error("There should ALWAYS be a locale provider available, as it is provided by the core."); return Locale.US; } }