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.netatmo.internal.servlet;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.BINDING_ID;
17 import javax.servlet.ServletException;
18 import javax.servlet.http.HttpServlet;
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;
28 * The {@link NetatmoServlet} is the ancestor class for Netatmo servlets
30 * @author Gaƫl L'hopital - Initial contribution
33 public abstract class NetatmoServlet extends HttpServlet {
34 private static final long serialVersionUID = 5671438863935117735L;
35 private static final String BASE_PATH = "/" + BINDING_ID + "/";
37 private final Logger logger = LoggerFactory.getLogger(this.getClass());
38 private final HttpService httpService;
40 protected final ApiBridgeHandler handler;
41 protected final String path;
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;
49 public void startListening() {
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());
58 public void dispose() {
59 logger.debug("Stopping Netatmo Servlet {}", path);
60 httpService.unregister(path);