From e9a4bcd776e82afb135091f7ad48ada90bf5d2e6 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 31 Jan 2018 07:21:35 +0100 Subject: [PATCH] WW-4741 Does not create session if it doesn't exist --- .../struts2/interceptor/I18nInterceptor.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java index 502c21bd0..ba4993660 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java @@ -281,15 +281,16 @@ public class I18nInterceptor extends AbstractInterceptor { @Override public Locale store(ActionInvocation invocation, Locale locale) { - //save it in session - Map session = invocation.getInvocationContext().getSession(); + LOG.debug("Do not create session if it doesn't exist"); + 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); } } + return locale; } @@ -298,19 +299,15 @@ public class I18nInterceptor extends AbstractInterceptor { Locale locale = null; LOG.debug("Checks session for saved locale"); - Map 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); } } }