|
4 | 4 | import org.javawebstack.abstractdata.mapper.naming.NamingPolicy; |
5 | 5 | import org.javawebstack.http.router.adapter.IHTTPSocketServer; |
6 | 6 | import org.javawebstack.http.router.handler.*; |
| 7 | +import org.javawebstack.http.router.multipart.content.PartContentCache; |
7 | 8 | import org.javawebstack.http.router.router.DefaultRouteAutoInjector; |
8 | 9 | import org.javawebstack.http.router.router.Route; |
9 | 10 | import org.javawebstack.http.router.router.RouteAutoInjector; |
@@ -43,6 +44,7 @@ public class HTTPRouter implements RouteParamTransformerProvider { |
43 | 44 | private final Map<String, AfterRequestHandler> afterMiddleware = new HashMap<>(); |
44 | 45 | private Function<Class<?>, Object> controllerInitiator = this::defaultControllerInitiator; |
45 | 46 | private boolean formMethods = true; |
| 47 | + private PartContentCache multipartContentCache; |
46 | 48 |
|
47 | 49 | public HTTPRouter(IHTTPSocketServer server) { |
48 | 50 | this.server = server; |
@@ -300,78 +302,86 @@ public void stop() { |
300 | 302 | public void execute(Exchange exchange) { |
301 | 303 | Exchange.exchanges.set(exchange); |
302 | 304 | try { |
303 | | - Object response = null; |
304 | 305 | try { |
305 | | - for (RequestInterceptor ic : beforeInterceptors) { |
306 | | - if (ic.intercept(exchange)) { |
307 | | - exchange.close(); |
308 | | - Exchange.exchanges.remove(); |
309 | | - return; |
310 | | - } |
311 | | - } |
312 | | - middlewares: |
313 | | - for (Route route : beforeRoutes) { |
314 | | - Map<String, Object> pathVariables = route.match(exchange); |
315 | | - if (pathVariables == null) |
316 | | - continue; |
317 | | - exchange.getPathVariables().putAll(pathVariables); |
318 | | - for (RequestHandler handler : route.getHandlers()) { |
319 | | - try { |
320 | | - response = handler.handle(exchange); |
321 | | - } catch (Throwable ex) { |
322 | | - response = exceptionHandler.handle(exchange, ex); |
| 306 | + if(multipartContentCache != null) |
| 307 | + exchange.enableMultipart(multipartContentCache); |
| 308 | + Object response = null; |
| 309 | + try { |
| 310 | + for (RequestInterceptor ic : beforeInterceptors) { |
| 311 | + if (ic.intercept(exchange)) { |
| 312 | + exchange.close(); |
| 313 | + Exchange.exchanges.remove(); |
| 314 | + return; |
323 | 315 | } |
324 | | - if (response != null) |
325 | | - break middlewares; |
326 | 316 | } |
327 | | - } |
328 | | - exchange.getPathVariables().clear(); |
329 | | - if (response == null) { |
330 | | - routes: |
331 | | - for (Route route : routes) { |
| 317 | + middlewares: |
| 318 | + for (Route route : beforeRoutes) { |
332 | 319 | Map<String, Object> pathVariables = route.match(exchange); |
333 | 320 | if (pathVariables == null) |
334 | 321 | continue; |
335 | 322 | exchange.getPathVariables().putAll(pathVariables); |
336 | 323 | for (RequestHandler handler : route.getHandlers()) { |
337 | | - response = handler.handle(exchange); |
338 | | - if (exchange.getMethod() == HTTPMethod.WEBSOCKET) { |
339 | | - Exchange.exchanges.remove(); |
340 | | - return; |
| 324 | + try { |
| 325 | + response = handler.handle(exchange); |
| 326 | + } catch (Throwable ex) { |
| 327 | + response = exceptionHandler.handle(exchange, ex); |
341 | 328 | } |
342 | 329 | if (response != null) |
343 | | - break routes; |
| 330 | + break middlewares; |
| 331 | + } |
| 332 | + } |
| 333 | + exchange.getPathVariables().clear(); |
| 334 | + if (response == null) { |
| 335 | + routes: |
| 336 | + for (Route route : routes) { |
| 337 | + Map<String, Object> pathVariables = route.match(exchange); |
| 338 | + if (pathVariables == null) |
| 339 | + continue; |
| 340 | + exchange.getPathVariables().putAll(pathVariables); |
| 341 | + for (RequestHandler handler : route.getHandlers()) { |
| 342 | + response = handler.handle(exchange); |
| 343 | + if (exchange.getMethod() == HTTPMethod.WEBSOCKET) { |
| 344 | + Exchange.exchanges.remove(); |
| 345 | + return; |
| 346 | + } |
| 347 | + if (response != null) |
| 348 | + break routes; |
| 349 | + } |
| 350 | + exchange.getPathVariables().clear(); |
344 | 351 | } |
345 | | - exchange.getPathVariables().clear(); |
346 | 352 | } |
| 353 | + } catch (Throwable ex) { |
| 354 | + response = exceptionHandler.handle(exchange, ex); |
347 | 355 | } |
348 | | - } catch (Throwable ex) { |
349 | | - response = exceptionHandler.handle(exchange, ex); |
350 | | - } |
351 | | - if (response == null) |
352 | | - response = notFoundHandler.handle(exchange); |
353 | | - exchange.getPathVariables().clear(); |
354 | | - for (Route route : afterRoutes) { |
355 | | - Map<String, Object> pathVariables = route.match(exchange); |
356 | | - if (pathVariables == null) |
357 | | - continue; |
358 | | - exchange.getPathVariables().putAll(pathVariables); |
359 | | - for (AfterRequestHandler handler : route.getAfterHandlers()) |
360 | | - response = handler.handleAfter(exchange, response); |
| 356 | + if (response == null) |
| 357 | + response = notFoundHandler.handle(exchange); |
361 | 358 | exchange.getPathVariables().clear(); |
| 359 | + for (Route route : afterRoutes) { |
| 360 | + Map<String, Object> pathVariables = route.match(exchange); |
| 361 | + if (pathVariables == null) |
| 362 | + continue; |
| 363 | + exchange.getPathVariables().putAll(pathVariables); |
| 364 | + for (AfterRequestHandler handler : route.getAfterHandlers()) |
| 365 | + response = handler.handleAfter(exchange, response); |
| 366 | + exchange.getPathVariables().clear(); |
| 367 | + } |
| 368 | + if (response != null) |
| 369 | + exchange.write(transformResponse(exchange, response)); |
| 370 | + if (exchange.getMethod() != HTTPMethod.WEBSOCKET) |
| 371 | + exchange.close(); |
| 372 | + Exchange.exchanges.remove(); |
| 373 | + return; |
| 374 | + } catch (Throwable ex) { |
| 375 | + try { |
| 376 | + exchange.write(transformResponse(exchange, exceptionHandler.handle(exchange, ex))); |
| 377 | + } catch (Throwable ex2) { |
| 378 | + exchange.status(500); |
| 379 | + logger.log(Level.SEVERE, ex2, () -> "An error occured in the exception handler!"); |
| 380 | + } |
362 | 381 | } |
363 | | - if (response != null) |
364 | | - exchange.write(transformResponse(exchange, response)); |
365 | | - if (exchange.getMethod() != HTTPMethod.WEBSOCKET) |
366 | | - exchange.close(); |
367 | | - Exchange.exchanges.remove(); |
368 | | - return; |
369 | | - } catch (Throwable ex) { |
370 | | - try { |
371 | | - exchange.write(transformResponse(exchange, exceptionHandler.handle(exchange, ex))); |
372 | | - } catch (Throwable ex2) { |
373 | | - logger.log(Level.SEVERE, ex2, () -> "An error occured in the exception handler!"); |
374 | | - } |
| 382 | + } catch (Exception ex) { |
| 383 | + // This should never be reached, just added this as a precaution |
| 384 | + logger.log(Level.SEVERE, ex, () -> "An unexpected error occured in the exception handling of the exception handler (probably while setting the status)"); |
375 | 385 | } |
376 | 386 | Exchange.exchanges.remove(); |
377 | 387 | exchange.close(); |
@@ -411,6 +421,11 @@ public ExceptionHandler getExceptionHandler() { |
411 | 421 | return exceptionHandler; |
412 | 422 | } |
413 | 423 |
|
| 424 | + public HTTPRouter enableMultipart(PartContentCache cache) { |
| 425 | + this.multipartContentCache = cache; |
| 426 | + return this; |
| 427 | + } |
| 428 | + |
414 | 429 | public boolean isFormMethods() { |
415 | 430 | return formMethods; |
416 | 431 | } |
|
0 commit comments