您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SyndicationServlet.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2011 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.text.MessageFormat;
  18. import java.util.List;
  19. import javax.servlet.http.HttpServlet;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import com.gitblit.models.RepositoryModel;
  25. import com.gitblit.utils.JGitUtils;
  26. import com.gitblit.utils.StringUtils;
  27. import com.gitblit.utils.SyndicationUtils;
  28. import com.gitblit.wicket.WicketUtils;
  29. public class SyndicationServlet extends HttpServlet {
  30. private static final long serialVersionUID = 1L;
  31. private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class);
  32. public static String asLink(String baseURL, String repository, String objectId, int length) {
  33. if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
  34. baseURL = baseURL.substring(0, baseURL.length() - 1);
  35. }
  36. StringBuilder url = new StringBuilder();
  37. url.append(baseURL);
  38. url.append(Constants.SYNDICATION_PATH);
  39. url.append(repository);
  40. if (!StringUtils.isEmpty(objectId) || length > 0) {
  41. StringBuilder parameters = new StringBuilder("?");
  42. if (StringUtils.isEmpty(objectId)) {
  43. parameters.append("l=");
  44. parameters.append(length);
  45. } else {
  46. parameters.append("h=");
  47. parameters.append(objectId);
  48. if (length > 0) {
  49. parameters.append("&l=");
  50. parameters.append(length);
  51. }
  52. }
  53. url.append(parameters);
  54. }
  55. return url.toString();
  56. }
  57. public static String getTitle(String repository, String objectId) {
  58. String id = objectId;
  59. if (!StringUtils.isEmpty(id)) {
  60. if (id.startsWith(org.eclipse.jgit.lib.Constants.R_HEADS)) {
  61. id = id.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length());
  62. } else if (id.startsWith(org.eclipse.jgit.lib.Constants.R_REMOTES)) {
  63. id = id.substring(org.eclipse.jgit.lib.Constants.R_REMOTES.length());
  64. } else if (id.startsWith(org.eclipse.jgit.lib.Constants.R_TAGS)) {
  65. id = id.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length());
  66. }
  67. }
  68. return MessageFormat.format("{0} ({1})", repository, id);
  69. }
  70. private void processRequest(javax.servlet.http.HttpServletRequest request,
  71. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  72. java.io.IOException {
  73. String hostURL = WicketUtils.getHostURL(request);
  74. String url = request.getRequestURI().substring(request.getServletPath().length());
  75. if (url.charAt(0) == '/' && url.length() > 1) {
  76. url = url.substring(1);
  77. }
  78. String repositoryName = url;
  79. String objectId = request.getParameter("h");
  80. String l = request.getParameter("l");
  81. int length = GitBlit.getInteger(Keys.web.syndicationEntries, 25);
  82. if (StringUtils.isEmpty(objectId)) {
  83. objectId = org.eclipse.jgit.lib.Constants.HEAD;
  84. }
  85. if (!StringUtils.isEmpty(l)) {
  86. try {
  87. length = Integer.parseInt(l);
  88. } catch (NumberFormatException x) {
  89. }
  90. }
  91. Repository repository = GitBlit.self().getRepository(repositoryName);
  92. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  93. List<RevCommit> commits = JGitUtils.getRevLog(repository, objectId, 0, length);
  94. try {
  95. SyndicationUtils.toRSS(hostURL, getTitle(model.name, objectId), model.description,
  96. model.name, commits, response.getOutputStream());
  97. } catch (Exception e) {
  98. logger.error("An error occurred during feed generation", e);
  99. }
  100. }
  101. @Override
  102. protected void doPost(javax.servlet.http.HttpServletRequest request,
  103. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  104. java.io.IOException {
  105. processRequest(request, response);
  106. }
  107. @Override
  108. protected void doGet(javax.servlet.http.HttpServletRequest request,
  109. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  110. java.io.IOException {
  111. processRequest(request, response);
  112. }
  113. }