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.permissions.AllPermission;
024    import org.apache.wiki.auth.permissions.WikiPermission;
025    
026    /**
027     * <p>Defines Commands for wiki-wide operations such as creating groups, editing
028     * preferences and profiles, and logging in/out. WikiCommands can be combined 
029     * with Strings (representing the name of a wiki instance) 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     * @see org.apache.wiki.WikiEngine#getApplicationName()
034     * @since 2.4.22
035     */
036    public final class WikiCommand extends AbstractCommand
037    {
038    
039        public static final Command CREATE_GROUP
040            = new WikiCommand( "createGroup", "%uNewGroup.jsp", "NewGroupContent.jsp", null, WikiPermission.CREATE_GROUPS_ACTION );
041    
042        public static final Command ERROR
043            = new WikiCommand( "error", "%uError.jsp", "DisplayMessage.jsp", null, null );
044    
045        public static final Command FIND
046            = new WikiCommand( "find", "%uSearch.jsp", "FindContent.jsp", null, null );
047    
048        public static final Command INSTALL
049            = new WikiCommand( "install", "%uInstall.jsp", null, null, null );
050    
051        public static final Command LOGIN
052            = new WikiCommand( "login", "%uLogin.jsp?redirect=%n", "LoginContent.jsp", null, WikiPermission.LOGIN_ACTION );
053    
054        public static final Command LOGOUT
055            = new WikiCommand( "logout", "%uLogout.jsp", null, null, WikiPermission.LOGIN_ACTION );
056    
057        public static final Command MESSAGE
058            = new WikiCommand( "message", "%uMessage.jsp", "DisplayMessage.jsp", null, null );
059        
060        public static final Command PREFS
061            = new WikiCommand( "prefs", "%uUserPreferences.jsp", "PreferencesContent.jsp", null, WikiPermission.EDIT_PROFILE_ACTION );
062    
063        public static final Command WORKFLOW
064            = new WikiCommand( "workflow", "%uWorkflow.jsp", "WorkflowContent.jsp", null, null );
065    
066        public static final Command ADMIN
067            = new WikiCommand( "admin", "%uadmin/Admin.jsp", "AdminContent.jsp", null );
068    
069        private final String m_action;
070        
071        private final Permission m_permission;
072        
073        /**
074         * Constructs a new Command with a specified wiki context, URL pattern,
075         * type, and content template. The WikiPage for this action is initialized
076         * to <code>null</code>.
077         * @param requestContext the request context
078         * @param urlPattern the URL pattern
079         * @param type the type
080         * @param contentTemplate the content template; may be <code>null</code>
081         * @param action The action
082         * @throws IllegalArgumentException if the request content, URL pattern, or
083         *         type is <code>null</code>
084         */
085        private WikiCommand( String requestContext, 
086                             String urlPattern, 
087                             String contentTemplate, 
088                             String target, 
089                             String action )
090        {
091            super( requestContext, urlPattern, contentTemplate, target );
092            m_action = action;
093            if ( target == null || m_action == null )
094            {
095                m_permission = null;
096            }
097            else
098            {
099                m_permission = new WikiPermission( target, action );
100            }
101        }
102    
103        /**
104         *  Constructs an admin command.
105         *  
106         *  @param requestContext
107         *  @param urlPattern
108         *  @param contentTemplate
109         */
110        private WikiCommand( String requestContext, 
111                             String urlPattern, 
112                             String contentTemplate,
113                             String target )
114        {
115            super( requestContext, urlPattern, contentTemplate, target );
116            m_action = null;
117    
118            m_permission = new AllPermission( target );
119        }
120        /**
121         * Creates and returns a targeted Command by combining a wiki
122         * (a String) with this Command. The supplied <code>target</code>
123         * object must be non-<code>null</code> and of type String.
124         * @param target the name of the wiki to combine into the current Command
125         * @return the new targeted command
126         * @throws IllegalArgumentException if the target is not of the correct type
127         */
128        public Command targetedCommand( Object target )
129        {
130            if ( !( target != null && target instanceof String ) )
131            {
132                throw new IllegalArgumentException( "Target must non-null and of type String." );
133            }
134            return new WikiCommand( getRequestContext(), getURLPattern(), getContentTemplate(), (String)target, m_action );
135        }
136        
137        /**
138         * Always returns the "friendly" JSP name.
139         * @see org.apache.wiki.ui.Command#getName()
140         */
141        public String getName()
142        {
143            return getJSPFriendlyName();
144        }
145    
146        /**
147         * @see org.apache.wiki.ui.Command#requiredPermission()
148         */
149        public Permission requiredPermission()
150        {
151            return m_permission;
152        }
153    }