[ https://issues.apache.org/jira/browse/WW-4921?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16374743#comment-16374743 ]
ASF GitHub Bot commented on WW-4921: ------------------------------------ yasserzamani closed pull request #213: WW-4921 revert I18nInterceptor logic from WW-4741 changes… URL: https://github.com/apache/struts/pull/213 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 682bbe6d7..8a9e7bafc 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java @@ -282,14 +282,15 @@ public Locale find() { @Override public Locale store(ActionInvocation invocation, Locale locale) { HttpSession session = ServletActionContext.getRequest().getSession(false); + Map<String, Object> invocationSession = invocation.getInvocationContext().getSession(); - if (session != null) { + if (session != null && invocationSession != null) { String sessionId = session.getId(); synchronized (sessionId.intern()) { - invocation.getInvocationContext().getSession().put(attributeName, locale); + invocationSession.put(attributeName, locale); } } else { - LOG.debug("session creation avoided as it doesn't exist already"); + LOG.debug("locale did not stored because either request or invocation does not have session"); } return locale; @@ -301,11 +302,12 @@ public Locale read(ActionInvocation invocation) { LOG.debug("Checks session for saved locale"); HttpSession session = ServletActionContext.getRequest().getSession(false); + Map<String, Object> invocationSession = invocation.getInvocationContext().getSession(); - if (session != null) { + if (session != null && invocationSession != null) { String sessionId = session.getId(); synchronized (sessionId.intern()) { - Object sessionLocale = invocation.getInvocationContext().getSession().get(attributeName); + Object sessionLocale = invocationSession.get(attributeName); if (sessionLocale != null && sessionLocale instanceof Locale) { locale = (Locale) sessionLocale; LOG.debug("Applied session locale: {}", locale); diff --git a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java index fcc0dd11b..171008b6b 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java @@ -83,6 +83,37 @@ public void testNoSessionButLocale() throws Exception { assertNull("should not be stored here", session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); } + public void testNoInvocationSessionNoLocale() throws Exception { + mai.getInvocationContext().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", mai.getInvocationContext().getSession()); + assertNull("should not be stored here", session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); + } + + public void testNoInvocationSessionButLocale() throws Exception { + prepare(I18nInterceptor.DEFAULT_PARAMETER, "da_DK"); //prevents shouldStore to being false + mai.getInvocationContext().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", mai.getInvocationContext().getSession()); + assertNull("should not be stored here", session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); + } + public void testDefaultLocale() throws Exception { prepare(I18nInterceptor.DEFAULT_PARAMETER, "_"); // bad locale that would get us default locale instead interceptor.intercept(mai); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > NPE in I18nInterceptor$SessionLocaleHandler.read > ------------------------------------------------ > > Key: WW-4921 > URL: https://issues.apache.org/jira/browse/WW-4921 > Project: Struts 2 > Issue Type: Bug > Components: Core Interceptors > Reporter: Michael Hintenaus > Priority: Major > Labels: test > Fix For: 2.5.16 > > > Calling > {code:java} > getActionProxy("/index.action").getInvocation().invoke(){code} > in a TestCase which extends from StrutsJUnit4TestCase will lead to a > NullPointerException. > Overriding getActionProxy like this will help: > {code:java} > @Override > protected ActionProxy getActionProxy(final String uri) { > final ActionProxy proxy = super.getActionProxy(uri); > final ActionContext context = > proxy.getInvocation().getInvocationContext(); > if (context.getSession() == null) { > context.setSession(new SessionMap<>(request)); > } > return proxy; > }{code} > -- This message was sent by Atlassian JIRA (v7.6.3#76005)