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.mielecloud.internal.config.servlet;
15 import java.io.IOException;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * Servlet showing a failure page.
25 * @author Björn Lange - Initial Contribution
28 public class FailureServlet extends AbstractShowPageServlet {
29 private static final long serialVersionUID = -5195984256535664942L;
31 public static final String OAUTH2_ERROR_PARAMETER_NAME = "oauth2Error";
32 public static final String ILLEGAL_RESPONSE_PARAMETER_NAME = "illegalResponse";
33 public static final String NO_ONGOING_AUTHORIZATION_PARAMETER_NAME = "noOngoingAuthorization";
34 public static final String FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME = "failedToCompleteAuthorization";
35 public static final String MISSING_BRIDGE_UID_PARAMETER_NAME = "missingBridgeUid";
36 public static final String MISSING_EMAIL_PARAMETER_NAME = "missingEmail";
37 public static final String MALFORMED_BRIDGE_UID_PARAMETER_NAME = "malformedBridgeUid";
38 public static final String MALFORMED_EMAIL_PARAMETER_NAME = "malformedEmail";
39 public static final String MISSING_REQUEST_URL_PARAMETER_NAME = "missingRequestUrl";
41 public static final String OAUTH2_ERROR_ACCESS_DENIED = "access_denied";
42 public static final String OAUTH2_ERROR_INVALID_REQUEST = "invalid_request";
43 public static final String OAUTH2_ERROR_UNAUTHORIZED_CLIENT = "unauthorized_client";
44 public static final String OAUTH2_ERROR_UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type";
45 public static final String OAUTH2_ERROR_INVALID_SCOPE = "invalid_scope";
46 public static final String OAUTH2_ERROR_SERVER_ERROR = "server_error";
47 public static final String OAUTH2_ERROR_TEMPORARY_UNAVAILABLE = "temporarily_unavailable";
49 private static final String ERROR_MESSAGE_TEXT_PLACEHOLDER = "<!-- ERROR MESSAGE TEXT -->";
52 * Creates a new {@link FailureServlet}.
54 * @param resourceLoader Loader to use for resources.
56 public FailureServlet(ResourceLoader resourceLoader) {
57 super(resourceLoader);
61 protected String handleGetRequest(HttpServletRequest request, HttpServletResponse response)
62 throws MieleHttpException, IOException {
63 return getResourceLoader().loadResourceAsString("failure.html").replace(ERROR_MESSAGE_TEXT_PLACEHOLDER,
64 getErrorMessage(request));
67 private String getErrorMessage(HttpServletRequest request) {
68 String oauth2Error = request.getParameter(OAUTH2_ERROR_PARAMETER_NAME);
69 if (oauth2Error != null) {
70 return getOAuth2ErrorMessage(oauth2Error);
71 } else if (ServletUtil.isParameterEnabled(request, ILLEGAL_RESPONSE_PARAMETER_NAME)) {
72 return "Miele cloud service returned an illegal response.";
73 } else if (ServletUtil.isParameterEnabled(request, NO_ONGOING_AUTHORIZATION_PARAMETER_NAME)) {
74 return "There is no ongoing authorization. Please start an authorization first.";
75 } else if (ServletUtil.isParameterEnabled(request, FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME)) {
76 return "Completing the final authorization request failed. Please try the config flow again.";
77 } else if (ServletUtil.isParameterEnabled(request, MISSING_BRIDGE_UID_PARAMETER_NAME)) {
78 return "Missing bridge UID.";
79 } else if (ServletUtil.isParameterEnabled(request, MISSING_EMAIL_PARAMETER_NAME)) {
80 return "Missing e-mail address.";
81 } else if (ServletUtil.isParameterEnabled(request, MALFORMED_BRIDGE_UID_PARAMETER_NAME)) {
82 return "Malformed bridge UID.";
83 } else if (ServletUtil.isParameterEnabled(request, MALFORMED_EMAIL_PARAMETER_NAME)) {
84 return "Malformed e-mail address.";
85 } else if (ServletUtil.isParameterEnabled(request, MISSING_REQUEST_URL_PARAMETER_NAME)) {
86 return "Missing request URL. Please try the config flow again.";
88 return "Unknown error.";
92 private String getOAuth2ErrorMessage(String oauth2Error) {
93 return "OAuth2 authentication with Miele cloud service failed: " + getOAuth2ErrorDetailMessage(oauth2Error);
96 private String getOAuth2ErrorDetailMessage(String oauth2Error) {
97 switch (oauth2Error) {
98 case OAUTH2_ERROR_ACCESS_DENIED:
99 return "Access denied.";
100 case OAUTH2_ERROR_INVALID_REQUEST:
101 return "Malformed request.";
102 case OAUTH2_ERROR_UNAUTHORIZED_CLIENT:
103 return "Account not authorized to request authorization code.";
104 case OAUTH2_ERROR_UNSUPPORTED_RESPONSE_TYPE:
105 return "Obtaining an authorization code is not supported.";
106 case OAUTH2_ERROR_INVALID_SCOPE:
107 return "Invalid scope.";
108 case OAUTH2_ERROR_SERVER_ERROR:
109 return "Unexpected server error.";
110 case OAUTH2_ERROR_TEMPORARY_UNAVAILABLE:
111 return "Authorization server temporarily unavailable.";
113 return "Unknown error code \"" + oauth2Error + "\".";