diff --git a/mangooio-core/src/main/documentation/changelog.asciidoc b/mangooio-core/src/main/documentation/changelog.asciidoc index bde327e7c5..bf7f53bfb5 100644 --- a/mangooio-core/src/main/documentation/changelog.asciidoc +++ b/mangooio-core/src/main/documentation/changelog.asciidoc @@ -1,5 +1,14 @@ = Changelog +== Version 2.2.0 + +[small]#Released at xx.xx.xxxx# + +* https://github.com/svenkubiak/mangooio/issues/98[#98] Improved RequestHandler (svenkubiak) +* Updated documentation (svenkubiak) +* Version bumps (svenkubiak) +** undertow-core 1.3.10.Final -> 1.3.11.Final + == Version 2.1.0 [small]#Released at 21.12.2015# diff --git a/mangooio-core/src/main/java/io/mangoo/routing/handlers/DispatcherHandler.java b/mangooio-core/src/main/java/io/mangoo/routing/handlers/DispatcherHandler.java index 902dc46ba3..5bfc69250a 100644 --- a/mangooio-core/src/main/java/io/mangoo/routing/handlers/DispatcherHandler.java +++ b/mangooio-core/src/main/java/io/mangoo/routing/handlers/DispatcherHandler.java @@ -8,33 +8,43 @@ import io.mangoo.configuration.Config; import io.mangoo.core.Application; +import io.mangoo.crypto.Crypto; +import io.mangoo.i18n.Messages; import io.mangoo.interfaces.MangooRequestFilter; import io.mangoo.routing.listeners.MetricsListener; +import io.mangoo.templating.TemplateEngine; import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; /** + * Main class for dispatching a request to a new RequestHandler * * @author svenkubiak * */ -@SuppressWarnings("all") public class DispatcherHandler implements HttpHandler { - private Map> methodParameters; - private Class controllerInstance; - private Class controllerClass; - private Config config; - private String controllerClassName; - private String controllerMethodName; - private int methodParametersCount; - private boolean metrics; - private boolean async; - private boolean hasRequestFilter; + private final TemplateEngine templateEngine; + private final Messages messages; + private final Crypto crypto; + private final MetricsListener metricsListener; + private final Map> methodParameters; + private final Class controllerClass; + private final Config config; + private final String controllerClassName; + private final String controllerMethodName; + private final int methodParametersCount; + private final boolean metrics; + private final boolean async; + private final boolean hasRequestFilter; public DispatcherHandler(Class controllerClass, String controllerMethod, boolean async) { Objects.requireNonNull(controllerClass, "controllerClass can not be null"); Objects.requireNonNull(controllerMethod, "controllerMethod can not be null"); + this.templateEngine = Application.getInstance(TemplateEngine.class); + this.messages = Application.getInstance(Messages.class); + this.metricsListener = Application.getInstance(MetricsListener.class); + this.crypto = Application.getInstance(Crypto.class); this.controllerClass = controllerClass; this.controllerMethodName = controllerMethod; this.controllerClassName = controllerClass.getSimpleName(); @@ -49,7 +59,7 @@ public DispatcherHandler(Class controllerClass, String controllerMethod, bool @Override public void handleRequest(HttpServerExchange exchange) throws Exception { if (this.metrics) { - exchange.addResponseCommitListener(Application.getInstance(MetricsListener.class)); + exchange.addResponseCommitListener(this.metricsListener); } new RequestHandler() @@ -60,6 +70,9 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { .withMethodParameters(this.methodParameters) .withMethodParameterCount(this.methodParametersCount) .withRequestFilter(this.hasRequestFilter) + .withMessages(this.messages) + .withTemplateEngine(this.templateEngine) + .withCrypto(this.crypto) .withConfig(this.config) .withAsync(this.async) .handleRequest(exchange); diff --git a/mangooio-core/src/main/java/io/mangoo/routing/handlers/RequestHandler.java b/mangooio-core/src/main/java/io/mangoo/routing/handlers/RequestHandler.java index edb4eddc0a..29163d31ba 100644 --- a/mangooio-core/src/main/java/io/mangoo/routing/handlers/RequestHandler.java +++ b/mangooio-core/src/main/java/io/mangoo/routing/handlers/RequestHandler.java @@ -67,7 +67,6 @@ public class RequestHandler implements HttpHandler { private static final int INDEX_2 = 2; private static final int INDEX_3 = 3; private static final int SESSION_PREFIX_LENGTH = 4; - private final Crypto crypto = Application.getInstance(Crypto.class); private int methodParametersCount; private Class controllerClass; private String controllerMethodName; @@ -83,6 +82,9 @@ public class RequestHandler implements HttpHandler { private Form form; private Request request; private Map requestParameter; + private Crypto crypto; + private Messages messages; + private TemplateEngine templateEngine; private boolean hasRequestFilter; private boolean async; @@ -114,13 +116,28 @@ public RequestHandler withControllerInstance(Object controllerInstance) { return this; } - public RequestHandler withAsync(boolean async) { - this.async = async; + public RequestHandler withCrypto(Crypto crypto) { + this.crypto = Objects.requireNonNull(crypto, "crypto can no be null"); return this; } public RequestHandler withMethodParameters(Map> methodParameters) { - this.methodParameters = methodParameters; + this.methodParameters = Objects.requireNonNull(methodParameters, "methodParameters can no be null"); + return this; + } + + public RequestHandler withMessages(Messages messages) { + this.messages = Objects.requireNonNull(messages, "messages can no be null"); + return this; + } + + public RequestHandler withTemplateEngine(TemplateEngine templateEngine) { + this.templateEngine = Objects.requireNonNull(templateEngine, "templateEngine can no be null"); + return this; + } + + public RequestHandler withAsync(boolean async) { + this.async = async; return this; } @@ -176,7 +193,6 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { * @param exchange The Undertow HttpServerExchange */ private void setLocale(HttpServerExchange exchange) { - final Messages messages = Application.getInstance(Messages.class); final HeaderValues headerValues = exchange.getRequestHeaders().get(Headers.ACCEPT_LANGUAGE_STRING); if (headerValues == null) { Locale.setDefault(Locale.forLanguageTag(this.config.getApplicationLanguage())); @@ -191,7 +207,7 @@ private void setLocale(HttpServerExchange exchange) { } } - messages.reload(); + this.messages.reload(); } /** @@ -298,8 +314,7 @@ private Response invokeController(HttpServerExchange exchange, Response response invokedResponse.andContent(response.getContent()); invokedResponse.andHeaders(response.getHeaders()); if (!invokedResponse.isRendered()) { - final TemplateEngine templateEngine = Application.getInstance(TemplateEngine.class); - invokedResponse.andBody(templateEngine.render(this.flash, this.session, this.form, Application.getInstance(Messages.class), getTemplatePath(invokedResponse), invokedResponse.getContent())); + invokedResponse.andBody(this.templateEngine.render(this.flash, this.session, this.form, this.messages, getTemplatePath(invokedResponse), invokedResponse.getContent())); } return invokedResponse; @@ -478,10 +493,9 @@ private void setAuthenticationCookie(HttpServerExchange exchange) { final String authenticatedUser = this.authentication.getAuthenticatedUser(); final LocalDateTime expires = this.authentication.isRemember() ? LocalDateTime.now().plusSeconds(this.config.getAuthenticationRememberExpires()) : this.authentication.getExpires(); final String version = this.config.getAuthCookieVersion(); - final String sign = DigestUtils.sha512Hex(authenticatedUser + expires + version + this.config.getApplicationSecret()); final StringBuilder buffer = new StringBuilder() - .append(sign) + .append(DigestUtils.sha512Hex(authenticatedUser + expires + version + this.config.getApplicationSecret())) .append(Default.DELIMITER.toString()) .append(expires) .append(Default.DELIMITER.toString()) @@ -583,15 +597,15 @@ private void getForm(HttpServerExchange exchange) throws IOException { exchange.startBlocking(); final FormData formData = formDataParser.parseBlocking(); - for (final String data : formData) { - for (final FormData.FormValue formValue : formData.get(data)) { + formData.forEach(data -> { + formData.get(data).forEach(formValue -> { if (formValue.isFile()) { this.form.addFile(formValue.getPath().toFile()); } else { this.form.addValue(new HttpString(data).toString(), formValue.getValue()); } - } - } + }); + }); this.form.setSubmitted(true); }