From 5f2429429f906d38fdeb1d11ea893689c2411b04 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 1 Feb 2014 11:04:36 +0100 Subject: [PATCH] Defensively handle ServletRequestAttributes casting in requestDestroyed callback Issue: SPR-11378 --- .../request/RequestContextListener.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/request/RequestContextListener.java b/spring-web/src/main/java/org/springframework/web/context/request/RequestContextListener.java index 1316d29694..8a6098c605 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/RequestContextListener.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/RequestContextListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,17 +64,19 @@ public class RequestContextListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent requestEvent) { - ServletRequestAttributes attributes = - (ServletRequestAttributes) requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); - ServletRequestAttributes threadAttributes = - (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + ServletRequestAttributes attributes = null; + Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); + if (reqAttr instanceof ServletRequestAttributes) { + attributes = (ServletRequestAttributes) reqAttr; + } + RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes(); if (threadAttributes != null) { // We're assumably within the original request thread... - if (attributes == null) { - attributes = threadAttributes; - } LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); + if (attributes == null && threadAttributes instanceof ServletRequestAttributes) { + attributes = (ServletRequestAttributes) threadAttributes; + } } if (attributes != null) { attributes.requestCompleted();