]> git.basschouten.com Git - openhab-addons.git/blob
01cbb07fc723402d6f8a7f2e3039c3c5df97e96a
[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 contributer
40  * @author Matthias Siegele - initial contributer
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      * @throws NullArgumentException if the interfaceKey is null
62      */
63     public static SimpleRequestBuilder buildNewRequest(String interfaceKey) throws IllegalArgumentException {
64         if (builder == null) {
65             builder = new SimpleRequestBuilder();
66         }
67         LOCK.lock();
68         return builder.buildNewRequestInt(interfaceKey);
69     }
70
71     /**
72      * Returns a {@link SimpleRequestBuilder} with the intefaceKey "Json" as chosen request-interface and adds the given
73      * requestClass to the request-string.
74      *
75      * @param requestClassKey must not be null
76      * @return simpleRequestBuilder with chosen requestClass
77      * @throws IllegalArgumentException if a requestClass is already chosen
78      * @throws NullArgumentException if the requestClassKey is null
79      */
80     public static SimpleRequestBuilder buildNewJsonRequest(String requestClassKey) throws IllegalArgumentException {
81         return buildNewRequest(InterfaceKeys.JSON).addRequestClass(requestClassKey);
82     }
83
84     private SimpleRequestBuilder buildNewRequestInt(String interfaceKey) {
85         if (interfaceKey == null) {
86             throw new IllegalArgumentException("interfaceKey is null");
87         }
88         request = "/" + interfaceKey + "/";
89         classIsChosen = false;
90         functionIsChosen = false;
91         parameterIsAdded = false;
92         return this;
93     }
94
95     /**
96      * Adds a requestClass to the request-string.
97      *
98      * @param requestClassKey must not be null
99      * @return simpleRequestBuilder with chosen requestClass
100      * @throws IllegalArgumentException if a requestClass is already chosen
101      * @throws NullArgumentException if the requestClassKey is null
102      */
103     public SimpleRequestBuilder addRequestClass(String requestClassKey) throws IllegalArgumentException {
104         return builder.addRequestClassInt(requestClassKey);
105     }
106
107     private SimpleRequestBuilder addRequestClassInt(String requestClassKey) {
108         if (!classIsChosen && requestClassKey != null) {
109             classIsChosen = true;
110             request = request + requestClassKey + "/";
111         } else {
112             if (!classIsChosen) {
113                 throw new IllegalArgumentException(ExeptionConstants.CLASS_ALREADY_ADDED);
114             } else {
115                 throw new IllegalArgumentException("requestClassKey is null");
116             }
117         }
118         return this;
119     }
120
121     /**
122      * Adds a function to the request-string.
123      *
124      * @param functionKey must not be null
125      * @return SimpleRequestBuilder with chosen function
126      * @throws IllegalArgumentException if a function is already chosen
127      * @throws NullArgumentException if the functionKey is null
128      */
129     public SimpleRequestBuilder addFunction(String functionKey) throws IllegalArgumentException {
130         return builder.addFunctionInt(functionKey);
131     }
132
133     private SimpleRequestBuilder addFunctionInt(String functionKey) {
134         if (!classIsChosen) {
135             throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
136         }
137         if (!functionIsChosen) {
138             if (functionKey != null) {
139                 functionIsChosen = true;
140                 request = request + functionKey;
141             } else {
142                 throw new IllegalArgumentException("functionKey is null");
143             }
144         } else {
145             throw new IllegalArgumentException(ExeptionConstants.FUNCTION_ALLREADY_ADDED);
146         }
147         return this;
148     }
149
150     /**
151      * Adds a parameter to the request-string, if the parameter value is not null.
152      *
153      * @param parameterKey must not be null
154      * @param parameterValue can be null
155      * @return SimpleRequestBuilder with added parameter
156      * @throws IllegalArgumentException if no class and function added
157      * @throws NullArgumentException if the parameterKey is null
158      */
159     public SimpleRequestBuilder addParameter(String parameterKey, String parameterValue)
160             throws IllegalArgumentException {
161         return builder.addParameterInt(parameterKey, parameterValue);
162     }
163
164     /**
165      * Adds the default parameter for zone-requests to the request-string, if the parameter value is not null.
166      *
167      * @param sessionToken
168      * @param zoneID
169      * @param zoneName
170      * @return SimpleRequestBuilder with added parameter
171      * @throws IllegalArgumentException if no class and function added
172      * @throws NullArgumentException if the parameterKey is null
173      */
174     public SimpleRequestBuilder addDefaultZoneParameter(String sessionToken, Integer zoneID, String zoneName)
175             throws IllegalArgumentException {
176         return addParameter(ParameterKeys.TOKEN, sessionToken).addParameter(ParameterKeys.ID, objectToString(zoneID))
177                 .addParameter(ParameterKeys.NAME, zoneName);
178     }
179
180     /**
181      * Adds a parameter for group-requests t the request-string, if the parameter value is not null.
182      *
183      * @param sessionToken
184      * @param groupID
185      * @param groupName
186      * @return SimpleRequestBuilder with added parameter
187      * @throws IllegalArgumentException if no class and function added
188      * @throws NullArgumentException if the parameterKey is null
189      */
190     public SimpleRequestBuilder addDefaultGroupParameter(String sessionToken, Short groupID, String groupName)
191             throws IllegalArgumentException {
192         return addParameter(ParameterKeys.TOKEN, sessionToken)
193                 .addParameter(ParameterKeys.GROUP_ID, objectToString(groupID))
194                 .addParameter(ParameterKeys.GROUP_NAME, groupName);
195     }
196
197     /**
198      * Adds a parameter for zone-group-requests t the request-string, if the parameter value is not null.
199      *
200      * @param sessionToken
201      * @param zoneID
202      * @param zoneName
203      * @param groupID
204      * @param groupName
205      * @return SimpleRequestBuilder with added parameter
206      * @throws IllegalArgumentException if no class and function added
207      * @throws NullArgumentException if the parameterKey is null
208      */
209     public SimpleRequestBuilder addDefaultZoneGroupParameter(String sessionToken, Integer zoneID, String zoneName,
210             Short groupID, String groupName) throws IllegalArgumentException {
211         return addDefaultZoneParameter(sessionToken, zoneID, zoneName)
212                 .addParameter(ParameterKeys.GROUP_ID, objectToString(groupID))
213                 .addParameter(ParameterKeys.GROUP_NAME, groupName);
214     }
215
216     /**
217      * Adds a parameter for device-requests the request-string, if the parameter value is not null.
218      *
219      * @param sessionToken
220      * @param dsid
221      * @param dSUID
222      * @param name
223      * @return SimpleRequestBuilder with added parameter
224      * @throws IllegalArgumentException if no class and function added
225      * @throws NullArgumentException if the parameterKey is null
226      */
227     public SimpleRequestBuilder addDefaultDeviceParameter(String sessionToken, DSID dsid, String dSUID, String name)
228             throws IllegalArgumentException {
229         return addParameter(ParameterKeys.TOKEN, sessionToken).addParameter(ParameterKeys.DSID, objectToString(dsid))
230                 .addParameter(ParameterKeys.DSUID, dSUID).addParameter(ParameterKeys.NAME, name);
231     }
232
233     private SimpleRequestBuilder addParameterInt(String parameterKey, String parameterValue) {
234         if (allRight()) {
235             if (parameterKey == null) {
236                 throw new IllegalArgumentException("parameterKey is null");
237             }
238             if (parameterValue != null) {
239                 if (!parameterIsAdded) {
240                     parameterIsAdded = true;
241                     request = request + "?" + parameterKey + "=" + parameterValue;
242                 } else {
243                     request = request + "&" + parameterKey + "=" + parameterValue;
244                 }
245             }
246         }
247         return this;
248     }
249
250     /**
251      * Returns the request string.
252      *
253      * @return request string
254      * @throws IllegalArgumentException if no class or function is added.
255      */
256     public String buildRequestString() throws IllegalArgumentException {
257         String request = builder.buildRequestStringInt();
258         LOCK.unlock();
259         return request;
260     }
261
262     private String buildRequestStringInt() {
263         return allRight() ? request : null;
264     }
265
266     private boolean allRight() {
267         if (!classIsChosen) {
268             throw new IllegalArgumentException(ExeptionConstants.NO_CLASS_ADDED);
269         }
270         if (!functionIsChosen) {
271             throw new IllegalArgumentException(ExeptionConstants.NO_FUNCTION);
272         }
273         return true;
274     }
275
276     /**
277      * Convert an {@link Object} to a {@link String} or null, if the obj was null or it was a negative {@link Number}.
278      *
279      * @param obj can be null
280      * @return the {@link String} or null
281      */
282     public static String objectToString(Object obj) {
283         if (obj == null) {
284             return null;
285         }
286         if (obj instanceof DSID) {
287             return ((DSID) obj).getValue();
288         }
289         if (obj instanceof Number) {
290             return ((Number) obj).intValue() > -1 ? obj.toString() : null;
291         }
292         return obj.toString();
293     }
294
295     /*
296      * (non-Javadoc)
297      *
298      * @see java.lang.Object#equals()
299      */
300     public boolean equals(SimpleRequestBuilder builder) {
301         return this.request.contains(builder.request);
302     }
303 }