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.8KB

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