Class SmallUtils

java.lang.Object
com.blackrook.small.util.SmallUtils

public final class SmallUtils extends Object
Utility library for common or useful functions.
Author:
Matthew Tropiano
  • Field Details

  • Method Details

    • isJSON

      public static boolean isJSON(String type)
      Checks if a MIME-Type is JSON-formatted.
      Parameters:
      type - the content type.
      Returns:
      true if so, false if not.
    • isXML

      public static boolean isXML(String type)
      Checks if a MIME-Type is XML-formatted.
      Parameters:
      type - the content type.
      Returns:
      true if so, false if not.
    • convertToHTMLEntities

      public static String convertToHTMLEntities(String input)
      Converts a String to an HTML-safe string.
      Parameters:
      input - the input string to convert.
      Returns:
      the converted string.
    • convertFromHTMLEntities

      public static String convertFromHTMLEntities(String input)
      Converts a String with HTML entities in it to one without.
      Parameters:
      input - the input string to convert.
      Returns:
      the converted string.
    • trimSlashes

      public static String trimSlashes(String str)
      Trims slashes from the ends.
      Parameters:
      str - the input string.
      Returns:
      the input string with slashes removed from both ends.
      See Also:
    • pathify

      public static String pathify(String str)
      Convenience for addBeginningSlash(removeEndingSlash(str)).
      Parameters:
      str - the input string.
      Returns:
      the resulting string with a beginning slash and no ending slash.
    • addBeginningSlash

      public static String addBeginningSlash(String str)
      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

      public static String removeBeginningSlash(String str)
      Removes the beginning slashes, if any, from a string.
      Parameters:
      str - the string.
      Returns:
      the resulting string without a beginning slash.
    • removeEndingSlash

      public static String removeEndingSlash(String str)
      Removes the ending slashes, if any, from a string.
      Parameters:
      str - the string.
      Returns:
      the resulting string without an ending slash.
    • removeExtension

      public static String removeExtension(String path)
      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

      public static boolean getAttributeExist(javax.servlet.http.HttpSession session, String attribName)
      Convenience method that calls session.getAttribute(attribName).
      Parameters:
      session - the session.
      attribName - the attribute name.
      Returns:
      true if it exists, false otherwise.
    • getEnvironment

      public static SmallEnvironment getEnvironment(javax.websocket.EndpointConfig config)
      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

      public static SmallEnvironment getEnvironment(javax.servlet.ServletContext context)
      Convenience method that gets the Small Application Environment from the servlet context.
      Parameters:
      context - the servlet context.
      Returns:
      the Small environment.
    • getConfiguration

      public static SmallConfiguration getConfiguration(javax.servlet.ServletContext context)
      Convenience method that gets the Small Configuration stub used to start this application.
      Parameters:
      context - the servlet context.
      Returns:
      the Small environment.
    • getMIMEType

      public static String getMIMEType(javax.servlet.ServletContext context, String filename)
      Gets the MIME type of a file (uses the provided MIMETypeDriver).
      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

      public static <T> T getApplicationBean(javax.servlet.ServletContext context, Class<T> clazz)
      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 (via Class.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

      public static File getApplicationFile(javax.servlet.ServletContext context, String relativePath)
      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

      public static boolean getAttributeExist(javax.servlet.ServletContext context, String attribName)
      Convenience method that calls context.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 to maxMillis milliseconds, used for simulating lag. For debugging and testing only!
      Parameters:
      maxMillis - the maximum amount of milliseconds to wait.
    • getResource

      public static URL getResource(String pathString)
      Finds a resource URL using the current thread's ClassLoader.
      Parameters:
      pathString - the resource pathname.
      Returns:
      a URL for the resource or null if not found.
      See Also:
    • getResource

      public static URL getResource(ClassLoader classLoader, String pathString)
      Finds a resource URL using the current thread's ClassLoader.
      Parameters:
      classLoader - the provided ClassLoader to use.
      pathString - the resource pathname.
      Returns:
      a URL for the resource or null if not found.
      See Also:
    • openResource

      public static InputStream openResource(String pathString)
      Opens an InputStream to a resource using the current thread's ClassLoader.
      Parameters:
      pathString - the resource pathname.
      Returns:
      an open InputStream for reading the resource or null if not found.
      See Also:
    • openResource

      public static InputStream openResource(ClassLoader classLoader, String pathString)
      Opens an InputStream to a resource using a provided ClassLoader.
      Parameters:
      classLoader - the provided ClassLoader to use.
      pathString - the resource pathname.
      Returns:
      an open InputStream for reading the resource or null if not found.
      See Also:
    • encapsulateResponseContent

      public static SmallResponse encapsulateResponseContent(Object content)
      Encapsulates an object in a SmallResponse object. If the object passed in is a SmallResponse, 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.
      • If content class is a Reader, CharSequence, String, StringBuilder, or StringBuffer, plain text is sent back. Content type is text/plain if unspecified.
      • If content class is byte[] or ByteBuffer binary data is sent back. Content type is application/octet-stream if unspecified.
      • If content class is any other object type,
        • ...and a XMLDriver component is found, and the specified content type is application/xml or an XML subtype, the object is converted to XML.
        • ...and a JSONDriver component is found, content type is application/json and the object is converted.
      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