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.core.ContextEnum;
022import org.apache.wiki.api.core.Engine;
023
024import javax.servlet.http.HttpServletRequest;
025import java.util.Properties;
026
027/**
028 *  A specific URL constructor that returns easy-to-grok URLs for VIEW and ATTACH contexts, but goes through JSP pages otherwise.
029 * 
030 *  @since 2.2
031 */
032public class ShortViewURLConstructor extends ShortURLConstructor {
033
034    /**
035     *  {@inheritDoc}
036     */
037    @Override public void initialize( final Engine engine, final Properties properties ) {
038        super.initialize( engine, properties );
039    }
040    
041    private String makeURL( final String context, final String name ) {
042        final String viewurl = "%p" + m_urlPrefix + "%n";
043        if( context.equals( ContextEnum.PAGE_VIEW.getRequestContext() ) ) {
044            if( name == null ) {
045                return doReplacement("%u","" );
046            }
047            return doReplacement( viewurl, name );
048        }
049
050        return doReplacement( DefaultURLConstructor.getURLPattern( context, name ), name );
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public String makeURL( final String context, final String name, String parameters ) {
058        if( parameters != null && !parameters.isEmpty() ) {
059            if( context.equals( ContextEnum.PAGE_ATTACH.getRequestContext() ) || context.equals( ContextEnum.PAGE_VIEW.getRequestContext() ) || name == null ) {
060                parameters = "?" + parameters;
061            } else if( context.equals(ContextEnum.PAGE_NONE.getRequestContext()) ) {
062                parameters = (name.indexOf('?') != -1 ) ? "&" : "?" + parameters;
063            } else {
064                parameters = "&" + parameters;
065            }
066        } else {
067            parameters = "";
068        }
069        return makeURL( context, name ) + parameters;
070    }
071    
072    /**
073     *   Since we're only called from WikiServlet, where we get the VIEW requests, we can safely return this.
074     *   
075     * @param request The HTTP Request that was used to end up in this page.
076     * @return always returns "Wiki.jsp"
077     */
078    @Override
079    public String getForwardPage( final HttpServletRequest request )
080    {        
081        return "Wiki.jsp";
082    }
083
084}