001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 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.ui;
021:
022: import java.security.Permission;
023:
024: import com.ecyrd.jspwiki.WikiPage;
025: import com.ecyrd.jspwiki.auth.permissions.PagePermission;
026: import com.ecyrd.jspwiki.auth.permissions.PermissionFactory;
027:
028: /**
029: * <p>Defines Commands for editing, renaming, and viewing wiki pages.
030: * PageCommands can be combined with WikiPages to produce
031: * targeted Commands.</p>
032: * <p>This class is not <code>final</code>; it may be extended in
033: * the future.</p>
034: * @author Andrew Jaquith
035: * @since 2.4.22
036: */
037: public final class PageCommand extends AbstractCommand {
038:
039: public static final Command ATTACH = new PageCommand("att",
040: "%uattach/%n", null, null, PagePermission.UPLOAD_ACTION);
041:
042: public static final Command COMMENT = new PageCommand("comment",
043: "%uComment.jsp?page=%n", "CommentContent.jsp", null,
044: PagePermission.COMMENT_ACTION);
045:
046: public static final Command CONFLICT = new PageCommand("conflict",
047: "%uPageModified.jsp?page=%n", "ConflictContent.jsp", null,
048: PagePermission.VIEW_ACTION);
049:
050: public static final Command DELETE = new PageCommand("del",
051: "%uDelete.jsp?page=%n", null, null,
052: PagePermission.DELETE_ACTION);
053:
054: public static final Command DIFF = new PageCommand("diff",
055: "%uDiff.jsp?page=%n", "DiffContent.jsp", null,
056: PagePermission.VIEW_ACTION);
057:
058: public static final Command EDIT = new PageCommand("edit",
059: "%uEdit.jsp?page=%n", "EditContent.jsp", null,
060: PagePermission.EDIT_ACTION);
061:
062: public static final Command INFO = new PageCommand("info",
063: "%uPageInfo.jsp?page=%n", "InfoContent.jsp", null,
064: PagePermission.VIEW_ACTION);
065:
066: public static final Command PREVIEW = new PageCommand("preview",
067: "%uPreview.jsp?page=%n", "PreviewContent.jsp", null,
068: PagePermission.VIEW_ACTION);
069:
070: public static final Command RENAME = new PageCommand("rename",
071: "%uRename.jsp?page=%n", null, null,
072: PagePermission.RENAME_ACTION);
073:
074: public static final Command RSS = new PageCommand("rss",
075: "%urss.jsp", null, null, PagePermission.VIEW_ACTION);
076:
077: public static final Command UPLOAD = new PageCommand("upload",
078: "%uUpload.jsp?page=%n", null, null,
079: PagePermission.UPLOAD_ACTION);
080:
081: public static final Command VIEW = new PageCommand("view",
082: "%uWiki.jsp?page=%n", "PageContent.jsp", null,
083: PagePermission.VIEW_ACTION);
084:
085: public static final Command NONE = new PageCommand("", "%u%n",
086: null, null, null);
087:
088: public static final Command OTHER = NONE;
089:
090: private final String m_action;
091:
092: private final Permission m_permission;
093:
094: /**
095: * Constructs a new Command with a specified wiki context, URL pattern,
096: * type, and content template. The target for this command is initialized to
097: * <code>null</code>.
098: * @param requestContext the request context
099: * @param urlPattern the URL pattern
100: * @param target the target of the command (a WikiPage); may be <code>null</code>
101: * @param action the action used to construct a suitable PagePermission
102: * @param contentTemplate the content template; may be <code>null</code>
103: * @return IllegalArgumentException if the request content, URL pattern, or
104: * type is <code>null</code>
105: */
106: private PageCommand(String requestContext, String urlPattern,
107: String contentTemplate, WikiPage target, String action) {
108: super (requestContext, urlPattern, contentTemplate, target);
109: m_action = action;
110: if (target == null || m_action == null) {
111: m_permission = null;
112: } else {
113: m_permission = PermissionFactory.getPagePermission(target,
114: action);
115: }
116: }
117:
118: /**
119: * Creates and returns a targeted Command by combining a WikiPage
120: * with this Command. The supplied <code>target</code> object
121: * must be non-<code>null</code> and of type WikiPage.
122: * @param target the WikiPage to combine into the current Command
123: * @return the new targeted command
124: * @throws IllegalArgumentException if the target is not of the correct type
125: */
126: public final Command targetedCommand(Object target) {
127: if (!(target != null && target instanceof WikiPage)) {
128: throw new IllegalArgumentException(
129: "Target must non-null and of type WikiPage.");
130: }
131: return new PageCommand(getRequestContext(), getURLPattern(),
132: getContentTemplate(), (WikiPage) target, m_action);
133: }
134:
135: /**
136: * @see com.ecyrd.jspwiki.ui.Command#getName()
137: */
138: public final String getName() {
139: Object target = getTarget();
140: if (target == null) {
141: return getJSPFriendlyName();
142: }
143: return ((WikiPage) target).getName();
144: }
145:
146: /**
147: * @see com.ecyrd.jspwiki.ui.Command#requiredPermission()
148: */
149: public final Permission requiredPermission() {
150: return m_permission;
151: }
152: }
|