Merge pull request #207 from lukaszlenart/sessionless

WW-4741: Do not create session
This commit is contained in:
Yasser Zamani
2018-01-31 16:48:17 +03:30
committed by GitHub
2 changed files with 47 additions and 19 deletions
@@ -281,15 +281,17 @@ public class I18nInterceptor extends AbstractInterceptor {
@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;
}
@@ -298,19 +300,15 @@ public class I18nInterceptor extends AbstractInterceptor {
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);
}
}
}
@@ -31,6 +31,7 @@ import org.apache.struts2.dispatcher.HttpParameters;
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;
@@ -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 {
@@ -235,7 +263,9 @@ public class I18nInterceptorTest extends TestCase {
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 {