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.lang3.StringUtils;
027import org.apache.logging.log4j.LogManager;
028import org.apache.logging.log4j.Logger;
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$
039 */
040public class DetermineRedirectRequestedValve
041    implements Valve
042{
043    private static final Logger log = LogManager.getLogger(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 = pipelineData.getRunData();
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}