]> source.dussan.org Git - gitblit.git/commitdiff
Improve pages servlet
authorJames Moger <james.moger@gitblit.com>
Fri, 15 Nov 2013 21:43:38 +0000 (16:43 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 15 Nov 2013 21:43:38 +0000 (16:43 -0500)
Change-Id: I5239808bc71441022b4b82e9d369866e9600bbfa

src/main/java/com/gitblit/PagesServlet.java

index e121f8aa8b00234bfbe6e6c4195e0824a898c347..3ed5b262563cc921475cb44649dfc7e49bb75f87 100644 (file)
@@ -20,6 +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
 \r
 import javax.servlet.ServletContext;\r
 import javax.servlet.ServletException;\r
@@ -33,8 +35,10 @@ import org.eclipse.jgit.revwalk.RevTree;
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+import com.gitblit.models.PathModel;\r
 import com.gitblit.models.RefModel;\r
 import com.gitblit.utils.ArrayUtils;\r
+import com.gitblit.utils.ByteFormat;\r
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.MarkdownUtils;\r
 import com.gitblit.utils.StringUtils;\r
@@ -148,29 +152,19 @@ public class PagesServlet extends HttpServlet {
                        String [] encodings = GitBlit.getEncodings();\r
 \r
                        RevTree tree = commit.getTree();\r
+\r
+                       String res = resource;\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
                        byte[] content = null;\r
-                       if (StringUtils.isEmpty(resource)) {\r
-                               // find resource\r
-                               List<String> extensions = new ArrayList<String>(processor.getMarkupExtensions());\r
-                               extensions.add("html");\r
-                               extensions.add("htm");\r
-                               for (String ext : extensions) {\r
-                                       String file = "index." + ext;\r
-                                       String stringContent = JGitUtils.getStringContent(r, tree, file, encodings);\r
-                                       if (stringContent == null) {\r
-                                               continue;\r
-                                       }\r
-                                       content = stringContent.getBytes(Constants.ENCODING);\r
-                                       if (content != null) {\r
-                                               resource = file;\r
-                                               // assume text/html unless the servlet container\r
-                                               // overrides\r
-                                               response.setContentType("text/html; charset=" + Constants.ENCODING);\r
-                                               break;\r
-                                       }\r
-                               }\r
-                       } else {\r
-                               // specific resource\r
+                       if (names.isEmpty()) {\r
+                               // not a path, a specific resource\r
                                try {\r
                                        String contentType = context.getMimeType(resource);\r
                                        if (contentType == null) {\r
@@ -185,31 +179,75 @@ public class PagesServlet extends HttpServlet {
                                        response.setContentType(contentType);\r
                                } catch (Exception e) {\r
                                }\r
+                       } else {\r
+                               // path\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
+\r
+                                       if (names.contains(file)) {\r
+                                               String stringContent = JGitUtils.getStringContent(r, tree, file, encodings);\r
+                                               if (stringContent == null) {\r
+                                                       continue;\r
+                                               }\r
+                                               content = stringContent.getBytes(Constants.ENCODING);\r
+                                               if (content != null) {\r
+                                                       resource = file;\r
+                                                       // assume text/html unless the servlet container\r
+                                                       // overrides\r
+                                                       response.setContentType("text/html; charset=" + Constants.ENCODING);\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                               }\r
                        }\r
 \r
                        // no content, try custom 404 page\r
                        if (ArrayUtils.isEmpty(content)) {\r
-                               String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);\r
-                               if (!StringUtils.isEmpty(custom404)) {\r
-                                       content = custom404.getBytes(Constants.ENCODING);\r
-                               }\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}\">{0}</a></td><td>{1}</td><td>{2}</td></tr>";\r
+                                       final ByteFormat byteFormat = new ByteFormat();\r
+                                       List<PathModel> entries = JGitUtils.getFilesInPath(r, resource, commit);\r
+                                       for (PathModel entry : entries) {\r
+                                               response.getWriter().append(MessageFormat.format(pattern, entry.name, JGitUtils.getPermissionsFromMode(entry.mode), byteFormat.format(entry.size)));\r
+                                       }\r
+                                       response.getWriter().append("</tbody>");\r
+                                       response.getWriter().append("</table>");\r
+                               } else {\r
+                                       // 404\r
+                                       String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);\r
+                                       if (!StringUtils.isEmpty(custom404)) {\r
+                                               content = custom404.getBytes(Constants.ENCODING);\r
+                                       }\r
 \r
-                               // still no content\r
-                               if (ArrayUtils.isEmpty(content)) {\r
-                                       String str = MessageFormat.format(\r
-                                                       "# Error\nSorry, the requested resource **{0}** was not found.",\r
-                                                       resource);\r
-                                       content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);\r
-                               }\r
+                                       // still no content\r
+                                       if (ArrayUtils.isEmpty(content)) {\r
+                                               String str = MessageFormat.format(\r
+                                                               "# Error\nSorry, the requested resource **{0}** was not found.",\r
+                                                               resource);\r
+                                               content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);\r
+                                       }\r
 \r
-                               try {\r
-                                       // output the content\r
-                                       logger.warn("Pages 404: " + resource);\r
-                                       response.setStatus(HttpServletResponse.SC_NOT_FOUND);\r
-                                       response.getOutputStream().write(content);\r
-                                       response.flushBuffer();\r
-                               } catch (Throwable t) {\r
-                                       logger.error("Failed to write page to client", t);\r
+                                       try {\r
+                                               // output the content\r
+                                               logger.warn("Pages 404: " + resource);\r
+                                               response.setStatus(HttpServletResponse.SC_NOT_FOUND);\r
+                                               response.getOutputStream().write(content);\r
+                                               response.flushBuffer();\r
+                                       } catch (Throwable t) {\r
+                                               logger.error("Failed to write page to client", t);\r
+                                       }\r
                                }\r
                                return;\r
                        }\r