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.mielecloud.internal.config.servlet;
15 import javax.servlet.http.HttpServletRequest;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
19 import org.openhab.binding.mielecloud.internal.config.OAuthAuthorizationHandler;
20 import org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException;
21 import org.openhab.core.thing.ThingUID;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Servlet processing the response by the Miele service after a login. This servlet is called as a result of a
27 * completed login to the Miele service and assumes that the OAuth 2 parameters are passed. Depending on the parameters
28 * and whether the token response can be fetched either the browser is redirected to the success or the failure page.
30 * @author Björn Lange - Initial Contribution
33 public final class ResultServlet extends AbstractRedirectionServlet {
34 private static final long serialVersionUID = 2157912755568949550L;
36 public static final String CODE_PARAMETER_NAME = "code";
37 public static final String STATE_PARAMETER_NAME = "state";
38 public static final String ERROR_PARAMETER_NAME = "error";
40 private final Logger logger = LoggerFactory.getLogger(ResultServlet.class);
42 private final OAuthAuthorizationHandler authorizationHandler;
45 * Creates a new {@link ResultServlet}.
47 * @param authorizationHandler Handler implementing the OAuth authorization.
49 public ResultServlet(OAuthAuthorizationHandler authorizationHandler) {
50 this.authorizationHandler = authorizationHandler;
54 protected String getRedirectionDestination(HttpServletRequest request) {
55 String error = request.getParameter(ERROR_PARAMETER_NAME);
57 logger.warn("Received error response: {}", error);
58 return "/mielecloud/failure?" + FailureServlet.OAUTH2_ERROR_PARAMETER_NAME + "=" + error;
61 String code = request.getParameter(CODE_PARAMETER_NAME);
63 logger.warn("Code is null");
64 return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
66 String state = request.getParameter(STATE_PARAMETER_NAME);
68 logger.warn("State is null");
69 return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
73 ThingUID bridgeId = authorizationHandler.getBridgeUid();
74 String email = authorizationHandler.getEmail();
76 StringBuffer requestUrl = request.getRequestURL();
77 if (requestUrl == null) {
78 return "/mielecloud/failure?" + FailureServlet.MISSING_REQUEST_URL_PARAMETER_NAME + "=true";
82 authorizationHandler.completeAuthorization(requestUrl.toString() + "?" + request.getQueryString());
83 } catch (OAuthException e) {
84 logger.warn("Failed to complete authorization.", e);
85 return "/mielecloud/failure?" + FailureServlet.FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME
89 return "/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=" + bridgeId.getAsString()
90 + "&" + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + email;
91 } catch (NoOngoingAuthorizationException e) {
92 logger.warn("Failed to complete authorization: There is no ongoing authorization or it timed out");
93 return "/mielecloud/failure?" + FailureServlet.NO_ONGOING_AUTHORIZATION_PARAMETER_NAME + "=true";