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.io.UnsupportedEncodingException;
17 import java.net.URLEncoder;
18 import java.nio.charset.StandardCharsets;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
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;
35 * @author Florian Schmidt - Initial contribution
38 public class AccountServlet extends HttpServlet {
39 private static final long serialVersionUID = -6321196284331950479L;
40 private final Logger logger = LoggerFactory.getLogger(AccountServlet.class);
42 private HttpService httpService;
43 private String bridgeId;
44 private GroheOndusAccountHandler accountHandler;
46 public AccountServlet(HttpService httpService, String bridgeId, GroheOndusAccountHandler accountHandler) {
47 this.httpService = httpService;
48 this.bridgeId = bridgeId;
49 this.accountHandler = accountHandler;
52 httpService.registerServlet(servletUrl(), this, null, httpService.createDefaultHttpContext());
53 } catch (Exception e) {
54 logger.warn("Register servlet fails", e);
58 private String servletUrl() throws UnsupportedEncodingException {
59 return "/groheondus/" + URLEncoder.encode(bridgeId, StandardCharsets.UTF_8.name());
63 protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
64 throws ServletException, IOException {
65 if (req == null || resp == null) {
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");
84 "<input type=\"submit\" value=\"Delete\" onclick=\"fetch(window.location.href, {method: 'DELETE'}).then(window.location.reload())\">");
86 htmlString.append("no");
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>");
97 resp.getWriter().write(htmlString.toString());
101 protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
102 throws ServletException, IOException {
110 Map<String, String[]> map = req.getParameterMap();
111 this.accountHandler.setRefreshToken(map.get("refreshToken")[0]);
113 resp.addHeader("Location", "/groheondus");
114 resp.setStatus(HttpStatus.MOVED_TEMPORARILY_302);
118 protected void doDelete(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
119 throws ServletException, IOException {
127 this.accountHandler.deleteRefreshToken();
129 resp.setStatus(HttpStatus.OK_200);
132 public void dispose() {
134 httpService.unregister(servletUrl());
135 } catch (UnsupportedEncodingException e) {
136 logger.warn("Unregistration of servlet failed", e);