Package net.bjmsw.hda.vs.helper
Class ResponseHelper
java.lang.Object
net.bjmsw.hda.vs.helper.ResponseHelper
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRepresents various error codes with their corresponding HTTP status codes and messages. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Generates a HTTP redirect response with the specified URL.static byte[]respondWithBHTML(String path, Map<String, String> variables) Responds to a client with a local file from the webroot.static byte[]respondWithError(int code, String message) Responds to a client with an error message.static byte[]Responds to a client with an error message.static byte[]respondWithError(ResponseHelper.ErrorCodes error, String message) Responds to a client with an error message.static byte[]Responds to a client with a file from a FormDataFile object.static byte[]respondWithJSON(com.google.gson.JsonElement root) Responds to a client with a JSON response.static byte[]respondWithJSON(com.google.gson.JsonElement root, int statusCode, String statusMessage) Responds to a client with a JSON response.static byte[]respondWithLocalFile(String path) Responds to a client with a local file from the webroot.static byte[]writePlainText(String text) static byte[]Writes raw text to an OutputStream.
-
Constructor Details
-
ResponseHelper
public ResponseHelper()
-
-
Method Details
-
writeRaw
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
-
respondWithFileFromFDF
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
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
Responds to a client with a local file from the webroot.- Parameters:
path- the path of the local file to be sentvariables- a map containing the variable name-value pairs- Returns:
- the byte array representation of the response
-
redirect
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
Responds to a client with an error message.- Parameters:
error- the error code- Returns:
- the byte array representation of the response
-
respondWithError
Responds to a client with an error message.- Parameters:
code- the HTTP status code of the error responsemessage- the error message to be included in the response- Returns:
- the byte array representation of the response
-
respondWithError
Responds to a client with an error message.- Parameters:
error- the error codemessage- 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 responsestatusCode- the HTTP status code of the responsestatusMessage- the status message to be included in the response- Returns:
- the byte array representation of the response
-