Skip to content

Instantly share code, notes, and snippets.

@Hopefuls
Created March 5, 2021 02:06
Show Gist options
  • Save Hopefuls/a5501b3888d50f8ef9b5aeb2c312ec0c to your computer and use it in GitHub Desktop.
Save Hopefuls/a5501b3888d50f8ef9b5aeb2c312ec0c to your computer and use it in GitHub Desktop.
Requesting Managers for HttpServer based things
package org.normies.pepegalog.utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Query {
private final String query;
public Query(String query) {
this.query = query;
}
public final Map<String, String> getQueryMap() {
Map<String, String> result = new HashMap<>();
Arrays.stream(this.query.split("&")).forEach(s -> {
String[] split2 = s.split("=");
try {
result.put(split2[0], split2[1]);
} catch (Exception ignored) {
// if some fucky-wucky happens, just add none none
result.put("none", "none");
}
});
return result;
}
public final String toString() {
return this.query;
}
}
package org.normies.pepegalog.utils;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class RequestManager {
private final HttpExchange exchange;
private final String requestString;
public RequestManager(HttpExchange exchange) {
this.exchange = exchange;
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(this.getRequestStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
br.lines().forEach(sb::append);
this.requestString = sb.toString();
}
public final InputStream getRequestStream() {
ArrayList<String> data = new ArrayList<>();
return this.exchange.getRequestBody();
}
public final String getString() {
return this.requestString;
}
public final JSONObject asJson() {
return new JSONObject(this.getString());
}
public final Headers getHeaders() {
return this.exchange.getRequestHeaders();
}
}
package org.normies.pepegalog.utils;
import com.sun.net.httpserver.HttpExchange;
import org.json.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
public class ResponseManager {
private final HttpExchange exchange;
private int responseCode = 200;
public ResponseManager(HttpExchange stream) {
this.exchange = stream;
}
public final OutputStream getResponseStream() {
return this.exchange.getResponseBody();
}
public final ResponseManager setResponseCode(int code) {
this.responseCode = code;
return this;
}
public final void writeResponse(String res) {
try {
// mmlol
this.exchange.getResponseHeaders().set("xignotic-stinky", "true");
// send length
this.exchange.sendResponseHeaders(this.responseCode, res.length());
// send response
this.getResponseStream().write(res.getBytes());
// close
this.getResponseStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
public final void flush(long size) {
try {
this.exchange.sendResponseHeaders(this.responseCode, size);
this.getResponseStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public final void writeResponse(JSONObject object) {
try {
// send length
this.exchange.sendResponseHeaders(this.responseCode, object.toString().length());
// send response
this.getResponseStream().write(object.toString().getBytes());
// close
this.getResponseStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Hopefuls
Copy link
Author

Hopefuls commented Mar 5, 2021

query

make more efficient if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment