LibHttpServer/src/dev/asdf00/lib/httpserver/HttpServerApiUtils.java
2023-10-12 13:30:23 +02:00

104 lines
3.6 KiB
Java

package dev.asdf00.lib.httpserver;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpPrincipal;
import dev.asdf00.lib.httpserver.exceptions.HttpHandlingException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class HttpServerApiUtils {
private static ClassLoader clsldr = HttpServerApiUtils.class.getClassLoader();
public static Stream patchFile(Map<String, String> patchMap, InputStream file) {
return new BufferedReader(new InputStreamReader(file)).lines().map(line -> {
for (String patch : patchMap.keySet()) {
String fullPatch = "${" + patch + "}";
line = line.replace(fullPatch, patchMap.get(patch));
}
return line;
});
}
public static InputStream readResource(Path path) throws HttpHandlingException {
InputStream res;
try {
res = clsldr.getResourceAsStream(path.toString());
} catch (InvalidPathException e) {
throw new HttpHandlingException(400);
}
if (res == null) {
throw new HttpHandlingException(404);
}
return res;
}
public static void printRequestInfo(HttpExchange exchange) {
System.out.println("\n-- headers --");
Headers requestHeaders = exchange.getRequestHeaders();
requestHeaders.entrySet().forEach(System.out::println);
System.out.println("-- principle --");
HttpPrincipal principal = exchange.getPrincipal();
System.out.println(principal);
System.out.println("-- HTTP method --");
String requestMethod = exchange.getRequestMethod();
System.out.println(requestMethod);
System.out.println("-- query --");
URI requestURI = exchange.getRequestURI();
String query = requestURI.getQuery();
System.out.println(query);
}
public static <T> T parseValue(Class<T> type, String value) {
if (String.class.equals(type)) {
return (T) value;
} else if (byte.class.equals(type)) {
return (T) Byte.valueOf(value);
} else if (short.class.equals(type)) {
return (T) Short.valueOf(value);
} else if (int.class.equals(type)) {
return (T) Integer.valueOf(value);
} else if (long.class.equals(type)) {
return (T) Long.valueOf(value);
} else if (float.class.equals(type)) {
return (T) Float.valueOf(value);
} else if (double.class.equals(type)) {
return (T) Double.valueOf(value);
} else if (boolean.class.equals(type)) {
return (T) Boolean.valueOf(value);
} else if (char.class.equals(type)) {
if (value.length() < 1) {
throw new IllegalArgumentException("cannon convert empty string to char");
}
return (T) Character.valueOf(value.charAt(0));
}
throw new IllegalArgumentException(type.getSimpleName() + " is not a primitive");
}
public static String readCookie(String key, List<String> cookies) {
String value = null;
outer:
for (var cookie : cookies) {
for (String s : cookie.split("; ")) {
if (s.startsWith(key + "=")) {
value = s.substring(key.length() + 1);
break outer;
}
}
}
return value;
}
}