]> git.basschouten.com Git - openhab-addons.git/blob
5d16632c530406df8173d0cd3b3a2bec895ceffd
[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.wundergroundupdatereceiver.internal;
14
15 import static java.util.stream.Collectors.toMap;
16 import static java.util.stream.Collectors.toSet;
17 import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.*;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.time.Instant;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.regex.Pattern;
29
30 import javax.servlet.Servlet;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Deactivate;
40 import org.osgi.service.component.annotations.Reference;
41 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
42 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * The {@link WundergroundUpdateReceiverServlet} is responsible for receiving updates,and
48  * updating the matching channels.
49  *
50  * @author Daniel Demus - Initial contribution
51  */
52 @NonNullByDefault
53 @HttpWhiteboardServletName(WundergroundUpdateReceiverServlet.SERVLET_URL)
54 @HttpWhiteboardServletPattern(WundergroundUpdateReceiverServlet.SERVLET_URL)
55 @Component(immediate = true, service = { Servlet.class, WundergroundUpdateReceiverServlet.class })
56 public class WundergroundUpdateReceiverServlet extends HttpServlet
57         implements WundergroundUpdateReceiverServletControls {
58
59     public static final String SERVLET_URL = "/weatherstation/updateweatherstation.php";
60     private static final long serialVersionUID = -5296703727081438023L;
61     private static final Pattern CLEANER = Pattern.compile("[^\\w-]");
62
63     private final Logger logger = LoggerFactory.getLogger(WundergroundUpdateReceiverServlet.class);
64     private final Map<String, WundergroundUpdateReceiverHandler> handlers = new HashMap<>();
65
66     private static final Object LOCK = new Object();
67     private final WundergroundUpdateReceiverDiscoveryService discoveryService;
68
69     private boolean active = false;
70     private String errorDetail = "";
71
72     @Activate
73     public WundergroundUpdateReceiverServlet(
74             final @Reference WundergroundUpdateReceiverDiscoveryService discoveryService) {
75         this.discoveryService = discoveryService;
76         errorDetail = "";
77         active = discoveryService.isBackgroundDiscoveryEnabled();
78     }
79
80     public boolean isActive() {
81         synchronized (LOCK) {
82             return this.active;
83         }
84     }
85
86     public String getErrorDetail() {
87         synchronized (LOCK) {
88             return this.errorDetail;
89         }
90     }
91
92     public Set<String> getStationIds() {
93         return this.handlers.keySet();
94     }
95
96     public void addHandler(WundergroundUpdateReceiverHandler handler) {
97         synchronized (this.handlers) {
98             if (this.handlers.containsKey(handler.getStationId())) {
99                 errorDetail = "Handler handling request for stationId " + handler.getStationId() + " is already added";
100                 logger.warn("Error during handler registration - StationId {} already being handled",
101                         handler.getStationId());
102                 return;
103             }
104             this.handlers.put(handler.getStationId(), handler);
105             errorDetail = "";
106             if (!isActive()) {
107                 enable();
108             }
109         }
110     }
111
112     public void removeHandler(String stationId) {
113         synchronized (this.handlers) {
114             WundergroundUpdateReceiverHandler handler = this.handlers.get(stationId);
115             if (handler != null) {
116                 this.handlers.remove(stationId);
117             }
118             if (this.handlers.isEmpty() && !this.discoveryService.isBackgroundDiscoveryEnabled()) {
119                 disable();
120             }
121         }
122     }
123
124     @Override
125     public void enable() {
126         active = true;
127     }
128
129     @Deactivate
130     public void disable() {
131         errorDetail = "";
132         active = false;
133     }
134
135     public void handlerConfigUpdated(WundergroundUpdateReceiverHandler handler) {
136         synchronized (this.handlers) {
137             final Set<Map.Entry<String, WundergroundUpdateReceiverHandler>> changedStationIds = this.handlers.entrySet()
138                     .stream().filter(entry -> handler.getThing().getUID().equals(entry.getValue().getThing().getUID()))
139                     .collect(toSet());
140             changedStationIds.forEach(entry -> {
141                 logger.debug("Re-assigning listener from station id {} to station id {}", entry.getKey(),
142                         handler.getStationId());
143                 this.removeHandler(entry.getKey());
144                 this.addHandler(handler);
145             });
146         }
147     }
148
149     public void dispose() {
150         synchronized (this.handlers) {
151             Set<String> stationIds = new HashSet<>(getStationIds());
152             stationIds.forEach(this::removeHandler);
153             disable();
154         }
155     }
156
157     protected Map<String, String> normalizeParameterMap(Map<String, String[]> parameterMap) {
158         return parameterMap.entrySet().stream()
159                 .collect(toMap(e -> makeUidSafeString(e.getKey()), e -> String.join("", e.getValue())));
160     }
161
162     @Override
163     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) throws IOException {
164         if (!active) {
165             return;
166         }
167         if (req == null) {
168             return;
169         }
170         if (resp == null) {
171             return;
172         }
173         if (req.getRequestURI() == null) {
174             return;
175         }
176         logger.trace("doGet {}", req.getQueryString());
177
178         String stationId = req.getParameter(STATION_ID_PARAMETER);
179         Map<String, String> states = normalizeParameterMap(req.getParameterMap());
180         Optional.ofNullable(this.handlers.get(stationId)).ifPresentOrElse(handler -> {
181             String queryString = req.getQueryString();
182             if (queryString != null && queryString.length() > 0) {
183                 states.put(LAST_QUERY, queryString);
184             }
185             handler.updateChannelStates(states);
186         }, () -> {
187             this.discoveryService.addUnhandledStationId(stationId, states);
188         });
189
190         resp.setStatus(HttpServletResponse.SC_OK);
191         resp.setContentType("text/html;charset=utf-8");
192         resp.setContentLength(7);
193         resp.setDateHeader("Date", Instant.now().toEpochMilli());
194         resp.setHeader("Connection", "close");
195         PrintWriter writer = resp.getWriter();
196         writer.write("success");
197         writer.flush();
198         writer.close();
199     }
200
201     protected Map<String, WundergroundUpdateReceiverHandler> getHandlers() {
202         return Collections.unmodifiableMap(this.handlers);
203     }
204
205     private String makeUidSafeString(String key) {
206         return CLEANER.matcher(key).replaceAll("-");
207     }
208 }