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 java.security.Permission;
022
023/**
024 * <p>Defines Commands for redirections to off-site special pages. 
025 * RedirectCommands do not have associated
026 * permissions; the {@link #requiredPermission()} method will
027 * always return <code>null</code>. When combined with a supplied String
028 * url, the {@link #getTarget()} method will return a String, the
029 * {@link #getURLPattern()} method will return the supplied target URL,
030 * and {@link #getJSP()} method will return the "cleansed" URL.</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 */
035public final class RedirectCommand extends AbstractCommand
036{
037
038    public static final Command REDIRECT
039        = new RedirectCommand( "", "%u%n", null, null );
040
041    /**
042     * Constructs a new Command with a specified wiki context, URL pattern,
043     * type, and content template. The WikiPage for this action is initialized
044     * to <code>null</code>.
045     * @param requestContext the request context
046     * @param urlPattern the URL pattern
047     * @param contentTemplate the content template; may be <code>null</code>
048     * @param target the target of the command
049     * @throws IllegalArgumentException if the request content, URL pattern, or
050     *         type is <code>null</code>
051     */
052    private RedirectCommand( String requestContext, String urlPattern, String contentTemplate, String target )
053    {
054        super( requestContext, urlPattern, contentTemplate, target );
055    }
056    
057    /**
058     * Creates and returns a targeted Command by combining a URL
059     * (as String) with this Command. The supplied <code>target</code>
060     * object must be non-<code>null</code> and of type String.
061     * The URL passed to the constructor is actually an
062     * URL pattern, but it will be converted to a JSP page if it is a partial
063     * URL. If it is a full URL (beginning with <code>http://</code> or
064     * <code>https://</code>), it will be "passed through" without
065     * conversion, and the URL pattern will be <code>null</code>.
066     * @param target the object to combine
067     * @throws IllegalArgumentException if the target is not of the correct type
068     */
069    public Command targetedCommand( Object target )
070    {
071        if ( !( target != null && target instanceof String ) )
072        {
073            throw new IllegalArgumentException( "Target must non-null and of type String." );
074        }
075        return new RedirectCommand( getRequestContext(), (String)target, getContentTemplate(), (String)target );
076    }
077    
078    /**
079     * @see org.apache.wiki.ui.Command#getName()
080     */
081    public String getName()
082    {
083        Object target = getTarget();
084        if ( target == null )
085        {
086            return getJSPFriendlyName();
087        }
088        return target.toString();
089    }
090
091    /**
092     * No-op; always returns <code>null</code>.
093     * @see org.apache.wiki.ui.Command#requiredPermission()
094     */
095    public Permission requiredPermission()
096    {
097        return null;
098    }
099}