Module-HTTPD/src/main/java/dev/plex/request/AbstractServlet.java

112 lines
3.8 KiB
Java
Raw Normal View History

2022-04-02 03:59:47 +00:00
package dev.plex.request;
import com.google.common.collect.Lists;
import dev.plex.HTTPDModule;
2022-04-02 20:51:14 +00:00
import dev.plex.logging.Log;
2022-04-02 03:59:47 +00:00
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
2022-04-17 21:39:08 +00:00
import java.io.BufferedReader;
2022-04-02 03:59:47 +00:00
import java.io.IOException;
2022-04-17 21:39:08 +00:00
import java.io.InputStream;
import java.io.InputStreamReader;
2022-04-02 03:59:47 +00:00
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
2022-04-17 21:39:08 +00:00
import java.util.Objects;
2022-04-02 04:14:12 +00:00
import lombok.Data;
import org.eclipse.jetty.servlet.ServletHolder;
2022-04-02 03:59:47 +00:00
2022-04-02 04:14:12 +00:00
public class AbstractServlet extends HttpServlet
{
2022-04-02 03:59:47 +00:00
private final List<Mapping> GET_MAPPINGS = Lists.newArrayList();
2022-04-02 04:14:12 +00:00
public AbstractServlet()
{
for (Method declaredMethod : this.getClass().getDeclaredMethods())
{
2022-04-02 03:59:47 +00:00
declaredMethod.setAccessible(true);
2022-04-02 04:14:12 +00:00
if (declaredMethod.isAnnotationPresent(GetMapping.class))
{
2022-04-02 03:59:47 +00:00
GetMapping getMapping = declaredMethod.getAnnotation(GetMapping.class);
Mapping mapping = new Mapping(declaredMethod, getMapping);
2022-04-02 04:14:12 +00:00
if (declaredMethod.isAnnotationPresent(MappingHeaders.class))
{
2022-04-02 03:59:47 +00:00
mapping.setHeaders(declaredMethod.getAnnotation(MappingHeaders.class));
}
GET_MAPPINGS.add(mapping);
ServletHolder holder = new ServletHolder(this);
HTTPDModule.context.addServlet(holder, getMapping.endpoint() + "*");
}
}
}
@Override
2022-04-02 04:14:12 +00:00
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
2022-04-02 23:34:19 +00:00
String ipAddress = req.getRemoteAddr();
2022-04-17 23:21:44 +00:00
if (ipAddress.equals("127.0.0.1"))
2022-04-02 04:14:12 +00:00
{
2022-04-02 23:34:19 +00:00
ipAddress = req.getHeader("X-FORWARDED-FOR");
2022-04-02 03:59:47 +00:00
}
2022-04-02 20:51:14 +00:00
Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue());
2022-04-02 03:59:47 +00:00
/*Enumeration<String> headerz = req.getHeaderNames();
while (headerz.hasMoreElements()) {
String header = headerz.nextElement();
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header));
}*/
2022-04-02 04:14:12 +00:00
GET_MAPPINGS.stream().filter(mapping -> mapping.getMapping().endpoint().substring(1, mapping.getMapping().endpoint().length() - 1).equalsIgnoreCase(req.getHttpServletMapping().getMatchValue())).forEach(mapping ->
{
if (mapping.headers != null)
{
for (String headers : mapping.headers.headers())
{
2022-04-02 03:59:47 +00:00
String header = headers.split(";")[0];
String value = headers.split(";")[1];
resp.addHeader(header, value);
}
}
resp.setStatus(HttpServletResponse.SC_OK);
2022-04-02 04:14:12 +00:00
try
{
2022-04-17 23:21:44 +00:00
Object object = mapping.method.invoke(this, req, resp);
if (object != null) resp.getWriter().println(object.toString());
2022-04-02 04:14:12 +00:00
}
catch (IOException | IllegalAccessException | InvocationTargetException e)
{
2022-04-02 03:59:47 +00:00
e.printStackTrace();
}
});
}
2022-04-17 21:39:08 +00:00
public String readFile(InputStream filename)
2022-04-15 18:55:40 +00:00
{
2022-04-17 21:39:08 +00:00
StringBuilder contentBuilder = new StringBuilder();
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(Objects.requireNonNull(filename)));
String str;
while ((str = in.readLine()) != null)
{
contentBuilder.append(str);
}
in.close();
}
catch (IOException ignored)
{
}
return contentBuilder.toString();
2022-04-15 18:55:40 +00:00
}
2022-04-02 03:59:47 +00:00
@Data
2022-04-02 20:51:14 +00:00
public static class Mapping
2022-04-02 04:14:12 +00:00
{
2022-04-02 03:59:47 +00:00
private final Method method;
private final GetMapping mapping;
private MappingHeaders headers;
}
}