]> git.basschouten.com Git - openhab-addons.git/blob
603686794a98102986727c7537ce1a6d30fba914
[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.wemo.internal.http;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Properties;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.wemo.internal.WemoBindingConstants;
23 import org.openhab.core.io.net.http.HttpUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link WemoHttpCall} is responsible for calling a WeMo device to send commands or retrieve status updates.
29  *
30  * @author Hans-Jörg Merk - Initial contribution
31  */
32 @NonNullByDefault
33 public class WemoHttpCall {
34
35     private final Logger logger = LoggerFactory.getLogger(WemoHttpCall.class);
36
37     public String executeCall(String wemoURL, String soapHeader, String content) throws IOException {
38         Properties wemoHeaders = new Properties();
39         wemoHeaders.setProperty("CONTENT-TYPE", WemoBindingConstants.HTTP_CALL_CONTENT_HEADER);
40         wemoHeaders.put("SOAPACTION", soapHeader);
41
42         InputStream wemoContent = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
43
44         logger.trace("Performing HTTP call for URL: '{}', header: '{}', request body: '{}'", wemoURL, soapHeader,
45                 content);
46         String responseBody = HttpUtil.executeUrl("POST", wemoURL, wemoHeaders, wemoContent, null, 2000);
47         logger.trace("HTTP response body: '{}'", responseBody);
48
49         return responseBody;
50     }
51
52     public boolean probeURL(String url) {
53         try {
54             HttpUtil.executeUrl("GET", url, 250);
55             return true;
56         } catch (IOException e) {
57             return false;
58         }
59     }
60 }