001 package 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
024 import java.io.IOException;
025
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028 import org.apache.turbine.util.RunData;
029 import org.apache.turbine.util.TurbineException;
030
031 /**
032 * Implements the Redirect Requested portion of the "Turbine classic"
033 * processing pipeline (from the Turbine 2.x series).
034 *
035 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
036 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
037 * @version $Id: DetermineRedirectRequestedValve.java 757213 2009-03-22 16:43:31Z tv $
038 */
039 public class DetermineRedirectRequestedValve
040 extends AbstractValve
041 {
042 private static final Log log = LogFactory.getLog(DetermineRedirectRequestedValve.class);
043 /**
044 * Creates a new instance.
045 */
046 public DetermineRedirectRequestedValve()
047 {
048 // empty constructor
049 }
050
051 /**
052 * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
053 */
054 public void invoke(PipelineData pipelineData, ValveContext context)
055 throws IOException, TurbineException
056 {
057 try
058 {
059 redirectRequested(pipelineData);
060 }
061 catch (Exception e)
062 {
063 throw new TurbineException(e);
064 }
065
066 // Pass control to the next Valve in the Pipeline
067 context.invokeNext(pipelineData);
068 }
069
070 /**
071 * Perform clean up after processing the request.
072 *
073 * @param data The run-time data.
074 */
075 protected void redirectRequested(PipelineData pipelineData)
076 throws Exception
077 {
078 RunData data = getRunData(pipelineData);
079 // handle a redirect request
080 boolean requestRedirected = ((data.getRedirectURI() != null)
081 && (data.getRedirectURI().length() > 0));
082 if (requestRedirected)
083 {
084 if (data.getResponse().isCommitted())
085 {
086 requestRedirected = false;
087 log.warn("redirect requested, response already committed: " +
088 data.getRedirectURI());
089 }
090 else
091 {
092 data.getResponse().sendRedirect(data.getRedirectURI());
093 }
094 }
095
096 if (!requestRedirected)
097 {
098 try
099 {
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 }