]> git.basschouten.com Git - openhab-addons.git/blob
bb1a57c279d1747c6444565cedd4acf4c557eed9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.binding.freeboxos.internal.api.rest;
14
15 import static org.eclipse.jetty.http.HttpMethod.*;
16
17 import java.net.URI;
18 import java.util.List;
19
20 import javax.ws.rs.core.UriBuilder;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
24 import org.openhab.binding.freeboxos.internal.api.PermissionException;
25 import org.openhab.binding.freeboxos.internal.api.Response;
26
27 /**
28  * Base class for the various rest managers available through the API
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  */
32 @NonNullByDefault
33 public class RestManager {
34     protected static final String REBOOT_ACTION = "reboot";
35     protected static final String SYSTEM_PATH = "system";
36
37     protected class GenericResponse extends Response<Object> {
38     }
39
40     private final UriBuilder uriBuilder;
41     protected final FreeboxOsSession session;
42
43     public RestManager(FreeboxOsSession session, LoginManager.Permission required, UriBuilder uri)
44             throws FreeboxException {
45         this.uriBuilder = uri;
46         this.session = session;
47         if (required != LoginManager.Permission.NONE && !session.hasPermission(required)) {
48             throw new PermissionException(required, "Permission missing: %s", required.toString());
49         }
50     }
51
52     protected UriBuilder getUriBuilder() {
53         return uriBuilder.clone();
54     }
55
56     private URI buildUri(String... pathElements) {
57         UriBuilder localBuilder = getUriBuilder();
58         for (String path : pathElements) {
59             localBuilder.path(path);
60         }
61         return localBuilder.build();
62     }
63
64     // Returns the first and supposed only element from the list. Presence of this element is expected and mandatory
65     private <F> F controlSingleton(List<F> result) {
66         if (result.size() == 1) {
67             return result.get(0);
68         }
69         throw new IllegalArgumentException("Result is empty or not singleton");
70     }
71
72     protected <F, T extends Response<F>> List<F> get(Class<T> clazz, String... pathElements) throws FreeboxException {
73         return session.execute(buildUri(pathElements), GET, clazz, null);
74     }
75
76     protected <F, T extends Response<F>> F getSingle(Class<T> clazz, String... pathElements) throws FreeboxException {
77         return controlSingleton(get(clazz, pathElements));
78     }
79
80     protected <F, T extends Response<F>> F post(Object payload, Class<T> clazz, String... pathElements)
81             throws FreeboxException {
82         return controlSingleton(session.execute(buildUri(pathElements), POST, clazz, payload));
83     }
84
85     protected void post(String... pathElements) throws FreeboxException {
86         session.execute(buildUri(pathElements), POST, GenericResponse.class, null);
87     }
88
89     protected <F, T extends Response<F>> F put(Class<T> clazz, F payload, String... pathElements)
90             throws FreeboxException {
91         return controlSingleton(session.execute(buildUri(pathElements), PUT, clazz, payload));
92     }
93
94     protected <F, T extends Response<F>> void put(F payload, String... pathElements) throws FreeboxException {
95         session.execute(buildUri(pathElements), PUT, GenericResponse.class, payload);
96     }
97 }