]> git.basschouten.com Git - openhab-addons.git/blob
c815b4c0dbd8857a4c8d566bd5169110d820addc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.NetworkUtils;
31 import org.openhab.io.hueemulation.internal.dto.HueUnauthorizedConfig;
32 import org.openhab.io.hueemulation.internal.dto.changerequest.HueChangeRequest;
33 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
34 import org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage;
35 import org.osgi.service.cm.ConfigurationAdmin;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38
39 import com.google.gson.reflect.TypeToken;
40
41 import io.swagger.annotations.ApiOperation;
42 import io.swagger.annotations.ApiParam;
43 import io.swagger.annotations.ApiResponse;
44 import io.swagger.annotations.ApiResponses;
45
46 /**
47  * @author David Graeff - Initial contribution
48  */
49 @Component(immediate = false, service = {
50         ConfigurationAccess.class }, property = "com.eclipsesource.jaxrs.publish=false")
51 @NonNullByDefault
52 @Path("")
53 @Produces(MediaType.APPLICATION_JSON)
54 public class ConfigurationAccess {
55     @Reference
56     protected @NonNullByDefault({}) ConfigStore cs;
57     @Reference
58     protected @NonNullByDefault({}) UserManagement userManagement;
59     @Reference
60     protected @NonNullByDefault({}) ConfigurationAdmin configAdmin;
61
62     @GET
63     @Path("config")
64     @Produces(MediaType.APPLICATION_JSON)
65     @ApiOperation(value = "Return the reduced configuration")
66     @ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
67     public Response getReducedConfigApi() {
68         return Response.ok(cs.gson.toJson(cs.ds.config, new TypeToken<HueUnauthorizedConfig>() {
69         }.getType())).build();
70     }
71
72     @GET
73     @Path("{username}")
74     @Produces(MediaType.APPLICATION_JSON)
75     @ApiOperation(value = "Return the full data store")
76     @ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
77     public Response getAllApi(@Context UriInfo uri,
78             @PathParam("username") @ApiParam(value = "username") String username) {
79         if (!userManagement.authorizeUser(username)) {
80             return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
81         }
82         return Response.ok(cs.gson.toJson(cs.ds)).build();
83     }
84
85     @GET
86     @Path("{username}/config")
87     @Produces(MediaType.APPLICATION_JSON)
88     @ApiOperation(value = "Return the configuration")
89     @ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
90     public Response getFullConfigApi(@Context UriInfo uri,
91             @PathParam("username") @ApiParam(value = "username") String username) {
92         if (!userManagement.authorizeUser(username)) {
93             return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
94         }
95         return Response.ok(cs.gson.toJson(cs.ds.config)).build();
96     }
97
98     @PUT
99     @Path("{username}/config")
100     @Produces(MediaType.APPLICATION_JSON)
101     @ApiOperation(value = "Return the reduced configuration")
102     @ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
103     public Response putFullConfigApi(@Context UriInfo uri,
104             @PathParam("username") @ApiParam(value = "username") String username, String body) {
105         if (!userManagement.authorizeUser(username)) {
106             return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
107         }
108         final HueChangeRequest changes;
109         changes = cs.gson.fromJson(body, HueChangeRequest.class);
110         String devicename = changes.devicename;
111         if (devicename != null) {
112             cs.ds.config.devicename = devicename;
113         }
114         Boolean dhcp = changes.dhcp;
115         if (dhcp != null) {
116             cs.ds.config.dhcp = dhcp;
117         }
118         Boolean linkbutton = changes.linkbutton;
119         if (linkbutton != null) {
120             cs.setLinkbutton(linkbutton, cs.getConfig().createNewUserOnEveryEndpoint,
121                     cs.getConfig().temporarilyEmulateV1bridge);
122         }
123         return Response.ok(cs.gson.toJson(cs.ds.config)).build();
124     }
125
126     @Path("{username}/{var:.+}")
127     @Produces(MediaType.APPLICATION_JSON)
128     public Response catchAll(@Context UriInfo uri) {
129         HueResponse e = new HueResponse(
130                 new HueErrorMessage(HueResponse.INVALID_JSON, uri.getPath().replace("/api", ""), "Invalid request: "));
131         String str = cs.gson.toJson(Collections.singleton(e), new TypeToken<List<?>>() {
132         }.getType());
133         return Response.status(404).entity(str).build();
134     }
135 }