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.ihc.internal.ws.http;
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;
21 import java.util.concurrent.atomic.AtomicInteger;
23 import javax.net.ssl.SSLHandshakeException;
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;
39 * Simple HTTP Client for IHC / ELKO LS Controller connection purposes.
41 * @author Pauli Anttila - Initial contribution
43 public abstract class IhcHttpsClient {
45 private final Logger logger = LoggerFactory.getLogger(IhcHttpsClient.class);
47 private IhcConnectionPool ihcConnectionPool;
48 private HttpClient client;
49 private HttpPost postReq;
50 private AtomicInteger counter = new AtomicInteger();
52 public IhcHttpsClient(IhcConnectionPool ihcConnectionPool) {
53 this.ihcConnectionPool = ihcConnectionPool;
57 * Init HTTP connection.
59 * @param url Url to connect.
61 private void initConnection(String url) throws IhcExecption {
63 client = ihcConnectionPool.getHttpClient();
65 postReq = new HttpPost(url);
69 * Send HTTP request and wait response from the server.
72 public String sendQuery(String url, Map<String, String> requestProperties, String query, int timeout)
75 setRequestProperty(requestProperties);
77 return sendQ(query, timeout);
78 } catch (SSLHandshakeException e) {
79 throw new IhcTlsExecption(e);
80 } catch (NoHttpResponseException | SocketTimeoutException e) {
82 logger.debug("No response received, resend query");
83 return sendQ(query, timeout);
84 } catch (IOException ee) {
85 throw new IhcExecption(ee);
87 } catch (IOException e) {
88 throw new IhcExecption(e);
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");
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);
106 final RequestConfig params = RequestConfig.custom().setConnectTimeout(timeout)
107 .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
108 postReq.setConfig(params);
111 LocalDateTime start = LocalDateTime.now();
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);
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());
132 * Set request properties.
135 private void setRequestProperty(Map<String, String> requestProperties) {
136 if (postReq != null && requestProperties != null) {
137 requestProperties.forEach((k, v) -> postReq.setHeader(k, v));