From 99d78675b1d237848b0b83b98cb2d80366a99439 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 13 Feb 2024 18:40:20 +0100 Subject: [PATCH] Created the Response.java --- .../java/de/gnmyt/mcdash/http/Response.java | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/main/java/de/gnmyt/mcdash/http/Response.java diff --git a/src/main/java/de/gnmyt/mcdash/http/Response.java b/src/main/java/de/gnmyt/mcdash/http/Response.java new file mode 100644 index 0000000..792c486 --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/http/Response.java @@ -0,0 +1,145 @@ +package de.gnmyt.mcdash.http; + +import java.util.HashMap; + +public class Response { + + private int code = 200; + private ContentType contentType = ContentType.TEXT; + private String output = ""; + private byte[] binaryOutput = null; + private HashMap headers = new HashMap<>(); + + /** + * Basic constructor of the response + */ + public Response() { + + } + + /** + * Advanced constructor with prefilled values + * @param code The response code + * @param contentType The response content type + * @param output The response output + * @param headers The response headers + */ + public Response(int code, ContentType contentType, String output, HashMap headers) { + this.code = code; + this.contentType = contentType; + this.output = output; + this.headers = headers; + } + + /** + * Gets the response code + * @return the response code + */ + public int getCode() { + return code; + } + + /** + * Gets the response output + * @return the response output + */ + public String getOutput() { + return output; + } + + /** + * Gets the binary response output + * @return the binary response output + */ + public byte[] getBinaryOutput() { + return binaryOutput; + } + + /** + * Gets the response content type + * @return the response content type + */ + public ContentType getContentType() { + return contentType; + } + + /** + * Gets the response headers + * @return the response headers + */ + public HashMap getHeaders() { + return headers; + } + + /** + * Sets the response code + * @param code The new response code + * @return the current {@link Response} instance + */ + public Response setCode(int code) { + this.code = code; + return this; + } + + /** + * Sets the response content type + * @param contentType The new response content type + * @return the current {@link Response} instance + */ + public Response setContentType(ContentType contentType) { + this.contentType = contentType; + return this; + } + + /** + * Sets the response output + * @param output The new response output + * @return the current {@link Response} instance + */ + public Response setOutput(String output) { + this.output = output; + return this; + } + + /** + * Sets the response headers + * @param headers The new response headers + * @return the current {@link Response} instance + */ + public Response setHeaders(HashMap headers) { + this.headers = headers; + return this; + } + + /** + * Sets the binary response output + * @param binaryOutput The new binary response output + * @return the current {@link Response} instance + */ + public Response setBinaryOutput(byte[] binaryOutput) { + this.binaryOutput = binaryOutput; + return this; + } + + /** + * Adds a header to the response + * @param key The key of the response header + * @param value The value of the response header + * @return the current {@link Response} instance + */ + public Response addHeader(String key, String value) { + headers.put(key, value); + return this; + } + + /** + * Adds a string to the output + * @param output The output you want to add + * @return the current {@link Response} instance + */ + public Response addOutput(String output) { + this.output += output; + return this; + } + +}