-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,094 additions
and
1 deletion.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/me/alex4386/gachon/network/common/http/HttpRequest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package me.alex4386.gachon.network.common.http; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HttpRequest { | ||
// 귀찮으니까 학교 과제물 낸걸로 대충 때워 | ||
// Removed JSON Dependencies Edition | ||
|
||
HttpRequestMethod method; | ||
URL url; | ||
|
||
String body = null; | ||
Map<String, String> headers = new HashMap<>(); | ||
|
||
boolean enableDebug = false; | ||
|
||
public HttpRequest(HttpRequestMethod method, URL url) { | ||
this.method = method; | ||
this.url = url; | ||
} | ||
|
||
public HttpRequest(HttpRequestMethod method, URL url, String contentType, String body) { | ||
this.method = method; | ||
this.url = url; | ||
|
||
this.body = body; | ||
|
||
headers.put("Content-Type", contentType); | ||
} | ||
|
||
public void addHeaders(Map<String, String> headers) { | ||
for (Map.Entry<String, String> header : headers.entrySet()) { | ||
String key = header.getKey(); | ||
|
||
if (this.headers.containsKey(key)) { | ||
this.headers.remove(key); | ||
} | ||
|
||
this.headers.put(key, header.getValue()); | ||
} | ||
} | ||
|
||
public void setHeaders(Map<String, String> headers) { | ||
this.headers = headers; | ||
} | ||
|
||
public boolean isDebug() { | ||
return this.enableDebug; | ||
} | ||
|
||
public void setDebug(boolean debug) { | ||
this.enableDebug = debug; | ||
} | ||
|
||
public HttpResponse getResponse() throws IOException { | ||
HttpURLConnection conn = (HttpURLConnection) this.url.openConnection(); | ||
conn.setRequestMethod(this.method.toString()); | ||
|
||
for (Map.Entry<String, String> headerEntry : this.headers.entrySet()) { | ||
conn.setRequestProperty(headerEntry.getKey(), headerEntry.getValue()); | ||
} | ||
|
||
if (this.enableDebug) { | ||
System.out.println(this.method.toString()+" "+this.url.toString()); | ||
} | ||
|
||
if (body != null) { | ||
if (this.enableDebug) { | ||
System.out.println(body); | ||
} | ||
|
||
conn.setDoOutput(true); | ||
|
||
OutputStream outputStream = conn.getOutputStream(); | ||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); | ||
|
||
writer.write(body); | ||
|
||
writer.flush(); | ||
writer.close(); | ||
} | ||
|
||
conn.connect(); | ||
|
||
HttpResponse response = new HttpResponse(conn); | ||
|
||
return response; | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
src/main/java/me/alex4386/gachon/network/common/http/HttpRequestMethod.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.alex4386.gachon.network.common.http; | ||
|
||
public enum HttpRequestMethod { | ||
GET("GET"), | ||
HEAD("HEAD"), | ||
POST("POST"), | ||
PUT("PUT"), | ||
PATCH("PATCH"), | ||
DELETE("DELETE"), | ||
OPTIONS("OPTIONS"), | ||
TRACE("TRACE"); | ||
|
||
private String rawString; | ||
|
||
HttpRequestMethod(String raw) { | ||
this.rawString = raw; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.rawString; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/me/alex4386/gachon/network/common/http/HttpResponse.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.alex4386.gachon.network.common.http; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
|
||
public class HttpResponse { | ||
public HttpResponseCode code; | ||
public String response; | ||
|
||
boolean enableDebug = false; | ||
|
||
public HttpResponse(HttpURLConnection conn) throws IOException { | ||
int responseCode = conn.getResponseCode(); | ||
this.code = HttpResponseCode.getResponse(responseCode); | ||
|
||
BufferedReader bufReader; | ||
|
||
if (this.code.isOK()) { | ||
bufReader = new BufferedReader( | ||
new InputStreamReader( | ||
conn.getInputStream() | ||
) | ||
); | ||
} else { | ||
bufReader = new BufferedReader( | ||
new InputStreamReader( | ||
conn.getErrorStream() | ||
) | ||
); | ||
} | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
String thisLine; | ||
while ((thisLine = bufReader.readLine()) != null) { | ||
stringBuilder.append(thisLine); | ||
stringBuilder.append("\n"); | ||
} | ||
|
||
bufReader.close(); | ||
conn.disconnect(); | ||
|
||
this.response = stringBuilder.toString(); | ||
|
||
if (this.enableDebug) { | ||
System.out.println("Code: "+this.code.getCode()); | ||
System.out.println("Resp: \n"+this.response); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.response; | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/main/java/me/alex4386/gachon/network/common/http/HttpResponseCode.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package me.alex4386.gachon.network.common.http; | ||
|
||
public enum HttpResponseCode { | ||
/* Informational */ | ||
CONTINUE(100, "Continue"), | ||
SWITCHING_PROTOCOLS(101, "Switching Protocols"), | ||
PROCESSING(102, "Processing"), | ||
EARLY_HINTS(103, "Early Hints"), | ||
|
||
/* Success */ | ||
OK(200, "OK"), | ||
CREATED(201, "Created"), | ||
ACCEPTED(202, "Accepted"), | ||
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"), | ||
NO_CONTENT(204, "No Content"), | ||
RESET_CONTENT(205, "Reset Content"), | ||
PARTIAL_CONTENT(206, "Partial Content"), | ||
MULTI_STATUS(207, "Multi-Status"), | ||
ALREADY_REPORTED(208, "Already Reported"), | ||
IM_USED(226, "IM Used"), | ||
|
||
/* Redirection */ | ||
MULTIPLE_CHOICES(300, "Multiple Choices"), | ||
MOVED_PERMANENTLY(301, "Moved Permanently"), | ||
FOUND(302, "Found"), | ||
SEE_OTHER(303, "See Other"), | ||
NOT_MODIFIED(304, "Not Modified"), | ||
USE_PROXY(305, "Use Proxy"), | ||
TEMPORARY_REDIRECT(307, "Temporary Redirect"), | ||
PERMANENT_REDIRECT(308, "Permanent Redirect"), | ||
|
||
/* Client Error */ | ||
BAD_REQUEST(400, "Bad Request"), | ||
UNAUTHORIZED(401, "Unauthorized"), | ||
PAYMENT_REQUIRED(402, "Payment Required"), | ||
FORBIDDEN(403, "Forbidden"), | ||
NOT_FOUND(404, "Not Found"), | ||
METHOD_NOT_ALLOWED(405, "Method Not Allowed"), | ||
NOT_ACCEPTABLE(406, "Not Acceptable"), | ||
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"), | ||
REQUEST_TIMEOUT(408, "Request Timeout"), | ||
CONFLICT(409, "Conflict"), | ||
GONE(410, "Gone"), | ||
LENGTH_REQUIRED(411, "Length Required"), | ||
PRECONDITION_FAILED(412, "Precondition Failed"), | ||
PAYLOAD_TOO_LARGE(413, "Payload Too Large"), | ||
URI_TOO_LONG(414, "URI Too Long"), | ||
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), | ||
REQUEST_RANGE_NOT_SATISFIABLE(416, "Request Range Not Satisfiable"), | ||
EXPECTATION_FAILED(417, "Expectation Failed"), | ||
IM_A_TEAPOT(418, "I'm a teapot"), | ||
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"), | ||
LOCKED(423, "Precondition Failed"), | ||
FAILED_DEPENDENCY(424, "Precondition Failed"), | ||
TOO_EARLY(425, "Too Early"), | ||
UPGRADE_REQUIRED(426, "Upgrade Required"), | ||
PRECONDITION_REQUIRED(428, "Precondition Required"), | ||
TOO_MANY_REQUESTS(429, "Too Many Requests"), | ||
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"), | ||
@Deprecated | ||
NO_RESPONSE(444, "No Response"), | ||
@Deprecated | ||
RETRY_WITH(449, "Retry With"), | ||
@Deprecated | ||
BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS(450, "Blocked by Windows Parental Controls"), | ||
UNAVAILABLE_FOR_LEGAL_REASONS(451, "Precondition Failed"), | ||
@Deprecated | ||
CLIENT_CLOSED_REQUEST(499, "Client Closed Request"), | ||
|
||
/* Server Error */ | ||
INTERNAL_SERVER_ERROR(500, "Internal Server Error"), | ||
NOT_IMPLEMENTED(501, "Not Implemented"), | ||
BAD_GATEWAY(502, "Bad Gateway"), | ||
SERVICE_UNAVAILABLE(503, "Service Unavailable"), | ||
GATEWAY_TIMEOUT(504, "Gateway Timeout"), | ||
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"), | ||
VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"), | ||
INSUFFICIENT_STORAGE(507, "Insufficient Storage"), | ||
LOOP_DETECTED(508, "Loop Detected"), | ||
BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded"), | ||
NOT_EXTENDED(510, "Not Extended"), | ||
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required"); | ||
|
||
private int code; | ||
private String name; | ||
|
||
HttpResponseCode(int code, String name) { | ||
this.code = code; | ||
this.name = name; | ||
} | ||
|
||
public boolean isOK() { | ||
return code == 200; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return 200 <= code && code < 300; | ||
} | ||
|
||
public boolean isClientError() { | ||
return 400 <= code && code < 499; | ||
} | ||
|
||
public boolean isServerError() { | ||
return 500 <= code && code < 599; | ||
} | ||
|
||
public int getCode() { | ||
return this.code; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
|
||
public static HttpResponseCode getResponse(int code) { | ||
for (HttpResponseCode respCode : HttpResponseCode.values()) { | ||
if (code == respCode.code) { | ||
return respCode; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.alex4386.gachon.sw14462.day24; | ||
|
||
import me.alex4386.gachon.sw14462.utils.Chainloader; | ||
|
||
public class Main { | ||
public static String chainloadTarget = "ex13_9"; | ||
|
||
public static void main(String[] args) throws Throwable { | ||
String packageName = Main.class.getPackage().getName(); | ||
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; | ||
|
||
try { | ||
Chainloader.chainloadTarget(chainLoadTargetClass, args); | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/me/alex4386/gachon/sw14462/day24/ex13_2/Main.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package me.alex4386.gachon.sw14462.day24.ex13_2; | ||
|
||
import java.io.File; | ||
import java.io.PrintWriter; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
File in = new File("in"); | ||
File out = new File("out"); | ||
|
||
if (!in.exists()) { | ||
System.err.println("File named 'in' not found"); | ||
System.exit(1); | ||
} | ||
|
||
if (out.exists()) { | ||
out.delete(); | ||
} | ||
|
||
try { | ||
out.createNewFile(); | ||
} catch (Exception e) { | ||
System.err.println("Failed to create file: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
// assume that the numbers in the input file are already ordered from smallest to largest: | ||
// which means the number came beforehand is same with current number, it is a duplicate. | ||
|
||
// read the file | ||
int prevInt = 0; | ||
try { | ||
Scanner scanner = new Scanner(in); | ||
PrintWriter writer = new PrintWriter(out); | ||
|
||
boolean isFirst = true; | ||
|
||
while (scanner.hasNextInt()) { | ||
int currentInt = scanner.nextInt(); | ||
|
||
if (isFirst) { | ||
isFirst = false; | ||
prevInt = currentInt; | ||
writer.println(currentInt); | ||
} else { | ||
if (prevInt != currentInt) { | ||
writer.println(currentInt); | ||
prevInt = currentInt; | ||
} | ||
} | ||
} | ||
|
||
scanner.close(); | ||
writer.close(); | ||
} catch (Exception e) { | ||
System.err.println("Failed to read/write file: " + e.getMessage()); | ||
return; | ||
} | ||
} | ||
} |
Oops, something went wrong.