Package com.blackrook.small.util
Class SmallUtils
java.lang.Object
com.blackrook.small.util.SmallUtils
Utility library for common or useful functions.
- Author:
- Matthew Tropiano
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
addBeginningSlash
(String str) Adds a beginning slash to the string, if no beginning slash exists.static String
convertFromHTMLEntities
(String input) Converts a String with HTML entities in it to one without.static String
convertToHTMLEntities
(String input) Converts a String to an HTML-safe string.static SmallResponse
encapsulateResponseContent
(Object content) Encapsulates an object in aSmallResponse
object.static <T> T
getApplicationBean
(javax.servlet.ServletContext context, Class<T> clazz) Gets and auto-casts an object bean stored at the application level.static <T> T
getApplicationBean
(javax.servlet.ServletContext context, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the application level.static <T> T
getApplicationBean
(javax.servlet.ServletContext context, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the application level.static File
getApplicationFile
(javax.servlet.ServletContext context, String relativePath) Gets a file that is on the application path.static InputStream
getApplicationFileAsStream
(javax.servlet.ServletContext context, String path) Opens an input stream to a resource using a path relative to the application context path.static String
getApplicationFilePath
(javax.servlet.ServletContext context, String relativePath) Gets a file path that is on the application path.static boolean
getAttributeExist
(javax.servlet.http.HttpSession session, String attribName) Convenience method that callssession.getAttribute(attribName)
.static boolean
getAttributeExist
(javax.servlet.ServletContext context, String attribName) Convenience method that callscontext.getAttribute(attribName)
.static SmallConfiguration
getConfiguration
(javax.servlet.ServletContext context) Convenience method that gets the Small Configuration stub used to start this application.static SmallEnvironment
getEnvironment
(javax.servlet.ServletContext context) Convenience method that gets the Small Application Environment from the servlet context.static SmallEnvironment
getEnvironment
(javax.websocket.EndpointConfig config) Convenience method that gets the Small Application Environment from WebSocket EndpointConfig.static String
getMIMEType
(javax.servlet.ServletContext context, String filename) Gets the MIME type of a file (uses the providedMIMETypeDriver
).static URL
getResource
(ClassLoader classLoader, String pathString) Finds a resource URL using the current thread'sClassLoader
.static URL
getResource
(String pathString) Finds a resource URL using the current thread'sClassLoader
.static javax.websocket.server.ServerContainer
getWebsocketServerContainer
(javax.servlet.ServletContext context) Attempts to fetch the Websocket Server Container from a servlet context.static <T extends Throwable>
voidhandleException
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Throwable thr) Convenience function for forwarding exception handling to the Small Environment, re-throwing the provided if a suitable handler was not found nor invoked.static void
handleView
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object model, String viewName) Convenience function for forwarding view handling to the Small Environment, throwing an exception if a suitable handler was not found nor invoked.static boolean
Checks if a MIME-Type is JSON-formatted.static boolean
Checks if a MIME-Type is XML-formatted.static InputStream
openResource
(ClassLoader classLoader, String pathString) Opens anInputStream
to a resource using a provided ClassLoader.static InputStream
openResource
(String pathString) Opens anInputStream
to a resource using the current thread'sClassLoader
.static String
Convenience foraddBeginningSlash(removeEndingSlash(str))
.static String
Removes the beginning slashes, if any, from a string.static String
removeEndingSlash
(String str) Removes the ending slashes, if any, from a string.static String
removeExtension
(String path) Removes the path extension, or rather, the part after the last dot in a path.static void
sendContent
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String attachmentFileName, Object content) Writes a content object to the client.static void
simulateLag
(long maxMillis) Pauses the current thread for up tomaxMillis
milliseconds, used for simulating lag.static String
trimSlashes
(String str) Trims slashes from the ends.
-
Field Details
-
VIEW_REDIRECT_PREFIX
View redirect prefix.- See Also:
-
CONTENT_MIME_TYPE_JSON
MIME type for JSON- See Also:
-
CONTENT_MIME_TYPE_XML
MIME type for XML- See Also:
-
ENTITY_NAME_MAP
Map of entity names to characters. -
UNICODE_ENTITY_MAP
Map of characters to entity names.
-
-
Method Details
-
isJSON
Checks if a MIME-Type is JSON-formatted.- Parameters:
type
- the content type.- Returns:
- true if so, false if not.
-
isXML
Checks if a MIME-Type is XML-formatted.- Parameters:
type
- the content type.- Returns:
- true if so, false if not.
-
convertToHTMLEntities
Converts a String to an HTML-safe string.- Parameters:
input
- the input string to convert.- Returns:
- the converted string.
-
convertFromHTMLEntities
Converts a String with HTML entities in it to one without.- Parameters:
input
- the input string to convert.- Returns:
- the converted string.
-
trimSlashes
Trims slashes from the ends.- Parameters:
str
- the input string.- Returns:
- the input string with slashes removed from both ends.
- See Also:
-
pathify
Convenience foraddBeginningSlash(removeEndingSlash(str))
.- Parameters:
str
- the input string.- Returns:
- the resulting string with a beginning slash and no ending slash.
-
addBeginningSlash
Adds a beginning slash to the string, if no beginning slash exists.- Parameters:
str
- the string.- Returns:
- the resulting string with a beginning slash.
-
removeBeginningSlash
Removes the beginning slashes, if any, from a string.- Parameters:
str
- the string.- Returns:
- the resulting string without a beginning slash.
-
removeEndingSlash
Removes the ending slashes, if any, from a string.- Parameters:
str
- the string.- Returns:
- the resulting string without an ending slash.
-
removeExtension
Removes the path extension, or rather, the part after the last dot in a path.- Parameters:
path
- the path string.- Returns:
- the resulting string without an extension.
-
getAttributeExist
Convenience method that callssession.getAttribute(attribName)
.- Parameters:
session
- the session.attribName
- the attribute name.- Returns:
- true if it exists, false otherwise.
-
getEnvironment
Convenience method that gets the Small Application Environment from WebSocket EndpointConfig.- Parameters:
config
- the endpoint config context.- Returns:
- the Small environment.
- Since:
- 1.6.0
-
getEnvironment
Convenience method that gets the Small Application Environment from the servlet context.- Parameters:
context
- the servlet context.- Returns:
- the Small environment.
-
getConfiguration
Convenience method that gets the Small Configuration stub used to start this application.- Parameters:
context
- the servlet context.- Returns:
- the Small environment.
-
getMIMEType
Gets the MIME type of a file (uses the providedMIMETypeDriver
).- Parameters:
context
- the servlet context.filename
- the file name to use to figure out a MIME type.- Returns:
- the MIME type, or
application/octet-stream
.
-
getApplicationBean
Gets and auto-casts an object bean stored at the application level. The bean is created and stored if it doesn't exist. The name used is the fully-qualified class name prefixed with "$$".- Type Parameters:
T
- the object type.- Parameters:
context
- the servlet context to use.clazz
- the class type of the object that should be returned.- Returns:
- a typecast object on the application scope.
- Throws:
IllegalArgumentException
- if the class provided in an anonymous class or array without a component type.BeanCreationException
- if the object cannot be instantiated for any reason.
-
getApplicationBean
public static <T> T getApplicationBean(javax.servlet.ServletContext context, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the application level. The bean is created and stored if it doesn't exist.- Type Parameters:
T
- the object type.- Parameters:
context
- the servlet context to use.clazz
- the class type of the object that should be returned.name
- the attribute name.- Returns:
- a typecast object on the application scope.
- Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getApplicationBean
public static <T> T getApplicationBean(javax.servlet.ServletContext context, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the application level.- Type Parameters:
T
- the object type.- Parameters:
context
- the servlet context to use.clazz
- the class type of the object that should be returned.name
- the attribute name.create
- if true, instantiate this class in the application's servlet context (viaClass.newInstance()
) if it doesn't exist.- Returns:
- a typecast object on the application scope, or null if it doesn't exist and wasn't created.
- Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getApplicationFile
Gets a file that is on the application path.- Parameters:
context
- the servlet context to use.relativePath
- the path to the file to get via a path relative to the application root.- Returns:
- a file representing the specified resource or null if it couldn't be found.
-
getWebsocketServerContainer
public static javax.websocket.server.ServerContainer getWebsocketServerContainer(javax.servlet.ServletContext context) Attempts to fetch the Websocket Server Container from a servlet context.- Parameters:
context
- the servlet context to use.- Returns:
- the server container, or null if it wasn't found, usually because it wasn't configured or it is unsupported.
-
getApplicationFilePath
public static String getApplicationFilePath(javax.servlet.ServletContext context, String relativePath) Gets a file path that is on the application path.- Parameters:
context
- the servlet context to use.relativePath
- the relative path to the file to get.- Returns:
- a file representing the specified resource or null if it couldn't be found.
-
getApplicationFileAsStream
public static InputStream getApplicationFileAsStream(javax.servlet.ServletContext context, String path) throws IOException Opens an input stream to a resource using a path relative to the application context path. Outside users should not be able to access this!- Parameters:
context
- the servlet context to use.path
- the path to the resource to open.- Returns:
- an open input stream to the specified resource or null if it couldn't be opened.
- Throws:
IOException
- if the stream cannot be opened.
-
getAttributeExist
Convenience method that callscontext.getAttribute(attribName)
.- Parameters:
context
- the servlet context.attribName
- the attribute name.- Returns:
- true if it exists, false otherwise.
-
handleView
public static void handleView(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object model, String viewName) throws NoViewDriverException, ViewProcessingException Convenience function for forwarding view handling to the Small Environment, throwing an exception if a suitable handler was not found nor invoked.- Parameters:
request
- the HTTP request object.response
- the HTTP response object.model
- the model to render using the view.viewName
- the name of the view to handle.- Throws:
NoViewDriverException
- if a suitable handler was not found nor invoked.ViewProcessingException
- if an error occurs on view processing of any kind.
-
handleException
public static <T extends Throwable> void handleException(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Throwable thr) throws T Convenience function for forwarding exception handling to the Small Environment, re-throwing the provided if a suitable handler was not found nor invoked.- Type Parameters:
T
- the exception type.- Parameters:
request
- the HTTP request object.response
- the HTTP response object.thr
- the throwable/exception to handle.- Throws:
T
- if a suitable handler was not found nor invoked.
-
simulateLag
public static void simulateLag(long maxMillis) Pauses the current thread for up tomaxMillis
milliseconds, used for simulating lag. For debugging and testing only!- Parameters:
maxMillis
- the maximum amount of milliseconds to wait.
-
getResource
Finds a resource URL using the current thread'sClassLoader
.- Parameters:
pathString
- the resource pathname.- Returns:
- a URL for the resource or null if not found.
- See Also:
-
getResource
Finds a resource URL using the current thread'sClassLoader
.- Parameters:
classLoader
- the providedClassLoader
to use.pathString
- the resource pathname.- Returns:
- a URL for the resource or null if not found.
- See Also:
-
openResource
Opens anInputStream
to a resource using the current thread'sClassLoader
.- Parameters:
pathString
- the resource pathname.- Returns:
- an open
InputStream
for reading the resource or null if not found. - See Also:
-
openResource
Opens anInputStream
to a resource using a provided ClassLoader.- Parameters:
classLoader
- the providedClassLoader
to use.pathString
- the resource pathname.- Returns:
- an open
InputStream
for reading the resource or null if not found. - See Also:
-
encapsulateResponseContent
Encapsulates an object in aSmallResponse
object. If the object passed in is aSmallResponse
, nothing is changed, and the same object is returned.- Parameters:
content
- the content to encapsulate.- Returns:
- a SmallResponse object.
- Since:
- 1.2.0
-
sendContent
public static void sendContent(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String attachmentFileName, Object content) throws NotFoundException, NoViewDriverException, ViewProcessingException, NoConverterException, IOException Writes a content object to the client.This converts an object into data that is put into the response body.
- If content class is a
SmallResponse
, its content is the object to convert (other headers and statuses are set on the response), and conversion continues below for the content... - If content class is a
SmallModelView
, the view is resolved and written to the response. - If content class is a
File
,- ...the content type is changed to the file's predicted MIME-type and the content is the file's content, verbatim. Unknown type is
application/octet-stream
. - ...and does not exist, a NotFoundException is thrown.
- ...the content type is changed to the file's predicted MIME-type and the content is the file's content, verbatim. Unknown type is
- If content class is a
Reader
,CharSequence
,String
,StringBuilder
, orStringBuffer
, plain text is sent back. Content type istext/plain
if unspecified. - If content class is byte[] or
ByteBuffer
binary data is sent back. Content type isapplication/octet-stream
if unspecified. - If content class is any other object type,
- ...and a
XMLDriver
component is found, and the specified content type isapplication/xml
or an XML subtype, the object is converted to XML. - ...and a
JSONDriver
component is found, content type isapplication/json
and the object is converted.
- ...and a
- Parameters:
request
- the HTTP request object.response
- the HTTP response object.attachmentFileName
- the name of the data to send (file name). If null, not sent as an attachment.content
- the object to write.- Throws:
NotFoundException
- if a file is the content, and it was not found, or it's a directory.NoViewDriverException
- if a suitable handler was not found nor invoked.ViewProcessingException
- if an error occurs on view processing of any kind.NoConverterException
- if the output object type could not be exported or converted to a suitable format.IOException
- if an I/O error occurs.- Since:
- 1.1.0
- If content class is a
-