Skip to content

Commit

Permalink
Merge pull request #126 from WilliamDunne/master
Browse files Browse the repository at this point in the history
Locale Handler
  • Loading branch information
svenkubiak committed Jan 25, 2016
2 parents 0ca6230 + c514324 commit bf22cf1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.settings/
.idea/
target/
*.jar
*.jar
*.class
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 application.i18n.cookie.name from application.yaml or default value if undefined
* @author William Dunne
*/
public String getLocaleCookieName() {
return getString(Key.LOCALE_COOKIE_NAME, Default.LOCALE_COOKIE_NAME.toString());
}

/**
* @return same value as isSessionCookieSecure()
*/
Expand Down
4 changes: 3 additions & 1 deletion mangooio-core/src/main/java/io/mangoo/enums/Default.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Default application values
*
* @author svenkubiak
* @author williamdunne
*
*/
public enum Default {
Expand Down Expand Up @@ -83,7 +84,8 @@ public enum Default {
WSS_CACHE_PREFIX("MANGOOIO-WSS-"),
AUTHENTICATION("@authentication"),
BLOCKING("@blocking"),
CACHE_CLASS("io.mangoo.cache.Cache");
CACHE_CLASS("io.mangoo.cache.Cache"),
LOCALE_COOKIE_NAME("lang");

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("application.i18n.cookie.name"),
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 Attachment requestAttachment = exchange.getAttachment(RequestUtils.ATTACHMENT_KEY);
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
14 changes: 14 additions & 0 deletions mangooio-core/src/main/java/io/mangoo/utils/CookieBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import io.undertow.server.handlers.Cookie;
import io.undertow.server.handlers.CookieImpl;

import io.mangoo.configuration.Config;
import io.mangoo.core.Application;

/**
*
* @author svenkubiak
* @author WilliamDunne
*
*/
public class CookieBuilder {
Expand All @@ -30,6 +34,14 @@ public static CookieBuilder create() {
return new CookieBuilder();
}

/**
* Sets up the cookie for locale settings
* @author WilliamDunne
*/
public CookieBuilder createLocale() {
return new CookieBuilder().name(Application.getInstance(Config.class).getLocaleCookieName());
}

/**
* Sets the name of the cookie
*
Expand Down Expand Up @@ -149,6 +161,8 @@ public CookieBuilder httpOnly(boolean httpOnly) {
return this;
}



public Cookie build() {
Cookie cookie = new CookieImpl(this.cookieName)
.setValue(this.cookieValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ public void testEnvironmentValues() {
assertThat(config.getString("smtp.username"), equalTo(""));
assertThat(config.getString("smtp.port"), equalTo("3055"));
}

@Test
public void testGetLocaleCookieName() {
//given
final Config config = Application.getInstance(Config.class);

//then
assertThat(config.getLocaleCookieName(), equalTo("lang"));
}
}

0 comments on commit bf22cf1

Please sign in to comment.