-
-
Save orangit/3662dccb55e57182769291c146d8652e to your computer and use it in GitHub Desktop.
OrangIT Java code review assignment, thanks to Wunderdog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fi.orangit.code-bananas; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
import java.net.http.HttpRequest.BodyPublisher; | |
import java.net.http.HttpRequest.BodyPublishers; | |
import java.net.http.HttpRequest.Builder; | |
import java.net.http.HttpResponse.BodyHandlers; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.swing.text.StyledEditorKit.BoldAction; | |
class HelloWorld { | |
/** | |
* Says hello to the world. | |
* | |
* @param args The arguments of the program. | |
*/ | |
public static void main(String[] args) { | |
// convert array into a list for ease of use! | |
List argsList = new ArrayList(); | |
for (int i = 0; i < args.length; i++) { | |
argsList.set(i, args[i]); | |
} | |
try { | |
Churl churl = new Churl(argsList, true); | |
Churl.run(churl.args); | |
} catch (Exception error) { | |
System.out.print("ERROR!!!!!"); | |
} | |
} | |
} | |
/** | |
* Churl is a CLI HTTP client like curl but made with Java 11!!! | |
*/ | |
class Churl { | |
public Args args; | |
Churl(List argsList, boolean verifyUri) throws Exception { | |
// parse uri arg! | |
String uriArg = parseUriArg(argsList); | |
// parse json arg! | |
String jsonArg = parseUriArg(argsList); | |
// parse download arg! | |
String downloadArg = parseDownloadArg(argsList); | |
// parse method arg! | |
String methodArg = parseMethodArg(argsList); | |
// parse method arg! | |
String bodyArg = parseBodyArg(argsList); | |
/* | |
if (verifyUri) { | |
try { | |
new URI(uriArg); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
*/ | |
args = new Args() { | |
{ | |
method = methodArg == null ? "GET" : methodArg == "" ? "GET" : methodArg; | |
uri = uriArg; | |
json = jsonArg != null; | |
download = downloadArg != null; | |
body = bodyArg; | |
} | |
}; | |
} | |
public static void run(Args args) throws Exception { | |
HttpClient httpClient = HttpClient.newHttpClient(); | |
Builder httpRequestBuilder = HttpRequest.newBuilder(); | |
httpRequestBuilder.uri(URI.create(args.uri)); | |
if (args.json == true) { | |
httpRequestBuilder.header("Contentype", "text/x-json"); | |
} | |
// GET is default! | |
if (args.method == "get" || args.method == null) { | |
httpRequestBuilder.GET(); | |
} | |
if (args.method == "post") { | |
BodyPublisher stringBodyPublisher = BodyPublishers.ofString(args.body); | |
httpRequestBuilder.POST(stringBodyPublisher); | |
} | |
HttpRequest httpRequest = httpRequestBuilder.build(); | |
HttpResponse httpResponse = httpClient.send(httpRequest, BodyHandlers.ofString()); | |
if (args.download) { | |
Path path = Paths.get(".", "downloded-file.txt"); | |
byte[] bytes = ((String) httpResponse.body()).getBytes(); | |
Files.write(path, bytes); | |
} else { | |
System.out.print(httpResponse.body()); | |
} | |
} | |
static protected String parseUriArg(List argsList) { | |
Object[] uriArgList = argsList.stream().filter((arg) -> { | |
try { | |
URI.create((String) arg); | |
return true; | |
} catch (Throwable err) { | |
return false; | |
} | |
}).toArray(); | |
return (String) uriArgList[0]; | |
} | |
static protected String parseJsonArg(List argsList) { | |
Object[] jsonArgList = argsList.stream().filter((arg) -> { | |
return arg == "--json"; | |
}).toArray(); | |
return (String) jsonArgList[0]; | |
} | |
static protected String parseDownloadArg(List argsList) { | |
Object[] downloadArgList = argsList.stream().filter((arg) -> { | |
return arg == "--download"; | |
}).toArray(); | |
return (String) downloadArgList[0]; | |
} | |
static protected String parseMethodArg(List argsList) { | |
Object[] jsonArgList = argsList.stream().filter((arg) -> { | |
return arg == "GET" || arg == "POST"; | |
}).toArray(); | |
return (String) jsonArgList[0]; | |
} | |
static protected String parseBodyArg(List argsList) { | |
Object[] jsonArgList = argsList.stream().filter((arg) -> { | |
String argString = (String) arg; | |
return argString.startsWith("--body="); | |
}).toArray(); | |
String bodyArg = (String) jsonArgList[0]; | |
String bodyString = bodyArg.replace("--body=", ""); | |
return (String) bodyString; | |
} | |
class Args { | |
public String method; | |
public String uri; | |
public String cookies; | |
public String body; | |
public boolean json; | |
public boolean download; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment