Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale Handler #126

Merged
merged 15 commits into from Jan 25, 2016
10 changes: 9 additions & 1 deletion mangooio-core/src/main/java/io/mangoo/configuration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Main configuration class for all properties configured in application.yaml
*
* @author svenkubiak
* @author William Dunne
* @author williamdunne
*
*/
@Singleton
Expand Down Expand Up @@ -402,6 +402,14 @@ public boolean isAuthenticationCookieSecure() {
return getBoolean(Key.AUTH_COOKIE_SECURE, Default.AUTH_COOKIE_SECURE.toBoolean());
}

/**
* @return i18n.cookie.name from application.yaml or default value if undefined
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be application.i18n.cookie.name, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @author William Dunne
*/
public String getLocaleCookieName() {
return getString(Key.LOCALE_COOKIE_NAME, Default.LOCALE_COOKIE_NAME.toString());
}

/**
* @return same value as isSessionCookieSecure()
*/
Expand Down
128 changes: 65 additions & 63 deletions mangooio-core/src/main/java/io/mangoo/enums/Default.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,88 @@
* Default application values
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you re-order the Default enums? Makes it hard so seet, what was changed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can undo it, but there seemed to be no particularly logical order. If you want I can put them back

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to only see, what you have changed.

*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind reverting your changes and only commiting a new file with your new changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @author svenkubiak
* @author williamdunne
*
*/
public enum Default {
LANGUAGE("en"),
DATA_DELIMITER("#"),
DELIMITER("|"),
FLASH_COOKIE_NAME("MANGOOIO-FLASH"),
AUTH_COOKIE_NAME("MANGOOIO-AUTH"),
COOKIE_EXPIRES("86400"),
LOCALHOST("127.0.0.1"), //NOSONAR
APPLICATION_ADMIN_CACHE("false"),
APPLICATION_ADMIN_CONFIG("false"),
APPLICATION_ADMIN_HEALTH("false"),
APPLICATION_ADMIN_MEMORY("false"),
APPLICATION_ADMIN_METRICS("false"),
APPLICATION_ADMIN_ROUTES("false"),
APPLICATION_ADMIN_SCHEDULER("false"),
APPLICATION_ADMIN_SYSTEM("false"),
APPLICATION_CONTROLLER("controllers."),
APPLICATION_HOST("127.0.0.1"), //NOSONAR
JBCRYPT_ROUNDS("12"),
APPLICATION_PORT("8080"),
BUNDLE_NAME("translations/messages"),
ASSETS_PATH("src/main/resources/files/assets/"),
CONFIG_PATH("/src/main/resources/application.yaml"),
STYLESHEET_FOLDER("stylesheets"),
JAVSCRIPT_FOLDER("javascripts"),
CONFIGURATION_FILE("application.yaml"),
DEFAULT_CONFIGURATION("default"),
VERSION_PROPERTIES("version.properties"),
LOGO_FILE("logo.txt"),
CONTENT_TYPE("text/html; charset=UTF-8"),
SCHEDULER_PREFIX("org.quartz."),
APPLICATION_SECRET_MIN_LENGTH("16"),
SERVER("Undertow"),
CACHE_NAME("mangooio"),
TEMPLATES_FOLDER("/templates/"),
TEMPLATE_SUFFIX(".ftl"),
AUTH_COOKIE_EXPIRES("3600"),
COOKIE_NAME("MANGOOIO-SESSION"),
SPLITTER("&"),
SEPERATOR(":"),
NOSNIFF("nosniff"),
SAMEORIGIN("SAMEORIGIN"),
FILTER_METHOD("execute"),
APPLICATION_TIMER("false"),
ASSETS_PATH("src/main/resources/files/assets/"),
AUTHENTICATION("@authentication"),
AUTHENTICITY_TOKEN("authenticityToken"),
XSS_PROTECTION("1"),
FILES_FOLDER("files"),
MODULE_CLASS("conf.Module"),
VERSION("unknown"),
LOGBACK_PROD_FILE("logback.prod.xml"),
NUMBER_FORMAT("0.######"),
EXCEPTION_TEMPLATE_NAME("exception.ftl"),
DEFAULT_TEMPLATES_DIR("/templates/defaults/"),
X_XSS_PPROTECTION("1"),
COOKIE_SECURE("false"), //NOSONAR
AUTH_COOKIE_ENCRYPT("false"),
AUTH_COOKIE_EXPIRES("3600"),
AUTH_COOKIE_NAME("MANGOOIO-AUTH"),
AUTH_COOKIE_REMEMBER_EXPIRES("1209600"),
AUTH_COOKIE_SECURE("false"), //NOSONAR
CACHE_MAX_SIZE("5000"),
AUTH_COOKIE_VERSION("0"),
BASICAUTH_CREDENTIALS_LENGTH("2"),
BLOCKING("@blocking"),
BUNDLE_NAME("translations/messages"),
CACHE_CLASS("io.mangoo.cache.Cache"),
CACHE_EXPIRES("afterAccess"),
CACHE_EXPIRES_ACCESS("3600"),
CACHE_EXPIRES_WRITE("3600"),
APPLICATION_ADMIN_HEALTH("false"),
APPLICATION_ADMIN_CACHE("false"),
APPLICATION_ADMIN_CONFIG("false"),
APPLICATION_ADMIN_ROUTES("false"),
APPLICATION_ADMIN_SYSTEM("false"),
APPLICATION_ADMIN_METRICS("false"),
APPLICATION_ADMIN_MEMORY("false"),
APPLICATION_ADMIN_SCHEDULER("false"),
APPLICATION_TIMER("false"),
AUTH_COOKIE_ENCRYPT("false"),
AUTH_COOKIE_VERSION("0"),
AUTH_COOKIE_REMEMBER_EXPIRES("1209600"),
CACHE_MAX_SIZE("5000"),
CACHE_NAME("mangooio"),
CONFIGURATION_FILE("application.yaml"),
CONFIG_PATH("/src/main/resources/application.yaml"),
CONTENT_TYPE("text/html; charset=UTF-8"),
COOKIE_ENCRYPTION("false"),
COOKIE_EXPIRES("86400"),
COOKIE_NAME("MANGOOIO-SESSION"),
COOKIE_SECURE("false"), //NOSONAR
COOKIE_VERSION("0"),
DATA_DELIMITER("#"),
DEFAULT_CONFIGURATION("default"),
DEFAULT_TEMPLATES_DIR("/templates/defaults/"),
DELIMITER("|"),
EXCEPTION_TEMPLATE_NAME("exception.ftl"),
EXECUTION_THREADPOOL("10"),
FILES_FOLDER("files"),
FILTER_METHOD("execute"),
FLASH_COOKIE_NAME("MANGOOIO-FLASH"),
JAVSCRIPT_FOLDER("javascripts"),
JBCRYPT_ROUNDS("12"),
LANGUAGE("en"),
LOCALE_COOKIE_NAME("lang"),
LOCALHOST("127.0.0.1"), //NOSONAR
LOGBACK_PROD_FILE("logback.prod.xml"),
LOGO_FILE("logo.txt"),
MODULE_CLASS("conf.Module"),
NOSNIFF("nosniff"),
NUMBER_FORMAT("0.######"),
OAUTH_REQUEST_PARAMETER("oauth"),
ROUTES_FILE("routes.yaml"),
SAMEORIGIN("SAMEORIGIN"),
SCHEDULER_AUTOSTART("true"),
SCHEDULER_JOB_GROUP("MangooSchedulerJobGroup"),
SCHEDULER_TRIGGER_GROUP("MangooSchedulerTriggerGroup"),
BASICAUTH_CREDENTIALS_LENGTH("2"),
OAUTH_REQUEST_PARAMETER("oauth"),
SCHEDULER_PACKAGE("jobs"),
COOKIE_ENCRYPTION("false"),
EXECUTION_THREADPOOL("10"),
ROUTES_FILE("routes.yaml"),
APPLICATION_CONTROLLER("controllers."),
SCHEDULER_PREFIX("org.quartz."),
SCHEDULER_TRIGGER_GROUP("MangooSchedulerTriggerGroup"),
SEPERATOR(":"),
SERVER("Undertow"),
SPLITTER("&"),
SSE_CACHE_PREFIX("MANGOOIO-SSE-"),
STYLESHEET_FOLDER("stylesheets"),
TEMPLATES_FOLDER("/templates/"),
TEMPLATE_SUFFIX(".ftl"),
VERSION("unknown"),
VERSION_PROPERTIES("version.properties"),
WSS_CACHE_PREFIX("MANGOOIO-WSS-"),
AUTHENTICATION("@authentication"),
BLOCKING("@blocking"),
CACHE_CLASS("io.mangoo.cache.Cache");
XSS_PROTECTION("1"),
X_XSS_PPROTECTION("1");

private final String value;

Expand Down
1 change: 1 addition & 0 deletions mangooio-core/src/main/java/io/mangoo/enums/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum Key {
COOKIE_VERSION("cookie.version"),
ERROR("error"),
WARNING("warning"),
LOCALE_COOKIE_NAME("i18n.cookie.name"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would imho be nicer to have this under application and not open a new subtree with i18n with atm only one entry.

SUCCESS("success"),
VALIDATION_REQUIRED("validation.required"),
VALIDATION_MIN("validation.min"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Locale;

import io.undertow.server.handlers.Cookie;
import org.apache.commons.lang3.StringUtils;

import io.mangoo.configuration.Config;
Expand All @@ -17,6 +18,7 @@
/**
*
* @author svenkubiak
* @author williamdunne
*
*/
public class LocaleHandler implements HttpHandler {
Expand All @@ -26,14 +28,22 @@ public class LocaleHandler implements HttpHandler {
public void handleRequest(HttpServerExchange exchange) throws Exception {
final RequestAttachment requestAttachment = exchange.getAttachment(RequestUtils.REQUEST_ATTACHMENT);
final HeaderValues headerValues = exchange.getRequestHeaders().get(Headers.ACCEPT_LANGUAGE_STRING);
final Cookie localeCookie = exchange.getRequestCookies().getOrDefault(CONFIG.getLocaleCookieName(), null);

Locale locale = Locale.forLanguageTag(CONFIG.getApplicationLanguage());

if (headerValues != null) {
String acceptLanguage = headerValues.element();
if (StringUtils.isNotBlank(acceptLanguage)) {
locale = LocaleUtils.getLocaleFromString(acceptLanguage);

if(localeCookie == null) {
if (headerValues != null) {
String acceptLanguage = headerValues.element();
if (StringUtils.isNotBlank(acceptLanguage)) {
locale = LocaleUtils.getLocaleFromString(acceptLanguage);
}
}
}
else {
locale = LocaleUtils.getLocaleFromString(localeCookie.getValue());
}


Locale.setDefault(locale);
requestAttachment.getMessages().reload();
Expand Down