2 * Copyright (c) 2010-2020 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.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;
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 {
44 private static final long serialVersionUID = -1453738923337413163L;
46 private final Logger logger = LoggerFactory.getLogger(BindingServlet.class);
48 String servletUrlWithoutRoot;
50 HttpService httpService;
52 List<Thing> accountHandlers = new ArrayList<>();
54 public BindingServlet(HttpService httpService) {
55 this.httpService = httpService;
56 servletUrlWithoutRoot = "amazonechocontrol";
57 servletUrl = "/" + servletUrlWithoutRoot;
59 httpService.registerServlet(servletUrl, this, null, httpService.createDefaultHttpContext());
60 } catch (NamespaceException | ServletException e) {
61 logger.warn("Register servlet fails", e);
65 public void addAccountThing(Thing accountThing) {
66 synchronized (accountHandlers) {
67 accountHandlers.add(accountThing);
71 public void removeAccountThing(Thing accountThing) {
72 synchronized (accountHandlers) {
73 accountHandlers.remove(accountThing);
77 public void dispose() {
78 httpService.unregister(servletUrl);
82 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
83 throws ServletException, IOException {
90 String uri = req.getRequestURI().substring(servletUrl.length());
91 String queryString = req.getQueryString();
92 if (queryString != null && queryString.length() > 0) {
93 uri += "?" + queryString;
95 logger.debug("doGet {}", uri);
97 if (!uri.equals("/")) {
98 String newUri = req.getServletPath() + "/";
99 resp.sendRedirect(newUri);
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>");
107 synchronized (accountHandlers) {
108 if (accountHandlers.isEmpty()) {
109 html.append("No Account thing created.");
111 for (Thing accountHandler : accountHandlers) {
112 String url = URLEncoder.encode(accountHandler.getUID().getId(), "UTF8");
113 html.append("<a href='./" + url + " '>" + StringEscapeUtils.escapeHtml(accountHandler.getLabel())
118 html.append("</body></html>");
120 resp.addHeader("content-type", "text/html;charset=UTF-8");
122 resp.getWriter().write(html.toString());
123 } catch (IOException e) {
124 logger.warn("return html failed with uri syntax error", e);