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.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28  import org.apache.turbine.util.RunData;
    29  import org.apache.turbine.util.TurbineException;
    30
    31  /**
    32   * Implements the Redirect Requested portion of the "Turbine classic"
    33   * processing pipeline (from the Turbine 2.x series).
    34   *
    35   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    36   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    37   * @version $Id: DetermineRedirectRequestedValve.java 1706239 2015-10-01 13:18:35Z tv $
    38   */
    39  public class DetermineRedirectRequestedValve
    40      extends AbstractValve
    41  {
    42      private static final Log log = LogFactory.getLog(DetermineRedirectRequestedValve.class);
    43      /**
    44       * Creates a new instance.
    45       */
    46      public DetermineRedirectRequestedValve()
    47      {
    48          // empty constructor
    49      }
    50
    51      /**
    52       * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
    53       */
    54      public void invoke(PipelineData pipelineData, ValveContext context)
    55          throws IOException, TurbineException
    56      {
    57          try
    58          {
    59              redirectRequested(pipelineData);
    60          }
    61          catch (Exception e)
    62          {
    63              throw new TurbineException(e);
    64          }
    65
    66          // Pass control to the next Valve in the Pipeline
    67          context.invokeNext(pipelineData);
    68      }
    69
    70      /**
    71       * Perform clean up after processing the request.
    72       *
    73       * @param pipelineData The run-time data.
    74       */
    75      protected void redirectRequested(PipelineData pipelineData)
    76          throws Exception
    77      {
    78          RunData data = getRunData(pipelineData);
    79          // handle a redirect request
    80          boolean requestRedirected = ((data.getRedirectURI() != null)
    81          && (data.getRedirectURI().length() > 0));
    82          if (requestRedirected)
    83          {
    84              if (data.getResponse().isCommitted())
    85              {
    86                  requestRedirected = false;
    87                  log.warn("redirect requested, response already committed: " +
    88                          data.getRedirectURI());
    89              }
    90              else
    91              {
    92                  data.getResponse().sendRedirect(data.getRedirectURI());
    93              }
    94          }
    95
    96          if (!requestRedirected)
    97          {
    98              try
    99              {
    100                 if (data.isPageSet() == false && data.isOutSet() == false)
    101                 {
    102                     throw new Exception("Nothing to output");
    103                 }
    104
    105                 // We are all done! if isPageSet() output that way
    106                 // otherwise, data.getOut() has already been written
    107                 // to the data.getOut().close() happens below in the
    108                 // finally.
    109                 if (data.isPageSet() && data.isOutSet() == false)
    110                 {
    111                     // Modules can override these.
    112                     data.getResponse().setLocale(data.getLocale());
    113                     data.getResponse().setContentType(
    114                             data.getContentType());
    115
    116                     // Set the status code.
    117                     data.getResponse().setStatus(data.getStatusCode());
    118                     // Output the Page.
    119                     data.getPage().output(data.getResponse().getWriter());
    120                 }
    121             }
    122             catch (Exception e)
    123             {
    124                 // The output stream was probably closed by the client
    125                 // end of things ie: the client clicked the Stop
    126                 // button on the browser, so ignore any errors that
    127                 // result.
    128                 log.debug("Output stream closed? ", e);
    129             }
    130         }
    131     }
    132 }