]> git.basschouten.com Git - openhab-addons.git/blob
8929af01255db6e6e527b3f6f017ea47a84bd733
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.netatmo.internal.webhook;
14
15 import java.io.IOException;
16 import java.util.Scanner;
17
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServlet;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler;
26 import org.osgi.service.http.HttpService;
27 import org.osgi.service.http.NamespaceException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.Gson;
32
33 /**
34  * Main OSGi service and HTTP servlet for Netatmo Welcome Webhook.
35  *
36  * @author GaĆ«l L'hopital - Initial contribution
37  */
38 @NonNullByDefault
39 public class WelcomeWebHookServlet extends HttpServlet {
40     private static final long serialVersionUID = 1288539782077957954L;
41     private static final String PATH = "/netatmo/%s/camera";
42     private static final String APPLICATION_JSON = "application/json";
43     private static final String CHARSET = "utf-8";
44
45     private final Gson gson = new Gson();
46
47     private final Logger logger = LoggerFactory.getLogger(WelcomeWebHookServlet.class);
48
49     private HttpService httpService;
50     private @Nullable NetatmoBridgeHandler bridgeHandler;
51     private String path;
52
53     public WelcomeWebHookServlet(HttpService httpService, String id) {
54         this.httpService = httpService;
55         this.path = String.format(PATH, id);
56     }
57
58     /**
59      * OSGi activation callback.
60      *
61      * @param config Service config.
62      */
63     public void activate(NetatmoBridgeHandler bridgeHandler) {
64         this.bridgeHandler = bridgeHandler;
65         try {
66             httpService.registerServlet(path, this, null, httpService.createDefaultHttpContext());
67             logger.debug("Started Netatmo Webhook servlet at {}", path);
68         } catch (ServletException | NamespaceException e) {
69             logger.error("Could not start Netatmo Webhook servlet: {}", e.getMessage(), e);
70         }
71     }
72
73     /**
74      * OSGi deactivation callback.
75      */
76     public void deactivate() {
77         httpService.unregister(path);
78         logger.debug("Netatmo webhook servlet stopped");
79         this.bridgeHandler = null;
80     }
81
82     @Override
83     protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
84             throws ServletException, IOException {
85         if (req == null || resp == null) {
86             return;
87         }
88
89         String data = inputStreamToString(req);
90         NetatmoBridgeHandler handler = bridgeHandler;
91         if (!data.isEmpty() && handler != null) {
92             NAWebhookCameraEvent event = gson.fromJson(data, NAWebhookCameraEvent.class);
93             logger.debug("Event transmitted from restService");
94             handler.webHookEvent(event);
95         }
96
97         setHeaders(resp);
98         resp.getWriter().write("");
99     }
100
101     private String inputStreamToString(HttpServletRequest req) throws IOException {
102         String value = "";
103         try (Scanner scanner = new Scanner(req.getInputStream())) {
104             scanner.useDelimiter("\\A");
105             value = scanner.hasNext() ? scanner.next() : "";
106         }
107         return value;
108     }
109
110     private void setHeaders(HttpServletResponse response) {
111         response.setCharacterEncoding(CHARSET);
112         response.setContentType(APPLICATION_JSON);
113         response.setHeader("Access-Control-Allow-Origin", "*");
114         response.setHeader("Access-Control-Allow-Methods", "POST");
115         response.setHeader("Access-Control-Max-Age", "3600");
116         response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
117     }
118
119     public String getPath() {
120         return path;
121     }
122 }