]> git.basschouten.com Git - openhab-addons.git/blob
e4ac5bd6e73f43d5d04037997e25073fc6190c34
[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.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder;
14
15 import java.util.concurrent.locks.Lock;
16 import java.util.concurrent.locks.ReentrantLock;
17
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;
22
23 /**
24  * The {@link SimpleRequestBuilder} build a request string.<br>
25  * <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>
29  * </span>
30  * <span style="padding-left:14em">{@link #addFunction(String)}.<br>
31  * </span>
32  * <span style="padding-left:14em">{@link #addParameter(String, String)}. (optional)<br>
33  * </span>
34  * <span style="padding-left:14em">{@link #addParameter(String, String)}. (optional)<br>
35  * </span>
36  * <span style="padding-left:14em">{@link #buildRequestString()};<br>
37  * </span></i>
38  *
39  * @author Michael Ochel - Initial contribution
40  * @author Matthias Siegele - Initial contribution
41  */
42 public class SimpleRequestBuilder {
43
44     // states
45     private boolean functionIsChosen = false;
46     private boolean parameterIsAdded = false;
47     private boolean classIsChosen = false;
48
49     private String request;
50     private static SimpleRequestBuilder builder;
51     private static final Lock LOCK = new ReentrantLock();
52
53     private SimpleRequestBuilder() {
54     }
55
56     /**
57      * Returns a {@link SimpleRequestBuilder} with the given intefaceKey as chosen request-interface.
58      *
59      * @param interfaceKey must not be null
60      * @return simpleRequestBuilder with chosen interface
61      */
62     public static SimpleRequestBuilder buildNewRequest(String interfaceKey) throws IllegalArgumentException {
63         if (builder == null) {
64             builder = new SimpleRequestBuilder();
65         }
66         LOCK.lock();
67         return builder.buildNewRequestInt(interfaceKey);
68     }
69
70     /**
71      * Returns a {@link SimpleRequestBuilder} with the intefaceKey "Json" as chosen request-interface and adds the given
72      * requestClass to the request-string.
73      *
74      * @param requestClassKey must not be null
75      * @return simpleRequestBuilder with chosen requestClass
76      * @throws IllegalArgumentException if a requestClass is already chosen
77      */
78     public static SimpleRequestBuilder buildNewJsonRequest(String requestClassKey) throws IllegalArgumentException {
79         return buildNewRequest(InterfaceKeys.JSON).addRequestClass(requestClassKey);
80     }
81
82     private SimpleRequestBuilder buildNewRequestInt(String interfaceKey) {
83         if (interfaceKey == null) {
84             throw new IllegalArgumentException("interfaceKey is null");
85         }
86         request = "/" + interfaceKey + "/";
87         classIsChosen = false;
88         functionIsChosen = false;
89         parameterIsAdded = false;
90         return this;
91     }
92
93     /**
94      * Adds a requestClass to the request-string.
95      *
96      * @param requestClassKey must not be null
97      * @return simpleRequestBuilder with chosen requestClass
98      * @throws IllegalArgumentException if a requestClass is already chosen
99      */
100     public SimpleRequestBuilder addRequestClass(String requestClassKey) throws IllegalArgumentException {
101         return builder.addRequestClassInt(requestClassKey);
102     }
103
104     private SimpleRequestBuilder addRequestClassInt(String requestClassKey) {
105         if (!classIsChosen && requestClassKey != null) {
106             classIsChosen = true;
107             request = request + requestClassKey + "/";
108         } else {
109             if (!classIsChosen) {
110                 throw new IllegalArgumentException(ExeptionConstants.CLASS_ALREADY_ADDED);
111             } else {
112                 throw new IllegalArgumentException("requestClassKey is null");
113             }
114         }
115         return this;
116     }
117
118     /**
119      * Adds a function to the request-string.
120      *
121      * @param functionKey must not be null
122      * @return SimpleRequestBuilder with chosen function
123      * @throws IllegalArgumentException if a function is already chosen
124      */
125     public SimpleRequestBuilder addFunction(String functionKey) throws IllegalArgumentException {
126         return builder.addFunctionInt(functionKey);
127     }
128
129     private SimpleRequestBuilder addFunctionInt(String functionKey) {
130         if (!classIsChosen) {
131             throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
132         }
133         if (!functionIsChosen) {
134             if (functionKey != null) {
135                 functionIsChosen = true;
136                 request = request + functionKey;
137             } else {
138                 throw new IllegalArgumentException("functionKey is null");
139             }
140         } else {
141             throw new IllegalArgumentException(ExeptionConstants.FUNCTION_ALLREADY_ADDED);
142         }
143         return this;
144     }
145
146     /**
147      * Adds a parameter to the request-string, if the parameter value is not null.
148      *
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
153      */
154     public SimpleRequestBuilder addParameter(String parameterKey, String parameterValue)
155             throws IllegalArgumentException {
156         return builder.addParameterInt(parameterKey, parameterValue);
157     }
158
159     /**
160      * Adds the default parameter for zone-requests to the request-string, if the parameter value is not null.
161      *
162      * @param sessionToken
163      * @param zoneID
164      * @param zoneName
165      * @return SimpleRequestBuilder with added parameter
166      * @throws IllegalArgumentException if no class and function added
167      */
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);
172     }
173
174     /**
175      * Adds a parameter for group-requests t the request-string, if the parameter value is not null.
176      *
177      * @param sessionToken
178      * @param groupID
179      * @param groupName
180      * @return SimpleRequestBuilder with added parameter
181      * @throws IllegalArgumentException if no class and function added
182      */
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);
188     }
189
190     /**
191      * Adds a parameter for zone-group-requests t the request-string, if the parameter value is not null.
192      *
193      * @param sessionToken
194      * @param zoneID
195      * @param zoneName
196      * @param groupID
197      * @param groupName
198      * @return SimpleRequestBuilder with added parameter
199      * @throws IllegalArgumentException if no class and function added
200      */
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);
206     }
207
208     /**
209      * Adds a parameter for device-requests the request-string, if the parameter value is not null.
210      *
211      * @param sessionToken
212      * @param dsid
213      * @param dSUID
214      * @param name
215      * @return SimpleRequestBuilder with added parameter
216      * @throws IllegalArgumentException if no class and function added
217      */
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);
222     }
223
224     private SimpleRequestBuilder addParameterInt(String parameterKey, String parameterValue) {
225         if (allRight()) {
226             if (parameterKey == null) {
227                 throw new IllegalArgumentException("parameterKey is null");
228             }
229             if (parameterValue != null) {
230                 if (!parameterIsAdded) {
231                     parameterIsAdded = true;
232                     request = request + "?" + parameterKey + "=" + parameterValue;
233                 } else {
234                     request = request + "&" + parameterKey + "=" + parameterValue;
235                 }
236             }
237         }
238         return this;
239     }
240
241     /**
242      * Returns the request string.
243      *
244      * @return request string
245      * @throws IllegalArgumentException if no class or function is added.
246      */
247     public String buildRequestString() throws IllegalArgumentException {
248         String request = builder.buildRequestStringInt();
249         LOCK.unlock();
250         return request;
251     }
252
253     private String buildRequestStringInt() {
254         return allRight() ? request : null;
255     }
256
257     private boolean allRight() {
258         if (!classIsChosen) {
259             throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
260         }
261         if (!functionIsChosen) {
262             throw new IllegalArgumentException(ExeptionConstants.NO_FUNCTION);
263         }
264         return true;
265     }
266
267     /**
268      * Convert an {@link Object} to a {@link String} or null, if the obj was null or it was a negative {@link Number}.
269      *
270      * @param obj can be null
271      * @return the {@link String} or null
272      */
273     public static String objectToString(Object obj) {
274         if (obj == null) {
275             return null;
276         }
277         if (obj instanceof DSID id) {
278             return id.getValue();
279         }
280         if (obj instanceof Number number) {
281             return number.intValue() > -1 ? obj.toString() : null;
282         }
283         return obj.toString();
284     }
285
286     /*
287      * (non-Javadoc)
288      *
289      * @see java.lang.Object#equals()
290      */
291     public boolean equals(SimpleRequestBuilder builder) {
292         return this.request.contains(builder.request);
293     }
294 }