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.amazonechocontrol.internal;
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.BINDING_NAME;
17 import java.io.IOException;
18 import java.net.URLEncoder;
19 import java.util.ArrayList;
20 import java.util.List;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.core.thing.Thing;
30 import org.osgi.service.http.HttpService;
31 import org.osgi.service.http.NamespaceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.unbescape.html.HtmlEscape;
37 * This servlet provides the base navigation page, with hyperlinks for the defined account things
39 * @author Michael Geramb - Initial Contribution
42 public class BindingServlet extends HttpServlet {
43 private static final long serialVersionUID = -1453738923337413163L;
45 private final Logger logger = LoggerFactory.getLogger(BindingServlet.class);
47 String servletUrlWithoutRoot;
49 HttpService httpService;
51 List<Thing> accountHandlers = new ArrayList<>();
53 public BindingServlet(HttpService httpService) {
54 this.httpService = httpService;
55 servletUrlWithoutRoot = "amazonechocontrol";
56 servletUrl = "/" + servletUrlWithoutRoot;
58 httpService.registerServlet(servletUrl, this, null, httpService.createDefaultHttpContext());
59 } catch (NamespaceException | ServletException e) {
60 logger.warn("Register servlet fails", e);
64 public void addAccountThing(Thing accountThing) {
65 synchronized (accountHandlers) {
66 accountHandlers.add(accountThing);
70 public void removeAccountThing(Thing accountThing) {
71 synchronized (accountHandlers) {
72 accountHandlers.remove(accountThing);
76 public void dispose() {
77 httpService.unregister(servletUrl);
81 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
82 throws ServletException, IOException {
89 String requestUri = req.getRequestURI();
90 if (requestUri == null) {
93 String uri = requestUri.substring(servletUrl.length());
94 String queryString = req.getQueryString();
95 if (queryString != null && queryString.length() > 0) {
96 uri += "?" + queryString;
98 logger.debug("doGet {}", uri);
100 if (!"/".equals(uri)) {
101 String newUri = req.getServletPath() + "/";
102 resp.sendRedirect(newUri);
106 StringBuilder html = new StringBuilder();
107 html.append("<html><head><title>" + HtmlEscape.escapeHtml4(BINDING_NAME) + "</title><head><body>");
108 html.append("<h1>" + HtmlEscape.escapeHtml4(BINDING_NAME) + "</h1>");
110 synchronized (accountHandlers) {
111 if (accountHandlers.isEmpty()) {
112 html.append("No Account thing created.");
114 for (Thing accountHandler : accountHandlers) {
115 String url = URLEncoder.encode(accountHandler.getUID().getId(), "UTF8");
116 html.append("<a href='./" + url + " '>" + HtmlEscape.escapeHtml4(accountHandler.getLabel())
121 html.append("</body></html>");
123 resp.addHeader("content-type", "text/html;charset=UTF-8");
125 resp.getWriter().write(html.toString());
126 } catch (IOException e) {
127 logger.warn("return html failed with uri syntax error", e);