You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitblitWicketFilter.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2013 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.wicket;
  17. import java.util.Date;
  18. import javax.servlet.http.HttpServletRequest;
  19. import org.apache.wicket.protocol.http.WicketFilter;
  20. import org.apache.wicket.util.string.Strings;
  21. import org.eclipse.jgit.lib.Repository;
  22. import org.eclipse.jgit.revwalk.RevCommit;
  23. import com.gitblit.GitBlit;
  24. import com.gitblit.Keys;
  25. import com.gitblit.models.ProjectModel;
  26. import com.gitblit.models.RepositoryModel;
  27. import com.gitblit.utils.JGitUtils;
  28. import com.gitblit.utils.StringUtils;
  29. /**
  30. *
  31. * Customization of the WicketFilter to allow smart browser-side caching of
  32. * some pages.
  33. *
  34. * @author James Moger
  35. *
  36. */
  37. public class GitblitWicketFilter extends WicketFilter {
  38. /**
  39. * Determines the last-modified date of the requested resource.
  40. *
  41. * @param servletRequest
  42. * @return The last modified time stamp
  43. */
  44. protected long getLastModified(final HttpServletRequest servletRequest) {
  45. final String pathInfo = getRelativePath(servletRequest);
  46. if (Strings.isEmpty(pathInfo))
  47. return -1;
  48. long lastModified = super.getLastModified(servletRequest);
  49. if (lastModified > -1) {
  50. return lastModified;
  51. }
  52. // try to match request against registered CacheControl pages
  53. String [] paths = pathInfo.split("/");
  54. String page = paths[0];
  55. String repo = "";
  56. String commitId = "";
  57. if (paths.length >= 2) {
  58. repo = paths[1];
  59. }
  60. if (paths.length >= 3) {
  61. commitId = paths[2];
  62. }
  63. if (!StringUtils.isEmpty(servletRequest.getParameter("r"))) {
  64. repo = servletRequest.getParameter("r");
  65. }
  66. if (!StringUtils.isEmpty(servletRequest.getParameter("h"))) {
  67. commitId = servletRequest.getParameter("h");
  68. }
  69. repo = repo.replace("%2f", "/").replace("%2F", "/").replace(GitBlit.getChar(Keys.web.forwardSlashCharacter, '/'), '/');
  70. GitBlitWebApp app = (GitBlitWebApp) getWebApplication();
  71. int expires = GitBlit.getInteger(Keys.web.pageCacheExpires, 0);
  72. if (!StringUtils.isEmpty(page) && app.isCacheablePage(page) && expires > 0) {
  73. // page can be cached by the browser
  74. CacheControl cacheControl = app.getCacheControl(page);
  75. Date bootDate = GitBlit.getBootDate();
  76. switch (cacheControl.value()) {
  77. case ACTIVITY:
  78. // returns the last activity date of the server
  79. Date activityDate = GitBlit.getLastActivityDate();
  80. if (activityDate != null) {
  81. return activityDate.after(bootDate) ? activityDate.getTime() : bootDate.getTime();
  82. }
  83. return bootDate.getTime();
  84. case BOOT:
  85. // return the boot date of the server
  86. return bootDate.getTime();
  87. case PROJECT:
  88. // return the latest change date for the project OR the boot date
  89. ProjectModel project = GitBlit.self().getProjectModel(StringUtils.getRootPath(repo));
  90. if (project != null) {
  91. return project.lastChange.after(bootDate) ? project.lastChange.getTime() : bootDate.getTime();
  92. }
  93. break;
  94. case REPOSITORY:
  95. // return the lastest change date for the repository OR the boot
  96. // date, whichever is latest
  97. RepositoryModel repository = GitBlit.self().getRepositoryModel(repo);
  98. if (repository != null && repository.lastChange != null) {
  99. return repository.lastChange.after(bootDate) ? repository.lastChange.getTime() : bootDate.getTime();
  100. }
  101. break;
  102. case COMMIT:
  103. // get the date of the specified commit
  104. if (StringUtils.isEmpty(commitId)) {
  105. // no commit id, use boot date
  106. return bootDate.getTime();
  107. } else {
  108. // last modified date is the commit date
  109. Repository r = null;
  110. try {
  111. // return the timestamp of the associated commit
  112. r = GitBlit.self().getRepository(repo);
  113. if (r != null) {
  114. RevCommit commit = JGitUtils.getCommit(r, commitId);
  115. if (commit != null) {
  116. Date date = JGitUtils.getCommitDate(commit);
  117. return date.after(bootDate) ? date.getTime() : bootDate.getTime();
  118. }
  119. }
  120. } finally {
  121. if (r != null) {
  122. r.close();
  123. }
  124. }
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. return -1;
  132. }
  133. }