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 */
019package org.apache.wiki.ui;
020
021import org.apache.wiki.api.core.Command;
022import org.apache.wiki.api.core.ContextEnum;
023import org.apache.wiki.auth.permissions.AllPermission;
024import org.apache.wiki.auth.permissions.WikiPermission;
025
026import java.security.Permission;
027
028/**
029 * <p>Defines Commands for wiki-wide operations such as creating groups, editing preferences and profiles, and logging in/out.
030 * WikiCommands can be combined with Strings (representing the name of a wiki instance) to produce targeted Commands.</p>
031 *
032 * @see org.apache.wiki.api.core.Engine#getApplicationName()
033 * @since 2.4.22
034 */
035public final class WikiCommand extends AbstractCommand {
036
037    public static final Command ADMIN = new WikiCommand( ContextEnum.WIKI_ADMIN, null );
038    public static final Command CREATE_GROUP = new WikiCommand( ContextEnum.WIKI_CREATE_GROUP, null, WikiPermission.CREATE_GROUPS_ACTION );
039    public static final Command ERROR = new WikiCommand( ContextEnum.WIKI_ERROR, null, null );
040    public static final Command FIND = new WikiCommand( ContextEnum.WIKI_FIND, null, null );
041    public static final Command INSTALL = new WikiCommand( ContextEnum.WIKI_INSTALL, null, null );
042    public static final Command LOGIN = new WikiCommand( ContextEnum.WIKI_LOGIN, null, WikiPermission.LOGIN_ACTION );
043    public static final Command LOGOUT = new WikiCommand( ContextEnum.WIKI_LOGOUT, null, WikiPermission.LOGIN_ACTION );
044    public static final Command MESSAGE = new WikiCommand( ContextEnum.WIKI_MESSAGE, null, null );
045    public static final Command PREFS = new WikiCommand( ContextEnum.WIKI_PREFS, null, WikiPermission.EDIT_PROFILE_ACTION );
046    public static final Command WORKFLOW = new WikiCommand( ContextEnum.WIKI_WORKFLOW, null, null );
047
048    private final String m_action;
049    
050    private final Permission m_permission;
051
052    /**
053     * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The WikiPage for this action is
054     * initialized to <code>null</code>.
055     *
056     * @param currentContext the current context.
057     * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code>
058     */
059    private WikiCommand( final ContextEnum currentContext, final String target ) {
060        this( currentContext.getRequestContext(), currentContext.getUrlPattern(), currentContext.getContentTemplate(), target );
061    }
062
063    /**
064     * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The WikiPage for this action is
065     * initialized to <code>null</code>.
066     *
067     * @param currentContext the current context.
068     * @param action The action
069     * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code>
070     */
071    private WikiCommand( final ContextEnum currentContext, final String target, final String action ) {
072        this( currentContext.getRequestContext(), currentContext.getUrlPattern(), currentContext.getContentTemplate(), target, action );
073    }
074    
075    /**
076     * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The WikiPage for this action is
077     * initialized to <code>null</code>.
078     *
079     * @param requestContext the request context
080     * @param urlPattern the URL pattern
081     * @param contentTemplate the content template; may be <code>null</code>
082     * @param action The action
083     * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code>
084     */
085    private WikiCommand( final String requestContext,
086                         final String urlPattern,
087                         final String contentTemplate,
088                         final String target,
089                         final String action ) {
090        super( requestContext, urlPattern, contentTemplate, target );
091        m_action = action;
092        if ( target == null || m_action == null ) {
093            m_permission = null;
094        } else {
095            m_permission = new WikiPermission( target, action );
096        }
097    }
098
099    /**
100     * Constructs an admin command.
101     *  
102     * @param requestContext the request context
103     * @param urlPattern the URL pattern
104     * @param contentTemplate the content template; may be <code>null</code>
105     * @param target the target of the command, such as a WikiPage; may be <code>null</code>
106     * @throws IllegalArgumentException if the request content or URL pattern is <code>null</code>
107     */
108    private WikiCommand( final String requestContext, final String urlPattern, final String contentTemplate, final String target ) {
109        super( requestContext, urlPattern, contentTemplate, target );
110        m_action = null;
111        m_permission = new AllPermission( target );
112    }
113
114    /**
115     * Creates and returns a targeted Command by combining a wiki (a String) with this Command. The supplied <code>target</code>
116     * object must be non-<code>null</code> and of type String.
117     *
118     * @param target the name of the wiki to combine into the current Command
119     * @return the new targeted command
120     * @throws IllegalArgumentException if the target is not of the correct type
121     */
122    @Override
123    public Command targetedCommand( final Object target ) {
124        if ( !( target instanceof String ) ) {
125            throw new IllegalArgumentException( "Target must non-null and of type String." );
126        }
127        return new WikiCommand( getRequestContext(), getURLPattern(), getContentTemplate(), (String)target, m_action );
128    }
129    
130    /**
131     * Always returns the "friendly" JSP name.
132     *
133     * @see org.apache.wiki.api.core.Command#getName()
134     */
135    @Override
136    public String getName() {
137        return getJSPFriendlyName();
138    }
139
140    /**
141     * @see org.apache.wiki.api.core.Command#requiredPermission()
142     */
143    @Override
144    public Permission requiredPermission() {
145        return m_permission;
146    }
147
148}