22
33import org .javawebstack .abstractdata .mapper .Mapper ;
44import org .javawebstack .abstractdata .mapper .naming .NamingPolicy ;
5+ import org .javawebstack .commons .inject .Injector ;
56import org .javawebstack .http .router .adapter .IHTTPSocketServer ;
67import org .javawebstack .http .router .handler .*;
78import org .javawebstack .http .router .multipart .content .PartContentCache ;
2122import java .io .File ;
2223import java .nio .charset .StandardCharsets ;
2324import java .util .*;
24- import java .util .function .Function ;
2525import java .util .logging .Level ;
2626import java .util .logging .Logger ;
2727
@@ -42,9 +42,8 @@ public class HTTPRouter implements RouteParamTransformerProvider {
4242 private final List <RouteAutoInjector > routeAutoInjectors = new ArrayList <>();
4343 private final Map <String , RequestHandler > beforeMiddleware = new HashMap <>();
4444 private final Map <String , AfterRequestHandler > afterMiddleware = new HashMap <>();
45- private Function <Class <?>, Object > controllerInitiator = this ::defaultControllerInitiator ;
46- private boolean formMethods = true ;
47- private HTTPRoutingOptions routingOptions = new HTTPRoutingOptions ();
45+ private Injector injector ;
46+ private final HTTPRoutingOptions routingOptions = new HTTPRoutingOptions ();
4847 private PartContentCache multipartContentCache ;
4948
5049 public HTTPRouter (IHTTPSocketServer server ) {
@@ -77,11 +76,15 @@ public Logger getLogger() {
7776 }
7877
7978 public HTTPRouter beforeInterceptor (RequestInterceptor handler ) {
79+ if (injector != null )
80+ injector .inject (handler );
8081 beforeInterceptors .add (handler );
8182 return this ;
8283 }
8384
8485 public HTTPRouter routeAutoInjector (RouteAutoInjector injector ) {
86+ if (this .injector != null )
87+ this .injector .inject (injector );
8588 routeAutoInjectors .add (injector );
8689 return this ;
8790 }
@@ -143,6 +146,8 @@ public HTTPRouter staticResourceDirectory(String pathPrefix, ClassLoader classLo
143146 }
144147
145148 public HTTPRouter staticHandler (String pathPrefix , StaticFileHandler handler ) {
149+ if (injector != null )
150+ injector .inject (handler );
146151 return get (pathPrefix + (pathPrefix .endsWith ("/" ) ? "" : "/" ) + "{*:path}" , handler );
147152 }
148153
@@ -159,16 +164,31 @@ public HTTPRouter route(HTTPMethod method, String pattern, RequestHandler... han
159164 }
160165
161166 public HTTPRouter route (String name , HTTPMethod method , String pattern , RequestHandler ... handlers ) {
167+ if (injector != null ) {
168+ for (RequestHandler handler : handlers ) {
169+ injector .inject (handler );
170+ }
171+ }
162172 routes .add (new Route (this , method , pattern , routingOptions , Arrays .asList (handlers )).setName (name ));
163173 return this ;
164174 }
165175
166176 public HTTPRouter beforeRoute (HTTPMethod method , String pattern , RequestHandler ... handlers ) {
177+ if (injector != null ) {
178+ for (RequestHandler handler : handlers ) {
179+ injector .inject (handler );
180+ }
181+ }
167182 beforeRoutes .add (new Route (this , method , pattern , routingOptions , Arrays .asList (handlers )));
168183 return this ;
169184 }
170185
171186 public HTTPRouter afterRoute (HTTPMethod method , String pattern , AfterRequestHandler ... handlers ) {
187+ if (injector != null ) {
188+ for (AfterRequestHandler handler : handlers ) {
189+ injector .inject (handler );
190+ }
191+ }
172192 afterRoutes .add (new Route (this , method , pattern , routingOptions , null ).setAfterHandlers (Arrays .asList (handlers )));
173193 return this ;
174194 }
@@ -216,54 +236,62 @@ public HTTPRouter afterAny(String pattern, AfterRequestHandler... handlers) {
216236 }
217237
218238 public HTTPRouter webSocket (String pattern , WebSocketHandler handler ) {
239+ if (injector != null )
240+ injector .inject (handler );
219241 if (!server .isWebSocketSupported ())
220242 throw new UnsupportedOperationException (server .getClass ().getName () + " does not support websockets!" );
221243 return route (HTTPMethod .WEBSOCKET , pattern , new InternalWebSocketRequestHandler (handler ));
222244 }
223245
224246 public HTTPRouter middleware (String name , RequestHandler handler ) {
247+ if (injector != null )
248+ injector .inject (handler );
225249 beforeMiddleware .put (name , handler );
226250 return this ;
227251 }
228252
229253 public HTTPRouter middleware (String name , AfterRequestHandler handler ) {
254+ if (injector != null )
255+ injector .inject (handler );
230256 afterMiddleware .put (name , handler );
231257 return this ;
232258 }
233259
234260 public HTTPRouter notFound (RequestHandler handler ) {
261+ if (injector != null )
262+ injector .inject (handler );
235263 notFoundHandler = handler ;
236264 return this ;
237265 }
238266
239267 public HTTPRouter routeParamTransformer (RouteParamTransformer transformer ) {
268+ if (injector != null )
269+ injector .inject (transformer );
240270 routeParamTransformers .add (transformer );
241271 return this ;
242272 }
243273
244274 public HTTPRouter responseTransformer (ResponseTransformer transformer ) {
275+ if (injector != null )
276+ injector .inject (transformer );
245277 responseTransformers .add (transformer );
246278 return this ;
247279 }
248280
249281 public HTTPRouter exceptionHandler (ExceptionHandler handler ) {
282+ if (injector != null )
283+ injector .inject (handler );
250284 exceptionHandler = handler ;
251285 return this ;
252286 }
253287
254- private Object defaultControllerInitiator (Class <?> clazz ) {
255- try {
256- return clazz .newInstance ();
257- } catch (InstantiationException | IllegalAccessException e ) {
258- e .printStackTrace ();
259- }
260-
261- return null ;
288+ public HTTPRouter injector (Injector injector ) {
289+ this .injector = injector ;
290+ return this ;
262291 }
263292
264- public HTTPRouter controllerInitiator (Function <Class <?>, Object > initiator ) {
265- controllerInitiator = initiator ;
266- return this ;
293+ public Injector getInjector () {
294+ return injector ;
267295 }
268296
269297 public HTTPRouter controller (Class <?> parentClass , Package p ) {
@@ -274,7 +302,17 @@ public HTTPRouter controller(String globalPrefix, Class<?> parentClass, Package
274302 Reflections reflections = new Reflections (p .getName ());
275303 reflections .getSubTypesOf (parentClass )
276304 .stream ()
277- .map (controllerInitiator )
305+ .map (t -> {
306+ if (injector == null ) {
307+ try {
308+ return t .newInstance ();
309+ } catch (InstantiationException | IllegalAccessException e ) {
310+ throw new RuntimeException (e );
311+ }
312+ } else {
313+ return injector .getInstance (t );
314+ }
315+ })
278316 .forEach (c -> controller (globalPrefix , c ));
279317 return this ;
280318 }
@@ -284,6 +322,8 @@ public HTTPRouter controller(Object controller) {
284322 }
285323
286324 public HTTPRouter controller (String globalPrefix , Object controller ) {
325+ if (injector != null )
326+ injector .inject (controller );
287327 routeBinder .bind (globalPrefix , controller );
288328 return this ;
289329 }
0 commit comments