001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.url;
020
021import org.apache.wiki.api.engine.Initializable;
022import org.apache.wiki.util.TextUtil;
023
024import javax.servlet.http.HttpServletRequest;
025import java.io.IOException;
026import java.nio.charset.Charset;
027
028
029/**
030 *  Provides an interface through which JSPWiki constructs URLs. JSPWiki calls the methods of this interface whenever an URL
031 *  that points to any JSPWiki internals is required.  For example, if you need to find an URL to the editor page for page
032 *  "TextFormattingRules", you would call makeURL( WikiContext.EDIT, "TextFormattingRules", false, null );
033 *
034 *  @since 2.2
035 */
036public interface URLConstructor extends Initializable {
037
038    /**
039     *  Constructs the URL with a bunch of parameters.
040     *
041     *  @param context The request context (@see WikiContext) that you want the URL for
042     *  @param name The page name (or in case of WikiContext.NONE, the auxiliary JSP page
043     *              or resource you want to point at).  This must be URL encoded.  Null is NOT safe.
044     *  @param parameters An URL parameter string (these must be URL-encoded, and separated with &)
045     *  @return An URL pointing to the resource.  Must never return null - throw an InternalWikiException  if something goes wrong.
046     */
047    String makeURL( String context, String name, String parameters );
048
049    /**
050     *  Should parse the "page" parameter from the actual request. This is essentially the reverse of makeURL() - whenever
051     *  a request constructed by calls to makeURL() is passed to this routine, it MUST be able to parse the resource name
052     *  (WikiPage, Attachment, other resource) from the request.
053     *
054     *  @param context In which request context the request was made (this should help in parsing)
055     *  @param request The HTTP request that was used when coming here
056     *  @param encoding The encoding with which the request was made (UTF-8 or ISO-8859-1).
057     *  @return This method must return the name of the resource.
058     *  @throws IOException If parsing failes
059     */
060    String parsePage( String context, HttpServletRequest request, Charset encoding ) throws IOException;
061
062    /**
063     *  Returns information which JSP page should continue handling this type of request.
064     *
065     * @param request The HTTP Request that was used to end up in this page.
066     * @return "Wiki.jsp", "PageInfo.jsp", etc.  Just return the name, JSPWiki will figure out the page.
067     */
068    String getForwardPage( HttpServletRequest request );
069
070    /**
071     *  Takes the name of the page from the request URI. The initial slash is also removed.  If there is no page, returns null.
072     *
073     *  @param request The request to parse
074     *  @param encoding The encoding to use
075     *
076     *  @return a parsed page name, or null, if it cannot be found
077     */
078    static String parsePageFromURL( final HttpServletRequest request, final Charset encoding ) {
079        final String name = request.getPathInfo();
080        if( name == null || name.length() <= 1 ) {
081            return request.getParameter("page");
082        } else if( name.charAt(0) == '/' ) {
083            return name.substring(1);
084        }
085
086        //  This is required, because by default all URLs are handled as Latin1, even if they are really UTF-8.
087        return TextUtil.urlDecode( name, encoding.name() );
088    }
089
090}