    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.api.core.Page;
    024import org.apache.wiki.auth.permissions.PagePermission;
    025import org.apache.wiki.auth.permissions.PermissionFactory;
    026
    027import java.security.Permission;
    028
    029/**
    030 * <p>Defines Commands for editing, renaming, and viewing wiki pages. PageCommands can be combined with WikiPages to produce
    031 * targeted Commands.</p>
    032 *
    033 * @since 2.4.22
    034 */
    035public final class PageCommand extends AbstractCommand {
    036
    037    public static final Command ATTACH = new PageCommand( ContextEnum.PAGE_ATTACH, null, PagePermission.UPLOAD_ACTION );
    038    public static final Command COMMENT = new PageCommand( ContextEnum.PAGE_COMMENT, null, PagePermission.COMMENT_ACTION );
    039    public static final Command CONFLICT = new PageCommand( ContextEnum.PAGE_CONFLICT, null, PagePermission.VIEW_ACTION );
    040    public static final Command DELETE = new PageCommand( ContextEnum.PAGE_DELETE, null, PagePermission.DELETE_ACTION );
    041    public static final Command DIFF = new PageCommand( ContextEnum.PAGE_DIFF, null, PagePermission.VIEW_ACTION );
    042    public static final Command EDIT = new PageCommand( ContextEnum.PAGE_EDIT, null, PagePermission.EDIT_ACTION );
    043    public static final Command INFO = new PageCommand( ContextEnum.PAGE_INFO, null, PagePermission.VIEW_ACTION );
    044    public static final Command PREVIEW = new PageCommand( ContextEnum.PAGE_PREVIEW, null, PagePermission.VIEW_ACTION );
    045    public static final Command RENAME = new PageCommand( ContextEnum.PAGE_RENAME, null, PagePermission.RENAME_ACTION );
    046    public static final Command RSS = new PageCommand( ContextEnum.PAGE_RSS, null, PagePermission.VIEW_ACTION );
    047    public static final Command UPLOAD = new PageCommand( ContextEnum.PAGE_UPLOAD, null, PagePermission.UPLOAD_ACTION );
    048    public static final Command VIEW = new PageCommand( ContextEnum.PAGE_VIEW, null, PagePermission.VIEW_ACTION );
    049    public static final Command NONE = new PageCommand( ContextEnum.PAGE_NONE, null, null );
    050    public static final Command OTHER = NONE;
    051
    052    private final String m_action;
    053
    054    private final Permission m_permission;
    055
    056    /**
    057     * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The target for this command is
    058     * initialized to <code>null</code>.
    059     *
    060     * @param currentContext the current context.
    061     * @param target the target of the command (a WikiPage); may be <code>null</code>
    062     * @param action the action used to construct a suitable PagePermission
    063     * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code>
    064     */
    065    private PageCommand( final ContextEnum currentContext, final Page target, final String action ) {
    066        this( currentContext.getRequestContext(), currentContext.getUrlPattern(), currentContext.getContentTemplate(), target, action );
    067    }
    068
    069    /**
    070     * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The target for this command is
    071     * initialized to <code>null</code>.
    072     *
    073     * @param requestContext the request context
    074     * @param urlPattern the URL pattern
    075     * @param target the target of the command (a WikiPage); may be <code>null</code>
    076     * @param action the action used to construct a suitable PagePermission
    077     * @param contentTemplate the content template; may be <code>null</code>
    078     * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code>
    079     */
    080    private PageCommand( final String requestContext,
    081                         final String urlPattern,
    082                         final String contentTemplate,
    083                         final Page target,
    084                         final String action ) {
    085        super( requestContext, urlPattern, contentTemplate, target );
    086        m_action = action;
    087        if( target == null || m_action == null ) {
    088            m_permission = null;
    089        } else {
    090            m_permission = PermissionFactory.getPagePermission( target, action );
    091        }
    092    }
    093
    094    /**
    095     * Creates and returns a targeted Command by combining a WikiPage with this Command. The supplied <code>target</code> object
    096     * must be non-<code>null</code> and of type WikiPage.
    097     *
    098     * @param target the WikiPage to combine into the current Command
    099     * @return the new targeted command
    100     * @throws IllegalArgumentException if the target is not of the correct type
    101     */
    102    @Override
    103    public Command targetedCommand(final Object target ) {
    104        if( !( target instanceof Page ) ) {
    105            throw new IllegalArgumentException( "Target must non-null and of type Page." );
    106        }
    107        return new PageCommand( getRequestContext(), getURLPattern(), getContentTemplate(), ( Page )target, m_action );
    108    }
    109
    110    /**
    111     * @see org.apache.wiki.api.core.Command#getName()
    112     */
    113    @Override
    114    public String getName() {
    115        final Object target = getTarget();
    116        if( target == null ) {
    117            return getJSPFriendlyName();
    118        }
    119        return ( ( Page )target ).getName();
    120    }
    121
    122    /**
    123     * @see org.apache.wiki.api.core.Command#requiredPermission()
    124     */
    125    @Override
    126    public Permission requiredPermission() {
    127        return m_permission;
    128    }
    129
    130}