]> git.basschouten.com Git - openhab-addons.git/blob
36a065eb4dd214ca9fc5d21950b5b8cd52c349d9
[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.groheondus.internal;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
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;
36
37 /**
38  * @author Florian Schmidt - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = AccountsServlet.class, scope = ServiceScope.SINGLETON)
42 public class AccountsServlet extends HttpServlet {
43     private static final long serialVersionUID = -9183159739446995608L;
44
45     private static final String SERVLET_URL = "/groheondus";
46
47     private final Logger logger = LoggerFactory.getLogger(AccountsServlet.class);
48     private final List<Thing> accounts = new ArrayList<>();
49     private HttpService httpService;
50
51     @Activate
52     public AccountsServlet(@Reference HttpService httpService) {
53         this.httpService = httpService;
54
55         try {
56             httpService.registerServlet(SERVLET_URL, this, null, httpService.createDefaultHttpContext());
57         } catch (ServletException | NamespaceException e) {
58             logger.warn("Register servlet fails", e);
59         }
60     }
61
62     public void addAccount(Thing accountThing) {
63         accounts.add(accountThing);
64     }
65
66     public void removeAccount(Thing accountThing) {
67         accounts.remove(accountThing);
68     }
69
70     public void deactivate() {
71         httpService.unregister(SERVLET_URL);
72     }
73
74     @Override
75     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
76             throws ServletException, IOException {
77         if (req == null || resp == null) {
78             return;
79         }
80
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()) {
88             htmlString.append(
89                     "Please first create an GROHE ONDUS account thing in openHAB in order to log into this account.");
90         } else {
91             htmlString.append(
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>");
105             });
106             htmlString.append("</ul>");
107         }
108         htmlString.append("</body>");
109         htmlString.append("</html>");
110
111         resp.setStatus(HttpStatus.OK_200);
112         resp.getWriter().write(htmlString.toString());
113     }
114 }