]> git.basschouten.com Git - openhab-addons.git/blob
beb05daae8217e3666d525a5951ea654e967e33f
[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.ihc.internal.ws.http;
14
15 import java.io.IOException;
16 import java.net.SocketTimeoutException;
17 import java.nio.charset.StandardCharsets;
18 import java.time.Duration;
19 import java.time.LocalDateTime;
20 import java.util.Map;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import javax.net.ssl.SSLHandshakeException;
24
25 import org.apache.http.HttpResponse;
26 import org.apache.http.NoHttpResponseException;
27 import org.apache.http.client.ClientProtocolException;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.client.config.RequestConfig;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.entity.StringEntity;
32 import org.apache.http.util.EntityUtils;
33 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
34 import org.openhab.binding.ihc.internal.ws.exeptions.IhcTlsExecption;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Simple HTTP Client for IHC / ELKO LS Controller connection purposes.
40  *
41  * @author Pauli Anttila - Initial contribution
42  */
43 public abstract class IhcHttpsClient {
44
45     private final Logger logger = LoggerFactory.getLogger(IhcHttpsClient.class);
46
47     private IhcConnectionPool ihcConnectionPool;
48     private HttpClient client;
49     private HttpPost postReq;
50     private AtomicInteger counter = new AtomicInteger();
51
52     public IhcHttpsClient(IhcConnectionPool ihcConnectionPool) {
53         this.ihcConnectionPool = ihcConnectionPool;
54     }
55
56     /**
57      * Init HTTP connection.
58      *
59      * @param url Url to connect.
60      */
61     private void initConnection(String url) throws IhcExecption {
62         if (client == null) {
63             client = ihcConnectionPool.getHttpClient();
64         }
65         postReq = new HttpPost(url);
66     }
67
68     /**
69      * Send HTTP request and wait response from the server.
70      *
71      */
72     public String sendQuery(String url, Map<String, String> requestProperties, String query, int timeout)
73             throws IhcExecption {
74         initConnection(url);
75         setRequestProperty(requestProperties);
76         try {
77             return sendQ(query, timeout);
78         } catch (SSLHandshakeException e) {
79             throw new IhcTlsExecption(e);
80         } catch (NoHttpResponseException | SocketTimeoutException e) {
81             try {
82                 logger.debug("No response received, resend query");
83                 return sendQ(query, timeout);
84             } catch (IOException ee) {
85                 throw new IhcExecption(ee);
86             }
87         } catch (IOException e) {
88             throw new IhcExecption(e);
89         }
90     }
91
92     private String sendQ(String query, int timeout)
93             throws ClientProtocolException, IOException, NoHttpResponseException {
94         postReq.setEntity(new StringEntity(query, StandardCharsets.UTF_8.name()));
95         postReq.addHeader("content-type", "text/xml");
96
97         int requestId = 0;
98
99         if (logger.isTraceEnabled()) {
100             requestId = counter.getAndIncrement();
101             logger.trace("Send query (url={}, connectionPool={}, clientId={} requestId={}, timeout={}, headers={}): {}",
102                     postReq.getURI(), ihcConnectionPool.hashCode(), client.hashCode(), requestId, timeout,
103                     postReq.getAllHeaders(), query);
104         }
105
106         final RequestConfig params = RequestConfig.custom().setConnectTimeout(timeout)
107                 .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
108         postReq.setConfig(params);
109
110         // Execute POST
111         LocalDateTime start = LocalDateTime.now();
112         try {
113             HttpResponse response = client.execute(postReq, ihcConnectionPool.getHttpContext());
114             String resp = EntityUtils.toString(response.getEntity());
115             if (logger.isTraceEnabled()) {
116                 logger.trace("Received response (connectionPool={}, clientId={} requestId={}, in {}, headers={}): {}",
117                         ihcConnectionPool.hashCode(), client.hashCode(), requestId,
118                         Duration.between(start, LocalDateTime.now()), response.getAllHeaders(), resp);
119             }
120             return resp;
121         } catch (Exception e) {
122             if (logger.isTraceEnabled()) {
123                 logger.trace("Exception occured (connectionPool={}, clientId={} requestId={}, in {}): {}",
124                         ihcConnectionPool.hashCode(), client.hashCode(), requestId,
125                         Duration.between(start, LocalDateTime.now()), e.getMessage());
126             }
127             throw (e);
128         }
129     }
130
131     /**
132      * Set request properties.
133      *
134      */
135     private void setRequestProperty(Map<String, String> requestProperties) {
136         if (postReq != null && requestProperties != null) {
137             requestProperties.forEach((k, v) -> postReq.setHeader(k, v));
138         }
139     }
140 }