2 * Copyright (c) 2010-2020 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 org.apache.http.HttpResponse;
24 import org.apache.http.NoHttpResponseException;
25 import org.apache.http.client.ClientProtocolException;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.client.config.RequestConfig;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.entity.StringEntity;
30 import org.apache.http.util.EntityUtils;
31 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Simple HTTP Client for IHC / ELKO LS Controller connection purposes.
38 * @author Pauli Anttila - Initial contribution
40 public abstract class IhcHttpsClient {
42 private final Logger logger = LoggerFactory.getLogger(IhcHttpsClient.class);
44 private IhcConnectionPool ihcConnectionPool;
45 private HttpClient client;
46 private HttpPost postReq;
47 private AtomicInteger counter = new AtomicInteger();
49 public IhcHttpsClient(IhcConnectionPool ihcConnectionPool) {
50 this.ihcConnectionPool = ihcConnectionPool;
54 * Init HTTP connection.
56 * @param url Url to connect.
58 private void initConnection(String url) throws IhcExecption {
60 client = ihcConnectionPool.getHttpClient();
62 postReq = new HttpPost(url);
66 * Send HTTP request and wait response from the server.
69 public String sendQuery(String url, Map<String, String> requestProperties, String query, int timeout)
72 setRequestProperty(requestProperties);
74 return sendQ(query, timeout);
75 } catch (NoHttpResponseException | SocketTimeoutException e) {
77 logger.debug("No response received, resend query");
78 return sendQ(query, timeout);
79 } catch (IOException ee) {
80 throw new IhcExecption(ee);
82 } catch (IOException e) {
83 throw new IhcExecption(e);
87 private String sendQ(String query, int timeout)
88 throws ClientProtocolException, IOException, NoHttpResponseException {
89 postReq.setEntity(new StringEntity(query, StandardCharsets.UTF_8.name()));
90 postReq.addHeader("content-type", "text/xml");
94 if (logger.isTraceEnabled()) {
95 requestId = counter.getAndIncrement();
96 logger.trace("Send query (url={}, connectionPool={}, clientId={} requestId={}, timeout={}, headers={}): {}",
97 postReq.getURI(), ihcConnectionPool.hashCode(), client.hashCode(), requestId, timeout,
98 postReq.getAllHeaders(), query);
101 final RequestConfig params = RequestConfig.custom().setConnectTimeout(timeout)
102 .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
103 postReq.setConfig(params);
106 LocalDateTime start = LocalDateTime.now();
108 HttpResponse response = client.execute(postReq, ihcConnectionPool.getHttpContext());
109 String resp = EntityUtils.toString(response.getEntity());
110 if (logger.isTraceEnabled()) {
111 logger.trace("Received response (connectionPool={}, clientId={} requestId={}, in {}, headers={}): {}",
112 ihcConnectionPool.hashCode(), client.hashCode(), requestId,
113 Duration.between(start, LocalDateTime.now()), response.getAllHeaders(), resp);
116 } catch (Exception e) {
117 if (logger.isTraceEnabled()) {
118 logger.trace("Exception occured (connectionPool={}, clientId={} requestId={}, in {}): {}",
119 ihcConnectionPool.hashCode(), client.hashCode(), requestId,
120 Duration.between(start, LocalDateTime.now()), e.getMessage());
127 * Set request properties.
130 private void setRequestProperty(Map<String, String> requestProperties) {
131 if (postReq != null && requestProperties != null) {
132 requestProperties.forEach((k, v) -> postReq.setHeader(k, v));