]> git.basschouten.com Git - openhab-addons.git/blob
c34595059aa52a3a8b7bb7712de40099120eac50
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.apache.commons.lang.StringEscapeUtils;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.core.thing.Thing;
31 import org.osgi.service.http.HttpService;
32 import org.osgi.service.http.NamespaceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
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
44     private static final long serialVersionUID = -1453738923337413163L;
45
46     private final Logger logger = LoggerFactory.getLogger(BindingServlet.class);
47
48     String servletUrlWithoutRoot;
49     String servletUrl;
50     HttpService httpService;
51
52     List<Thing> accountHandlers = new ArrayList<>();
53
54     public BindingServlet(HttpService httpService) {
55         this.httpService = httpService;
56         servletUrlWithoutRoot = "amazonechocontrol";
57         servletUrl = "/" + servletUrlWithoutRoot;
58         try {
59             httpService.registerServlet(servletUrl, this, null, httpService.createDefaultHttpContext());
60         } catch (NamespaceException | ServletException e) {
61             logger.warn("Register servlet fails", e);
62         }
63     }
64
65     public void addAccountThing(Thing accountThing) {
66         synchronized (accountHandlers) {
67             accountHandlers.add(accountThing);
68         }
69     }
70
71     public void removeAccountThing(Thing accountThing) {
72         synchronized (accountHandlers) {
73             accountHandlers.remove(accountThing);
74         }
75     }
76
77     public void dispose() {
78         httpService.unregister(servletUrl);
79     }
80
81     @Override
82     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
83             throws ServletException, IOException {
84         if (req == null) {
85             return;
86         }
87         if (resp == null) {
88             return;
89         }
90         String uri = req.getRequestURI().substring(servletUrl.length());
91         String queryString = req.getQueryString();
92         if (queryString != null && queryString.length() > 0) {
93             uri += "?" + queryString;
94         }
95         logger.debug("doGet {}", uri);
96
97         if (!uri.equals("/")) {
98             String newUri = req.getServletPath() + "/";
99             resp.sendRedirect(newUri);
100             return;
101         }
102
103         StringBuilder html = new StringBuilder();
104         html.append("<html><head><title>" + StringEscapeUtils.escapeHtml(BINDING_NAME) + "</title><head><body>");
105         html.append("<h1>" + StringEscapeUtils.escapeHtml(BINDING_NAME) + "</h1>");
106
107         synchronized (accountHandlers) {
108             if (accountHandlers.isEmpty()) {
109                 html.append("No Account thing created.");
110             } else {
111                 for (Thing accountHandler : accountHandlers) {
112                     String url = URLEncoder.encode(accountHandler.getUID().getId(), "UTF8");
113                     html.append("<a href='./" + url + " '>" + StringEscapeUtils.escapeHtml(accountHandler.getLabel())
114                             + "</a><br>");
115                 }
116             }
117         }
118         html.append("</body></html>");
119
120         resp.addHeader("content-type", "text/html;charset=UTF-8");
121         try {
122             resp.getWriter().write(html.toString());
123         } catch (IOException e) {
124             logger.warn("return html failed with uri syntax error", e);
125         }
126     }
127 }