Index: java/org/apache/catalina/servlets/DefaultServlet.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- java/org/apache/catalina/servlets/DefaultServlet.java (revision 1642802) +++ java/org/apache/catalina/servlets/DefaultServlet.java (revision ) @@ -33,7 +33,9 @@ import java.io.StringWriter; import java.security.AccessController; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.Enumeration; import java.util.Iterator; import java.util.Locale; @@ -259,7 +261,10 @@ */ protected boolean showServerInfo = true; - + /** + * File list sorting parameters. + */ + protected String sortingParams = null; // --------------------------------------------------------- Public Methods /** @@ -701,7 +706,7 @@ throws IOException, ServletException { boolean serveContent = content; - + sortingParams=request.getQueryString(); // Identify the requested resource path String path = getRelativePath(request); if (debug > 0) { @@ -1281,7 +1286,6 @@ // rewriteUrl(contextPath) is expensive. cache result for later reuse String rewrittenContextPath = rewriteUrl(contextPath); String directoryWebappPath = resource.getWebappPath(); - for (String entry : entries) { if (entry.equalsIgnoreCase("WEB-INF") || @@ -1455,7 +1459,24 @@ sb.append(""); boolean shade = false; + Comparator cic = null; + if (sortingParams == null) { + cic = new NameAscComparator(); + } else { + cic = getComparator(sortingParams); + } + ArrayList resourceList = new ArrayList(); for (String entry : entries) { + WebResource childResource = + resources.getResource(directoryWebappPath + entry); + resourceList.add(childResource); + } + WebResource[] resourceArray = resourceList.toArray(new WebResource[resourceList.size()]); + Arrays.sort(resourceArray, cic); + for (int i = 0; i < entries.length; i++) { + entries[i] = resourceArray[i].getName(); + } + for (String entry : entries) { if (entry.equalsIgnoreCase("WEB-INF") || entry.equalsIgnoreCase("META-INF")) continue; @@ -2229,6 +2250,104 @@ IOException { throw new SAXException(sm.getString("defaultServlet.blockExternalEntity2", name, publicId, baseURI, systemId)); + } + } + private Comparator getComparator(String sortingParams) { + if (sortingParams.equals("size=asc")) { + return new SizeAscComparator(); + } else if (sortingParams.equals("size=dsc")) { + return new SizeDscComparator(); + } else if (sortingParams.equals("name=asc")) { + return new NameAscComparator(); + } else if (sortingParams.equals("name=dsc")) { + return new NameDscComparator(); + } else if (sortingParams.equals("modify=asc")) { + return new ModifyAscComparator(); + } else if (sortingParams.equals("modify=dsc")) { + return new ModifyDscComparator(); + } else return new NameAscComparator(); + } + + class NameAscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory() && strB.isDirectory()) { + return 1; + } else + return strA.getName().compareToIgnoreCase(strB.getName()); + } + } + + class NameDscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory() && strB.isDirectory()) { + return 1; + } else + return strB.getName().compareToIgnoreCase(strA.getName()); + } + } + + class SizeAscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory() && strB.isDirectory()) { + return 1; + } else if (strA.getContentLength() > strB.getContentLength()) { + return 1; + } else if (strA.getContentLength() < strB.getContentLength()) { + return -1; + } else + return 0; + } + } + + class SizeDscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory()&& strB.isDirectory()) { + return 1; + } else if (strA.getContentLength() > strB.getContentLength()) { + return -1; + } else if (strA.getContentLength() < strB.getContentLength()) { + return 1; + } else + return 0; + } + } + + class ModifyAscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory() && strB.isDirectory()) { + return 1; + } else if (strA.getLastModified() > strB.getLastModified()) { + return 1; + } else if (strA.getLastModified() < strB.getLastModified()) { + return -1; + } else + return 0; + } + } + + class ModifyDscComparator implements Comparator { + public int compare(WebResource strA, WebResource strB) { + if (strA.isDirectory() && !strB.isDirectory()) { + return -1; + } else if (!strA.isDirectory() && strB.isDirectory()) { + return 1; + } else if (strA.getLastModified() > strB.getLastModified()) { + return -1; + } else if (strA.getLastModified() < strB.getLastModified()) { + return 1; + } else + return 0; } } }