Class SmallResponseUtils

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

public final class SmallResponseUtils extends Object
Utility class for HttpServletResponse manipulation.
Author:
Matthew Tropiano
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    sendData(javax.servlet.http.HttpServletResponse response, InputStream inStream)
    Sends the entire contents of a stream out through the response.
    static void
    sendData(javax.servlet.http.HttpServletResponse response, InputStream inStream, Long length)
    Sends the contents of a stream out through the response.
    static void
    sendData(javax.servlet.http.HttpServletResponse response, String mimeType, InputStream inStream, Long length)
    Sends the contents of a stream out through the response.
    static void
    sendData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, InputStream inStream, Long length)
    Sends the contents of a stream out through the response.
    static void
    sendData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String encoding, InputStream inStream, Long length)
    Sends the contents of a stream out through the response.
    static void
    sendError(javax.servlet.http.HttpServletResponse response, int statusCode, String message)
    Sends request to the error page with a status code.
    static void
    sendFile(javax.servlet.http.HttpServletResponse response, String mimeType, File file)
    Sends a file to the client.
    static void
    sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, File file)
    Sends contents of a file to the client.
    static void
    sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, File file)
    Sends the contents of a file to the client.
    static void
    sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String encoding, File file)
    Sends the contents of a file to the client.
    static void
    sendRedirect(javax.servlet.http.HttpServletResponse response, String url)
    Forwards the client abruptly to another document or servlet (new client request).
    static void
    sendStringData(javax.servlet.http.HttpServletResponse response, String data)
    Writes string data to the response as "text/plain".
    static void
    sendStringData(javax.servlet.http.HttpServletResponse response, String mimeType, String data)
    Writes string data to the response.
    static void
    sendStringData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String data)
    Writes string data to the response.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • sendFile

      public static void sendFile(javax.servlet.http.HttpServletResponse response, String mimeType, File file)
      Sends a file to the client. The file's name becomes the filename in the content's "disposition". Via this method, most browsers will be forced to download the file in question separately, as this adds "Content-Disposition" headers to the response.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + file.getName() + "\"".
      • The "Content-Length" portion of the header is set to the file's length in bytes.
      Parameters:
      response - servlet response object.
      mimeType - the MIME Type of the content to send. If null, "application/octet-stream" is sent.
      file - the file content to send.
    • sendFileContents

      public static void sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, File file)
      Sends contents of a file to the client. This is not sent as an "attachment".
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Length" portion of the header is set to the file's length in bytes.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      file - the file content to send.
    • sendFileContents

      public static void sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, File file)
      Sends the contents of a file to the client.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + attachmentFileName + "\"", if not null.
      • The "Content-Length" portion of the header is set to the file's length in bytes.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      file - the file content to send.
      attachmentFileName - the file name. If null, not sent as an attachment. care whether the file is downloaded or opened by the browser.
    • sendFileContents

      public static void sendFileContents(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String encoding, File file)
      Sends the contents of a file to the client.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Encoding" portion of the header is set to encoding, if not null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + attachmentFileName + "\"", if not null.
      • The "Content-Length" portion of the header is set to the file's length in bytes.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      file - the file content to send.
      attachmentFileName - the file name. If null, not sent as an attachment.
      encoding - if not null, adds a "Content-Encoding" header (not to be confused with charset - that should be set on the MIME-Type). care whether the file is downloaded or opened by the browser.
    • sendStringData

      public static void sendStringData(javax.servlet.http.HttpServletResponse response, String data)
      Writes string data to the response as "text/plain".
      • The "Content-Type" portion of the header is set to "text/plain".
      • The "Content-Length" portion of the header is set to the byte length.
      Parameters:
      response - the response object.
      data - the string data to send.
    • sendStringData

      public static void sendStringData(javax.servlet.http.HttpServletResponse response, String mimeType, String data)
      Writes string data to the response.
      • The "Content-Type" portion of the header is set to mimeType, or "text/plain" if null.
      • The "Content-Length" portion of the header is set to the byte length.
      Parameters:
      response - the response object.
      mimeType - the response MIME-Type. If null, "text/plain" is used.
      data - the string data to send.
    • sendStringData

      public static void sendStringData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String data)
      Writes string data to the response. Charset is always UTF-8.
      • The "Content-Type" portion of the header is set to mimeType, or "text/plain" if null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + attachmentFileName + "\"", if not null.
      • The "Content-Length" portion of the header is set to the byte length.
      Parameters:
      response - the response object.
      mimeType - the response MIME-Type. If null, "text/plain" is used.
      attachmentFileName - the file name. If null, not sent as an attachment.
      data - the string data to send.
    • sendData

      public static void sendData(javax.servlet.http.HttpServletResponse response, InputStream inStream)
      Sends the entire contents of a stream out through the response. The input stream is not closed after the data is sent. No content type is set.
      • The "Content-Length" portion of the header is set to length, if not null and positive.
      Parameters:
      response - servlet response object.
      inStream - the input stream to read.
    • sendData

      public static void sendData(javax.servlet.http.HttpServletResponse response, InputStream inStream, Long length)
      Sends the contents of a stream out through the response. The input stream is not closed after the data is sent. No content type is set.
      • The "Content-Length" portion of the header is set to length, if not null and positive.
      Parameters:
      response - servlet response object.
      inStream - the input stream to read.
      length - the length of data in bytes to send.
    • sendData

      public static void sendData(javax.servlet.http.HttpServletResponse response, String mimeType, InputStream inStream, Long length)
      Sends the contents of a stream out through the response. The input stream is not closed after the data is sent.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Length" portion of the header is set to length, if not null and positive.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      inStream - the input stream to read.
      length - the length of data in bytes to send.
    • sendData

      public static void sendData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, InputStream inStream, Long length)
      Sends the contents of a stream out through the response. The input stream is not closed after the data is sent.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + attachmentFileName + "\"", if not null.
      • The "Content-Length" portion of the header is set to length, if not null and positive.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      attachmentFileName - the name of the data to send (file name). If null, not sent as an attachment.
      inStream - the input stream to read.
      length - the length of data in bytes to send.
    • sendData

      public static void sendData(javax.servlet.http.HttpServletResponse response, String mimeType, String attachmentFileName, String encoding, InputStream inStream, Long length)
      Sends the contents of a stream out through the response. The input stream is not closed after the data is sent.
      • The "Content-Type" portion of the header is set to mimeType, if not null.
      • The "Content-Encoding" portion of the header is set to encoding, if not null.
      • The "Content-Disposition" portion of the header is set to "attachment; filename=\"" + attachmentFileName + "\"", if not null.
      • The "Content-Length" portion of the header is set to length, if not null and positive.
      Parameters:
      response - servlet response object.
      mimeType - the MIME-Type of the stream. If null, no type is set.
      attachmentFileName - the name of the data to send (file name). If null, not sent as an attachment.
      encoding - if not null, adds a "Content-Encoding" header (not to be confused with charset - that should be set on the MIME-Type).
      inStream - the input stream to read.
      length - the length of data in bytes to send, or null to not specify (response may be chunked).
    • sendRedirect

      public static void sendRedirect(javax.servlet.http.HttpServletResponse response, String url)
      Forwards the client abruptly to another document or servlet (new client request).
      Parameters:
      response - servlet response object.
      url - the target URL.
      See Also:
      • HttpServletResponse.sendRedirect(String)
    • sendError

      public static void sendError(javax.servlet.http.HttpServletResponse response, int statusCode, String message)
      Sends request to the error page with a status code.
      Parameters:
      response - servlet response object.
      statusCode - the status code to use.
      message - the status message.
      See Also:
      • HttpServletResponse.sendError(int, String)