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