Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<buildVersion>1.0.1-SNAPSHOT</buildVersion>
<buildVersion>1.0.2-SNAPSHOT</buildVersion>
</properties>

<groupId>org.javawebstack</groupId>
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/org/javawebstack/httpserver/HTTPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -52,6 +52,7 @@ public class HTTPServer implements RouteParamTransformerProvider {
private List<RouteAutoInjector> routeAutoInjectors = new ArrayList<>();
private final Map<String, RequestHandler> beforeMiddleware = new HashMap<>();
private final Map<String, AfterRequestHandler> afterMiddleware = new HashMap<>();
private Function<Class<?>, Object> controllerInitiator = this::defaultControllerInitiator;

public HTTPServer() {
routeParamTransformers.add(DefaultRouteParamTransformer.INSTANCE);
Expand Down Expand Up @@ -233,19 +234,31 @@ public HTTPServer exceptionHandler(ExceptionHandler handler) {
return this;
}

private Object defaultControllerInitiator (Class<?> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}

return null;
}

public HTTPServer controllerInitiator (Function<Class<?>, Object> initiator) {
controllerInitiator = initiator;
return this;
}

public HTTPServer controller(Class<?> parentClass, Package p) {
return controller("", parentClass, p);
}

public HTTPServer controller(String globalPrefix, Class<?> parentClass, Package p) {
Reflections reflections = new Reflections(p.getName());
reflections.getSubTypesOf(parentClass).forEach(c -> {
try {
Object controller = c.newInstance();
controller(globalPrefix, controller);
} catch (InstantiationException | IllegalAccessException e) {
}
});
reflections.getSubTypesOf(parentClass)
.stream()
.map(controllerInitiator)
.forEach(c -> controller(globalPrefix, c));
return this;
}

Expand Down