I've attached a patch file to make a change to turbine for jetspeed.
The change relates to how an Action class can request a URL redirect for the
response, rather than processing the response to completion with a body and
such.
To follow the Servlet HTTP rules, we must do the redirect before committing
any headers or body to the response, and once we do a redirect we must not
do anything else with the response.
Rundata already has a mechanism for specifying a redirect:
setRedirectURI(). I propose that this is the standard way for an Action
class to ask for a redirect. Just call this on your rundata with the URI for
the redirect.
To smooth the path back from the Action exec, DefaultPage is modified by the
patch to recognize that a redirect was set in the data, and to do an early
return before doing anything else to the data's response, such as executing
the screen and layout and such.
Finally, Turbine is modified by the patch to recognize the request for a
redirect, implement the request, and make sure not to accidently open the
output stream on the response in the attempt to make sure it is closed.
I have applied this patch to both the tip of the cvs, and to the version of
turbine that Jetspeed is currently using (-r TURBINE_2_2_B1). It works in
both.
Thank you for your help in resolving this problem.
- Glenn
--------------------------------------------
Glenn R. Golden, Systems Research Programmer
University of Michigan School of Information
[EMAIL PROTECTED] 734-615-1419
http://www-personal.si.umich.edu/~ggolden/
--------------------------------------------
> -----Original Message-----
> From: John McNally [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, March 26, 2002 10:36 AM
> To: Turbine Developers List
> Subject: Re: Jetspeed / Turbine problems with redirects
>
>
> Please send your changes as a "cvs diff -u".
>
> john mcnally
>
Index: src/java/org/apache/turbine/Turbine.java
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-2/src/java/org/apache/turbine/Turbine.java,v
retrieving revision 1.11
diff -u -r1.11 Turbine.java
--- src/java/org/apache/turbine/Turbine.java 13 Mar 2002 19:44:19 -0000 1.11
+++ src/java/org/apache/turbine/Turbine.java 26 Mar 2002 18:46:22 -0000
@@ -376,6 +376,9 @@
throws IOException,
ServletException
{
+ // set to true if the request is to be redirected by the page
+ boolean requestRedirected = false;
+
// Placeholder for the RunData object.
RunData data = null;
try
@@ -584,46 +587,64 @@
AccessControlList.SESSION_KEY);
}
- try
+ // handle a redirect request
+ requestRedirected = ((data.getRedirectURI() != null) &&
+(data.getRedirectURI().length() > 0));
+ if (requestRedirected)
{
- if ( data.isPageSet() == false &&
- data.isOutSet() == false )
- throw new Exception ( "Nothing to output" );
-
- // We are all done! if isPageSet() output that way
- // otherwise, data.getOut() has already been written
- // to the data.getOut().close() happens below in the
- // finally.
- if ( data.isPageSet() && data.isOutSet() == false )
+ if (data.getResponse().isCommitted())
+ {
+ requestRedirected = false;
+ log ("redirect requested, response already committed: " +
+data.getRedirectURI());
+ }
+ else
{
- // Modules can override these.
- data.getResponse()
- .setLocale( data.getLocale() );
- data.getResponse()
- .setContentType( data.getContentType() );
-
- // Handle the case where a module may want to send
- // a redirect.
- if ( ( data.getStatusCode() == 301 ||
- data.getStatusCode() == 302 ) &&
- data.getRedirectURI() != null )
+ data.getResponse().sendRedirect(data.getRedirectURI());
+ }
+ }
+
+ if (!requestRedirected)
+ {
+ try
+ {
+ if ( data.isPageSet() == false &&
+ data.isOutSet() == false )
+ throw new Exception ( "Nothing to output" );
+
+ // We are all done! if isPageSet() output that way
+ // otherwise, data.getOut() has already been written
+ // to the data.getOut().close() happens below in the
+ // finally.
+ if ( data.isPageSet() && data.isOutSet() == false )
{
+ // Modules can override these.
data.getResponse()
- .sendRedirect ( data.getRedirectURI() );
- }
+ .setLocale( data.getLocale() );
+ data.getResponse()
+ .setContentType( data.getContentType() );
- // Set the status code.
- data.getResponse().setStatus ( data.getStatusCode() );
- // Output the Page.
- data.getPage().output (data.getOut());
+ // Handle the case where a module may want to send
+ // a redirect.
+ if ( ( data.getStatusCode() == 301 ||
+ data.getStatusCode() == 302 ) &&
+ data.getRedirectURI() != null )
+ {
+ data.getResponse()
+ .sendRedirect ( data.getRedirectURI() );
+ }
+
+ // Set the status code.
+ data.getResponse().setStatus ( data.getStatusCode() );
+ // Output the Page.
+ data.getPage().output (data.getOut());
+ }
+ }
+ catch ( Exception e )
+ {
+ // The output stream was probably closed by the client
+ // end of things ie: the client clicked the Stop
+ // button on the browser, so ignore any errors that
+ // result.
}
- }
- catch ( Exception e )
- {
- // The output stream was probably closed by the client
- // end of things ie: the client clicked the Stop
- // button on the browser, so ignore any errors that
- // result.
}
}
catch ( Exception e )
@@ -637,13 +658,17 @@
finally
{
// Make sure to close the outputstream when we are done.
- try
+ // Note: not for redirects, when we must not get a printwriter on the
+response output stream.
+ if (!requestRedirected)
{
- data.getOut().close();
- }
- catch (Exception e)
- {
- // Ignore.
+ try
+ {
+ data.getOut().close();
+ }
+ catch (Exception e)
+ {
+ // Ignore.
+ }
}
// Return the used RunData to the factory for recycling.
Index: src/java/org/apache/turbine/modules/pages/DefaultPage.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine-2/src/java/org/apache/turbine/modules/pages/DefaultPage.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 DefaultPage.java
--- src/java/org/apache/turbine/modules/pages/DefaultPage.java 16 Aug 2001 05:08:36
-0000 1.1.1.1
+++ src/java/org/apache/turbine/modules/pages/DefaultPage.java 26 Mar 2002 18:46:23
+-0000
@@ -143,6 +143,9 @@
ActionLoader.getInstance().exec ( data, data.getAction() );
}
+ // if a redirect was setup in data, don't do anything else
+ if ((data.getRedirectURI() != null) && (data.getRedirectURI().length() > 0))
+return;
+
// Set the default doctype from the value given in
// TurbineResources.properties.
setDefaultDoctype(data);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>