diff options
author | James Moger <james.moger@gitblit.com> | 2013-11-15 16:43:38 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2013-11-15 16:43:38 -0500 |
commit | a5c69b6b7070c7cac977f2e837e81a17a208b3d5 (patch) | |
tree | 86b46fa221952e073e9831f4f675a646753d59d5 /src/main/java | |
parent | ddf2fbdbd304cc29c48fc13c449001b4cb6e2092 (diff) | |
download | gitblit-a5c69b6b7070c7cac977f2e837e81a17a208b3d5.tar.gz gitblit-a5c69b6b7070c7cac977f2e837e81a17a208b3d5.zip |
Improve pages servlet
Change-Id: I5239808bc71441022b4b82e9d369866e9600bbfa
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/gitblit/PagesServlet.java | 120 |
1 files changed, 79 insertions, 41 deletions
diff --git a/src/main/java/com/gitblit/PagesServlet.java b/src/main/java/com/gitblit/PagesServlet.java index e121f8aa..3ed5b262 100644 --- a/src/main/java/com/gitblit/PagesServlet.java +++ b/src/main/java/com/gitblit/PagesServlet.java @@ -20,6 +20,8 @@ import java.text.MessageFormat; import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@@ -33,8 +35,10 @@ import org.eclipse.jgit.revwalk.RevTree; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.gitblit.models.PathModel;
import com.gitblit.models.RefModel;
import com.gitblit.utils.ArrayUtils;
+import com.gitblit.utils.ByteFormat;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.MarkdownUtils;
import com.gitblit.utils.StringUtils;
@@ -148,29 +152,19 @@ public class PagesServlet extends HttpServlet { String [] encodings = GitBlit.getEncodings();
RevTree tree = commit.getTree();
+
+ String res = resource;
+ if (res.endsWith("/")) {
+ res = res.substring(0, res.length() - 1);
+ }
+ Set<String> names = new TreeSet<String>();
+ for (PathModel entry : JGitUtils.getFilesInPath(r, res, commit)) {
+ names.add(entry.name);
+ }
+
byte[] content = null;
- if (StringUtils.isEmpty(resource)) {
- // find resource
- List<String> extensions = new ArrayList<String>(processor.getMarkupExtensions());
- extensions.add("html");
- extensions.add("htm");
- for (String ext : extensions) {
- String file = "index." + ext;
- String stringContent = JGitUtils.getStringContent(r, tree, file, encodings);
- if (stringContent == null) {
- continue;
- }
- content = stringContent.getBytes(Constants.ENCODING);
- if (content != null) {
- resource = file;
- // assume text/html unless the servlet container
- // overrides
- response.setContentType("text/html; charset=" + Constants.ENCODING);
- break;
- }
- }
- } else {
- // specific resource
+ if (names.isEmpty()) {
+ // not a path, a specific resource
try {
String contentType = context.getMimeType(resource);
if (contentType == null) {
@@ -185,31 +179,75 @@ public class PagesServlet extends HttpServlet { response.setContentType(contentType);
} catch (Exception e) {
}
+ } else {
+ // path
+ List<String> extensions = new ArrayList<String>();
+ extensions.add("html");
+ extensions.add("htm");
+ extensions.addAll(processor.getMarkupExtensions());
+ for (String ext : extensions) {
+ String file = "index." + ext;
+
+ if (names.contains(file)) {
+ String stringContent = JGitUtils.getStringContent(r, tree, file, encodings);
+ if (stringContent == null) {
+ continue;
+ }
+ content = stringContent.getBytes(Constants.ENCODING);
+ if (content != null) {
+ resource = file;
+ // assume text/html unless the servlet container
+ // overrides
+ response.setContentType("text/html; charset=" + Constants.ENCODING);
+ break;
+ }
+ }
+ }
}
// no content, try custom 404 page
if (ArrayUtils.isEmpty(content)) {
- String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);
- if (!StringUtils.isEmpty(custom404)) {
- content = custom404.getBytes(Constants.ENCODING);
- }
+ String ext = StringUtils.getFileExtension(resource);
+ if (StringUtils.isEmpty(ext)) {
+ // document list
+ response.setContentType("text/html");
+ response.getWriter().append("<style>table th, table td { min-width: 150px; text-align: left; }</style>");
+ response.getWriter().append("<table>");
+ response.getWriter().append("<thead><tr><th>path</th><th>mode</th><th>size</th></tr>");
+ response.getWriter().append("</thead>");
+ response.getWriter().append("<tbody>");
+ String pattern = "<tr><td><a href=\"{0}\">{0}</a></td><td>{1}</td><td>{2}</td></tr>";
+ final ByteFormat byteFormat = new ByteFormat();
+ List<PathModel> entries = JGitUtils.getFilesInPath(r, resource, commit);
+ for (PathModel entry : entries) {
+ response.getWriter().append(MessageFormat.format(pattern, entry.name, JGitUtils.getPermissionsFromMode(entry.mode), byteFormat.format(entry.size)));
+ }
+ response.getWriter().append("</tbody>");
+ response.getWriter().append("</table>");
+ } else {
+ // 404
+ String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);
+ if (!StringUtils.isEmpty(custom404)) {
+ content = custom404.getBytes(Constants.ENCODING);
+ }
- // still no content
- if (ArrayUtils.isEmpty(content)) {
- String str = MessageFormat.format(
- "# Error\nSorry, the requested resource **{0}** was not found.",
- resource);
- content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);
- }
+ // still no content
+ if (ArrayUtils.isEmpty(content)) {
+ String str = MessageFormat.format(
+ "# Error\nSorry, the requested resource **{0}** was not found.",
+ resource);
+ content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);
+ }
- try {
- // output the content
- logger.warn("Pages 404: " + resource);
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
- response.getOutputStream().write(content);
- response.flushBuffer();
- } catch (Throwable t) {
- logger.error("Failed to write page to client", t);
+ try {
+ // output the content
+ logger.warn("Pages 404: " + resource);
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+ response.getOutputStream().write(content);
+ response.flushBuffer();
+ } catch (Throwable t) {
+ logger.error("Failed to write page to client", t);
+ }
}
return;
}
|