2 * Copyright (c) 2010-2023 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.mielecloud.internal.config.servlet;
15 import java.io.IOException;
16 import java.util.Optional;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
24 * Servlet showing the pair account page.
26 * @author Björn Lange - Initial Contribution
29 public final class PairAccountServlet extends AbstractShowPageServlet {
30 private static final long serialVersionUID = 6565378471951635420L;
32 public static final String CLIENT_ID_PARAMETER_NAME = "clientId";
33 public static final String CLIENT_SECRET_PARAMETER_NAME = "clientSecret";
35 public static final String MISSING_CLIENT_ID_PARAMETER_NAME = "missingClientId";
36 public static final String MISSING_CLIENT_SECRET_PARAMETER_NAME = "missingClientSecret";
37 public static final String MISSING_BRIDGE_ID_PARAMETER_NAME = "missingBridgeId";
38 public static final String MISSING_EMAIL_PARAMETER_NAME = "missingEmail";
39 public static final String MALFORMED_BRIDGE_ID_PARAMETER_NAME = "malformedBridgeId";
40 public static final String FAILED_TO_DERIVE_REDIRECT_URL_PARAMETER_NAME = "failedToDeriveRedirectUrl";
41 public static final String ONGOING_AUTHORIZATION_IN_STEP1_EXPIRES_IN_MINUTES_PARAMETER_NAME = "ongoingAuthorizationInStep1ExpiresInMinutes";
42 public static final String ONGOING_AUTHORIZATION_UNKNOWN_EXPIRY_TIME = "unknown";
43 public static final String NO_ONGOING_AUTHORIZATION_IN_STEP2_PARAMETER_NAME = "noOngoingAuthorizationInStep2";
44 public static final String MISSING_REQUEST_URL_PARAMETER_NAME = "missingRequestUrl";
46 private static final String PAIR_ACCOUNT_SKELETON = "pairing.html";
48 private static final String CLIENT_ID_PLACEHOLDER = "<!-- CLIENT ID -->";
49 private static final String CLIENT_SECRET_PLACEHOLDER = "<!-- CLIENT SECRET -->";
50 private static final String ERROR_MESSAGE_PLACEHOLDER = "<!-- ERROR MESSAGE -->";
53 * Creates a new {@link PairAccountServlet}.
55 * @param resourceLoader Loader for resources.
57 public PairAccountServlet(ResourceLoader resourceLoader) {
58 super(resourceLoader);
62 protected String handleGetRequest(HttpServletRequest request, HttpServletResponse response)
63 throws MieleHttpException, IOException {
64 String skeleton = getResourceLoader().loadResourceAsString(PAIR_ACCOUNT_SKELETON);
65 skeleton = renderClientIdAndClientSecret(request, skeleton);
66 skeleton = renderErrorMessage(request, skeleton);
70 private String renderClientIdAndClientSecret(HttpServletRequest request, String skeleton) {
71 String prefilledClientId = Optional.ofNullable(request.getParameter(CLIENT_ID_PARAMETER_NAME)).orElse("");
72 String prefilledClientSecret = Optional.ofNullable(request.getParameter(CLIENT_SECRET_PARAMETER_NAME))
74 return skeleton.replace(CLIENT_ID_PLACEHOLDER, prefilledClientId).replace(CLIENT_SECRET_PLACEHOLDER,
75 prefilledClientSecret);
78 private String renderErrorMessage(HttpServletRequest request, String skeleton) {
79 if (ServletUtil.isParameterEnabled(request, MISSING_CLIENT_ID_PARAMETER_NAME)) {
80 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
81 "<div class=\"alert alert-danger\" role=\"alert\">Missing client ID.</div>");
82 } else if (ServletUtil.isParameterEnabled(request, MISSING_CLIENT_SECRET_PARAMETER_NAME)) {
83 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
85 "<div class=\"alert alert-danger\" role=\"alert\">Missing client secret.</div>");
86 } else if (ServletUtil.isParameterEnabled(request, MISSING_BRIDGE_ID_PARAMETER_NAME)) {
87 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
88 "<div class=\"alert alert-danger\" role=\"alert\">Missing bridge ID.</div>");
89 } else if (ServletUtil.isParameterEnabled(request, MISSING_EMAIL_PARAMETER_NAME)) {
90 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
91 "<div class=\"alert alert-danger\" role=\"alert\">Missing e-mail address.</div>");
92 } else if (ServletUtil.isParameterEnabled(request, MALFORMED_BRIDGE_ID_PARAMETER_NAME)) {
93 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
94 "<div class=\"alert alert-danger\" role=\"alert\">Malformed bridge ID. A bridge ID may only contain letters, numbers, '-' and '_'!</div>");
95 } else if (ServletUtil.isParameterEnabled(request, FAILED_TO_DERIVE_REDIRECT_URL_PARAMETER_NAME)) {
96 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
97 "<div class=\"alert alert-danger\" role=\"alert\">Failed to derive redirect URL.</div>");
98 } else if (ServletUtil.isParameterPresent(request,
99 ONGOING_AUTHORIZATION_IN_STEP1_EXPIRES_IN_MINUTES_PARAMETER_NAME)) {
100 String minutesUntilExpiry = request
101 .getParameter(ONGOING_AUTHORIZATION_IN_STEP1_EXPIRES_IN_MINUTES_PARAMETER_NAME);
102 if (ONGOING_AUTHORIZATION_UNKNOWN_EXPIRY_TIME.equals(minutesUntilExpiry)) {
103 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
104 "<div class=\"alert alert-danger\" role=\"alert\">There is an authorization ongoing at the moment. Please complete that authorization prior to starting a new one or try again later.</div>");
106 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
107 "<div class=\"alert alert-danger\" role=\"alert\">There is an authorization ongoing at the moment. Please complete that authorization prior to starting a new one or try again in "
108 + minutesUntilExpiry + " minutes.</div>");
110 } else if (ServletUtil.isParameterEnabled(request, NO_ONGOING_AUTHORIZATION_IN_STEP2_PARAMETER_NAME)) {
111 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
112 "<div class=\"alert alert-danger\" role=\"alert\">Failed to start auhtorization process. Are you trying to perform multiple authorizations at the same time?</div>");
113 } else if (ServletUtil.isParameterEnabled(request, MISSING_REQUEST_URL_PARAMETER_NAME)) {
114 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER,
115 "<div class=\"alert alert-danger\" role=\"alert\">Missing request URL. Please try again.</div>");
117 return skeleton.replace(ERROR_MESSAGE_PLACEHOLDER, "");