]> git.basschouten.com Git - openhab-addons.git/blob
fa1e4c5d239a1e2461b6c758a734a2a6af2676a1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.wemo.internal.WemoBindingConstants;
24 import org.openhab.core.io.net.http.HttpUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link WemoHttpCall} is responsible for calling a WeMo device to send commands or retrieve status updates.
30  *
31  * @author Hans-Jörg Merk - Initial contribution
32  */
33 @NonNullByDefault
34 public class WemoHttpCall {
35
36     private final Logger logger = LoggerFactory.getLogger(WemoHttpCall.class);
37
38     public @Nullable String executeCall(String wemoURL, String soapHeader, String content) {
39         try {
40             Properties wemoHeaders = new Properties();
41             wemoHeaders.setProperty("CONTENT-TYPE", WemoBindingConstants.HTTP_CALL_CONTENT_HEADER);
42             wemoHeaders.put("SOAPACTION", soapHeader);
43
44             InputStream wemoContent = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
45
46             String wemoCallResponse = HttpUtil.executeUrl("POST", wemoURL, wemoHeaders, wemoContent, null, 2000);
47             return wemoCallResponse;
48         } catch (IOException e) {
49             // throw new IllegalStateException("Could not call WeMo", e);
50             logger.debug("Could not make HTTP call to WeMo");
51             return null;
52         }
53     }
54 }