Skip to content

Commit

Permalink
#98 Improved RequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkubiak committed Dec 23, 2015
1 parent df53fe0 commit de224a5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
9 changes: 9 additions & 0 deletions mangooio-core/src/main/documentation/changelog.asciidoc
Original file line number Diff line number Diff line change
@@ -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#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Class<?>> 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<String, Class<?>> 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();
Expand All @@ -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()
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -83,6 +82,9 @@ public class RequestHandler implements HttpHandler {
private Form form;
private Request request;
private Map<String, String> requestParameter;
private Crypto crypto;
private Messages messages;
private TemplateEngine templateEngine;
private boolean hasRequestFilter;
private boolean async;

Expand Down Expand Up @@ -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<String, Class<?>> 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;
}

Expand Down Expand Up @@ -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()));
Expand All @@ -191,7 +207,7 @@ private void setLocale(HttpServerExchange exchange) {
}
}

messages.reload();
this.messages.reload();
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit de224a5

Please sign in to comment.