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
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
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
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