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