001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.url;
021:
022: import java.io.UnsupportedEncodingException;
023: import java.util.Properties;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.log4j.Logger;
028:
029: import com.ecyrd.jspwiki.InternalWikiException;
030: import com.ecyrd.jspwiki.TextUtil;
031: import com.ecyrd.jspwiki.WikiContext;
032: import com.ecyrd.jspwiki.WikiEngine;
033:
034: /**
035: * Provides a way to do short URLs of the form /wiki/PageName.
036: *
037: * @author Janne Jalkanen
038: *
039: * @since 2.2
040: */
041: public class ShortURLConstructor extends DefaultURLConstructor {
042: private static final String DEFAULT_PREFIX = "wiki/";
043:
044: static Logger log = Logger.getLogger(ShortURLConstructor.class);
045:
046: /**
047: * Contains the path part after the JSPWiki base URL
048: */
049: protected String m_urlPrefix = "";
050:
051: /**
052: * This corresponds to your WikiServlet path. By default, it is assumed to
053: * be "wiki/", but you can set it to whatever you like - including an empty
054: * name.
055: */
056: public static final String PROP_PREFIX = "jspwiki.shortURLConstructor.prefix";
057:
058: /** {@inheritDoc} */
059: public void initialize(WikiEngine engine, Properties properties) {
060: super .initialize(engine, properties);
061:
062: m_urlPrefix = TextUtil.getStringProperty(properties,
063: PROP_PREFIX, null);
064:
065: if (m_urlPrefix == null) {
066: m_urlPrefix = DEFAULT_PREFIX;
067: }
068:
069: log
070: .info("Short URL prefix path=" + m_urlPrefix
071: + " (You can use " + PROP_PREFIX
072: + " to override this)");
073: }
074:
075: /**
076: * Constructs the actual URL based on the context.
077: */
078: private String makeURL(String context, String name, boolean absolute) {
079: String viewurl = "%p" + m_urlPrefix + "%n";
080:
081: if (absolute)
082: viewurl = "%u" + m_urlPrefix + "%n";
083:
084: if (context.equals(WikiContext.VIEW)) {
085: if (name == null)
086: return doReplacement("%u", "", absolute);
087: return doReplacement(viewurl, name, absolute);
088: } else if (context.equals(WikiContext.PREVIEW)) {
089: if (name == null)
090: return doReplacement("%u", "", absolute);
091: return doReplacement(viewurl + "?do=Preview", name,
092: absolute);
093: } else if (context.equals(WikiContext.EDIT)) {
094: return doReplacement(viewurl + "?do=Edit", name, absolute);
095: } else if (context.equals(WikiContext.ATTACH)) {
096: return doReplacement("%uattach/%n", name, absolute);
097: } else if (context.equals(WikiContext.INFO)) {
098: return doReplacement(viewurl + "?do=PageInfo", name,
099: absolute);
100: } else if (context.equals(WikiContext.DIFF)) {
101: return doReplacement(viewurl + "?do=Diff", name, absolute);
102: } else if (context.equals(WikiContext.NONE)) {
103: return doReplacement("%u%n", name, absolute);
104: } else if (context.equals(WikiContext.UPLOAD)) {
105: return doReplacement(viewurl + "?do=Upload", name, absolute);
106: } else if (context.equals(WikiContext.COMMENT)) {
107: return doReplacement(viewurl + "?do=Comment", name,
108: absolute);
109: } else if (context.equals(WikiContext.LOGIN)) {
110: return doReplacement(viewurl + "?do=Login", name, absolute);
111: } else if (context.equals(WikiContext.DELETE)) {
112: return doReplacement(viewurl + "?do=Delete", name, absolute);
113: } else if (context.equals(WikiContext.CONFLICT)) {
114: return doReplacement(viewurl + "?do=PageModified", name,
115: absolute);
116: } else if (context.equals(WikiContext.PREFS)) {
117: return doReplacement(viewurl + "?do=UserPreferences", name,
118: absolute);
119: } else if (context.equals(WikiContext.FIND)) {
120: return doReplacement(viewurl + "?do=Search", name, absolute);
121: } else if (context.equals(WikiContext.ERROR)) {
122: return doReplacement("%uError.jsp", name, absolute);
123: } else if (context.equals(WikiContext.CREATE_GROUP)) {
124: return doReplacement(viewurl + "?do=NewGroup", name,
125: absolute);
126: } else if (context.equals(WikiContext.DELETE_GROUP)) {
127: return doReplacement(viewurl + "?do=DeleteGroup", name,
128: absolute);
129: } else if (context.equals(WikiContext.EDIT_GROUP)) {
130: return doReplacement(viewurl + "?do=EditGroup", name,
131: absolute);
132: } else if (context.equals(WikiContext.VIEW_GROUP)) {
133: return doReplacement(viewurl + "?do=Group&group=%n", name,
134: absolute);
135: }
136:
137: throw new InternalWikiException(
138: "Requested unsupported context " + context);
139: }
140:
141: /**
142: * {@inheritDoc}
143: */
144: public String makeURL(String context, String name,
145: boolean absolute, String parameters) {
146: if (parameters != null && parameters.length() > 0) {
147: if (context.equals(WikiContext.ATTACH)
148: || context.equals(WikiContext.VIEW)) {
149: parameters = "?" + parameters;
150: } else if (context.equals(WikiContext.NONE)) {
151: parameters = (name.indexOf('?') != -1) ? "&" : "?"
152: + parameters;
153: } else {
154: parameters = "&" + parameters;
155: }
156: } else {
157: parameters = "";
158: }
159: return makeURL(context, name, absolute) + parameters;
160: }
161:
162: /**
163: * {@inheritDoc}
164: */
165: public String parsePage(String context, HttpServletRequest request,
166: String encoding) throws UnsupportedEncodingException {
167: request.setCharacterEncoding(encoding);
168: String pagereq = request.getParameter("page");
169:
170: if (pagereq == null) {
171: pagereq = parsePageFromURL(request, encoding);
172: }
173:
174: return pagereq;
175: }
176:
177: /**
178: * {@inheritDoc}
179: */
180: public String getForwardPage(HttpServletRequest req) {
181: String jspPage = req.getParameter("do");
182: if (jspPage == null)
183: jspPage = "Wiki";
184:
185: return jspPage + ".jsp";
186: }
187: }
|