]> git.basschouten.com Git - openhab-addons.git/blob
749428d281c5e75a3968a0357956656321628b41
[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.dwdunwetter.internal.dto;
14
15 import java.io.IOException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
18
19 import org.openhab.core.io.net.http.HttpUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Provides the access to the API Endpoint
25  *
26  * @author Martin Koehler - Initial contribution
27  */
28 public class DwdWarningDataAccess {
29
30     private final Logger logger = LoggerFactory.getLogger(DwdWarningDataAccess.class);
31
32     // URL of the Service
33     private static final String DWD_URL = "https://maps.dwd.de/geoserver/dwd/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=dwd:Warnungen_Gemeinden";
34
35     /**
36      * Returns the raw Data from the Endpoint.
37      * In case of errors or empty cellId value, returns an empty String.
38      *
39      * @param cellId The warnCell-Id for which the warnings should be returned
40      * @return The raw data received or an empty string.
41      */
42     public String getDataFromEndpoint(String cellId) {
43         try {
44             if (cellId == null || cellId.isBlank()) {
45                 logger.warn("No cellId provided");
46                 return "";
47             }
48
49             StringBuilder stringBuilder = new StringBuilder();
50             stringBuilder.append(DWD_URL);
51             stringBuilder.append("&CQL_FILTER=");
52             stringBuilder.append(URLEncoder.encode("WARNCELLID LIKE '" + cellId + "'", StandardCharsets.UTF_8));
53             logger.debug("Refreshing Data for cell {}", cellId);
54             String rawData = getByURL(stringBuilder.toString());
55             logger.trace("Raw request: {}", stringBuilder);
56             logger.trace("Raw response: {}", rawData);
57
58             if (rawData == null || !rawData.startsWith("<?xml") || !rawData.contains("FeatureCollection")) {
59                 logger.warn("Communication error occurred while getting data, response is not in expected XML-format");
60                 return "";
61             }
62             return rawData;
63         } catch (IOException e) {
64             logger.warn("Communication error occurred while getting data: {}", e.getMessage());
65             logger.debug("Communication error trace", e);
66         }
67
68         return "";
69     }
70
71     public String getByURL(String url) throws IOException {
72         return HttpUtil.executeUrl("GET", url, 5000);
73     }
74 }