2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.rest;
15 import java.util.List;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.UriInfo;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.openhab.io.hueemulation.internal.ConfigStore;
30 import org.openhab.io.hueemulation.internal.HueEmulationService;
31 import org.openhab.io.hueemulation.internal.NetworkUtils;
32 import org.openhab.io.hueemulation.internal.dto.HueUnauthorizedConfig;
33 import org.openhab.io.hueemulation.internal.dto.changerequest.HueChangeRequest;
34 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
35 import org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage;
36 import org.osgi.service.cm.ConfigurationAdmin;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;
40 import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsApplicationSelect;
41 import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
43 import com.google.gson.reflect.TypeToken;
45 import io.swagger.v3.oas.annotations.Operation;
46 import io.swagger.v3.oas.annotations.Parameter;
47 import io.swagger.v3.oas.annotations.responses.ApiResponse;
50 * @author David Graeff - Initial contribution
52 @Component(immediate = false, service = ConfigurationAccess.class)
54 @JaxrsApplicationSelect("(" + JaxrsWhiteboardConstants.JAX_RS_NAME + "=" + HueEmulationService.REST_APP_NAME + ")")
57 @Produces(MediaType.APPLICATION_JSON)
58 public class ConfigurationAccess {
60 protected @NonNullByDefault({}) ConfigStore cs;
62 protected @NonNullByDefault({}) UserManagement userManagement;
64 protected @NonNullByDefault({}) ConfigurationAdmin configAdmin;
68 @Produces(MediaType.APPLICATION_JSON)
69 @Operation(summary = "Return the reduced configuration", responses = {
70 @ApiResponse(responseCode = "200", description = "OK") })
71 public Response getReducedConfigApi() {
72 return Response.ok(cs.gson.toJson(cs.ds.config, new TypeToken<HueUnauthorizedConfig>() {
73 }.getType())).build();
78 @Produces(MediaType.APPLICATION_JSON)
79 @Operation(summary = "Return the full data store", responses = {
80 @ApiResponse(responseCode = "200", description = "OK") })
81 public Response getAllApi(@Context UriInfo uri,
82 @PathParam("username") @Parameter(description = "username") String username) {
83 if (!userManagement.authorizeUser(username)) {
84 return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
86 return Response.ok(cs.gson.toJson(cs.ds)).build();
90 @Path("{username}/config")
91 @Produces(MediaType.APPLICATION_JSON)
92 @Operation(summary = "Return the configuration", responses = {
93 @ApiResponse(responseCode = "200", description = "OK") })
94 public Response getFullConfigApi(@Context UriInfo uri,
95 @PathParam("username") @Parameter(description = "username") String username) {
96 if (!userManagement.authorizeUser(username)) {
97 return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
99 return Response.ok(cs.gson.toJson(cs.ds.config)).build();
103 @Path("{username}/config")
104 @Produces(MediaType.APPLICATION_JSON)
105 @Operation(summary = "Return the reduced configuration", responses = {
106 @ApiResponse(responseCode = "200", description = "OK") })
107 public Response putFullConfigApi(@Context UriInfo uri,
108 @PathParam("username") @Parameter(description = "username") String username, String body) {
109 if (!userManagement.authorizeUser(username)) {
110 return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
112 final HueChangeRequest changes;
113 changes = cs.gson.fromJson(body, HueChangeRequest.class);
114 String devicename = changes.devicename;
115 if (devicename != null) {
116 cs.ds.config.devicename = devicename;
118 Boolean dhcp = changes.dhcp;
120 cs.ds.config.dhcp = dhcp;
122 Boolean linkbutton = changes.linkbutton;
123 if (linkbutton != null) {
124 cs.setLinkbutton(linkbutton, cs.getConfig().createNewUserOnEveryEndpoint,
125 cs.getConfig().temporarilyEmulateV1bridge);
127 return Response.ok(cs.gson.toJson(cs.ds.config)).build();
130 @Path("{username}/{var:.+}")
131 @Produces(MediaType.APPLICATION_JSON)
132 public Response catchAll(@Context UriInfo uri) {
133 HueResponse e = new HueResponse(
134 new HueErrorMessage(HueResponse.INVALID_JSON, uri.getPath().replace("/api", ""), "Invalid request: "));
135 String str = cs.gson.toJson(Set.of(e), new TypeToken<List<?>>() {
137 return Response.status(404).entity(str).build();