]> git.basschouten.com Git - openhab-addons.git/blob
a24802b3b298f5de4c301a555b1c8a0148506949
[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.mielecloud.internal.config.servlet;
14
15 import java.io.IOException;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Servlet showing a failure page.
24  *
25  * @author Björn Lange - Initial Contribution
26  */
27 @NonNullByDefault
28 public class FailureServlet extends AbstractShowPageServlet {
29     private static final long serialVersionUID = -5195984256535664942L;
30
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";
40
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";
48
49     private static final String ERROR_MESSAGE_TEXT_PLACEHOLDER = "<!-- ERROR MESSAGE TEXT -->";
50
51     /**
52      * Creates a new {@link FailureServlet}.
53      *
54      * @param resourceLoader Loader to use for resources.
55      */
56     public FailureServlet(ResourceLoader resourceLoader) {
57         super(resourceLoader);
58     }
59
60     @Override
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));
65     }
66
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.";
87         } else {
88             return "Unknown error.";
89         }
90     }
91
92     private String getOAuth2ErrorMessage(String oauth2Error) {
93         return "OAuth2 authentication with Miele cloud service failed: " + getOAuth2ErrorDetailMessage(oauth2Error);
94     }
95
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.";
112             default:
113                 return "Unknown error code \"" + oauth2Error + "\".";
114         }
115     }
116 }