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