Package com.sodius.oslc.server
Class ContextParameters
- java.lang.Object
-
- com.sodius.oslc.server.ContextParameters
-
public class ContextParameters extends Object
Provides access to parameters defined on servlet context and Java system properties.The strategy implemented in methods of this class is to first lookup the specified parameter name in the servlet context initialization parameters and then, if not found, in Java system properties.
Here is a snippet demonstrating how to obtain a context parameter value:
String value = ContextParameters.of(request).getString("myParameterName", "theDefaultValue");
- Since:
- 1.4.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
getBoolean(String name, boolean defaultValue)
Returns the boolean value associated with the specified context parameter or default value if parameter is not set.Optional<File>
getFile(String name)
Returns the File value associated with the specified context parameter name.boolean
getRequiredBoolean(String name)
Returns the boolean value associated with the specified context parameter name that is required to exist.File
getRequiredFile(String name)
Returns the File value associated with the specified context parameter name that is required to exist.String
getRequiredString(String name)
Returns the string value associated with the specified context parameter name that is required to exist.List<String>
getRequiredStrings(String name, char separator)
Returns the values associated with the specified context parameter that is required to exist.Optional<String>
getString(String name)
Returns the string value associated with the specified context parameter name.String
getString(String name, String defaultValue)
Returns the string value associated with the specified context parameter or default value if parameter is not set.List<String>
getStrings(String name, char separator)
Returns the values associated with the specified context parameter.static ContextParameters
of(FilterConfig config)
Returns an instance that wraps the servlet context of the specified filter configuration.static ContextParameters
of(HttpServletRequest request)
Returns an instance that wraps the servlet context of the specified request.static ContextParameters
of(ServletContext context)
Returns an instance that wraps the specified servlet context.
-
-
-
Method Detail
-
of
public static ContextParameters of(ServletContext context)
Returns an instance that wraps the specified servlet context.- Parameters:
context
- the servlet context.- Returns:
- a new ContextParameters instance.
-
of
public static ContextParameters of(HttpServletRequest request)
Returns an instance that wraps the servlet context of the specified request.- Parameters:
request
- a servlet request.- Returns:
- a new ContextParameters instance.
-
of
public static ContextParameters of(FilterConfig config)
Returns an instance that wraps the servlet context of the specified filter configuration.- Parameters:
config
- a filter configuration.- Returns:
- a new ContextParameters instance.
-
getString
public Optional<String> getString(String name)
Returns the string value associated with the specified context parameter name. This method first looks for the specified parameter name in the servlet context initialization parameters and then, if not found, in Java system properties.- Parameters:
name
- the parameter name.- Returns:
- an Optional wrapping the string value associated to the parameter name, Optional.empty() if none.
-
getRequiredString
public String getRequiredString(String name)
Returns the string value associated with the specified context parameter name that is required to exist.- Parameters:
name
- the parameter name.- Returns:
- the string value associated to the parameter name.
- Throws:
WebApplicationException
- if the parameter is not set in servlet context parameters nor in Java system properties.
-
getString
public String getString(String name, String defaultValue)
Returns the string value associated with the specified context parameter or default value if parameter is not set.- Parameters:
name
- the parameter name.defaultValue
- the default value to use if the context parameter is not set.- Returns:
- the string value associated to the parameter name or the specified default value if the context parameter is not set.
-
getRequiredBoolean
public boolean getRequiredBoolean(String name)
Returns the boolean value associated with the specified context parameter name that is required to exist.- Parameters:
name
- the parameter name.- Returns:
- the boolean value associated to the parameter name.
- Throws:
WebApplicationException
- if the parameter is not set in servlet context parameters nor in Java system properties.
-
getBoolean
public boolean getBoolean(String name, boolean defaultValue)
Returns the boolean value associated with the specified context parameter or default value if parameter is not set.- Parameters:
name
- the parameter name.defaultValue
- the default value to use if the context parameter is not set.- Returns:
- the boolean value associated to the parameter name or the specified default value if the context parameter is not set.
-
getStrings
public List<String> getStrings(String name, char separator)
Returns the values associated with the specified context parameter. The string value is split using the specified separator to obtain the list of parameter values.- Parameters:
name
- the parameter name.separator
- the character used as delimiter in the parameter string value.- Returns:
- the parsed list of values associated to the parameter name.
-
getRequiredStrings
public List<String> getRequiredStrings(String name, char separator)
Returns the values associated with the specified context parameter that is required to exist. The string value is split using the specified separator to obtain the list of parameter values.- Parameters:
name
- the parameter name.separator
- the character used as delimiter in the parameter string value.- Returns:
- the parsed list of values associated to the parameter name.
- Throws:
WebApplicationException
- if the parameter is not set in servlet context parameters nor in Java system properties.
-
getFile
public Optional<File> getFile(String name)
Returns the File value associated with the specified context parameter name. This method first looks for the specified parameter name in the servlet context initialization parameters and then, if not found, in Java system properties.If the parameter value is a URL whose protocol is
"file:"
, this method converts the URL to a File instance.- Parameters:
name
- the parameter name.- Returns:
- an Optional wrapping the File value associated to the parameter name, Optional.empty() if none.
-
getRequiredFile
public File getRequiredFile(String name)
Returns the File value associated with the specified context parameter name that is required to exist.- Parameters:
name
- the parameter name.- Returns:
- the File value associated to the parameter name.
- Throws:
WebApplicationException
- if the parameter is not set in servlet context parameters nor in Java system properties.- See Also:
getFile(String)
-
-