]> git.basschouten.com Git - openhab-addons.git/blob
93d95fc88e4b1d36b833c550b01047e821e022d3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Map;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServlet;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.openhab.binding.groheondus.internal.handler.GroheOndusAccountHandler;
29 import org.osgi.service.http.HttpService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * @author Florian Schmidt - Initial contribution
35  */
36 @NonNullByDefault
37 public class AccountServlet extends HttpServlet {
38     private static final long serialVersionUID = -6321196284331950479L;
39     private final Logger logger = LoggerFactory.getLogger(AccountServlet.class);
40
41     private HttpService httpService;
42     private String bridgeId;
43     private GroheOndusAccountHandler accountHandler;
44
45     public AccountServlet(HttpService httpService, String bridgeId, GroheOndusAccountHandler accountHandler) {
46         this.httpService = httpService;
47         this.bridgeId = bridgeId;
48         this.accountHandler = accountHandler;
49
50         try {
51             httpService.registerServlet(servletUrl(), this, null, httpService.createDefaultHttpContext());
52         } catch (Exception e) {
53             logger.warn("Register servlet fails", e);
54         }
55     }
56
57     private String servletUrl() {
58         return "/groheondus/" + URLEncoder.encode(bridgeId, StandardCharsets.UTF_8);
59     }
60
61     @Override
62     protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
63             throws ServletException, IOException {
64         if (req == null || resp == null) {
65             return;
66         }
67         resp.addHeader("content-type", "text/html;charset=UTF-8");
68         StringBuilder htmlString = new StringBuilder();
69         htmlString.append("<html>");
70         htmlString.append("<head>");
71         htmlString.append("<title>Set refresh token</title>");
72         htmlString.append("</head>");
73         htmlString.append("<body>");
74         htmlString.append("<header>");
75         htmlString.append("<h1>Set refresh token for accout: ");
76         htmlString.append(bridgeId);
77         htmlString.append("</h1>");
78         htmlString.append("</header>");
79         htmlString.append("<div>Has refresh token: ");
80         if (this.accountHandler.hasRefreshToken()) {
81             htmlString.append("yes");
82             htmlString.append(
83                     "<input type=\"submit\" value=\"Delete\" onclick=\"fetch(window.location.href, {method: 'DELETE'}).then(window.location.reload())\">");
84         } else {
85             htmlString.append("no");
86         }
87         htmlString.append("</div>");
88         htmlString.append("<form method=\"post\">");
89         htmlString.append("<label for=\"refreshToken\">Refresh Token: </label>");
90         htmlString.append("<input type=\"text\" id=\"refreshToken\" autocomplete=\"off\" name=\"refreshToken\">");
91         htmlString.append("<input type=\"submit\" value=\"Save\">");
92         htmlString.append("</form>");
93         htmlString.append("</body>");
94         htmlString.append("</html>");
95
96         resp.getWriter().write(htmlString.toString());
97     }
98
99     @Override
100     protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
101             throws ServletException, IOException {
102         if (req == null) {
103             return;
104         }
105         if (resp == null) {
106             return;
107         }
108
109         Map<String, String[]> map = req.getParameterMap();
110         this.accountHandler.setRefreshToken(map.get("refreshToken")[0]);
111
112         resp.addHeader("Location", "/groheondus");
113         resp.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
114     }
115
116     @Override
117     protected void doDelete(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
118             throws ServletException, IOException {
119         if (req == null) {
120             return;
121         }
122         if (resp == null) {
123             return;
124         }
125
126         this.accountHandler.deleteRefreshToken();
127
128         resp.setStatus(HttpStatus.OK_200);
129     }
130
131     public void dispose() {
132         httpService.unregister(servletUrl());
133     }
134 }