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.log4j.Logger;
022import org.apache.wiki.InternalWikiException;
023import org.apache.wiki.WikiContext;
024import org.apache.wiki.WikiEngine;
025import org.apache.wiki.util.TextUtil;
026
027import javax.servlet.http.HttpServletRequest;
028import java.nio.charset.Charset;
029import java.util.Properties;
030
031
032/**
033 *  Provides a way to do short URLs of the form /wiki/PageName.
034 *
035 *
036 *  @since 2.2
037 */
038public class ShortURLConstructor
039    extends DefaultURLConstructor
040{
041    private static final String DEFAULT_PREFIX = "wiki/";
042
043    static Logger log = Logger.getLogger( ShortURLConstructor.class );
044    
045    /**
046     *  Contains the path part after the JSPWiki base URL
047     */
048    protected String m_urlPrefix = "";
049    
050    /**
051     *  This corresponds to your WikiServlet path.  By default, it is assumed to
052     *  be "wiki/", but you can set it to whatever you like - including an empty
053     *  name.
054     */
055    public static final String PROP_PREFIX = "jspwiki.shortURLConstructor.prefix";
056    
057    /** {@inheritDoc} */
058    public void initialize( WikiEngine engine, 
059                            Properties properties )
060    {
061        super.initialize( engine, properties );
062        
063        m_urlPrefix = TextUtil.getStringProperty( properties, PROP_PREFIX, null );
064        
065        if( m_urlPrefix == null )
066        {
067            m_urlPrefix = DEFAULT_PREFIX;
068        }
069
070        log.info("Short URL prefix path="+m_urlPrefix+" (You can use "+PROP_PREFIX+" to override this)");
071    }
072
073    /**
074     *  Constructs the actual URL based on the context.
075     */
076    private String makeURL( String context,
077                            String name,
078                            boolean absolute )
079    {
080        String viewurl = "%p"+m_urlPrefix+"%n";
081
082        if( absolute ) 
083            viewurl = "%u"+m_urlPrefix+"%n";
084
085        if( context.equals(WikiContext.VIEW) )
086        {
087            if( name == null ) return doReplacement("%u","",absolute);
088            return doReplacement( viewurl, name, absolute );
089        }
090        else if( context.equals(WikiContext.PREVIEW) )
091        {
092            if( name == null ) return doReplacement("%u","",absolute);
093            return doReplacement( viewurl+"?do=Preview", name, absolute);
094        }
095        else if( context.equals(WikiContext.EDIT) )
096        {
097            return doReplacement( viewurl+"?do=Edit", name, absolute );
098        }
099        else if( context.equals(WikiContext.ATTACH) )
100        {
101            return doReplacement( "%uattach/%n", name, absolute );
102        }
103        else if( context.equals(WikiContext.INFO) )
104        {
105            return doReplacement( viewurl+"?do=PageInfo", name, absolute );
106        }
107        else if( context.equals(WikiContext.DIFF) )
108        {
109            return doReplacement( viewurl+"?do=Diff", name, absolute );
110        }
111        else if( context.equals(WikiContext.NONE) )
112        {
113            return doReplacement( "%u%n", name, absolute );
114        }
115        else if( context.equals(WikiContext.UPLOAD) )
116        {
117            return doReplacement( viewurl+"?do=Upload", name, absolute ); 
118        }
119        else if( context.equals(WikiContext.COMMENT) )
120        {
121            return doReplacement( viewurl+"?do=Comment", name, absolute ); 
122        }
123        else if( context.equals(WikiContext.LOGIN) )
124        {
125            String loginUrl = absolute ? "%uLogin.jsp?redirect=%n" : "%pLogin.jsp?redirect=%n";
126            return doReplacement( loginUrl, name, absolute ); 
127        }
128        else if( context.equals(WikiContext.DELETE) )
129        {
130            return doReplacement( viewurl+"?do=Delete", name, absolute ); 
131        }
132        else if( context.equals(WikiContext.CONFLICT) )
133        {
134            return doReplacement( viewurl+"?do=PageModified", name, absolute ); 
135        }
136        else if( context.equals(WikiContext.PREFS) )
137        {
138            return doReplacement( viewurl+"?do=UserPreferences", name, absolute ); 
139        }
140        else if( context.equals(WikiContext.FIND) )
141        {
142            return doReplacement( viewurl+"?do=Search", name, absolute ); 
143        }
144        else if( context.equals(WikiContext.ERROR) )
145        {
146            return doReplacement( "%uError.jsp", name, absolute );
147        }
148        else if( context.equals(WikiContext.CREATE_GROUP) )
149        {
150            return doReplacement( viewurl+"?do=NewGroup", name, absolute );
151        }
152        else if( context.equals(WikiContext.DELETE_GROUP) )
153        {
154            return doReplacement( viewurl+"?do=DeleteGroup", name, absolute );
155        }        
156        else if( context.equals(WikiContext.EDIT_GROUP) )
157        {
158            return doReplacement( viewurl+"?do=EditGroup", name, absolute );
159        }
160        else if( context.equals(WikiContext.VIEW_GROUP) )
161        {
162            return doReplacement( viewurl+"?do=Group&group=%n", name, absolute );
163        }
164        
165        throw new InternalWikiException("Requested unsupported context "+context);
166    }
167
168    /**
169     *  {@inheritDoc}
170     */
171    public String makeURL( String context,
172                           String name,
173                           boolean absolute,
174                           String parameters )
175    {
176        if( parameters != null && parameters.length() > 0 )
177        {            
178            if( context.equals(WikiContext.ATTACH) || context.equals(WikiContext.VIEW) )
179            {
180                parameters = "?"+parameters;
181            }
182            else if( context.equals(WikiContext.NONE) )
183            {
184                parameters = (name.indexOf('?') != -1 ) ? "&" : "?" + parameters;
185            }
186            else
187            {
188                parameters = "&"+parameters;
189            }
190        }
191        else
192        {
193            parameters = "";
194        }
195        return makeURL( context, name, absolute )+parameters;
196    }
197
198    /**
199     * {@inheritDoc}
200     */
201    public String parsePage( final String context, final HttpServletRequest request, final Charset encoding ) {
202        final String pagereq = request.getParameter( "page" );
203        if( pagereq == null ) {
204            return parsePageFromURL( request, encoding );
205        }
206
207        return pagereq;
208    }
209
210    /**
211     *  {@inheritDoc}
212     */
213    public String getForwardPage( HttpServletRequest req )
214    {
215        String jspPage = req.getParameter( "do" );
216        if( jspPage == null ) jspPage = "Wiki";
217    
218        return jspPage+".jsp";
219    }
220}