From 935cb6640381619e5ff4835b098f05e639a654c8 Mon Sep 17 00:00:00 2001 From: zontreck Date: Tue, 5 Dec 2023 12:13:52 -0700 Subject: [PATCH] Adds some HTTP Helpers to simplify things --- .../zontreck/ariaslib/http/HTTPMethod.java | 9 ++ .../zontreck/ariaslib/http/HTTPRequest.java | 21 +++ .../ariaslib/http/HTTPRequestBuilder.java | 134 ++++++++++++++++++ .../zontreck/ariaslib/http/HTTPResponse.java | 24 ++++ 4 files changed, 188 insertions(+) create mode 100644 src/main/java/dev/zontreck/ariaslib/http/HTTPMethod.java create mode 100644 src/main/java/dev/zontreck/ariaslib/http/HTTPRequest.java create mode 100644 src/main/java/dev/zontreck/ariaslib/http/HTTPRequestBuilder.java create mode 100644 src/main/java/dev/zontreck/ariaslib/http/HTTPResponse.java diff --git a/src/main/java/dev/zontreck/ariaslib/http/HTTPMethod.java b/src/main/java/dev/zontreck/ariaslib/http/HTTPMethod.java new file mode 100644 index 0000000..162477a --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/http/HTTPMethod.java @@ -0,0 +1,9 @@ +package dev.zontreck.ariaslib.http; + +public enum HTTPMethod +{ + GET, + POST, + PUT, + DELETE +} diff --git a/src/main/java/dev/zontreck/ariaslib/http/HTTPRequest.java b/src/main/java/dev/zontreck/ariaslib/http/HTTPRequest.java new file mode 100644 index 0000000..c7a80e4 --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/http/HTTPRequest.java @@ -0,0 +1,21 @@ +package dev.zontreck.ariaslib.http; + +import com.sun.istack.internal.NotNull; + +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class HTTPRequest +{ + + public String url; + + public String method; + public String body; + public String contentType; + + protected HTTPRequest(){ + + } +} diff --git a/src/main/java/dev/zontreck/ariaslib/http/HTTPRequestBuilder.java b/src/main/java/dev/zontreck/ariaslib/http/HTTPRequestBuilder.java new file mode 100644 index 0000000..c68c9b7 --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/http/HTTPRequestBuilder.java @@ -0,0 +1,134 @@ +package dev.zontreck.ariaslib.http; + +import com.sun.istack.internal.NotNull; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class HTTPRequestBuilder +{ + + private HttpURLConnection connection; + private URL url; + private HTTPRequest request = new HTTPRequest(); + + public static HTTPRequestBuilder builder() + { + return new HTTPRequestBuilder(); + } + + protected HTTPRequestBuilder() + { + + } + + /** + * Sets the url in this request to the one supplied + * @param url The url to connect to + * @return Builder instance + * @throws MalformedURLException If the URL supplied was invalid + */ + public HTTPRequestBuilder withURL(@NotNull String url) throws MalformedURLException { + request.url = url; + this.url = new URL(url); + + return this; + } + + /** + * Sets the HTTP Request method + * @param method The method you want to use + * @see HTTPMethod + * @return Builder instance + */ + public HTTPRequestBuilder withMethod(HTTPMethod method) + { + switch(method) + { + case GET: + { + request.method = "GET"; + break; + } + case POST: { + request.method = "POST"; + if(request.contentType.isEmpty()) request.contentType = "application/x-www-form-urlencoded"; + break; + } + case DELETE:{ + request.method = "DELETE"; + break; + } + case PUT:{ + request.method = "PUT"; + if(request.contentType.isEmpty()) request.contentType = "application/x-www-form-urlencoded"; + break; + } + } + + return this; + } + + /** + * Sets the request body. This may only be processed by the server when using POST or PUT, depending on the server's setup + * @param body The body to upload + * @return Builder Instance + */ + public HTTPRequestBuilder withBody(String body) + { + request.body = body; + return this; + } + + /** + * Sets the content type header + * Default: application/x-www-form-urlencoded for POST/PUT, and null/not present for GET + * @param type + * @return + */ + public HTTPRequestBuilder withContentType(String type) + { + request.contentType = type; + return this; + } + + public HTTPResponse build() + { + try { + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(request.method); + byte[] array = request.body.getBytes("UTF-8"); + connection.setRequestProperty("Content-Length" , "" + array.length); + connection.setRequestProperty("Content-Type", request.contentType); + connection.setDoOutput(true); + DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); + dos.write(array); + connection.setDoInput(true); + dos.flush(); + dos.close(); + + + // Get the response body + InputStream inputStream = connection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder response = new StringBuilder(); + String line; + + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + inputStream.close(); + + String responseBody = response.toString(); + + return new HTTPResponse(connection.getContentType(), connection.getResponseCode(), responseBody, request); + } catch (IOException e) { + throw new RuntimeException(e); + }finally { + connection.disconnect(); + } + } +} diff --git a/src/main/java/dev/zontreck/ariaslib/http/HTTPResponse.java b/src/main/java/dev/zontreck/ariaslib/http/HTTPResponse.java new file mode 100644 index 0000000..4444b81 --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/http/HTTPResponse.java @@ -0,0 +1,24 @@ +package dev.zontreck.ariaslib.http; + +public class HTTPResponse +{ + private String ContentType; + private int ResponseCode; + private String ResponseBody; + + protected HTTPResponse(String contentType, int code, String body, HTTPRequest request){ + + } + + public String getContentType() { + return ContentType; + } + + public int getResponseCode() { + return ResponseCode; + } + + public String getResponseBody() { + return ResponseBody; + } +}