Protect against NPE in Tiles 3 Views

Change the org.apache.tiles.request.servlet.ServletRequest created
in TileView.checkResource to include the HttpServletRequest. This
is required when using a TilesConfigurer with useMutableTilesContainer
set to true. Without a HttpServletRequest the getRequestScope() method
will always return null causing exceptions in the CachingTilesContainer
class.

Unfortunately the checkResource method does not provide access to the
HttpServletRequest so it must be obtained via the RequestContextHolder
thread-local.

Issue: SPR-10223
This commit is contained in:
Phillip Webb 2013-02-12 15:32:46 -08:00
parent 66ae626f91
commit e551f0408d
1 changed files with 10 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -30,6 +30,9 @@ import org.apache.tiles.request.Request;
import org.apache.tiles.request.render.Renderer;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
@ -95,7 +98,12 @@ public class TilesView extends AbstractUrlBasedView {
@Override
public boolean checkResource(final Locale locale) throws Exception {
Request request = new ServletRequest(this.applicationContext, null, null) {
HttpServletRequest servletRequest = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
servletRequest = ((ServletRequestAttributes)requestAttributes).getRequest();
}
Request request = new ServletRequest(this.applicationContext, servletRequest, null) {
@Override
public Locale getRequestLocale() {
return locale;