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 5.1KB

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