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.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.InternalWikiException;
024import org.apache.wiki.api.core.ContextEnum;
025import org.apache.wiki.api.core.Engine;
026import org.apache.wiki.util.TextUtil;
027
028import javax.servlet.http.HttpServletRequest;
029import java.nio.charset.Charset;
030import java.util.Properties;
031
032
033/**
034 *  Provides a way to do short URLs of the form /wiki/PageName.
035 *
036 *  @since 2.2
037 */
038public class ShortURLConstructor extends DefaultURLConstructor {
039    
040    private static final String DEFAULT_PREFIX = "wiki/";
041    private static final Logger LOG = LogManager.getLogger( ShortURLConstructor.class );
042    
043    /** Contains the path part after the JSPWiki base URL */
044    protected String m_urlPrefix = "";
045    
046    /**
047     *  This corresponds to your WikiServlet path.  By default, it is assumed to be "wiki/", but you can set it to whatever you
048     *  like - including an empty name.
049     */
050    public static final String PROP_PREFIX = "jspwiki.shortURLConstructor.prefix";
051    
052    /** {@inheritDoc} */
053    @Override
054    public void initialize( final Engine engine, final Properties properties ) {
055        super.initialize( engine, properties );
056        
057        m_urlPrefix = TextUtil.getStringProperty( properties, PROP_PREFIX, null );
058        
059        if( m_urlPrefix == null ) {
060            m_urlPrefix = DEFAULT_PREFIX;
061        }
062
063        LOG.info("Short URL prefix path="+m_urlPrefix+" (You can use "+PROP_PREFIX+" to override this)");
064    }
065
066    /**
067     *  Constructs the actual URL based on the context.
068     */
069    private String makeURL( final String context, final String name ) {
070        final String viewurl = "%p" + m_urlPrefix + "%n";
071
072        if( context.equals( ContextEnum.PAGE_VIEW.getRequestContext() ) ) {
073            if( name == null ) {
074                return doReplacement("%u","" );
075            }
076            return doReplacement( viewurl, name );
077        } else if( context.equals( ContextEnum.PAGE_PREVIEW.getRequestContext() ) ) {
078            if( name == null ) {
079                return doReplacement("%u","" );
080            }
081            return doReplacement( viewurl + "?do=Preview", name );
082        } else if( context.equals( ContextEnum.PAGE_EDIT.getRequestContext() ) ) {
083            return doReplacement( viewurl + "?do=Edit", name );
084        } else if( context.equals( ContextEnum.PAGE_ATTACH.getRequestContext() ) ) {
085            return doReplacement( "%uattach/%n", name );
086        } else if( context.equals( ContextEnum.PAGE_INFO.getRequestContext() ) ) {
087            return doReplacement( viewurl + "?do=PageInfo", name );
088        } else if( context.equals( ContextEnum.PAGE_DIFF.getRequestContext() ) ) {
089            return doReplacement( viewurl + "?do=Diff", name );
090        } else if( context.equals( ContextEnum.PAGE_NONE.getRequestContext() ) ) {
091            return doReplacement( "%u%n", name );
092        } else if( context.equals( ContextEnum.PAGE_UPLOAD.getRequestContext() ) ) {
093            return doReplacement( viewurl + "?do=Upload", name ); 
094        } else if( context.equals( ContextEnum.PAGE_COMMENT.getRequestContext() ) ) {
095            return doReplacement( viewurl + "?do=Comment", name ); 
096        } else if( context.equals( ContextEnum.WIKI_LOGIN.getRequestContext() ) ) {
097            final String loginUrl = "%pLogin.jsp?redirect=%n";
098            return doReplacement( loginUrl, name ); 
099        } else if( context.equals( ContextEnum.PAGE_DELETE.getRequestContext() ) ) {
100            return doReplacement( viewurl + "?do=Delete", name ); 
101        } else if( context.equals( ContextEnum.PAGE_CONFLICT.getRequestContext() ) ) {
102            return doReplacement( viewurl + "?do=PageModified", name ); 
103        } else if( context.equals( ContextEnum.WIKI_PREFS.getRequestContext() ) ) {
104            return doReplacement( viewurl + "?do=UserPreferences", name ); 
105        } else if( context.equals( ContextEnum.WIKI_FIND.getRequestContext() ) ) {
106            return doReplacement( viewurl + "?do=Search", name ); 
107        } else if( context.equals( ContextEnum.WIKI_ERROR.getRequestContext() ) ) {
108            return doReplacement( "%uError.jsp", name );
109        } else if( context.equals( ContextEnum.WIKI_CREATE_GROUP.getRequestContext() ) ) {
110            return doReplacement( viewurl + "?do=NewGroup", name );
111        } else if( context.equals( ContextEnum.GROUP_DELETE.getRequestContext() ) ) {
112            return doReplacement( viewurl + "?do=DeleteGroup", name );
113        } else if( context.equals( ContextEnum.GROUP_EDIT.getRequestContext() ) ) {
114            return doReplacement( viewurl + "?do=EditGroup", name );
115        } else if( context.equals( ContextEnum.GROUP_VIEW.getRequestContext() ) ) {
116            return doReplacement( viewurl + "?do=Group&group=%n", name );
117        }
118        
119        throw new InternalWikiException( "Requested unsupported context " + context );
120    }
121
122    /**
123     *  {@inheritDoc}
124     */
125    @Override
126    public String makeURL( final String context, final String name, String parameters ) {
127        if( parameters != null && !parameters.isEmpty() ) {
128            if( context.equals( ContextEnum.PAGE_ATTACH.getRequestContext() ) || context.equals( ContextEnum.PAGE_VIEW.getRequestContext() ) ) {
129                parameters = "?" + parameters;
130            } else if( context.equals(ContextEnum.PAGE_NONE.getRequestContext()) ) {
131                parameters = (name.indexOf('?') != -1 ) ? "&" : "?" + parameters;
132            } else {
133                parameters = "&"+parameters;
134            }
135        } else {
136            parameters = "";
137        }
138        return makeURL( context, name )+parameters;
139    }
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public String parsePage( final String context, final HttpServletRequest request, final Charset encoding ) {
146        final String pagereq = request.getParameter( "page" );
147        if( pagereq == null ) {
148            return URLConstructor.parsePageFromURL( request, encoding );
149        }
150
151        return pagereq;
152    }
153
154    /**
155     *  {@inheritDoc}
156     */
157    @Override
158    public String getForwardPage( final HttpServletRequest req ) {
159        String jspPage = req.getParameter( "do" );
160        if( jspPage == null ) {
161            jspPage = "Wiki";
162        }
163    
164        return jspPage + ".jsp";
165    }
166
167}