]> git.basschouten.com Git - openhab-addons.git/blob
8a6f98b4bbd7a875c0ee24ab1ecfcb8e31979fa6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.rest;
14
15 import java.util.Collections;
16 import java.util.List;
17
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;
27
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;
42
43 import com.google.gson.reflect.TypeToken;
44
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;
48
49 /**
50  * @author David Graeff - Initial contribution
51  */
52 @Component(immediate = false, service = ConfigurationAccess.class)
53 @JaxrsResource
54 @JaxrsApplicationSelect("(" + JaxrsWhiteboardConstants.JAX_RS_NAME + "=" + HueEmulationService.REST_APP_NAME + ")")
55 @NonNullByDefault
56 @Path("")
57 @Produces(MediaType.APPLICATION_JSON)
58 public class ConfigurationAccess {
59     @Reference
60     protected @NonNullByDefault({}) ConfigStore cs;
61     @Reference
62     protected @NonNullByDefault({}) UserManagement userManagement;
63     @Reference
64     protected @NonNullByDefault({}) ConfigurationAdmin configAdmin;
65
66     @GET
67     @Path("config")
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();
74     }
75
76     @GET
77     @Path("{username}")
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");
85         }
86         return Response.ok(cs.gson.toJson(cs.ds)).build();
87     }
88
89     @GET
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");
98         }
99         return Response.ok(cs.gson.toJson(cs.ds.config)).build();
100     }
101
102     @PUT
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");
111         }
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;
117         }
118         Boolean dhcp = changes.dhcp;
119         if (dhcp != null) {
120             cs.ds.config.dhcp = dhcp;
121         }
122         Boolean linkbutton = changes.linkbutton;
123         if (linkbutton != null) {
124             cs.setLinkbutton(linkbutton, cs.getConfig().createNewUserOnEveryEndpoint,
125                     cs.getConfig().temporarilyEmulateV1bridge);
126         }
127         return Response.ok(cs.gson.toJson(cs.ds.config)).build();
128     }
129
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(Collections.singleton(e), new TypeToken<List<?>>() {
136         }.getType());
137         return Response.status(404).entity(str).build();
138     }
139 }