Class ResponseHelper

java.lang.Object
net.bjmsw.hda.vs.helper.ResponseHelper

public class ResponseHelper extends Object
Helper class for writing responses to the output stream of a socket.

This class provides methods to write the correct HTTP response headers and body to the output stream.

  • Constructor Details

    • ResponseHelper

      public ResponseHelper()
  • Method Details

    • writeRaw

      public static byte[] writeRaw(String text)
      Writes raw text to an OutputStream. Disregards any HTTP headers and writes the text as-is.
      Parameters:
      text - the text to write
      Throws:
      RuntimeException - if an error occurs while writing the text to the OutputStream
    • writePlainText

      public static byte[] writePlainText(String text)
    • respondWithFileFromFDF

      public static byte[] respondWithFileFromFDF(FormDataFile file)
      Responds to a client with a file from a FormDataFile object.
      Parameters:
      file - the FormDataFile object representing the file to be sent
      Throws:
      RuntimeException - if an error occurs while writing the response to the OutputStream
    • respondWithLocalFile

      public static byte[] respondWithLocalFile(String path) throws IOException
      Responds to a client with a local file from the webroot.
      Parameters:
      path - the path of the local file to be sent
      Throws:
      IOException - if an error occurs while reading the file or writing the response to the OutputStream
    • respondWithBHTML

      public static byte[] respondWithBHTML(String path, Map<String,String> variables)
      Responds to a client with a local file from the webroot.
      Parameters:
      path - the path of the local file to be sent
      variables - a map containing the variable name-value pairs
      Returns:
      the byte array representation of the response
    • redirect

      public static byte[] redirect(String newURL)
      Generates a HTTP redirect response with the specified URL.
      Parameters:
      newURL - the URL to redirect to
      Returns:
      the byte array representation of the HTTP redirect response
    • respondWithError

      public static byte[] respondWithError(ResponseHelper.ErrorCodes error)
      Responds to a client with an error message.
      Parameters:
      error - the error code
      Returns:
      the byte array representation of the response
    • respondWithError

      public static byte[] respondWithError(int code, String message)
      Responds to a client with an error message.
      Parameters:
      code - the HTTP status code of the error response
      message - the error message to be included in the response
      Returns:
      the byte array representation of the response
    • respondWithError

      public static byte[] respondWithError(ResponseHelper.ErrorCodes error, String message)
      Responds to a client with an error message.
      Parameters:
      error - the error code
      message - the error message to be included in the response
      Returns:
      the byte array representation of the response
    • respondWithJSON

      public static byte[] respondWithJSON(com.google.gson.JsonElement root)
      Responds to a client with a JSON response.
      Parameters:
      root - the JSON element to be included in the response
      Returns:
      the byte array representation of the response
    • respondWithJSON

      public static byte[] respondWithJSON(com.google.gson.JsonElement root, int statusCode, String statusMessage)
      Responds to a client with a JSON response. The response includes the specified status code and status message.
      Parameters:
      root - the JSON element to be included in the response
      statusCode - the HTTP status code of the response
      statusMessage - the status message to be included in the response
      Returns:
      the byte array representation of the response