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     */
019    package org.apache.wiki.url;
020    
021    import java.util.Properties;
022    
023    import javax.servlet.http.HttpServletRequest;
024    
025    import org.apache.wiki.WikiContext;
026    import org.apache.wiki.WikiEngine;
027    
028    /**
029     *  A specific URL constructor that returns easy-to-grok URLs for
030     *  VIEW and ATTACH contexts, but goes through JSP pages otherwise.
031     * 
032     *
033     *  @since 2.2
034     */
035    public class ShortViewURLConstructor 
036        extends ShortURLConstructor
037    {
038        /**
039         *  {@inheritDoc}
040         */
041        public void initialize( WikiEngine engine, 
042                                Properties properties )
043        {
044            super.initialize( engine, properties );
045        }
046        
047        private String makeURL( String context,
048                                String name,
049                                boolean absolute )
050        {
051            String viewurl = "%p"+m_urlPrefix+"%n";
052    
053            if( absolute ) 
054                viewurl = "%u"+m_urlPrefix+"%n";
055    
056            if( context.equals(WikiContext.VIEW) )
057            {
058                if( name == null ) return doReplacement("%u","",absolute);
059                return doReplacement( viewurl, name, absolute );
060            }
061    
062            return doReplacement( DefaultURLConstructor.getURLPattern(context,name),
063                                  name,
064                                  absolute );
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public String makeURL( String context,
071                               String name,
072                               boolean absolute,
073                               String parameters )
074        {
075            if( parameters != null && parameters.length() > 0 )
076            {            
077                if( context.equals(WikiContext.ATTACH) || context.equals(WikiContext.VIEW) || name == null )
078                {
079                    parameters = "?"+parameters;
080                }
081                else if( context.equals(WikiContext.NONE) )
082                {
083                    parameters = (name.indexOf('?') != -1 ) ? "&" : "?" + parameters;
084                }
085                else
086                {
087                    parameters = "&"+parameters;
088                }
089            }
090            else
091            {
092                parameters = "";
093            }
094            return makeURL( context, name, absolute )+parameters;
095        }
096        
097        /**
098         *   Since we're only called from WikiServlet, where we get the VIEW requests,
099         *   we can safely return this.
100         *   
101         * @param request The HTTP Request that was used to end up in this page.
102         * @return always returns "Wiki.jsp"
103         */
104        public String getForwardPage( HttpServletRequest request )
105        {        
106            return "Wiki.jsp";
107        }
108    }