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;
022
023import javax.servlet.http.HttpServletRequest;
024import java.io.IOException;
025import java.nio.charset.Charset;
026
027
028/**
029 *  Provides an interface through which JSPWiki constructs URLs. JSPWiki calls the methods of this interface whenever an URL
030 *  that points to any JSPWiki internals is required.  For example, if you need to find an URL to the editor page for page
031 *  "TextFormattingRules", you would call makeURL( WikiContext.EDIT, "TextFormattingRules", false, null );
032 *
033 *  @since 2.2
034 */
035public interface URLConstructor extends Initializable {
036
037    /**
038     *  Constructs the URL with a bunch of parameters.
039     *
040     *  @param context The request context (@see WikiContext) that you want the URL for
041     *  @param name The page name (or in case of WikiContext.NONE, the auxiliary JSP page
042     *              or resource you want to point at).  This must be URL encoded.  Null is NOT safe.
043     *  @param parameters An URL parameter string (these must be URL-encoded, and separated with &)
044     *  @return An URL pointing to the resource.  Must never return null - throw an InternalWikiException  if something goes wrong.
045     */
046    String makeURL( String context, String name, String parameters );
047
048    /**
049     *  Should parse the "page" parameter from the actual request. This is essentially the reverse of makeURL() - whenever
050     *  a request constructed by calls to makeURL() is passed to this routine, it MUST be able to parse the resource name
051     *  (WikiPage, Attachment, other resource) from the request.
052     *
053     *  @param context In which request context the request was made (this should help in parsing)
054     *  @param request The HTTP request that was used when coming here
055     *  @param encoding The encoding with which the request was made (UTF-8 or ISO-8859-1).
056     *  @return This method must return the name of the resource.
057     *  @throws IOException If parsing failes
058     */
059    String parsePage( String context, HttpServletRequest request, Charset encoding ) throws IOException;
060
061    /**
062     *  Returns information which JSP page should continue handling this type of request.
063     *
064     * @param request The HTTP Request that was used to end up in this page.
065     * @return "Wiki.jsp", "PageInfo.jsp", etc.  Just return the name, JSPWiki will figure out the page.
066     */
067    String getForwardPage( HttpServletRequest request );
068
069    /**
070     *  Takes the name of the page from the request URI. The initial slash is also removed.  If there is no page, returns null.
071     *
072     *  @param request The request to parse
073     *  @param encoding The encoding to use
074     *
075     *  @return a parsed page name, or null, if it cannot be found
076     */
077    static String parsePageFromURL( final HttpServletRequest request, final Charset encoding ) {
078        final String name = request.getPathInfo();
079        if( name == null || name.length() <= 1 ) {
080            return null;
081        } else if( name.charAt(0) == '/' ) {
082            return name.substring(1);
083        }
084
085        //  This is required, because by default all URLs are handled as Latin1, even if they are really UTF-8.
086        // name = TextUtil.urlDecode( name, encoding );
087
088        return name;
089    }
090
091}