Separate out header-defined behavior and add in authentication functionality

main
sigonasr2, Sig, Sigo 3 years ago
parent 38238b589f
commit dfe1e58dce
  1. 20
      sig/client.java
  2. 42
      sig/requests/GETRequest.java
  3. 1
      test.html

@ -1,5 +1,7 @@
package sig; package sig;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import sig.exceptions.FailedResponseException; import sig.exceptions.FailedResponseException;
import sig.requests.GETRequest; import sig.requests.GETRequest;
@ -7,8 +9,26 @@ import sig.requests.GETRequest;
public class client { public class client {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
//Regular get request:
GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2"); GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
System.out.println(((HttpResponse<String>)res.run()).body()); System.out.println(((HttpResponse<String>)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<Path>)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<String>)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<String>)res.run()).body());
//Get request with authentication:
res = new GETRequest("https://postman-echo.com/basic-auth","postman","password",30000,null);
System.out.println(((HttpResponse<String>)res.run()).body());
} catch (FailedResponseException e) { } catch (FailedResponseException e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -1,11 +1,14 @@
package sig.requests; package sig.requests;
import java.io.IOException; import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Duration; import java.time.Duration;
@ -17,19 +20,30 @@ public class GETRequest{
String[] headers; String[] headers;
long timeout; long timeout;
Path file; Path file;
String user="";
String pass="";
private HttpRequest req; private HttpRequest req;
private HttpClient client; private HttpClient client;
/** /**
* @param file The file path info, use this for file downloads or set to null for standard text. * @param file The file path info, use this for file downloads or set to null for standard text.
* @param timeout in milliseconds * @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.url = url;
this.user=username;
this.pass=password;
this.headers = headers; this.headers = headers;
this.timeout = timeout; this.timeout = timeout;
this.file=file; this.file=file;
build(); 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 * @param timeout in milliseconds
* */ * */
@ -37,7 +51,7 @@ public class GETRequest{
this(url,timeout,null,headers); this(url,timeout,null,headers);
} }
public GETRequest(String url){ public GETRequest(String url){
this(url,30000,"default","default"); this(url,30000,null);
} }
public HttpResponse<?> run() throws FailedResponseException { public HttpResponse<?> run() throws FailedResponseException {
try { try {
@ -52,15 +66,27 @@ public class GETRequest{
throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!"); throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!");
} }
private void build(){ private void build(){
boolean AUTH_REQUIRED=user.length()>0&&pass.length()>0;
try { try {
req = HttpRequest.newBuilder(new URI(url)) java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url))
.version(HttpClient.Version.HTTP_2) .version(HttpClient.Version.HTTP_2)
.headers(headers)
.timeout(Duration.ofMillis(timeout)) .timeout(Duration.ofMillis(timeout))
.GET().build(); .GET();
client = HttpClient.newBuilder() if (headers!=null&&headers.length>0) {
.followRedirects(HttpClient.Redirect.ALWAYS) requestBuild.headers(headers);
.build(); }
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) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -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"}
Loading…
Cancel
Save