From 8daefa09d99774639a355c0dfa2b989fa1007f5f Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 11 Oct 2012 17:11:18 -0400
Subject: [PATCH] Created static repository close functions for unit testing and fixed Windows sharing violations
---
src/com/gitblit/PagesServlet.java | 48 +++++++++++++++++++++++++++++++++++-------------
1 files changed, 35 insertions(+), 13 deletions(-)
diff --git a/src/com/gitblit/PagesServlet.java b/src/com/gitblit/PagesServlet.java
index 58d67b0..ad9276b 100644
--- a/src/com/gitblit/PagesServlet.java
+++ b/src/com/gitblit/PagesServlet.java
@@ -141,13 +141,15 @@
}
response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
+ String [] encodings = GitBlit.getEncodings();
+
RevTree tree = commit.getTree();
byte[] content = null;
if (StringUtils.isEmpty(resource)) {
// find resource
String[] files = { "index.html", "index.htm", "index.mkd" };
for (String file : files) {
- content = JGitUtils.getStringContent(r, tree, file)
+ content = JGitUtils.getStringContent(r, tree, file, encodings)
.getBytes(Constants.ENCODING);
if (content != null) {
resource = file;
@@ -159,27 +161,47 @@
}
} else {
// specific resource
- String contentType = context.getMimeType(resource);
- if (contentType.startsWith("text")) {
- content = JGitUtils.getStringContent(r, tree, resource).getBytes(
- Constants.ENCODING);
- } else {
- content = JGitUtils.getByteContent(r, tree, resource);
+ try {
+ String contentType = context.getMimeType(resource);
+ if (contentType == null) {
+ contentType = "text/plain";
+ }
+ if (contentType.startsWith("text")) {
+ content = JGitUtils.getStringContent(r, tree, resource, encodings).getBytes(
+ Constants.ENCODING);
+ } else {
+ content = JGitUtils.getByteContent(r, tree, resource);
+ }
+ response.setContentType(contentType);
+ } catch (Exception e) {
}
- response.setContentType(contentType);
}
// no content, try custom 404 page
if (ArrayUtils.isEmpty(content)) {
- content = JGitUtils.getStringContent(r, tree, "404.html").getBytes(
- Constants.ENCODING);
+ 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)) {
- content = (MessageFormat.format(
+ String str = MessageFormat.format(
"# Error\nSorry, the requested resource **{0}** was not found.",
- resource)).getBytes(Constants.ENCODING);
- resource = "404.mkd";
+ 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);
+ }
+ return;
}
// check to see if we should transform markdown files
--
Gitblit v1.9.1