]> git.basschouten.com Git - openhab-addons.git/blob
c3ee8a1783b5422ef1ae0cd2e79be10777c24884
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ServletException;
18 import javax.servlet.http.HttpServlet;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Base class for servlets that have no visible frontend and just serve the purpose of redirecting the user to another
29  * website.
30  *
31  * @author Björn Lange - Initial Contribution
32  */
33 @NonNullByDefault
34 public abstract class AbstractRedirectionServlet extends HttpServlet {
35     private static final long serialVersionUID = 4280026301732437523L;
36
37     private final Logger logger = LoggerFactory.getLogger(AbstractRedirectionServlet.class);
38
39     @Override
40     protected void doGet(@Nullable HttpServletRequest request, @Nullable HttpServletResponse response)
41             throws ServletException, IOException {
42         if (response == null) {
43             logger.warn("Ignoring received request without response.");
44             return;
45         }
46         if (request == null) {
47             logger.warn("Ignoring illegal request.");
48             response.sendError(HttpServletResponse.SC_BAD_REQUEST);
49             return;
50         }
51
52         response.sendRedirect(getRedirectionDestination(request));
53     }
54
55     /**
56      * Gets the redirection destination. This can be a relative or absolute path or a link to another website.
57      *
58      * @param request The original request sent by the browser.
59      * @return The redirection destination.
60      */
61     protected abstract String getRedirectionDestination(HttpServletRequest request);
62 }