import java.text.ParseException;\r
import java.util.ArrayList;\r
import java.util.List;\r
-import java.util.Set;\r
-import java.util.TreeSet;\r
+import java.util.Map;\r
+import java.util.TreeMap;\r
\r
import javax.servlet.ServletContext;\r
import javax.servlet.ServletException;\r
// strip leading /\r
path = path.substring(1);\r
}\r
- if (path.charAt(path.length() - 1) == '/') {\r
- // strip trailing slash\r
- path = path.substring(0, path.length() - 1);\r
- }\r
\r
// determine repository and resource from url\r
String repository = "";\r
if (res.endsWith("/")) {\r
res = res.substring(0, res.length() - 1);\r
}\r
- Set<String> names = new TreeSet<String>();\r
- for (PathModel entry : JGitUtils.getFilesInPath(r, res, commit)) {\r
- names.add(entry.name);\r
- }\r
+\r
+ List<PathModel> pathEntries = JGitUtils.getFilesInPath(r, res, commit);\r
\r
byte[] content = null;\r
- if (names.isEmpty()) {\r
+ if (pathEntries.isEmpty()) {\r
// not a path, a specific resource\r
try {\r
- String contentType = context.getMimeType(resource);\r
+ String contentType = context.getMimeType(res);\r
if (contentType == null) {\r
contentType = "text/plain";\r
}\r
if (contentType.startsWith("text")) {\r
- content = JGitUtils.getStringContent(r, tree, resource, encodings).getBytes(\r
+ content = JGitUtils.getStringContent(r, tree, res, encodings).getBytes(\r
Constants.ENCODING);\r
} else {\r
- content = JGitUtils.getByteContent(r, tree, resource, false);\r
+ content = JGitUtils.getByteContent(r, tree, res, false);\r
}\r
response.setContentType(contentType);\r
} catch (Exception e) {\r
}\r
} else {\r
- // path\r
+ // path request\r
+ if (!request.getPathInfo().endsWith("/")) {\r
+ // redirect to trailing '/' url\r
+ response.sendRedirect(request.getServletPath() + request.getPathInfo() + "/");\r
+ return;\r
+ }\r
+\r
+ Map<String, String> names = new TreeMap<String, String>();\r
+ for (PathModel entry : pathEntries) {\r
+ names.put(entry.name.toLowerCase(), entry.name);\r
+ }\r
+\r
List<String> extensions = new ArrayList<String>();\r
extensions.add("html");\r
extensions.add("htm");\r
extensions.addAll(processor.getMarkupExtensions());\r
for (String ext : extensions) {\r
- String file = "index." + ext;\r
+ String key = "index." + ext;\r
\r
- if (names.contains(file)) {\r
- String stringContent = JGitUtils.getStringContent(r, tree, file, encodings);\r
+ if (names.containsKey(key)) {\r
+ String fileName = names.get(key);\r
+ String fullPath = fileName;\r
+ if (!res.isEmpty()) {\r
+ fullPath = res + "/" + fileName;\r
+ }\r
+ String stringContent = JGitUtils.getStringContent(r, tree, fullPath, encodings);\r
if (stringContent == null) {\r
continue;\r
}\r
content = stringContent.getBytes(Constants.ENCODING);\r
if (content != null) {\r
- resource = file;\r
+ res = fullPath;\r
// assume text/html unless the servlet container\r
// overrides\r
response.setContentType("text/html; charset=" + Constants.ENCODING);\r
}\r
}\r
\r
- // no content, try custom 404 page\r
+ // no content, document list or custom 404 page\r
if (ArrayUtils.isEmpty(content)) {\r
- String ext = StringUtils.getFileExtension(resource);\r
- if (StringUtils.isEmpty(ext)) {\r
- // document list\r
- response.setContentType("text/html");\r
- response.getWriter().append("<style>table th, table td { min-width: 150px; text-align: left; }</style>");\r
- response.getWriter().append("<table>");\r
- response.getWriter().append("<thead><tr><th>path</th><th>mode</th><th>size</th></tr>");\r
- response.getWriter().append("</thead>");\r
- response.getWriter().append("<tbody>");\r
- String pattern = "<tr><td><a href=\"{0}/{1}\">{1}</a></td><td>{2}</td><td>{3}</td></tr>";\r
- final ByteFormat byteFormat = new ByteFormat();\r
- List<PathModel> entries = JGitUtils.getFilesInPath(r, resource, commit);\r
- if (!entries.isEmpty()) {\r
- if (entries.get(0).path.indexOf('/') > -1) {\r
- // we are in a subdirectory, add parent directory link\r
- entries.add(0, new PathModel("..", resource + "/..", 0, FileMode.TREE.getBits(), null, null));\r
- }\r
- }\r
-\r
- String basePath = request.getServletPath() + request.getPathInfo();\r
- if (basePath.charAt(basePath.length() - 1) == '/') {\r
- // strip trailing slash\r
- basePath = basePath.substring(0, basePath.length() - 1);\r
- }\r
- for (PathModel entry : entries) {\r
- response.getWriter().append(MessageFormat.format(pattern, basePath, entry.name,\r
- JGitUtils.getPermissionsFromMode(entry.mode), byteFormat.format(entry.size)));\r
- }\r
- response.getWriter().append("</tbody>");\r
- response.getWriter().append("</table>");\r
- } else {\r
+ if (pathEntries.isEmpty()) {\r
// 404\r
String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);\r
if (!StringUtils.isEmpty(custom404)) {\r
} catch (Throwable t) {\r
logger.error("Failed to write page to client", t);\r
}\r
+ } else {\r
+ // document list\r
+ response.setContentType("text/html");\r
+ response.getWriter().append("<style>table th, table td { min-width: 150px; text-align: left; }</style>");\r
+ response.getWriter().append("<table>");\r
+ response.getWriter().append("<thead><tr><th>path</th><th>mode</th><th>size</th></tr>");\r
+ response.getWriter().append("</thead>");\r
+ response.getWriter().append("<tbody>");\r
+ String pattern = "<tr><td><a href=\"{0}/{1}\">{1}</a></td><td>{2}</td><td>{3}</td></tr>";\r
+ final ByteFormat byteFormat = new ByteFormat();\r
+ if (!pathEntries.isEmpty()) {\r
+ if (pathEntries.get(0).path.indexOf('/') > -1) {\r
+ // we are in a subdirectory, add parent directory link\r
+ pathEntries.add(0, new PathModel("..", resource + "/..", 0, FileMode.TREE.getBits(), null, null));\r
+ }\r
+ }\r
+\r
+ String basePath = request.getServletPath() + request.getPathInfo();\r
+ if (basePath.charAt(basePath.length() - 1) == '/') {\r
+ // strip trailing slash\r
+ basePath = basePath.substring(0, basePath.length() - 1);\r
+ }\r
+ for (PathModel entry : pathEntries) {\r
+ response.getWriter().append(MessageFormat.format(pattern, basePath, entry.name,\r
+ JGitUtils.getPermissionsFromMode(entry.mode), byteFormat.format(entry.size)));\r
+ }\r
+ response.getWriter().append("</tbody>");\r
+ response.getWriter().append("</table>");\r
}\r
return;\r
}\r