]> git.basschouten.com Git - openhab-addons.git/blob
15329f8f3823ee2b92b88fd700e3c189358ec472
[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 javax.servlet.http.HttpServletRequest;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Utility class for common servlet tasks.
21  *
22  * @author Björn Lange - Initial Contribution
23  */
24 @NonNullByDefault
25 public final class ServletUtil {
26     private ServletUtil() {
27         throw new UnsupportedOperationException();
28     }
29
30     /**
31      * Gets the value of a request parameter or returns a default if the parameter is not present.
32      */
33     public static String getParameterValueOrDefault(HttpServletRequest request, String parameterName,
34             String defaultValue) {
35         String parameterValue = request.getParameter(parameterName);
36         if (parameterValue == null) {
37             return defaultValue;
38         } else {
39             return parameterValue;
40         }
41     }
42
43     /**
44      * Checks whether a request parameter is enabled.
45      */
46     public static boolean isParameterEnabled(HttpServletRequest request, String parameterName) {
47         return "true".equalsIgnoreCase(getParameterValueOrDefault(request, parameterName, "false"));
48     }
49
50     /**
51      * Checks whether a parameter is present in a request.
52      */
53     public static boolean isParameterPresent(HttpServletRequest request, String parameterName) {
54         String parameterValue = request.getParameter(parameterName);
55         return parameterValue != null && !parameterValue.trim().isEmpty();
56     }
57 }