2 * Copyright (c) 2010-2024 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.binding.freeboxos.internal.api.rest;
15 import static org.eclipse.jetty.http.HttpMethod.*;
18 import java.util.List;
20 import javax.ws.rs.core.UriBuilder;
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;
28 * Base class for the various rest managers available through the API
30 * @author Gaƫl L'hopital - Initial contribution
33 public class RestManager {
34 protected static final String REBOOT_ACTION = "reboot";
35 protected static final String SYSTEM_PATH = "system";
37 protected class GenericResponse extends Response<Object> {
40 private final UriBuilder uriBuilder;
41 protected final FreeboxOsSession session;
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());
52 protected UriBuilder getUriBuilder() {
53 return uriBuilder.clone();
56 private URI buildUri(String... pathElements) {
57 UriBuilder localBuilder = getUriBuilder();
58 for (String path : pathElements) {
59 localBuilder.path(path);
61 return localBuilder.build();
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) {
69 throw new IllegalArgumentException("Result is empty or not singleton");
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);
76 protected <F, T extends Response<F>> F getSingle(Class<T> clazz, String... pathElements) throws FreeboxException {
77 return controlSingleton(get(clazz, pathElements));
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));
85 protected void post(String... pathElements) throws FreeboxException {
86 session.execute(buildUri(pathElements), POST, GenericResponse.class, null);
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));
94 protected <F, T extends Response<F>> void put(F payload, String... pathElements) throws FreeboxException {
95 session.execute(buildUri(pathElements), PUT, GenericResponse.class, payload);