001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 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.auth.GroupPrincipal;
025: import com.ecyrd.jspwiki.auth.permissions.GroupPermission;
026:
027: /**
028: * <p>Defines Commands for viewing, editing and deleting wiki groups.
029: * GroupCommands can be combined with GroupPrincipals to produce
030: * targeted Commands.</p>
031: * <p>This class is not <code>final</code>; it may be extended in
032: * the future.</p>
033: * @author Andrew Jaquith
034: * @since 2.4.22
035: */
036: public final class GroupCommand extends AbstractCommand {
037:
038: /** GroupCommand for deleting a group. */
039: public static final Command DELETE_GROUP = new GroupCommand(
040: "deleteGroup", "%uDeleteGroup.jsp?group=%n", null, null,
041: GroupPermission.DELETE_ACTION);
042:
043: /** GroupCommand for editing a group. */
044: public static final Command EDIT_GROUP = new GroupCommand(
045: "editGroup", "%uEditGroup.jsp?group=%n",
046: "EditGroupContent.jsp", null, GroupPermission.EDIT_ACTION);
047:
048: /** GroupCommand for viewing a group. */
049: public static final Command VIEW_GROUP = new GroupCommand(
050: "viewGroup", "%uGroup.jsp?group=%n", "GroupContent.jsp",
051: null, GroupPermission.VIEW_ACTION);
052:
053: private final String m_action;
054:
055: private final Permission m_permission;
056:
057: /**
058: * Constructs a new Command with a specified wiki context, URL pattern,
059: * type, and content template. The WikiPage for this command is initialized
060: * to <code>null</code>.
061: * @param requestContext the request context
062: * @param urlPattern the URL pattern
063: * @param target the target of this command (a GroupPrincipal representing a Group); may be <code>null</code>
064: * @param action the action used to construct a suitable GroupPermission
065: * @param contentTemplate the content template; may be <code>null</code>
066: * @return IllegalArgumentException if the request content, URL pattern, or
067: * type is <code>null</code>
068: */
069: private GroupCommand(String requestContext, String urlPattern,
070: String contentTemplate, GroupPrincipal target, String action) {
071: super (requestContext, urlPattern, contentTemplate, target);
072: m_action = action;
073: if (target == null || m_action == null) {
074: m_permission = null;
075: } else {
076: m_permission = new GroupPermission(target.getName(), action);
077: }
078: }
079:
080: /**
081: * Creates and returns a targeted Command by combining a GroupPrincipal
082: * with this Command. The supplied <code>target</code> object
083: * must be non-<code>null</code> and of type GroupPrincipal.
084: * If the target is not of the correct type, this method throws an
085: * {@link IllegalArgumentException}.
086: * @param target the GroupPrincipal to combine into the current Command
087: * @return the new, targeted command
088: */
089: public final Command targetedCommand(Object target) {
090: if (!(target != null && target instanceof GroupPrincipal)) {
091: throw new IllegalArgumentException(
092: "Target must non-null and of type GroupPrincipal.");
093: }
094: return new GroupCommand(getRequestContext(), getURLPattern(),
095: getContentTemplate(), (GroupPrincipal) target, m_action);
096: }
097:
098: /**
099: * Returns the name of the command, which will either be
100: * the target (if specified), or the "friendly name" for the JSP.
101: * @return the name
102: * @see com.ecyrd.jspwiki.ui.Command#getName()
103: */
104: public final String getName() {
105: Object target = getTarget();
106: if (target == null) {
107: return getJSPFriendlyName();
108: }
109: return ((GroupPrincipal) target).getName();
110: }
111:
112: /**
113: * Returns the permission required to execute this GroupCommand.
114: * @return the permission
115: * @see com.ecyrd.jspwiki.ui.Command#requiredPermission()
116: */
117: public final Permission requiredPermission() {
118: return m_permission;
119: }
120:
121: }
|