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

WW-4741: Do not create session #207

Merged
merged 6 commits into from Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -281,15 +281,17 @@ public Locale find() {

@Override
public Locale store(ActionInvocation invocation, Locale locale) {
//save it in session
Map<String, Object> session = invocation.getInvocationContext().getSession();
HttpSession session = ServletActionContext.getRequest().getSession(false);

if (session != null) {
String sessionId = ServletActionContext.getRequest().getSession().getId();
String sessionId = session.getId();
synchronized (sessionId.intern()) {
session.put(attributeName, locale);
invocation.getInvocationContext().getSession().put(attributeName, locale);
}
} else {
LOG.debug("session creation avoided as it doesn't exist already");
}

return locale;
Copy link
Member

Choose a reason for hiding this comment

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

When shouldStore field is not false (i.e. we should store), shouldn't we forcefully create a session and store the locale? if we should not, shouldn't we return null here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we should forcefully create the session but I would leave that to a user, if there was no session just ignore (a user fault). And returning null can break action processing as then the null will be set in ActionContext

Copy link
Member

Choose a reason for hiding this comment

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

👍

}

Expand All @@ -298,19 +300,15 @@ public Locale read(ActionInvocation invocation) {
Locale locale = null;

LOG.debug("Checks session for saved locale");
Map<String, Object> session = invocation.getInvocationContext().getSession();
HttpSession session = ServletActionContext.getRequest().getSession(false);

if (session != null) {
//[WW-4741] Do not force session creation while this is a read operation
HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
if(null != httpSession) {
String sessionId = httpSession.getId();
synchronized (sessionId.intern()) {
Object sessionLocale = session.get(attributeName);
if (sessionLocale != null && sessionLocale instanceof Locale) {
locale = (Locale) sessionLocale;
LOG.debug("Applied session locale: {}", locale);
}
String sessionId = session.getId();
synchronized (sessionId.intern()) {
Object sessionLocale = invocation.getInvocationContext().getSession().get(attributeName);
if (sessionLocale != null && sessionLocale instanceof Locale) {
locale = (Locale) sessionLocale;
LOG.debug("Applied session locale: {}", locale);
}
}
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -45,14 +46,41 @@ public class I18nInterceptorTest extends TestCase {
private ActionInvocation mai;
private ActionContext ac;
private Map session;
private MockHttpServletRequest request;

public void testEmptyParamAndSession() throws Exception {
interceptor.intercept(mai);
}

public void testNoSession() throws Exception {
ac.setSession(null);
interceptor.intercept(mai);
public void testNoSessionNoLocale() throws Exception {
request.setSession(null);
try {
interceptor.intercept(mai);
assertTrue(true);
} catch (Exception ignore) {
fail("Shouldn't throw any exception!");
}

assertFalse("should have been removed",
mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined());
assertNull("should not be created", request.getSession(false));
assertNull("should not be stored here", session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));
}

public void testNoSessionButLocale() throws Exception {
prepare(I18nInterceptor.DEFAULT_PARAMETER, "da_DK"); //prevents shouldStore to being false
request.setSession(null);
try {
interceptor.intercept(mai);
assertTrue(true);
} catch (Exception ignore) {
fail("Shouldn't throw any exception!");
}

assertFalse("should have been removed",
mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined());
assertNull("should not be created", request.getSession(false));
assertNull("should not be stored here", session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));
}

public void testDefaultLocale() throws Exception {
Expand Down Expand Up @@ -235,7 +263,9 @@ public void setUp() throws Exception {
ac = new ActionContext(ctx);

ServletActionContext.setContext(ac);
ServletActionContext.setRequest(new MockHttpServletRequest());
request = new MockHttpServletRequest();
request.setSession(new MockHttpSession());
ServletActionContext.setRequest(request);

Action action = new Action() {
public String execute() throws Exception {
Expand Down