| | |
| | |
|
| | | import java.util.Date;
|
| | |
|
| | | import javax.inject.Inject;
|
| | | import javax.inject.Singleton;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | import org.apache.wicket.protocol.http.WicketFilter;
|
| | | import org.apache.wicket.util.string.Strings;
|
| | | import org.eclipse.jgit.lib.Repository;
|
| | | import org.eclipse.jgit.revwalk.RevCommit;
|
| | |
|
| | | import com.gitblit.GitBlit;
|
| | | import com.gitblit.IStoredSettings;
|
| | | import com.gitblit.Keys;
|
| | | import com.gitblit.dagger.DaggerWicketFilter;
|
| | | import com.gitblit.manager.IProjectManager;
|
| | | import com.gitblit.manager.IRepositoryManager;
|
| | | import com.gitblit.manager.IRuntimeManager;
|
| | | import com.gitblit.models.ProjectModel;
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.utils.JGitUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | | /**
|
| | | * |
| | | *
|
| | | * Customization of the WicketFilter to allow smart browser-side caching of
|
| | | * some pages.
|
| | | * |
| | | *
|
| | | * @author James Moger
|
| | | *
|
| | | */
|
| | | public class GitblitWicketFilter extends WicketFilter {
|
| | | |
| | | @Singleton
|
| | | public class GitblitWicketFilter extends DaggerWicketFilter {
|
| | |
|
| | | private final IStoredSettings settings;
|
| | |
|
| | | private final IRuntimeManager runtimeManager;
|
| | |
|
| | | private final IRepositoryManager repositoryManager;
|
| | |
|
| | | private final IProjectManager projectManager;
|
| | |
|
| | | @Inject
|
| | | public GitblitWicketFilter(
|
| | | IRuntimeManager runtimeManager,
|
| | | IRepositoryManager repositoryManager,
|
| | | IProjectManager projectManager) {
|
| | |
|
| | | super();
|
| | | this.settings = runtimeManager.getSettings();
|
| | | this.runtimeManager = runtimeManager;
|
| | | this.repositoryManager = repositoryManager;
|
| | | this.projectManager = projectManager;
|
| | | }
|
| | |
|
| | | /**
|
| | | * Determines the last-modified date of the requested resource.
|
| | | * |
| | | *
|
| | | * @param servletRequest
|
| | | * @return The last modified time stamp
|
| | | */
|
| | | @Override
|
| | | protected long getLastModified(final HttpServletRequest servletRequest) {
|
| | | final String pathInfo = getRelativePath(servletRequest);
|
| | | if (Strings.isEmpty(pathInfo))
|
| | | if (Strings.isEmpty(pathInfo)) {
|
| | | return -1;
|
| | | }
|
| | | long lastModified = super.getLastModified(servletRequest);
|
| | | if (lastModified > -1) {
|
| | | return lastModified;
|
| | | }
|
| | | |
| | |
|
| | | // try to match request against registered CacheControl pages
|
| | | String [] paths = pathInfo.split("/");
|
| | | |
| | |
|
| | | String page = paths[0];
|
| | | String repo = "";
|
| | | String commitId = "";
|
| | |
| | | if (paths.length >= 3) {
|
| | | commitId = paths[2];
|
| | | }
|
| | | |
| | |
|
| | | if (!StringUtils.isEmpty(servletRequest.getParameter("r"))) {
|
| | | repo = servletRequest.getParameter("r");
|
| | | }
|
| | | if (!StringUtils.isEmpty(servletRequest.getParameter("h"))) {
|
| | | commitId = servletRequest.getParameter("h");
|
| | | }
|
| | | |
| | | repo = repo.replace("%2f", "/").replace("%2F", "/").replace(GitBlit.getChar(Keys.web.forwardSlashCharacter, '/'), '/');
|
| | |
|
| | | repo = repo.replace("%2f", "/").replace("%2F", "/").replace(settings.getChar(Keys.web.forwardSlashCharacter, '/'), '/');
|
| | |
|
| | | GitBlitWebApp app = (GitBlitWebApp) getWebApplication();
|
| | | int expires = GitBlit.getInteger(Keys.web.pageCacheExpires, 0);
|
| | | int expires = settings.getInteger(Keys.web.pageCacheExpires, 0);
|
| | | if (!StringUtils.isEmpty(page) && app.isCacheablePage(page) && expires > 0) {
|
| | | // page can be cached by the browser
|
| | | CacheControl cacheControl = app.getCacheControl(page);
|
| | | Date bootDate = GitBlit.getBootDate();
|
| | | Date bootDate = runtimeManager.getBootDate();
|
| | | switch (cacheControl.value()) {
|
| | | case ACTIVITY:
|
| | | // returns the last activity date of the server
|
| | | Date activityDate = GitBlit.getLastActivityDate();
|
| | | Date activityDate = repositoryManager.getLastActivityDate();
|
| | | if (activityDate != null) {
|
| | | return activityDate.after(bootDate) ? activityDate.getTime() : bootDate.getTime();
|
| | | }
|
| | |
| | | return bootDate.getTime();
|
| | | case PROJECT:
|
| | | // return the latest change date for the project OR the boot date
|
| | | ProjectModel project = GitBlit.self().getProjectModel(StringUtils.getRootPath(repo));
|
| | | ProjectModel project = projectManager.getProjectModel(StringUtils.getRootPath(repo));
|
| | | if (project != null) {
|
| | | return project.lastChange.after(bootDate) ? project.lastChange.getTime() : bootDate.getTime();
|
| | | }
|
| | |
| | | case REPOSITORY:
|
| | | // return the lastest change date for the repository OR the boot
|
| | | // date, whichever is latest
|
| | | RepositoryModel repository = GitBlit.self().getRepositoryModel(repo);
|
| | | RepositoryModel repository = repositoryManager.getRepositoryModel(repo);
|
| | | if (repository != null && repository.lastChange != null) {
|
| | | return repository.lastChange.after(bootDate) ? repository.lastChange.getTime() : bootDate.getTime();
|
| | | }
|
| | |
| | | // no commit id, use boot date
|
| | | return bootDate.getTime();
|
| | | } else {
|
| | | // last modified date is the commit date |
| | | // last modified date is the commit date
|
| | | Repository r = null;
|
| | | try {
|
| | | // return the timestamp of the associated commit
|
| | | r = GitBlit.self().getRepository(repo);
|
| | | r = repositoryManager.getRepository(repo);
|
| | | if (r != null) {
|
| | | RevCommit commit = JGitUtils.getCommit(r, commitId);
|
| | | if (commit != null) {
|
| | |
| | | default:
|
| | | break;
|
| | | }
|
| | | } |
| | | }
|
| | |
|
| | | return -1;
|
| | | }
|