2 * Copyright (c) 2010-2022 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.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServlet;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
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;
34 * @author Florian Schmidt - Initial contribution
37 public class AccountServlet extends HttpServlet {
38 private static final long serialVersionUID = -6321196284331950479L;
39 private final Logger logger = LoggerFactory.getLogger(AccountServlet.class);
41 private HttpService httpService;
42 private String bridgeId;
43 private GroheOndusAccountHandler accountHandler;
45 public AccountServlet(HttpService httpService, String bridgeId, GroheOndusAccountHandler accountHandler) {
46 this.httpService = httpService;
47 this.bridgeId = bridgeId;
48 this.accountHandler = accountHandler;
51 httpService.registerServlet(servletUrl(), this, null, httpService.createDefaultHttpContext());
52 } catch (Exception e) {
53 logger.warn("Register servlet fails", e);
57 private String servletUrl() {
58 return "/groheondus/" + URLEncoder.encode(bridgeId, StandardCharsets.UTF_8);
62 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
63 throws ServletException, IOException {
64 if (req == null || resp == null) {
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");
83 "<input type=\"submit\" value=\"Delete\" onclick=\"fetch(window.location.href, {method: 'DELETE'}).then(window.location.reload())\">");
85 htmlString.append("no");
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>");
96 resp.getWriter().write(htmlString.toString());
100 protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
101 throws ServletException, IOException {
109 Map<String, String[]> map = req.getParameterMap();
110 this.accountHandler.setRefreshToken(map.get("refreshToken")[0]);
112 resp.addHeader("Location", "/groheondus");
113 resp.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
117 protected void doDelete(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
118 throws ServletException, IOException {
126 this.accountHandler.deleteRefreshToken();
128 resp.setStatus(HttpStatus.OK_200);
131 public void dispose() {
132 httpService.unregister(servletUrl());