Refactor code to use Java 5 features
- Apply and use Generics - Remove JdkVersion.isAtLeastJava15() conditionals - Replace iterator loops with foreach syntax - Switch on warning in eclipse Issues: SWF-1532
This commit is contained in:
@@ -65,7 +65,7 @@ public class AjaxTilesView extends TilesView {
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
super.afterPropertiesSet();
|
||||
tilesRequestContextFactory = new ServletTilesRequestContextFactory();
|
||||
tilesRequestContextFactory.init(new HashMap());
|
||||
tilesRequestContextFactory.init(new HashMap<String, String>());
|
||||
}
|
||||
|
||||
public AjaxHandler getAjaxHandler() {
|
||||
@@ -76,8 +76,8 @@ public class AjaxTilesView extends TilesView {
|
||||
this.ajaxHandler = ajaxHandler;
|
||||
}
|
||||
|
||||
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
|
||||
HttpServletResponse response) throws Exception {
|
||||
|
||||
ServletContext servletContext = getServletContext();
|
||||
if (ajaxHandler.isAjaxRequest(request, response)) {
|
||||
@@ -101,12 +101,12 @@ public class AjaxTilesView extends TilesView {
|
||||
exposeModelAsRequestAttributes(model, request);
|
||||
JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
|
||||
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(container
|
||||
.getApplicationContext(), new Object[] { request, response });
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(
|
||||
container.getApplicationContext(), new Object[] { request, response });
|
||||
Definition compositeDefinition = container.getDefinitionsFactory().getDefinition(getUrl(),
|
||||
tilesRequestContext);
|
||||
|
||||
Map flattenedAttributeMap = new HashMap();
|
||||
Map<String, Attribute> flattenedAttributeMap = new HashMap<String, Attribute>();
|
||||
flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition, request,
|
||||
response);
|
||||
addRuntimeAttributes(container, flattenedAttributeMap, request, response);
|
||||
@@ -115,11 +115,11 @@ public class AjaxTilesView extends TilesView {
|
||||
request.setAttribute(ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME, true);
|
||||
}
|
||||
|
||||
for (int i = 0; i < fragmentsToRender.length; i++) {
|
||||
Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(fragmentsToRender[i]);
|
||||
for (String element : fragmentsToRender) {
|
||||
Attribute attributeToRender = flattenedAttributeMap.get(element);
|
||||
|
||||
if (attributeToRender == null) {
|
||||
throw new ServletException("No tiles attribute with a name of '" + fragmentsToRender[i]
|
||||
throw new ServletException("No tiles attribute with a name of '" + element
|
||||
+ "' could be found for the current view: " + this);
|
||||
} else {
|
||||
container.startContext(request, response).inheritCascadedAttributes(compositeDefinition);
|
||||
@@ -132,7 +132,8 @@ public class AjaxTilesView extends TilesView {
|
||||
}
|
||||
}
|
||||
|
||||
protected String[] getRenderFragments(Map model, HttpServletRequest request, HttpServletResponse response) {
|
||||
protected String[] getRenderFragments(Map<String, Object> model, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
String attrName = request.getParameter(FRAGMENTS_PARAM);
|
||||
String[] renderFragments = StringUtils.commaDelimitedListToStringArray(attrName);
|
||||
return StringUtils.trimArrayElements(renderFragments);
|
||||
@@ -153,10 +154,11 @@ public class AjaxTilesView extends TilesView {
|
||||
* @param response the servlet response
|
||||
*/
|
||||
protected void flattenAttributeMap(BasicTilesContainer container, TilesRequestContext requestContext,
|
||||
Map resultMap, Definition compositeDefinition, HttpServletRequest request, HttpServletResponse response) {
|
||||
Iterator iterator = compositeDefinition.getAttributeNames();
|
||||
Map<String, Attribute> resultMap, Definition compositeDefinition, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
Iterator<String> iterator = compositeDefinition.getAttributeNames();
|
||||
while (iterator.hasNext()) {
|
||||
String attributeName = (String) iterator.next();
|
||||
String attributeName = iterator.next();
|
||||
Attribute attribute = compositeDefinition.getAttribute(attributeName);
|
||||
if (attribute.getValue() == null || !(attribute.getValue() instanceof String)) {
|
||||
continue;
|
||||
@@ -184,19 +186,19 @@ public class AjaxTilesView extends TilesView {
|
||||
* @param request the Servlet request
|
||||
* @param response the Servlet response
|
||||
*/
|
||||
protected void addRuntimeAttributes(BasicTilesContainer container, Map resultMap, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
protected void addRuntimeAttributes(BasicTilesContainer container, Map<String, Attribute> resultMap,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
AttributeContext attributeContext = container.getAttributeContext(new Object[] { request, response });
|
||||
Set attributeNames = new HashSet();
|
||||
Set<String> attributeNames = new HashSet<String>();
|
||||
if (attributeContext.getLocalAttributeNames() != null) {
|
||||
attributeNames.addAll(attributeContext.getLocalAttributeNames());
|
||||
}
|
||||
if (attributeContext.getCascadedAttributeNames() != null) {
|
||||
attributeNames.addAll(attributeContext.getCascadedAttributeNames());
|
||||
}
|
||||
Iterator iterator = attributeNames.iterator();
|
||||
Iterator<String> iterator = attributeNames.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String name = (String) iterator.next();
|
||||
String name = iterator.next();
|
||||
Attribute attr = attributeContext.getAttribute(name);
|
||||
resultMap.put(name, attr);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class ResourceServlet extends HttpServletBean {
|
||||
|
||||
private boolean gzipEnabled = true;
|
||||
|
||||
private Set allowedResourcePaths = new HashSet();
|
||||
private Set<String> allowedResourcePaths = new HashSet<String>();
|
||||
{
|
||||
allowedResourcePaths.add("/**/*.css");
|
||||
allowedResourcePaths.add("/**/*.gif");
|
||||
@@ -90,7 +90,7 @@ public class ResourceServlet extends HttpServletBean {
|
||||
allowedResourcePaths.add("META-INF/**/*.png");
|
||||
};
|
||||
|
||||
private Map defaultMimeTypes = new HashMap();
|
||||
private Map<String, String> defaultMimeTypes = new HashMap<String, String>();
|
||||
{
|
||||
defaultMimeTypes.put(".css", "text/css");
|
||||
defaultMimeTypes.put(".gif", "image/gif");
|
||||
@@ -101,7 +101,7 @@ public class ResourceServlet extends HttpServletBean {
|
||||
defaultMimeTypes.put(".png", "image/png");
|
||||
}
|
||||
|
||||
private Set compressedMimeTypes = new HashSet();
|
||||
private Set<String> compressedMimeTypes = new HashSet<String>();
|
||||
{
|
||||
compressedMimeTypes.add("text/*");
|
||||
}
|
||||
@@ -131,8 +131,8 @@ public class ResourceServlet extends HttpServletBean {
|
||||
OutputStream out = selectOutputStream(request, response);
|
||||
|
||||
try {
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
URLConnection resourceConn = resources[i].openConnection();
|
||||
for (URL resource : resources) {
|
||||
URLConnection resourceConn = resource.openConnection();
|
||||
InputStream in = resourceConn.getInputStream();
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
@@ -166,9 +166,9 @@ public class ResourceServlet extends HttpServletBean {
|
||||
|
||||
private boolean matchesCompressedMimeTypes(String mimeType) {
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
Iterator compressedMimeTypesIt = compressedMimeTypes.iterator();
|
||||
Iterator<String> compressedMimeTypesIt = compressedMimeTypes.iterator();
|
||||
while (compressedMimeTypesIt.hasNext()) {
|
||||
String compressedMimeType = (String) compressedMimeTypesIt.next();
|
||||
String compressedMimeType = compressedMimeTypesIt.next();
|
||||
if (pathMatcher.match(compressedMimeType, mimeType)) {
|
||||
return true;
|
||||
}
|
||||
@@ -181,16 +181,16 @@ public class ResourceServlet extends HttpServletBean {
|
||||
long lastModified = -1;
|
||||
int contentLength = 0;
|
||||
String mimeType = null;
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
URLConnection resourceConn = resources[i].openConnection();
|
||||
for (URL resource : resources) {
|
||||
URLConnection resourceConn = resource.openConnection();
|
||||
if (resourceConn.getLastModified() > lastModified) {
|
||||
lastModified = resourceConn.getLastModified();
|
||||
}
|
||||
|
||||
String currentMimeType = getServletContext().getMimeType(resources[i].getPath());
|
||||
String currentMimeType = getServletContext().getMimeType(resource.getPath());
|
||||
if (currentMimeType == null) {
|
||||
String extension = resources[i].getPath().substring(resources[i].getPath().lastIndexOf('.'));
|
||||
currentMimeType = (String) defaultMimeTypes.get(extension);
|
||||
String extension = resource.getPath().substring(resource.getPath().lastIndexOf('.'));
|
||||
currentMimeType = defaultMimeTypes.get(extension);
|
||||
}
|
||||
if (mimeType == null) {
|
||||
mimeType = currentMimeType;
|
||||
@@ -226,10 +226,10 @@ public class ResourceServlet extends HttpServletBean {
|
||||
|
||||
long lastModified = -1;
|
||||
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
for (URL resource : resources) {
|
||||
URLConnection resourceConn;
|
||||
try {
|
||||
resourceConn = resources[i].openConnection();
|
||||
resourceConn = resource.openConnection();
|
||||
} catch (IOException e) {
|
||||
return -1;
|
||||
}
|
||||
@@ -298,9 +298,9 @@ public class ResourceServlet extends HttpServletBean {
|
||||
return false;
|
||||
}
|
||||
PathMatcher pathMatcher = new AntPathMatcher();
|
||||
Iterator allowedResourcePathsIt = allowedResourcePaths.iterator();
|
||||
Iterator<String> allowedResourcePathsIt = allowedResourcePaths.iterator();
|
||||
while (allowedResourcePathsIt.hasNext()) {
|
||||
String pattern = (String) allowedResourcePathsIt.next();
|
||||
String pattern = allowedResourcePathsIt.next();
|
||||
if (pathMatcher.match(pattern, resourcePath)) {
|
||||
return true;
|
||||
}
|
||||
@@ -380,14 +380,6 @@ public class ResourceServlet extends HttpServletBean {
|
||||
}
|
||||
gzipStream.write(b, off, len);
|
||||
}
|
||||
|
||||
public boolean closed() {
|
||||
return (this.closed);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -404,8 +396,8 @@ public class ResourceServlet extends HttpServletBean {
|
||||
* @see AntPathMatcher
|
||||
*/
|
||||
public void setAllowedResourcePaths(String allowedResourcePaths) {
|
||||
this.allowedResourcePaths = new HashSet(Arrays.asList(StringUtils.tokenizeToStringArray(allowedResourcePaths,
|
||||
",", true, true)));
|
||||
this.allowedResourcePaths = new HashSet<String>(Arrays.asList(StringUtils.tokenizeToStringArray(
|
||||
allowedResourcePaths, ",", true, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -415,8 +407,8 @@ public class ResourceServlet extends HttpServletBean {
|
||||
* @see AntPathMatcher
|
||||
*/
|
||||
public void setCompressedMimeTypes(String compressedMimeTypes) {
|
||||
this.compressedMimeTypes = new HashSet(Arrays.asList(StringUtils.tokenizeToStringArray(compressedMimeTypes,
|
||||
",", true, true)));
|
||||
this.compressedMimeTypes = new HashSet<String>(Arrays.asList(StringUtils.tokenizeToStringArray(
|
||||
compressedMimeTypes, ",", true, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,7 +59,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
setupStaticWebApplicationContext();
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertEquals("/WEB-INF/layout.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addHeader("Accept", SpringJavascriptAjaxHandler.AJAX_ACCEPT_CONTENT_TYPE);
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertEquals("/WEB-INF/layout.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addParameter("fragments", "searchResults");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertEquals("/WEB-INF/searchResults.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addParameter("fragments", "body");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertEquals("/WEB-INF/search.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
@@ -98,16 +98,16 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addParameter("fragments", "searchNavigation");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertEquals("/WEB-INF/searchNavigation.jsp", response.getForwardedUrl());
|
||||
}
|
||||
|
||||
public void testRenderFragment_InheritCascadedAttribute() throws Exception {
|
||||
BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getCurrentContainer(request, servletContext);
|
||||
ServletTilesRequestContextFactory tilesRequestContextFactory = new ServletTilesRequestContextFactory();
|
||||
tilesRequestContextFactory.init(new HashMap());
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(container
|
||||
.getApplicationContext(), new Object[] { request, response });
|
||||
tilesRequestContextFactory.init(new HashMap<String, String>());
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(
|
||||
container.getApplicationContext(), new Object[] { request, response });
|
||||
Definition definition = container.getDefinitionsFactory().getDefinition("search.body", tilesRequestContext);
|
||||
definition.setPreparer("org.springframework.js.ajax.tiles2.AjaxTilesViewTests$AttributeTestingPreparer");
|
||||
|
||||
@@ -116,7 +116,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addParameter("fragments", "body");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertTrue(AttributeTestingPreparer.invoked);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
Object[] requestItems = new Object[] { request, response };
|
||||
AttributeContext attributeContext = container.startContext(requestItems);
|
||||
attributeContext.putAttribute("body", new Attribute("/WEB-INF/dynamicTemplate.jsp"));
|
||||
Map resultMap = new HashMap();
|
||||
Map<String, Attribute> resultMap = new HashMap<String, Attribute>();
|
||||
ajaxTilesView.addRuntimeAttributes(container, resultMap, request, response);
|
||||
assertNotNull(resultMap.get("body"));
|
||||
assertEquals("/WEB-INF/dynamicTemplate.jsp", resultMap.get("body").toString());
|
||||
@@ -138,7 +138,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
request.addParameter("fragments", "body,searchNavigation");
|
||||
ajaxTilesView.setUrl("search");
|
||||
ajaxTilesView.afterPropertiesSet();
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response);
|
||||
ajaxTilesView.renderMergedOutputModel(new HashMap<String, Object>(), request, response);
|
||||
assertTrue("Multiple fragments should result in include, not forward", response.getIncludedUrls().size() == 2);
|
||||
assertEquals("/WEB-INF/search.jsp", response.getIncludedUrls().get(0));
|
||||
assertEquals("/WEB-INF/searchNavigation.jsp", response.getIncludedUrls().get(1));
|
||||
@@ -146,12 +146,12 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
|
||||
public void testFlattenAttributeMap() throws Exception {
|
||||
TilesRequestContextFactory tilesRequestContextFactory = new ServletTilesRequestContextFactory();
|
||||
tilesRequestContextFactory.init(new HashMap());
|
||||
tilesRequestContextFactory.init(new HashMap<String, String>());
|
||||
BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getCurrentContainer(request, servletContext);
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(container
|
||||
.getApplicationContext(), new Object[] { request, response });
|
||||
TilesRequestContext tilesRequestContext = tilesRequestContextFactory.createRequestContext(
|
||||
container.getApplicationContext(), new Object[] { request, response });
|
||||
Definition compositeDefinition = container.getDefinitionsFactory().getDefinition("search", tilesRequestContext);
|
||||
Map resultMap = new HashMap();
|
||||
Map<String, Attribute> resultMap = new HashMap<String, Attribute>();
|
||||
ajaxTilesView.flattenAttributeMap(container, tilesRequestContext, resultMap, compositeDefinition, request,
|
||||
response);
|
||||
assertNotNull(resultMap.get("body"));
|
||||
@@ -161,7 +161,7 @@ public class AjaxTilesViewTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testGetRenderFragments() throws Exception {
|
||||
Map model = new HashMap();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
request.setParameter("fragments", "f1,f2, f3");
|
||||
String[] fragments = ajaxTilesView.getRenderFragments(model, request, response);
|
||||
assertEquals("f1", fragments[0]);
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ResourceServletTests extends TestCase {
|
||||
|
||||
ServletContext servletContext = new ResourceTestMockServletContext();
|
||||
@@ -43,7 +44,7 @@ public class ResourceServletTests extends TestCase {
|
||||
|
||||
String requestPath = "/org/springframework/js/resource/TestResource1.js";
|
||||
request.setPathInfo(requestPath);
|
||||
Map params = new HashMap();
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("appended", "/org/springframework/js/resource/TestResource2.js");
|
||||
request.setParameters(params);
|
||||
servlet.doGet(request, response);
|
||||
|
||||
Reference in New Issue
Block a user