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.plugwiseha.internal.api.model;
15 import java.io.StringReader;
16 import java.io.StringWriter;
17 import java.net.ConnectException;
18 import java.net.SocketTimeoutException;
19 import java.net.UnknownHostException;
20 import java.nio.ByteBuffer;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Base64;
23 import java.util.HashMap;
24 import java.util.Iterator;
26 import java.util.Map.Entry;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import java.util.stream.Collectors;
32 import javax.xml.transform.Transformer;
33 import javax.xml.transform.TransformerException;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
37 import org.eclipse.jdt.annotation.NonNullByDefault;
38 import org.eclipse.jdt.annotation.Nullable;
39 import org.eclipse.jetty.client.HttpClient;
40 import org.eclipse.jetty.client.api.ContentProvider;
41 import org.eclipse.jetty.client.api.ContentResponse;
42 import org.eclipse.jetty.client.api.Request;
43 import org.eclipse.jetty.client.util.StringContentProvider;
44 import org.eclipse.jetty.http.HttpHeader;
45 import org.eclipse.jetty.http.HttpMethod;
46 import org.eclipse.jetty.http.HttpScheme;
47 import org.eclipse.jetty.http.HttpStatus;
48 import org.eclipse.jetty.http.HttpURI;
49 import org.eclipse.jetty.http.MimeTypes;
50 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHABadRequestException;
51 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHAException;
52 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHAForbiddenException;
53 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHATimeoutException;
54 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHAUnauthorizedException;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.thoughtworks.xstream.XStream;
61 * The {@link PlugwiseHAControllerRequest} class is a utility class to create
62 * API requests to the Plugwise Home Automation controller and to deserialize
63 * incoming XML into the appropriate model objects to be used by the {@link
64 * PlugwiseHAController}.
66 * @author B. van Wetten - Initial contribution
67 * @author Leo Siepel - Adjustments to timeout logic
70 public class PlugwiseHAControllerRequest<T> {
72 private static final String CONTENT_TYPE_TEXT_XML = MimeTypes.Type.TEXT_XML_8859_1.toString();
73 private static final long TIMEOUT_SECONDS = 5;
74 private static final int REQUEST_MAX_RETRY_COUNT = 3;
76 private final Logger logger = LoggerFactory.getLogger(PlugwiseHAControllerRequest.class);
77 private final XStream xStream;
78 private final HttpClient httpClient;
79 private final String host;
80 private final int port;
81 private final Class<T> resultType;
82 private final @Nullable Transformer transformer;
84 private Map<String, String> headers = new HashMap<>();
85 private Map<String, String> queryParameters = new HashMap<>();
86 private @Nullable Object bodyParameter;
87 private String serverDateTime = "";
88 private String path = "/";
92 <X extends XStream> PlugwiseHAControllerRequest(Class<T> resultType, X xStream, @Nullable Transformer transformer,
93 HttpClient httpClient, String host, int port, String username, String password) {
94 this.resultType = resultType;
95 this.xStream = xStream;
96 this.transformer = transformer;
97 this.httpClient = httpClient;
101 setHeader(HttpHeader.ACCEPT.toString(), CONTENT_TYPE_TEXT_XML);
103 // Create Basic Auth header if username and password are supplied
104 if (!username.isBlank() && !password.isBlank()) {
105 setHeader(HttpHeader.AUTHORIZATION.toString(), "Basic " + Base64.getEncoder()
106 .encodeToString(String.format("%s:%s", username, password).getBytes(StandardCharsets.UTF_8)));
112 public void setPath(String path) {
113 this.setPath(path, (HashMap<String, String>) null);
116 public void setPath(String path, @Nullable HashMap<String, String> pathParameters) {
119 if (pathParameters != null) {
120 this.path += pathParameters.entrySet().stream().map(Object::toString).collect(Collectors.joining(";"));
124 public void setHeader(String key, Object value) {
125 this.headers.put(key, String.valueOf(value));
128 public void addPathParameter(String key) {
129 this.path += String.format(";%s", key);
132 public void addPathParameter(String key, Object value) {
133 this.path += String.format(";%s=%s", key, value);
136 public void addPathFilter(String key, String operator, Object value) {
137 this.path += String.format(";%s:%s:%s", key, operator, value);
140 public void setQueryParameter(String key, Object value) {
141 this.queryParameters.put(key, String.valueOf(value));
144 public void setBodyParameter(Object body) {
145 this.bodyParameter = body;
148 public String getServerDateTime() {
149 return this.serverDateTime;
152 @SuppressWarnings("unchecked")
153 public @Nullable T execute() throws PlugwiseHAException {
155 String xml = getContent();
157 if (String.class.equals(resultType)) {
158 if (this.transformer != null) {
159 result = (T) this.transformXML(xml);
163 } else if (!Void.class.equals(resultType)) {
164 if (this.transformer != null) {
165 result = (T) this.xStream.fromXML(this.transformXML(xml));
167 result = (T) this.xStream.fromXML(xml);
176 // Protected and private methods
178 private String transformXML(String xml) throws PlugwiseHAException {
179 StringReader input = new StringReader(xml);
180 StringWriter output = new StringWriter();
181 Transformer localTransformer = this.transformer;
182 if (localTransformer != null) {
184 localTransformer.transform(new StreamSource(input), new StreamResult(output));
185 } catch (TransformerException e) {
186 logger.debug("Could not apply XML stylesheet", e);
187 throw new PlugwiseHAException("Could not apply XML stylesheet", e);
190 throw new PlugwiseHAException("Could not transform XML stylesheet, the transformer is null");
193 return output.toString();
196 private String getContent() throws PlugwiseHAException {
198 ContentResponse response = getContentResponse(REQUEST_MAX_RETRY_COUNT);
200 int status = response.getStatus();
202 case HttpStatus.OK_200:
203 case HttpStatus.ACCEPTED_202:
204 content = response.getContentAsString();
205 if (logger.isTraceEnabled()) {
206 logger.trace("<< {} {} \n{}", status, HttpStatus.getMessage(status), content);
209 case HttpStatus.BAD_REQUEST_400:
210 throw new PlugwiseHABadRequestException("Bad request");
211 case HttpStatus.UNAUTHORIZED_401:
212 throw new PlugwiseHAUnauthorizedException("Unauthorized");
213 case HttpStatus.FORBIDDEN_403:
214 throw new PlugwiseHAForbiddenException("Forbidden");
216 throw new PlugwiseHAException("Unknown HTTP status code " + status + " returned by the controller");
219 this.serverDateTime = response.getHeaders().get("Date");
224 private ContentResponse getContentResponse(int retries) throws PlugwiseHAException {
225 Request request = newRequest();
226 ContentResponse response;
228 this.logger.debug("Performing API request: {} {}", request.getMethod(), request.getURI());
230 if (logger.isTraceEnabled()) {
232 final ContentProvider provider = request.getContent();
233 if (provider != null) {
234 final Iterator<ByteBuffer> it = provider.iterator();
235 while (it.hasNext()) {
236 final ByteBuffer next = it.next();
237 final byte[] bytes = new byte[next.capacity()];
239 content += String.format("{}\n", new String(bytes, StandardCharsets.UTF_8));
243 logger.trace(">> \n{}", content);
247 response = request.send();
248 } catch (InterruptedException e) {
249 this.logger.trace("InterruptedException occured {} {}", e.getMessage(), e.getStackTrace());
250 Thread.currentThread().interrupt();
251 throw new PlugwiseHATimeoutException(e);
252 } catch (TimeoutException e) {
254 this.logger.debug("TimeoutException occured, remaining retries {}", retries - 1);
255 return getContentResponse(retries - 1);
257 throw new PlugwiseHATimeoutException(e);
259 } catch (ExecutionException e) {
260 // Unwrap the cause and try to cleanly handle it
261 Throwable cause = e.getCause();
262 if (cause instanceof UnknownHostException) {
264 throw new PlugwiseHAException(cause);
265 } else if (cause instanceof ConnectException) {
267 throw new PlugwiseHAException(cause);
268 } else if (cause instanceof SocketTimeoutException) {
269 throw new PlugwiseHATimeoutException(cause);
270 } else if (cause == null) {
272 throw new PlugwiseHAException(e);
275 throw new PlugwiseHAException(cause);
281 private Request newRequest() {
282 HttpMethod method = bodyParameter == null ? HttpMethod.GET : HttpMethod.PUT;
283 HttpURI uri = new HttpURI(HttpScheme.HTTP.asString(), this.host, this.port, this.path);
284 Request request = httpClient.newRequest(uri.toString()).timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
287 for (Entry<String, String> entry : this.headers.entrySet()) {
288 request.header(entry.getKey(), entry.getValue());
291 for (Entry<String, String> entry : this.queryParameters.entrySet()) {
292 request.param(entry.getKey(), entry.getValue());
295 if (this.bodyParameter != null) {
296 String xmlBody = getRequestBodyAsXml();
297 ContentProvider content = new StringContentProvider(CONTENT_TYPE_TEXT_XML, xmlBody, StandardCharsets.UTF_8);
298 request = request.content(content);
303 private String getRequestBodyAsXml() {
304 return this.xStream.toXML(this.bodyParameter);