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