2 * Copyright (c) 2010-2023 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.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder;
15 import java.util.concurrent.locks.Lock;
16 import java.util.concurrent.locks.ReentrantLock;
18 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder.constants.ExeptionConstants;
19 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder.constants.InterfaceKeys;
20 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder.constants.ParameterKeys;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DSID;
24 * The {@link SimpleRequestBuilder} build a request string.<br>
26 * <i><b>Code example</b><br>
27 * String requestString = {@link SimpleRequestBuilder}.{@link #buildNewRequest(String)}.<br>
28 * <span style="padding-left:14em">{@link #addRequestClass(String)}.<br>
30 * <span style="padding-left:14em">{@link #addFunction(String)}.<br>
32 * <span style="padding-left:14em">{@link #addParameter(String, String)}. (optional)<br>
34 * <span style="padding-left:14em">{@link #addParameter(String, String)}. (optional)<br>
36 * <span style="padding-left:14em">{@link #buildRequestString()};<br>
39 * @author Michael Ochel - Initial contribution
40 * @author Matthias Siegele - Initial contribution
42 public class SimpleRequestBuilder {
45 private boolean functionIsChosen = false;
46 private boolean parameterIsAdded = false;
47 private boolean classIsChosen = false;
49 private String request;
50 private static SimpleRequestBuilder builder;
51 private static final Lock LOCK = new ReentrantLock();
53 private SimpleRequestBuilder() {
57 * Returns a {@link SimpleRequestBuilder} with the given intefaceKey as chosen request-interface.
59 * @param interfaceKey must not be null
60 * @return simpleRequestBuilder with chosen interface
62 public static SimpleRequestBuilder buildNewRequest(String interfaceKey) throws IllegalArgumentException {
63 if (builder == null) {
64 builder = new SimpleRequestBuilder();
67 return builder.buildNewRequestInt(interfaceKey);
71 * Returns a {@link SimpleRequestBuilder} with the intefaceKey "Json" as chosen request-interface and adds the given
72 * requestClass to the request-string.
74 * @param requestClassKey must not be null
75 * @return simpleRequestBuilder with chosen requestClass
76 * @throws IllegalArgumentException if a requestClass is already chosen
78 public static SimpleRequestBuilder buildNewJsonRequest(String requestClassKey) throws IllegalArgumentException {
79 return buildNewRequest(InterfaceKeys.JSON).addRequestClass(requestClassKey);
82 private SimpleRequestBuilder buildNewRequestInt(String interfaceKey) {
83 if (interfaceKey == null) {
84 throw new IllegalArgumentException("interfaceKey is null");
86 request = "/" + interfaceKey + "/";
87 classIsChosen = false;
88 functionIsChosen = false;
89 parameterIsAdded = false;
94 * Adds a requestClass to the request-string.
96 * @param requestClassKey must not be null
97 * @return simpleRequestBuilder with chosen requestClass
98 * @throws IllegalArgumentException if a requestClass is already chosen
100 public SimpleRequestBuilder addRequestClass(String requestClassKey) throws IllegalArgumentException {
101 return builder.addRequestClassInt(requestClassKey);
104 private SimpleRequestBuilder addRequestClassInt(String requestClassKey) {
105 if (!classIsChosen && requestClassKey != null) {
106 classIsChosen = true;
107 request = request + requestClassKey + "/";
109 if (!classIsChosen) {
110 throw new IllegalArgumentException(ExeptionConstants.CLASS_ALREADY_ADDED);
112 throw new IllegalArgumentException("requestClassKey is null");
119 * Adds a function to the request-string.
121 * @param functionKey must not be null
122 * @return SimpleRequestBuilder with chosen function
123 * @throws IllegalArgumentException if a function is already chosen
125 public SimpleRequestBuilder addFunction(String functionKey) throws IllegalArgumentException {
126 return builder.addFunctionInt(functionKey);
129 private SimpleRequestBuilder addFunctionInt(String functionKey) {
130 if (!classIsChosen) {
131 throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
133 if (!functionIsChosen) {
134 if (functionKey != null) {
135 functionIsChosen = true;
136 request = request + functionKey;
138 throw new IllegalArgumentException("functionKey is null");
141 throw new IllegalArgumentException(ExeptionConstants.FUNCTION_ALLREADY_ADDED);
147 * Adds a parameter to the request-string, if the parameter value is not null.
149 * @param parameterKey must not be null
150 * @param parameterValue can be null
151 * @return SimpleRequestBuilder with added parameter
152 * @throws IllegalArgumentException if no class and function added
154 public SimpleRequestBuilder addParameter(String parameterKey, String parameterValue)
155 throws IllegalArgumentException {
156 return builder.addParameterInt(parameterKey, parameterValue);
160 * Adds the default parameter for zone-requests to the request-string, if the parameter value is not null.
162 * @param sessionToken
165 * @return SimpleRequestBuilder with added parameter
166 * @throws IllegalArgumentException if no class and function added
168 public SimpleRequestBuilder addDefaultZoneParameter(String sessionToken, Integer zoneID, String zoneName)
169 throws IllegalArgumentException {
170 return addParameter(ParameterKeys.TOKEN, sessionToken).addParameter(ParameterKeys.ID, objectToString(zoneID))
171 .addParameter(ParameterKeys.NAME, zoneName);
175 * Adds a parameter for group-requests t the request-string, if the parameter value is not null.
177 * @param sessionToken
180 * @return SimpleRequestBuilder with added parameter
181 * @throws IllegalArgumentException if no class and function added
183 public SimpleRequestBuilder addDefaultGroupParameter(String sessionToken, Short groupID, String groupName)
184 throws IllegalArgumentException {
185 return addParameter(ParameterKeys.TOKEN, sessionToken)
186 .addParameter(ParameterKeys.GROUP_ID, objectToString(groupID))
187 .addParameter(ParameterKeys.GROUP_NAME, groupName);
191 * Adds a parameter for zone-group-requests t the request-string, if the parameter value is not null.
193 * @param sessionToken
198 * @return SimpleRequestBuilder with added parameter
199 * @throws IllegalArgumentException if no class and function added
201 public SimpleRequestBuilder addDefaultZoneGroupParameter(String sessionToken, Integer zoneID, String zoneName,
202 Short groupID, String groupName) throws IllegalArgumentException {
203 return addDefaultZoneParameter(sessionToken, zoneID, zoneName)
204 .addParameter(ParameterKeys.GROUP_ID, objectToString(groupID))
205 .addParameter(ParameterKeys.GROUP_NAME, groupName);
209 * Adds a parameter for device-requests the request-string, if the parameter value is not null.
211 * @param sessionToken
215 * @return SimpleRequestBuilder with added parameter
216 * @throws IllegalArgumentException if no class and function added
218 public SimpleRequestBuilder addDefaultDeviceParameter(String sessionToken, DSID dsid, String dSUID, String name)
219 throws IllegalArgumentException {
220 return addParameter(ParameterKeys.TOKEN, sessionToken).addParameter(ParameterKeys.DSID, objectToString(dsid))
221 .addParameter(ParameterKeys.DSUID, dSUID).addParameter(ParameterKeys.NAME, name);
224 private SimpleRequestBuilder addParameterInt(String parameterKey, String parameterValue) {
226 if (parameterKey == null) {
227 throw new IllegalArgumentException("parameterKey is null");
229 if (parameterValue != null) {
230 if (!parameterIsAdded) {
231 parameterIsAdded = true;
232 request = request + "?" + parameterKey + "=" + parameterValue;
234 request = request + "&" + parameterKey + "=" + parameterValue;
242 * Returns the request string.
244 * @return request string
245 * @throws IllegalArgumentException if no class or function is added.
247 public String buildRequestString() throws IllegalArgumentException {
248 String request = builder.buildRequestStringInt();
253 private String buildRequestStringInt() {
254 return allRight() ? request : null;
257 private boolean allRight() {
258 if (!classIsChosen) {
259 throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
261 if (!functionIsChosen) {
262 throw new IllegalArgumentException(ExeptionConstants.NO_FUNCTION);
268 * Convert an {@link Object} to a {@link String} or null, if the obj was null or it was a negative {@link Number}.
270 * @param obj can be null
271 * @return the {@link String} or null
273 public static String objectToString(Object obj) {
277 if (obj instanceof DSID id) {
278 return id.getValue();
280 if (obj instanceof Number number) {
281 return number.intValue() > -1 ? obj.toString() : null;
283 return obj.toString();
289 * @see java.lang.Object#equals()
291 public boolean equals(SimpleRequestBuilder builder) {
292 return this.request.contains(builder.request);