From 15685d3b2d316b46b0e0a87401d946de1882893a Mon Sep 17 00:00:00 2001 From: Martin Spielmann Date: Sat, 30 Jul 2016 11:07:56 +0200 Subject: [PATCH] Replaced getRelativePathPrefixToContextRoot with method in GitBlitRequestUtils --- .../gitblit/utils/GitBlitRequestUtils.java | 36 +++++++++++++++---- .../com/gitblit/wicket/MarkupProcessor.java | 7 ++-- .../com/gitblit/wicket/pages/BasePage.java | 2 +- .../gitblit/wicket/pages/EditTeamPage.java | 1 + .../com/gitblit/wicket/pages/ProjectPage.java | 3 +- .../com/gitblit/wicket/pages/RawPage.java | 17 ++++----- .../gitblit/wicket/pages/RepositoryPage.java | 6 ++-- .../com/gitblit/wicket/pages/TreePage.java | 3 +- .../wicket/panels/ProjectRepositoryPanel.java | 3 +- 9 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/gitblit/utils/GitBlitRequestUtils.java b/src/main/java/com/gitblit/utils/GitBlitRequestUtils.java index 7a7e5290..74b3854b 100644 --- a/src/main/java/com/gitblit/utils/GitBlitRequestUtils.java +++ b/src/main/java/com/gitblit/utils/GitBlitRequestUtils.java @@ -4,22 +4,44 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.wicket.Page; +import org.apache.wicket.protocol.http.servlet.ServletWebRequest; +import org.apache.wicket.request.Request; import org.apache.wicket.request.Url; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.mapper.parameter.PageParameters; public class GitBlitRequestUtils { - public static HttpServletRequest getServletRequest(){ - return (HttpServletRequest)RequestCycle.get().getRequest().getContainerRequest(); + public static HttpServletRequest getServletRequest() { + return (HttpServletRequest) RequestCycle.get().getRequest().getContainerRequest(); } - - public static HttpServletResponse getServletResponse(){ - return (HttpServletResponse)RequestCycle.get().getResponse().getContainerResponse(); + + public static HttpServletResponse getServletResponse() { + return (HttpServletResponse) RequestCycle.get().getResponse().getContainerResponse(); } - public static String toAbsoluteUrl(Class pageClass, PageParameters params){ + public static String toAbsoluteUrl(Class pageClass, PageParameters params) { String relativeUrl = RequestCycle.get().urlFor(pageClass, params).toString(); return RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse(relativeUrl)); } -} + /** + * COPIED FROM WICKET 1.3 Docs: + * + * Gets a prefix to make this relative to the context root. + * + * For example, if your context root is http://server.com/myApp/ and the + * request is for /myApp/mountedPage/, then the prefix returned might be + * "../../". + * + * For a particular technology, this might return either an absolute prefix + * or a relative one. + */ + public static String getRelativePathPrefixToContextRoot() { + // String contextUrl = + // RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot(); + // TODO: test it! i thing deeper mounted pages will not work yet + Request r = RequestCycle.get().getRequest(); + String p = r.getPrefixToContextPath(); + return p; + } +} diff --git a/src/main/java/com/gitblit/wicket/MarkupProcessor.java b/src/main/java/com/gitblit/wicket/MarkupProcessor.java index 7826a3b2..50d64697 100644 --- a/src/main/java/com/gitblit/wicket/MarkupProcessor.java +++ b/src/main/java/com/gitblit/wicket/MarkupProcessor.java @@ -57,6 +57,7 @@ import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.models.PathModel; import com.gitblit.servlet.RawServlet; +import com.gitblit.utils.GitBlitRequestUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; @@ -268,7 +269,7 @@ public class MarkupProcessor { if (imagePath.indexOf("://") == -1) { // relative image String path = doc.getRelativePath(imagePath); - String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot(); + String contextUrl = GitBlitRequestUtils.getRelativePathPrefixToContextRoot(); url = RawServlet.asLink(contextUrl, repositoryName, commitId, path); } else { // absolute image @@ -325,7 +326,7 @@ public class MarkupProcessor { if (node.url.indexOf("://") == -1) { // repository-relative image link String path = doc.getRelativePath(node.url); - String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot(); + String contextUrl = GitBlitRequestUtils.getRelativePathPrefixToContextRoot(); String url = RawServlet.asLink(contextUrl, repositoryName, commitId, path); return new Rendering(url, text); } @@ -339,7 +340,7 @@ public class MarkupProcessor { if (url.indexOf("://") == -1) { // repository-relative image link String path = doc.getRelativePath(url); - String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot(); + String contextUrl = GitBlitRequestUtils.getRelativePathPrefixToContextRoot(); String wurl = RawServlet.asLink(contextUrl, repositoryName, commitId, path); rendering = new Rendering(wurl, alt); } else { diff --git a/src/main/java/com/gitblit/wicket/pages/BasePage.java b/src/main/java/com/gitblit/wicket/pages/BasePage.java index 1b44e875..06b6a6b8 100644 --- a/src/main/java/com/gitblit/wicket/pages/BasePage.java +++ b/src/main/java/com/gitblit/wicket/pages/BasePage.java @@ -105,7 +105,7 @@ public abstract class BasePage extends SessionPage { } protected String getContextUrl() { - return getRequest().getRelativePathPrefixToContextRoot(); + return GitBlitRequestUtils.getRelativePathPrefixToContextRoot(); } protected String getCanonicalUrl() { diff --git a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java index 9f9e6b1c..8c735a5d 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Set; import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.AttributeModifier; import org.apache.wicket.extensions.markup.html.form.palette.Palette; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; diff --git a/src/main/java/com/gitblit/wicket/pages/ProjectPage.java b/src/main/java/com/gitblit/wicket/pages/ProjectPage.java index 5da07e6d..101f89e1 100644 --- a/src/main/java/com/gitblit/wicket/pages/ProjectPage.java +++ b/src/main/java/com/gitblit/wicket/pages/ProjectPage.java @@ -35,6 +35,7 @@ import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.servlet.SyndicationServlet; +import com.gitblit.utils.GitBlitRequestUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.CacheControl; @@ -107,7 +108,7 @@ public class ProjectPage extends DashboardPage { add(new Label("projectTitle", project.getDisplayName())); add(new Label("projectDescription", project.description)); - String feedLink = SyndicationServlet.asLink(getRequest().getRelativePathPrefixToContextRoot(), projectName, null, 0); + String feedLink = SyndicationServlet.asLink(GitBlitRequestUtils.getRelativePathPrefixToContextRoot(), projectName, null, 0); add(new ExternalLink("syndication", feedLink)); add(WicketUtils.syndicationDiscoveryLink(SyndicationServlet.getTitle(project.getDisplayName(), diff --git a/src/main/java/com/gitblit/wicket/pages/RawPage.java b/src/main/java/com/gitblit/wicket/pages/RawPage.java index 2faa4185..5f94198e 100644 --- a/src/main/java/com/gitblit/wicket/pages/RawPage.java +++ b/src/main/java/com/gitblit/wicket/pages/RawPage.java @@ -27,8 +27,8 @@ import javax.servlet.http.HttpServletResponse; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.http.WebRequest; import org.apache.wicket.request.http.WebResponse; +import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException; import org.apache.wicket.request.mapper.parameter.PageParameters; -import org.apache.wicket.protocol.http.servlet.AbortWithWebErrorCodeException; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.slf4j.Logger; @@ -37,6 +37,7 @@ import org.slf4j.LoggerFactory; import com.gitblit.Keys; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; +import com.gitblit.utils.GitBlitRequestUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; @@ -95,7 +96,7 @@ public class RawPage extends SessionPage { final String objectNotFound = MessageFormat.format("Raw page failed to find object {0} in {1}", objectId, repositoryName); logger.error(objectNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, objectNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, objectNotFound); } contentType = "application/octet-stream"; response.setContentType(contentType); @@ -112,7 +113,7 @@ public class RawPage extends SessionPage { final String commitNotFound = MessageFormat.format("Raw page failed to find commit {0} in {1}", objectId, repositoryName); logger.error(commitNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, commitNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, commitNotFound); } String filename = blobPath; @@ -148,7 +149,7 @@ public class RawPage extends SessionPage { byte[] image = JGitUtils.getByteContent(r, commit.getTree(), blobPath, true); if (image == null) { logger.error(blobNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); } contentType = "image/" + extension.toLowerCase(); response.setContentType(contentType); @@ -164,7 +165,7 @@ public class RawPage extends SessionPage { byte[] binary = JGitUtils.getByteContent(r, commit.getTree(), blobPath, true); if (binary == null) { logger.error(blobNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); } contentType = "application/octet-stream"; response.setContentLength(binary.length); @@ -172,7 +173,7 @@ public class RawPage extends SessionPage { try { WebRequest request = (WebRequest) requestCycle.getRequest(); - String userAgent = request.getHttpServletRequest().getHeader("User-Agent"); + String userAgent = GitBlitRequestUtils.getServletRequest().getHeader("User-Agent"); if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) { response.setHeader("Content-Disposition", "filename=\"" @@ -201,7 +202,7 @@ public class RawPage extends SessionPage { blobPath, encodings); if (content == null) { logger.error(blobNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); } contentType = "text/plain; charset=UTF-8"; response.setContentType(contentType); @@ -218,7 +219,7 @@ public class RawPage extends SessionPage { encodings); if (content == null) { logger.error(blobNotFound); - throw new AbortWithWebErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); + throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, blobNotFound); } contentType = "text/plain; charset=UTF-8"; response.setContentType(contentType); diff --git a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java index dd8df4a3..504d83c1 100644 --- a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java @@ -168,7 +168,7 @@ public abstract class RepositoryPage extends RootPage { NavigationPanel navigationPanel = new NavigationPanel("repositoryNavPanel", getRepoNavPageClass(), navLinks); add(navigationPanel); - add(new ExternalLink("syndication", SyndicationServlet.asLink(getRequest() + add(new ExternalLink("syndication", SyndicationServlet.asLink(GitBlitRequestUtils .getRelativePathPrefixToContextRoot(), getRepositoryName(), null, 0))); // add floating search form @@ -246,7 +246,7 @@ public abstract class RepositoryPage extends RootPage { // per-repository extra navlinks if (JGitUtils.getPagesBranch(r) != null) { ExternalNavLink pagesLink = new ExternalNavLink("gb.pages", PagesServlet.asLink( - getRequest().getRelativePathPrefixToContextRoot(), getRepositoryName(), null), true); + GitBlitRequestUtils.getRelativePathPrefixToContextRoot(), getRepositoryName(), null), true); navLinks.add(pagesLink); } @@ -426,7 +426,7 @@ public abstract class RepositoryPage extends RootPage { protected void addSyndicationDiscoveryLink() { add(WicketUtils.syndicationDiscoveryLink(SyndicationServlet.getTitle(repositoryName, - objectId), SyndicationServlet.asLink(getRequest() + objectId), SyndicationServlet.asLink(GitBlitRequestUtils .getRelativePathPrefixToContextRoot(), repositoryName, objectId, 0))); } diff --git a/src/main/java/com/gitblit/wicket/pages/TreePage.java b/src/main/java/com/gitblit/wicket/pages/TreePage.java index 73a03f86..c1adf6e1 100644 --- a/src/main/java/com/gitblit/wicket/pages/TreePage.java +++ b/src/main/java/com/gitblit/wicket/pages/TreePage.java @@ -39,6 +39,7 @@ import com.gitblit.models.SubmoduleModel; import com.gitblit.models.UserModel; import com.gitblit.servlet.RawServlet; import com.gitblit.utils.ByteFormat; +import com.gitblit.utils.GitBlitRequestUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.wicket.CacheControl; import com.gitblit.wicket.GitBlitWebSession; @@ -64,7 +65,7 @@ public class TreePage extends RepositoryPage { // tree page links add(new BookmarkablePageLink("historyLink", HistoryPage.class, WicketUtils.newPathParameter(repositoryName, objectId, path))); - add(new CompressedDownloadsPanel("compressedLinks", getRequest() + add(new CompressedDownloadsPanel("compressedLinks", GitBlitRequestUtils .getRelativePathPrefixToContextRoot(), repositoryName, objectId, path)); add(new CommitHeaderPanel("commitHeader", repositoryName, commit)); diff --git a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java index 73b72bbc..503b3d12 100644 --- a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java @@ -31,6 +31,7 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.servlet.SyndicationServlet; import com.gitblit.utils.ArrayUtils; +import com.gitblit.utils.GitBlitRequestUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.WicketUtils; @@ -158,7 +159,7 @@ public class ProjectRepositoryPanel extends BasePanel { add(new Label("repositorySize", localizer.getString("gb.empty", parent)).setEscapeModelStrings(false)); } - add(new ExternalLink("syndication", SyndicationServlet.asLink(getRequest() + add(new ExternalLink("syndication", SyndicationServlet.asLink(GitBlitRequestUtils .getRelativePathPrefixToContextRoot(), entry.name, null, 0))); } } -- 2.39.5