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