2 * Copyright (c) 2010-2021 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.groheondus.internal;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.openhab.core.thing.Thing;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.osgi.service.component.annotations.ServiceScope;
32 import org.osgi.service.http.HttpService;
33 import org.osgi.service.http.NamespaceException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * @author Florian Schmidt - Initial contribution
41 @Component(service = AccountsServlet.class, scope = ServiceScope.SINGLETON)
42 public class AccountsServlet extends HttpServlet {
43 private static final long serialVersionUID = -9183159739446995608L;
45 private static final String SERVLET_URL = "/groheondus";
47 private final Logger logger = LoggerFactory.getLogger(AccountsServlet.class);
48 private final List<Thing> accounts = new ArrayList<>();
49 private HttpService httpService;
52 public AccountsServlet(@Reference HttpService httpService) {
53 this.httpService = httpService;
56 httpService.registerServlet(SERVLET_URL, this, null, httpService.createDefaultHttpContext());
57 } catch (ServletException | NamespaceException e) {
58 logger.warn("Register servlet fails", e);
62 public void addAccount(Thing accountThing) {
63 accounts.add(accountThing);
66 public void removeAccount(Thing accountThing) {
67 accounts.remove(accountThing);
70 public void deactivate() {
71 httpService.unregister(SERVLET_URL);
75 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
76 throws ServletException, IOException {
77 if (req == null || resp == null) {
81 StringBuilder htmlString = new StringBuilder();
82 htmlString.append("<html>");
83 htmlString.append("<head>");
84 htmlString.append("<title>GROHE Ondus Account login</title>");
85 htmlString.append("</head>");
86 htmlString.append("<body>");
87 if (accounts.isEmpty()) {
89 "Please first create an GROHE ONDUS account thing in openHAB in order to log into this account.");
92 "You've the following GROHE ONDUS account things, click on the one you want to manage:<br />");
93 htmlString.append("<ul>");
94 accounts.forEach(account -> {
95 String accountId = account.getUID().getId();
96 htmlString.append("<li>");
97 htmlString.append("<a href=\"");
98 htmlString.append(SERVLET_URL);
99 htmlString.append("/");
100 htmlString.append(accountId);
101 htmlString.append("\">");
102 htmlString.append(accountId);
103 htmlString.append("</a>");
104 htmlString.append("</li>");
106 htmlString.append("</ul>");
108 htmlString.append("</body>");
109 htmlString.append("</html>");
111 resp.setStatus(HttpStatus.OK_200);
112 resp.getWriter().write(htmlString.toString());