Skip to content

Commit

Permalink
WW-4722 Refactors code to use predefined storage locatio
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Dec 1, 2016
1 parent 6e1ea61 commit 6457f00
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}

public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}

public void setRequestOnlyParameterName(String requestOnlyParameterName) {
this.requestOnlyParameterName = requestOnlyParameterName;
}
Expand All @@ -123,10 +127,6 @@ public void setRequestCookieParameterName(String requestCookieParameterName) {
this.requestCookieParameterName = requestCookieParameterName;
}

public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}

public void setLocaleStorage(String storageName) {
if (storageName == null || "".equals(storageName)) {
this.storage = Storage.NONE;
Expand All @@ -152,9 +152,9 @@ public String intercept(ActionInvocation invocation) throws Exception {
invocation.getProxy().getNamespace(), invocation.getProxy().getActionName());
}

LocaleFinder localeFinder = new CookieLocaleFinder(invocation);
Locale locale = getLocaleFromParam(localeFinder.getRequestedLocale());
locale = storeLocale(invocation, locale, localeFinder.getStorage());
RequestOnlyLocaleFinder localeFinder = getLocaleFinder(invocation);
Locale locale = getLocaleFromParam(localeFinder.find());
locale = storeLocale(invocation, locale, storage);
saveLocale(invocation, locale);

if (LOG.isDebugEnabled()) {
Expand All @@ -171,6 +171,20 @@ public String intercept(ActionInvocation invocation) throws Exception {
return result;
}

protected RequestOnlyLocaleFinder getLocaleFinder(ActionInvocation invocation) {
RequestOnlyLocaleFinder localeFinder;
if (this.storage == Storage.COOKIE) {
localeFinder = new CookieLocaleFinder(invocation);
} else if (this.storage == Storage.SESSION) {
localeFinder = new SessionLocaleFinder(invocation);
} else {
localeFinder = new RequestOnlyLocaleFinder(invocation);
}

LOG.debug("Using LocaleFinder implementation {}", localeFinder.getClass().getName());
return localeFinder;
}

/**
* Store the locale to the chosen storage, like f. e. the session
*
Expand Down Expand Up @@ -308,71 +322,74 @@ protected void saveLocale(ActionInvocation invocation, Locale locale) {
invocation.getInvocationContext().setLocale(locale);
}

protected class LocaleFinder {
protected Storage storage = Storage.SESSION;
protected Parameter requestedLocale = null;
protected class RequestOnlyLocaleFinder {

protected ActionInvocation actionInvocation = null;

protected LocaleFinder(ActionInvocation invocation) {
protected RequestOnlyLocaleFinder(ActionInvocation invocation) {
actionInvocation = invocation;
find();
}

protected void find() {
//get requested locale
public String find() {
HttpParameters params = actionInvocation.getInvocationContext().getParameters();

storage = Storage.SESSION;

requestedLocale = findLocaleParameter(params, parameterName);
if (requestedLocale.isDefined()) {
return;
}

requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
Parameter requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
if (requestedLocale.isDefined()) {
storage = Storage.NONE;
return requestedLocale.getValue();
}

return null;
}
}

protected class SessionLocaleFinder extends RequestOnlyLocaleFinder {

public Storage getStorage() {
return storage;
protected SessionLocaleFinder(ActionInvocation invocation) {
super(invocation);
}

public String getRequestedLocale() {
public String find() {
String requestOnlyLocale = super.find();

if (requestOnlyLocale != null) {
return requestOnlyLocale;
}

HttpParameters params = actionInvocation.getInvocationContext().getParameters();

Parameter requestedLocale = findLocaleParameter(params, parameterName);
if (requestedLocale.isDefined()) {
return requestedLocale.getValue();
}

return requestedLocale.getValue();
}

}

protected class CookieLocaleFinder extends LocaleFinder {
protected class CookieLocaleFinder extends RequestOnlyLocaleFinder {
protected CookieLocaleFinder(ActionInvocation invocation) {
super(invocation);
}

@Override
protected void find() {
//get requested locale
HttpParameters params = actionInvocation.getInvocationContext().getParameters();
storage = Storage.SESSION;

requestedLocale = findLocaleParameter(params, parameterName);
public String find() {
String requestOnlySessionLocale = super.find();

if (requestedLocale.isDefined()) {
return;
if (requestOnlySessionLocale != null) {
return requestOnlySessionLocale;
}

requestedLocale = findLocaleParameter(params, requestCookieParameterName);
if (requestedLocale.isDefined()) {
storage = Storage.COOKIE;
return;
}
HttpParameters params = actionInvocation.getInvocationContext().getParameters();

requestedLocale = findLocaleParameter(params, requestOnlyParameterName);
Parameter requestedLocale = findLocaleParameter(params, requestCookieParameterName);
if (requestedLocale.isDefined()) {
storage = Storage.NONE;
storage = Storage.COOKIE;
return requestedLocale.getValue();
}

return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@ public void testActionContextLocaleIsPreservedWhenNotOverridden() throws Excepti
assertEquals(locale1, locale);
}

public void testCookieCreation() throws Exception {

prepare(I18nInterceptor.DEFAULT_COOKIE_PARAMETER, "da_DK");

final Cookie cookie = new Cookie(I18nInterceptor.DEFAULT_COOKIE_ATTRIBUTE, "da_DK");

HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
response.addCookie(CookieMatcher.eqCookie(cookie));
EasyMock.replay(response);

ac.put(StrutsStatics.HTTP_RESPONSE, response);
interceptor.setLocaleStorage(I18nInterceptor.Storage.COOKIE.name());
interceptor.intercept(mai);

EasyMock.verify(response);

Locale denmark = new Locale("da", "DK");
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
}

private void prepare(String key, Serializable value) {
Map<String, Serializable> params = new HashMap<>();
params.put(key, value);
Expand Down Expand Up @@ -255,23 +276,4 @@ public void appendTo(StringBuffer buffer) {
}
}

public void testCookieCreation() throws Exception {

prepare(I18nInterceptor.DEFAULT_COOKIE_PARAMETER, "da_DK");

final Cookie cookie = new Cookie(I18nInterceptor.DEFAULT_COOKIE_ATTRIBUTE, "da_DK");

HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
response.addCookie(CookieMatcher.eqCookie(cookie));
EasyMock.replay(response);

ac.put(StrutsStatics.HTTP_RESPONSE, response);
interceptor.intercept(mai);

EasyMock.verify(response);

Locale denmark = new Locale("da", "DK");
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
}
}

0 comments on commit 6457f00

Please sign in to comment.