Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PagesServlet.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import java.text.ParseException;
  20. import javax.servlet.ServletContext;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpServlet;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import org.eclipse.jgit.lib.Repository;
  26. import org.eclipse.jgit.revwalk.RevCommit;
  27. import org.eclipse.jgit.revwalk.RevTree;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import com.gitblit.models.RefModel;
  31. import com.gitblit.utils.ArrayUtils;
  32. import com.gitblit.utils.JGitUtils;
  33. import com.gitblit.utils.MarkdownUtils;
  34. import com.gitblit.utils.StringUtils;
  35. /**
  36. * Serves the content of a gh-pages branch.
  37. *
  38. * @author James Moger
  39. *
  40. */
  41. public class PagesServlet extends HttpServlet {
  42. private static final long serialVersionUID = 1L;
  43. private transient Logger logger = LoggerFactory.getLogger(PagesServlet.class);
  44. public PagesServlet() {
  45. super();
  46. }
  47. /**
  48. * Returns an url to this servlet for the specified parameters.
  49. *
  50. * @param baseURL
  51. * @param repository
  52. * @param path
  53. * @return an url
  54. */
  55. public static String asLink(String baseURL, String repository, String path) {
  56. if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
  57. baseURL = baseURL.substring(0, baseURL.length() - 1);
  58. }
  59. return baseURL + Constants.PAGES + repository + "/" + (path == null ? "" : ("/" + path));
  60. }
  61. /**
  62. * Retrieves the specified resource from the gh-pages branch of the
  63. * repository.
  64. *
  65. * @param request
  66. * @param response
  67. * @throws javax.servlet.ServletException
  68. * @throws java.io.IOException
  69. */
  70. private void processRequest(HttpServletRequest request, HttpServletResponse response)
  71. throws ServletException, IOException {
  72. String path = request.getPathInfo();
  73. if (path.toLowerCase().endsWith(".git")) {
  74. // forward to url with trailing /
  75. // this is important for relative pages links
  76. response.sendRedirect(request.getServletPath() + path + "/");
  77. return;
  78. }
  79. if (path.charAt(0) == '/') {
  80. // strip leading /
  81. path = path.substring(1);
  82. }
  83. // determine repository and resource from url
  84. String repository = "";
  85. String resource = "";
  86. Repository r = null;
  87. int offset = 0;
  88. while (r == null) {
  89. int slash = path.indexOf('/', offset);
  90. if (slash == -1) {
  91. repository = path;
  92. } else {
  93. repository = path.substring(0, slash);
  94. }
  95. r = GitBlit.self().getRepository(repository, false);
  96. offset = slash + 1;
  97. if (offset > 0) {
  98. resource = path.substring(offset);
  99. }
  100. if (repository.equals(path)) {
  101. // either only repository in url or no repository found
  102. break;
  103. }
  104. }
  105. ServletContext context = request.getSession().getServletContext();
  106. try {
  107. if (r == null) {
  108. // repository not found!
  109. String mkd = MessageFormat.format(
  110. "# Error\nSorry, no valid **repository** specified in this url: {0}!",
  111. repository);
  112. error(response, mkd);
  113. return;
  114. }
  115. // retrieve the content from the repository
  116. RefModel pages = JGitUtils.getPagesBranch(r);
  117. RevCommit commit = JGitUtils.getCommit(r, pages.getObjectId().getName());
  118. if (commit == null) {
  119. // branch not found!
  120. String mkd = MessageFormat.format(
  121. "# Error\nSorry, the repository {0} does not have a **gh-pages** branch!",
  122. repository);
  123. error(response, mkd);
  124. r.close();
  125. return;
  126. }
  127. response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
  128. String [] encodings = GitBlit.getEncodings();
  129. RevTree tree = commit.getTree();
  130. byte[] content = null;
  131. if (StringUtils.isEmpty(resource)) {
  132. // find resource
  133. String[] files = { "index.html", "index.htm", "index.mkd" };
  134. for (String file : files) {
  135. content = JGitUtils.getStringContent(r, tree, file, encodings)
  136. .getBytes(Constants.ENCODING);
  137. if (content != null) {
  138. resource = file;
  139. // assume text/html unless the servlet container
  140. // overrides
  141. response.setContentType("text/html; charset=" + Constants.ENCODING);
  142. break;
  143. }
  144. }
  145. } else {
  146. // specific resource
  147. try {
  148. String contentType = context.getMimeType(resource);
  149. if (contentType == null) {
  150. contentType = "text/plain";
  151. }
  152. if (contentType.startsWith("text")) {
  153. content = JGitUtils.getStringContent(r, tree, resource, encodings).getBytes(
  154. Constants.ENCODING);
  155. } else {
  156. content = JGitUtils.getByteContent(r, tree, resource, false);
  157. }
  158. response.setContentType(contentType);
  159. } catch (Exception e) {
  160. }
  161. }
  162. // no content, try custom 404 page
  163. if (ArrayUtils.isEmpty(content)) {
  164. String custom404 = JGitUtils.getStringContent(r, tree, "404.html", encodings);
  165. if (!StringUtils.isEmpty(custom404)) {
  166. content = custom404.getBytes(Constants.ENCODING);
  167. }
  168. // still no content
  169. if (ArrayUtils.isEmpty(content)) {
  170. String str = MessageFormat.format(
  171. "# Error\nSorry, the requested resource **{0}** was not found.",
  172. resource);
  173. content = MarkdownUtils.transformMarkdown(str).getBytes(Constants.ENCODING);
  174. }
  175. try {
  176. // output the content
  177. logger.warn("Pages 404: " + resource);
  178. response.setStatus(HttpServletResponse.SC_NOT_FOUND);
  179. response.getOutputStream().write(content);
  180. response.flushBuffer();
  181. } catch (Throwable t) {
  182. logger.error("Failed to write page to client", t);
  183. }
  184. return;
  185. }
  186. // check to see if we should transform markdown files
  187. for (String ext : GitBlit.getStrings(Keys.web.markdownExtensions)) {
  188. if (resource.endsWith(ext)) {
  189. String mkd = new String(content, Constants.ENCODING);
  190. content = MarkdownUtils.transformMarkdown(mkd).getBytes(Constants.ENCODING);
  191. break;
  192. }
  193. }
  194. try {
  195. // output the content
  196. response.getOutputStream().write(content);
  197. response.flushBuffer();
  198. } catch (Throwable t) {
  199. logger.error("Failed to write page to client", t);
  200. }
  201. // close the repository
  202. r.close();
  203. } catch (Throwable t) {
  204. logger.error("Failed to write page to client", t);
  205. }
  206. }
  207. private void error(HttpServletResponse response, String mkd) throws ServletException,
  208. IOException, ParseException {
  209. String content = MarkdownUtils.transformMarkdown(mkd);
  210. response.setContentType("text/html; charset=" + Constants.ENCODING);
  211. response.getWriter().write(content);
  212. }
  213. @Override
  214. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  215. throws ServletException, IOException {
  216. processRequest(request, response);
  217. }
  218. @Override
  219. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  220. throws ServletException, IOException {
  221. processRequest(request, response);
  222. }
  223. }