]> source.dussan.org Git - gitblit.git/commitdiff
Fix subdirectory navigation in pages servlet (issue-412)
authorJames Moger <james.moger@gitblit.com>
Wed, 23 Apr 2014 16:52:23 +0000 (12:52 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 23 Apr 2014 16:52:23 +0000 (12:52 -0400)
releases.moxie
src/main/java/com/gitblit/servlet/PagesServlet.java

index 53fc3ead4c2b96bf99b607d004a37bde1fe47c59..12b82e7ae879a4f9b48cd8c0ad3d55d0fbcf023a 100644 (file)
@@ -11,6 +11,7 @@ r23: {
     security: ~
     fixes:
     - Fix subdirectory links in pages servlet (issue-411)
+    - Fix subdirectory navigation in pages servlet (issue-412)
     changes:
     - improve French translation (pr-176)
     - simplify current plugin release detection and ignore the currentRelease registry field 
index f4e8bb234762e5ad6803fe16e20c57bc880bf3fa..1d767deffaf69ba1619ba2f9dae346f5827862e7 100644 (file)
@@ -20,8 +20,8 @@ import java.text.MessageFormat;
 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
@@ -111,10 +111,6 @@ public class PagesServlet extends DaggerServlet {
                        // 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
@@ -174,45 +170,59 @@ public class PagesServlet extends DaggerServlet {
                        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
@@ -222,39 +232,9 @@ public class PagesServlet extends DaggerServlet {
                                }\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
@@ -278,6 +258,34 @@ public class PagesServlet extends DaggerServlet {
                                        } 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