2 * Copyright (c) 2010-2020 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.Collections;
16 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.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;
39 import com.google.gson.reflect.TypeToken;
41 import io.swagger.annotations.ApiOperation;
42 import io.swagger.annotations.ApiParam;
43 import io.swagger.annotations.ApiResponse;
44 import io.swagger.annotations.ApiResponses;
47 * @author David Graeff - Initial contribution
49 @Component(immediate = false, service = {
50 ConfigurationAccess.class }, property = "com.eclipsesource.jaxrs.publish=false")
53 @Produces(MediaType.APPLICATION_JSON)
54 public class ConfigurationAccess {
56 protected @NonNullByDefault({}) ConfigStore cs;
58 protected @NonNullByDefault({}) UserManagement userManagement;
60 protected @NonNullByDefault({}) ConfigurationAdmin configAdmin;
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();
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");
82 return Response.ok(cs.gson.toJson(cs.ds)).build();
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");
95 return Response.ok(cs.gson.toJson(cs.ds.config)).build();
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");
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;
114 Boolean dhcp = changes.dhcp;
116 cs.ds.config.dhcp = dhcp;
118 Boolean linkbutton = changes.linkbutton;
119 if (linkbutton != null) {
120 cs.setLinkbutton(linkbutton, cs.getConfig().createNewUserOnEveryEndpoint,
121 cs.getConfig().temporarilyEmulateV1bridge);
123 return Response.ok(cs.gson.toJson(cs.ds.config)).build();
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<?>>() {
133 return Response.status(404).entity(str).build();