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