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.dwdunwetter.internal.dto;
15 import java.io.IOException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
19 import org.openhab.core.io.net.http.HttpUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Provides the access to the API Endpoint
26 * @author Martin Koehler - Initial contribution
28 public class DwdWarningDataAccess {
30 private final Logger logger = LoggerFactory.getLogger(DwdWarningDataAccess.class);
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";
36 * Returns the raw Data from the Endpoint.
37 * In case of errors or empty cellId value, returns an empty String.
39 * @param cellId The warnCell-Id for which the warnings should be returned
40 * @return The raw data received or an empty string.
42 public String getDataFromEndpoint(String cellId) {
44 if (cellId == null || cellId.isBlank()) {
45 logger.warn("No cellId provided");
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);
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");
63 } catch (IOException e) {
64 logger.warn("Communication error occurred while getting data: {}", e.getMessage());
65 logger.debug("Communication error trace", e);
71 public String getByURL(String url) throws IOException {
72 return HttpUtil.executeUrl("GET", url, 5000);