View Javadoc
1   package org.apache.turbine.pipeline;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.io.IOException;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.apache.turbine.util.RunData;
30  import org.apache.turbine.util.TurbineException;
31  
32  /**
33   * Implements the Redirect Requested portion of the "Turbine classic"
34   * processing pipeline (from the Turbine 2.x series).
35   *
36   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
37   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
38   * @version $Id$
39   */
40  public class DetermineRedirectRequestedValve
41      implements Valve
42  {
43      private static final Logger log = LogManager.getLogger(DetermineRedirectRequestedValve.class);
44  
45      /**
46       * Creates a new instance.
47       */
48      public DetermineRedirectRequestedValve()
49      {
50          // empty constructor
51      }
52  
53      /**
54       * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
55       */
56      @Override
57      public void invoke(PipelineData pipelineData, ValveContext context)
58          throws IOException, TurbineException
59      {
60          redirectRequested(pipelineData);
61  
62          // Pass control to the next Valve in the Pipeline
63          context.invokeNext(pipelineData);
64      }
65  
66      /**
67       * Perform clean up after processing the request.
68       *
69       * @param pipelineData The run-time data.
70       *
71       * @throws IOException if sending the redirect fails
72       */
73      protected void redirectRequested(PipelineData pipelineData)
74          throws IOException
75      {
76          RunData data = pipelineData.getRunData();
77          // handle a redirect request
78          boolean requestRedirected = StringUtils.isNotEmpty(data.getRedirectURI());
79          if (requestRedirected)
80          {
81              if (data.getResponse().isCommitted())
82              {
83                  log.warn("redirect requested, response already committed: {}", data.getRedirectURI());
84              }
85              else
86              {
87                  data.getResponse().sendRedirect(data.getRedirectURI());
88              }
89          }
90      }
91  }