]> git.basschouten.com Git - openhab-addons.git/blob
6ec223e70799cefd251547087c07dacc6a00a0d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.amazonechocontrol.internal;
14
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.BINDING_NAME;
16
17 import java.io.IOException;
18 import java.net.URLEncoder;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
35
36 /**
37  * This servlet provides the base navigation page, with hyperlinks for the defined account things
38  *
39  * @author Michael Geramb - Initial Contribution
40  */
41 @NonNullByDefault
42 public class BindingServlet extends HttpServlet {
43     private static final long serialVersionUID = -1453738923337413163L;
44
45     private final Logger logger = LoggerFactory.getLogger(BindingServlet.class);
46
47     String servletUrlWithoutRoot;
48     String servletUrl;
49     HttpService httpService;
50
51     List<Thing> accountHandlers = new ArrayList<>();
52
53     public BindingServlet(HttpService httpService) {
54         this.httpService = httpService;
55         servletUrlWithoutRoot = "amazonechocontrol";
56         servletUrl = "/" + servletUrlWithoutRoot;
57         try {
58             httpService.registerServlet(servletUrl, this, null, httpService.createDefaultHttpContext());
59         } catch (NamespaceException | ServletException e) {
60             logger.warn("Register servlet fails", e);
61         }
62     }
63
64     public void addAccountThing(Thing accountThing) {
65         synchronized (accountHandlers) {
66             accountHandlers.add(accountThing);
67         }
68     }
69
70     public void removeAccountThing(Thing accountThing) {
71         synchronized (accountHandlers) {
72             accountHandlers.remove(accountThing);
73         }
74     }
75
76     public void dispose() {
77         httpService.unregister(servletUrl);
78     }
79
80     @Override
81     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
82             throws ServletException, IOException {
83         if (req == null) {
84             return;
85         }
86         if (resp == null) {
87             return;
88         }
89         String requestUri = req.getRequestURI();
90         if (requestUri == null) {
91             return;
92         }
93         String uri = requestUri.substring(servletUrl.length());
94         String queryString = req.getQueryString();
95         if (queryString != null && queryString.length() > 0) {
96             uri += "?" + queryString;
97         }
98         logger.debug("doGet {}", uri);
99
100         if (!"/".equals(uri)) {
101             String newUri = req.getServletPath() + "/";
102             resp.sendRedirect(newUri);
103             return;
104         }
105
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>");
109
110         synchronized (accountHandlers) {
111             if (accountHandlers.isEmpty()) {
112                 html.append("No Account thing created.");
113             } else {
114                 for (Thing accountHandler : accountHandlers) {
115                     String url = URLEncoder.encode(accountHandler.getUID().getId(), "UTF8");
116                     html.append("<a href='./" + url + " '>" + HtmlEscape.escapeHtml4(accountHandler.getLabel())
117                             + "</a><br>");
118                 }
119             }
120         }
121         html.append("</body></html>");
122
123         resp.addHeader("content-type", "text/html;charset=UTF-8");
124         try {
125             resp.getWriter().write(html.toString());
126         } catch (IOException e) {
127             logger.warn("return html failed with uri syntax error", e);
128         }
129     }
130 }