From: James Moger Date: Fri, 15 Nov 2013 21:43:38 +0000 (-0500) Subject: Improve pages servlet X-Git-Tag: v1.4.0~203 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a5c69b6b7070c7cac977f2e837e81a17a208b3d5;p=gitblit.git Improve pages servlet Change-Id: I5239808bc71441022b4b82e9d369866e9600bbfa --- 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 names = new TreeSet(); + for (PathModel entry : JGitUtils.getFilesInPath(r, res, commit)) { + names.add(entry.name); + } + byte[] content = null; - if (StringUtils.isEmpty(resource)) { - // find resource - List extensions = new ArrayList(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 extensions = new ArrayList(); + 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(""); + response.getWriter().append(""); + response.getWriter().append(""); + response.getWriter().append(""); + response.getWriter().append(""); + String pattern = ""; + final ByteFormat byteFormat = new ByteFormat(); + List 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(""); + response.getWriter().append("
pathmodesize
{0}{1}{2}
"); + } 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; }