This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit c6a58f79353b1d6dd689e0070b3326f20902ebe4 Author: juanpablo <[email protected]> AuthorDate: Wed Feb 26 19:35:37 2020 +0100 small refactor move parsePageFromURL( HttpServletRequest, Charset ) from DefaultURLConstructor to URLConstructor --- .../src/main/java/org/apache/wiki/WikiServlet.java | 3 +- .../java/org/apache/wiki/ui/WikiJSPFilter.java | 8 ++-- .../org/apache/wiki/url/DefaultURLConstructor.java | 43 +++++----------------- .../org/apache/wiki/url/ShortURLConstructor.java | 2 +- .../java/org/apache/wiki/url/URLConstructor.java | 22 +++++++++++ 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java index a2dcdaa..d401dd4 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java @@ -21,7 +21,6 @@ package org.apache.wiki; import net.sf.ehcache.CacheManager; import org.apache.log4j.Logger; import org.apache.wiki.api.core.Engine; -import org.apache.wiki.url.DefaultURLConstructor; import org.apache.wiki.url.URLConstructor; import javax.servlet.RequestDispatcher; @@ -83,7 +82,7 @@ public class WikiServlet extends HttpServlet { */ @Override public void doGet( final HttpServletRequest req, final HttpServletResponse res ) throws IOException, ServletException { - String pageName = DefaultURLConstructor.parsePageFromURL( req, m_engine.getContentEncoding() ); + String pageName = URLConstructor.parsePageFromURL( req, m_engine.getContentEncoding() ); log.info( "Request for page: " + pageName ); if( pageName == null ) { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java index 3924000..a3f9139 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java @@ -26,7 +26,7 @@ import org.apache.wiki.WikiContext; import org.apache.wiki.api.core.Engine; import org.apache.wiki.event.WikiEventManager; import org.apache.wiki.event.WikiPageEvent; -import org.apache.wiki.url.DefaultURLConstructor; +import org.apache.wiki.url.URLConstructor; import org.apache.wiki.util.TextUtil; import javax.servlet.FilterChain; @@ -94,15 +94,13 @@ public class WikiJSPFilter extends WikiServletFilter { final HttpServletResponseWrapper responseWrapper = new JSPWikiServletResponseWrapper( ( HttpServletResponse )response, m_wiki_encoding, useEncoding ); // fire PAGE_REQUESTED event - final String pagename = DefaultURLConstructor.parsePageFromURL( ( HttpServletRequest )request, - Charset.forName( response.getCharacterEncoding() ) ); + final String pagename = URLConstructor.parsePageFromURL( ( HttpServletRequest )request, Charset.forName( response.getCharacterEncoding() ) ); fireEvent( WikiPageEvent.PAGE_REQUESTED, pagename ); super.doFilter( request, responseWrapper, chain ); // The response is now complete. Lets replace the markers now. - // WikiContext is only available after doFilter! (That is after - // interpreting the jsp) + // WikiContext is only available after doFilter! (That is after interpreting the jsp) try { w.enterState( "Delivering response", 30 ); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/url/DefaultURLConstructor.java b/jspwiki-main/src/main/java/org/apache/wiki/url/DefaultURLConstructor.java index f127c06..370b6c4 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/url/DefaultURLConstructor.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/url/DefaultURLConstructor.java @@ -40,16 +40,15 @@ public class DefaultURLConstructor implements URLConstructor { protected Engine m_engine; - /** - * Contains the absolute path of the JSPWiki Web application without the actual servlet (which is the m_urlPrefix). - */ + /** Contains the absolute path of the JSPWiki Web application without the actual servlet (which is the m_urlPrefix). */ protected String m_pathPrefix = ""; /** * * {@inheritDoc} */ - @Override public void initialize( final Engine engine, final Properties properties ) { + @Override + public void initialize( final Engine engine, final Properties properties ) { m_engine = engine; m_pathPrefix = engine.getBaseURL() + "/"; } @@ -125,7 +124,8 @@ public class DefaultURLConstructor implements URLConstructor { * * {@inheritDoc} */ - @Override public String makeURL( final String context, final String name, String parameters ) { + @Override + public String makeURL( final String context, final String name, String parameters ) { if( parameters != null && parameters.length() > 0 ) { if( context.equals( WikiContext.ATTACH ) ) { parameters = "?" + parameters; @@ -145,47 +145,24 @@ public class DefaultURLConstructor implements URLConstructor { * * {@inheritDoc} */ - @Override public String parsePage( final String context, final HttpServletRequest request, final Charset encoding ) { + @Override + public String parsePage( final String context, final HttpServletRequest request, final Charset encoding ) { String pagereq = request.getParameter( "page" ); if( context.equals(WikiContext.ATTACH) ) { - pagereq = parsePageFromURL( request, encoding ); + pagereq = URLConstructor.parsePageFromURL( request, encoding ); } return pagereq; } /** - * Takes the name of the page from the request URI. The initial slash is also removed. If there is no page, returns null. - * - * @param request The request to parse - * @param encoding The encoding to use - * - * @return a parsed page name, or null, if it cannot be found - */ - public static String parsePageFromURL( final HttpServletRequest request, final Charset encoding ) { - final String name = request.getPathInfo(); - if( name == null || name.length() <= 1 ) { - return null; - } else if( name.charAt(0) == '/' ) { - return name.substring(1); - } - - // - // This is required, because by default all URLs are handled as Latin1, even if they are really UTF-8. - // - // name = TextUtil.urlDecode( name, encoding ); - - return name; - } - - - /** * This method is not needed for the DefaultURLConstructor. * * @param request The HTTP Request that was used to end up in this page. * @return "Wiki.jsp", "PageInfo.jsp", etc. Just return the name, JSPWiki will figure out the page. */ - @Override public String getForwardPage( final HttpServletRequest request ) { + @Override + public String getForwardPage( final HttpServletRequest request ) { return "Wiki.jsp"; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/url/ShortURLConstructor.java b/jspwiki-main/src/main/java/org/apache/wiki/url/ShortURLConstructor.java index 69530db..b94a7bf 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/url/ShortURLConstructor.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/url/ShortURLConstructor.java @@ -144,7 +144,7 @@ public class ShortURLConstructor extends DefaultURLConstructor { public String parsePage( final String context, final HttpServletRequest request, final Charset encoding ) { final String pagereq = request.getParameter( "page" ); if( pagereq == null ) { - return parsePageFromURL( request, encoding ); + return URLConstructor.parsePageFromURL( request, encoding ); } return pagereq; diff --git a/jspwiki-main/src/main/java/org/apache/wiki/url/URLConstructor.java b/jspwiki-main/src/main/java/org/apache/wiki/url/URLConstructor.java index a0ac40f..862e275 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/url/URLConstructor.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/url/URLConstructor.java @@ -77,4 +77,26 @@ public interface URLConstructor { */ String getForwardPage( HttpServletRequest request ); + /** + * Takes the name of the page from the request URI. The initial slash is also removed. If there is no page, returns null. + * + * @param request The request to parse + * @param encoding The encoding to use + * + * @return a parsed page name, or null, if it cannot be found + */ + static String parsePageFromURL( final HttpServletRequest request, final Charset encoding ) { + final String name = request.getPathInfo(); + if( name == null || name.length() <= 1 ) { + return null; + } else if( name.charAt(0) == '/' ) { + return name.substring(1); + } + + // This is required, because by default all URLs are handled as Latin1, even if they are really UTF-8. + // name = TextUtil.urlDecode( name, encoding ); + + return name; + } + }
