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.WikiPage;
024    import org.apache.wiki.auth.permissions.PagePermission;
025    import org.apache.wiki.auth.permissions.PermissionFactory;
026    
027    /**
028     * <p>Defines Commands for editing, renaming, and viewing wiki pages.
029     * PageCommands can be combined with WikiPages 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     * @since 2.4.22
034     */
035    public final class PageCommand extends AbstractCommand
036    {
037    
038        public static final Command ATTACH
039            = new PageCommand( "att", "%uattach/%n", null, null, PagePermission.UPLOAD_ACTION );
040    
041        public static final Command COMMENT
042            = new PageCommand( "comment", "%uComment.jsp?page=%n", "CommentContent.jsp", null, PagePermission.COMMENT_ACTION );
043    
044        public static final Command CONFLICT 
045            = new PageCommand( "conflict", "%uPageModified.jsp?page=%n", "ConflictContent.jsp", null, PagePermission.VIEW_ACTION );
046    
047        public static final Command DELETE
048            = new PageCommand( "del", "%uDelete.jsp?page=%n", null, null, PagePermission.DELETE_ACTION );
049    
050        public static final Command DIFF
051            = new PageCommand( "diff", "%uDiff.jsp?page=%n", "DiffContent.jsp", null, PagePermission.VIEW_ACTION );
052    
053        public static final Command EDIT
054            = new PageCommand( "edit", "%uEdit.jsp?page=%n", "EditContent.jsp", null, PagePermission.EDIT_ACTION );
055    
056        public static final Command INFO
057            = new PageCommand( "info", "%uPageInfo.jsp?page=%n", "InfoContent.jsp", null, PagePermission.VIEW_ACTION );
058    
059        public static final Command PREVIEW
060            = new PageCommand( "preview", "%uPreview.jsp?page=%n", "PreviewContent.jsp", null, PagePermission.VIEW_ACTION );
061    
062        public static final Command RENAME
063            = new PageCommand( "rename", "%uRename.jsp?page=%n", "InfoContent.jsp", null, PagePermission.RENAME_ACTION );
064    
065        public static final Command RSS
066            = new PageCommand( "rss", "%urss.jsp", null, null, PagePermission.VIEW_ACTION );
067    
068        public static final Command UPLOAD
069            = new PageCommand( "upload", "%uUpload.jsp?page=%n", null, null, PagePermission.UPLOAD_ACTION );
070    
071        public static final Command VIEW
072            = new PageCommand( "view", "%uWiki.jsp?page=%n", "PageContent.jsp", null, PagePermission.VIEW_ACTION );
073    
074        public static final Command NONE
075            = new PageCommand( "", "%u%n", null, null, null );
076    
077        public static final Command OTHER = NONE;
078    
079        private final String m_action;
080        
081        private final Permission m_permission;
082        
083        /**
084         * Constructs a new Command with a specified wiki context, URL pattern,
085         * type, and content template. The target for this command is initialized to
086         * <code>null</code>.
087         * @param requestContext the request context
088         * @param urlPattern the URL pattern
089         * @param target the target of the command (a WikiPage); may be <code>null</code>
090         * @param action the action used to construct a suitable PagePermission
091         * @param contentTemplate the content template; may be <code>null</code>
092         * @throws IllegalArgumentException if the request content, URL pattern, or
093         *         type is <code>null</code>
094         */
095        private PageCommand( String requestContext, String urlPattern, String contentTemplate, WikiPage target, String action )
096        {
097            super( requestContext, urlPattern, contentTemplate, target );
098            m_action = action;
099            if( target == null || m_action == null )
100            {
101                m_permission = null;
102            }
103            else
104            {
105                m_permission = PermissionFactory.getPagePermission( target, action );
106            }
107        }
108    
109        /**
110         * Creates and returns a targeted Command by combining a WikiPage 
111         * with this Command. The supplied <code>target</code> object 
112         * must be non-<code>null</code> and of type WikiPage.
113         * @param target the WikiPage to combine into the current Command
114         * @return the new targeted command
115         * @throws IllegalArgumentException if the target is not of the correct type
116         */
117        public Command targetedCommand( Object target )
118        {
119            if( !( target != null && target instanceof WikiPage ) )
120            {
121                throw new IllegalArgumentException( "Target must non-null and of type WikiPage." );
122            }
123            return new PageCommand( getRequestContext(), getURLPattern(), getContentTemplate(), (WikiPage)target, m_action );
124        }
125    
126        /**
127         * @see org.apache.wiki.ui.Command#getName()
128         */
129        public String getName()
130        {
131            Object target = getTarget();
132            if( target == null )
133            {
134                return getJSPFriendlyName();
135            }
136            return ( (WikiPage) target ).getName();
137        }
138    
139        /**
140         * @see org.apache.wiki.ui.Command#requiredPermission()
141         */
142        public Permission requiredPermission()
143        {
144            return m_permission;
145        }
146    }