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;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.List;
|
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 03:59:47 +00:00
|
|
|
String ipAddress = req.getHeader("X-FORWARDED-FOR");
|
2022-04-02 04:14:12 +00:00
|
|
|
if (ipAddress == null)
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
ipAddress = req.getRemoteAddr();
|
|
|
|
}
|
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-02 03:59:47 +00:00
|
|
|
Object object = mapping.method.invoke(this, req);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|