    001package org.apache.turbine.pipeline;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import java.io.IOException;
    025
    026import org.apache.commons.lang.StringUtils;
    027import org.apache.commons.logging.Log;
    028import org.apache.commons.logging.LogFactory;
    029import org.apache.turbine.util.RunData;
    030import org.apache.turbine.util.TurbineException;
    031
    032/**
    033 * Implements the Redirect Requested portion of the "Turbine classic"
    034 * processing pipeline (from the Turbine 2.x series).
    035 *
    036 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    037 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    038 * @version $Id: DetermineRedirectRequestedValve.java 1773378 2016-12-09 13:19:59Z tv $
    039 */
    040public class DetermineRedirectRequestedValve
    041    extends AbstractValve
    042{
    043    private static final Log log = LogFactory.getLog(DetermineRedirectRequestedValve.class);
    044
    045    /**
    046     * Creates a new instance.
    047     */
    048    public DetermineRedirectRequestedValve()
    049    {
    050        // empty constructor
    051    }
    052
    053    /**
    054     * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
    055     */
    056    @Override
    057    public void invoke(PipelineData pipelineData, ValveContext context)
    058        throws IOException, TurbineException
    059    {
    060        redirectRequested(pipelineData);
    061
    062        // Pass control to the next Valve in the Pipeline
    063        context.invokeNext(pipelineData);
    064    }
    065
    066    /**
    067     * Perform clean up after processing the request.
    068     *
    069     * @param pipelineData The run-time data.
    070     *
    071     * @throws IOException if sending the redirect fails
    072     */
    073    protected void redirectRequested(PipelineData pipelineData)
    074        throws IOException
    075    {
    076        RunData data = getRunData(pipelineData);
    077        // handle a redirect request
    078        boolean requestRedirected = StringUtils.isNotEmpty(data.getRedirectURI());
    079        if (requestRedirected)
    080        {
    081            if (data.getResponse().isCommitted())
    082            {
    083                log.warn("redirect requested, response already committed: " + data.getRedirectURI());
    084            }
    085            else
    086            {
    087                data.getResponse().sendRedirect(data.getRedirectURI());
    088            }
    089        }
    090    }
    091}