]> git.basschouten.com Git - openhab-addons.git/blob
81760aca63f6a02c714d2ceb9da2751be5d9c694
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.servlet;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.BINDING_ID;
16
17 import javax.servlet.ServletException;
18 import javax.servlet.http.HttpServlet;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
22 import org.osgi.service.http.HttpService;
23 import org.osgi.service.http.NamespaceException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link NetatmoServlet} is the ancestor class for Netatmo servlets
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class NetatmoServlet extends HttpServlet {
34     private static final long serialVersionUID = 5671438863935117735L;
35     private static final String BASE_PATH = "/" + BINDING_ID + "/";
36
37     private final Logger logger = LoggerFactory.getLogger(this.getClass());
38     private final HttpService httpService;
39
40     protected final ApiBridgeHandler handler;
41     protected final String path;
42
43     public NetatmoServlet(ApiBridgeHandler handler, HttpService httpService, String localPath) {
44         this.path = BASE_PATH + localPath + "/" + handler.getId();
45         this.handler = handler;
46         this.httpService = httpService;
47     }
48
49     public void startListening() {
50         try {
51             httpService.registerServlet(path, this, null, httpService.createDefaultHttpContext());
52             logger.info("Registered Netatmo servlet at '{}'", path);
53         } catch (NamespaceException | ServletException e) {
54             logger.warn("Registering servlet failed:{}", e.getMessage());
55         }
56     }
57
58     public void dispose() {
59         logger.debug("Stopping Netatmo Servlet {}", path);
60         httpService.unregister(path);
61         this.destroy();
62     }
63 }