From dfe1e58dce6ea7e2251d0d7eaa836fe3ec04c132 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Tue, 26 Apr 2022 19:42:12 +0000 Subject: [PATCH] Separate out header-defined behavior and add in authentication functionality --- sig/client.java | 20 +++++++++++++++++ sig/requests/GETRequest.java | 42 +++++++++++++++++++++++++++++------- test.html | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 test.html diff --git a/sig/client.java b/sig/client.java index 94bc39e..5c1c691 100644 --- a/sig/client.java +++ b/sig/client.java @@ -1,5 +1,7 @@ package sig; import java.net.http.HttpResponse; +import java.nio.file.Path; +import java.nio.file.Paths; import sig.exceptions.FailedResponseException; import sig.requests.GETRequest; @@ -7,8 +9,26 @@ import sig.requests.GETRequest; public class client { public static void main(String[] args) { try { + + //Regular get request: GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2"); System.out.println(((HttpResponse)res.run()).body()); + + //Download to file: + //GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2",30000,Paths.get("test.html"),"default","value"); + //System.out.println((Path)((HttpResponse)res.run()).body()); + + //Get request with headers: + res = new GETRequest("https://postman-echo.com/headers",30000,"test-header1","value1","test-header2","value2"); + System.out.println(((HttpResponse)res.run()).body()); + + //Get request with headers: + res = new GETRequest("https://postman-echo.com/response-headers?foo1=bar1&foo2=bar2",30000,"test-header1","value1","test-header2","value2"); + System.out.println(((HttpResponse)res.run()).body()); + + //Get request with authentication: + res = new GETRequest("https://postman-echo.com/basic-auth","postman","password",30000,null); + System.out.println(((HttpResponse)res.run()).body()); } catch (FailedResponseException e) { e.printStackTrace(); } diff --git a/sig/requests/GETRequest.java b/sig/requests/GETRequest.java index 8791e25..05793c4 100644 --- a/sig/requests/GETRequest.java +++ b/sig/requests/GETRequest.java @@ -1,11 +1,14 @@ package sig.requests; import java.io.IOException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.net.http.HttpClient.Builder; import java.net.http.HttpResponse.BodyHandlers; import java.nio.file.Path; import java.time.Duration; @@ -17,19 +20,30 @@ public class GETRequest{ String[] headers; long timeout; Path file; + String user=""; + String pass=""; private HttpRequest req; private HttpClient client; /** * @param file The file path info, use this for file downloads or set to null for standard text. * @param timeout in milliseconds * */ - public GETRequest(String url, long timeout, Path file, String...headers){ + public GETRequest(String url, String username, String password, long timeout, Path file, String...headers){ this.url = url; + this.user=username; + this.pass=password; this.headers = headers; this.timeout = timeout; this.file=file; build(); } + /** + * @param file The file path info, use this for file downloads or set to null for standard text. + * @param timeout in milliseconds + * */ + public GETRequest(String url, long timeout, Path file, String...headers){ + this(url,"","",timeout,file,headers); + } /** * @param timeout in milliseconds * */ @@ -37,7 +51,7 @@ public class GETRequest{ this(url,timeout,null,headers); } public GETRequest(String url){ - this(url,30000,"default","default"); + this(url,30000,null); } public HttpResponse run() throws FailedResponseException { try { @@ -52,15 +66,27 @@ public class GETRequest{ throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!"); } private void build(){ + boolean AUTH_REQUIRED=user.length()>0&&pass.length()>0; try { - req = HttpRequest.newBuilder(new URI(url)) + java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url)) .version(HttpClient.Version.HTTP_2) - .headers(headers) .timeout(Duration.ofMillis(timeout)) - .GET().build(); - client = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .build(); + .GET(); + if (headers!=null&&headers.length>0) { + requestBuild.headers(headers); + } + req = requestBuild.build(); + Builder clientBuild=HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS); + if (AUTH_REQUIRED) { + clientBuild.authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(user,pass.toCharArray()); + } + }); + } + client = clientBuild.build(); } catch (URISyntaxException e) { e.printStackTrace(); } diff --git a/test.html b/test.html new file mode 100644 index 0000000..02344bf --- /dev/null +++ b/test.html @@ -0,0 +1 @@ +{"args":{"foo1":"bar1","foo2":"bar2"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6268427f-1fb34bbf61bee0cc55bcfced","default":"value","user-agent":"Java-http-client/11.0.13"},"url":"https://postman-echo.com/get?foo1=bar1&foo2=bar2"} \ No newline at end of file