You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by up...@apache.org on 2003/06/24 17:20:30 UTC
cvs commit: cocoon-2.1/src/java/org/apache/cocoon/environment/http HttpEnvironment.java HttpResponse.java
upayavira 2003/06/24 08:20:29
Modified: src/java/org/apache/cocoon/components/treeprocessor/sitemap
RedirectToNodeBuilder.java RedirectToURINode.java
src/java/org/apache/cocoon/environment
ForwardRedirector.java
src/java/org/apache/cocoon/environment/http
HttpEnvironment.java HttpResponse.java
Added: src/java/org/apache/cocoon/environment
PermanentRedirector.java
Log:
Added permanent redirects to <map:redirect-to> using permanent="true" attribute.
Permanent redirect sends status 301 instead of 302. This is interpreted differently
by user agents in relation to whether they cache the page that is returned, thus
permanent redirects are far more search-engine friendly.
Upayavira
Revision Changes Path
1.5 +3 -2 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/RedirectToNodeBuilder.java
Index: RedirectToNodeBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/RedirectToNodeBuilder.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RedirectToNodeBuilder.java 13 May 2003 20:31:31 -0000 1.4
+++ RedirectToNodeBuilder.java 24 Jun 2003 15:20:27 -0000 1.5
@@ -108,7 +108,8 @@
ProcessingNode URINode = new RedirectToURINode(
VariableResolverFactory.getResolver(config.getAttribute("uri"), this.manager),
config.getAttributeAsBoolean("session", false),
- config.getAttributeAsBoolean("global", false)
+ config.getAttributeAsBoolean("global", false),
+ config.getAttributeAsBoolean("permanent", false)
);
return this.treeBuilder.setupNode(URINode, config);
1.2 +10 -2 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/RedirectToURINode.java
Index: RedirectToURINode.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/RedirectToURINode.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RedirectToURINode.java 9 Mar 2003 00:09:22 -0000 1.1
+++ RedirectToURINode.java 24 Jun 2003 15:20:27 -0000 1.2
@@ -55,6 +55,7 @@
import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.Redirector;
+import org.apache.cocoon.environment.PermanentRedirector;
import org.apache.cocoon.sitemap.PatternException;
/**
@@ -72,12 +73,15 @@
private boolean global;
- public RedirectToURINode(VariableResolver uri, boolean createSession, boolean global )
+ private boolean permanent;
+
+ public RedirectToURINode(VariableResolver uri, boolean createSession, boolean global, boolean permanent )
throws PatternException
{
this.global = global;
this.uri = uri;
this.createSession = createSession;
+ this.permanent = permanent;
}
public final boolean invoke(Environment env, InvokeContext context)
@@ -93,6 +97,10 @@
if( this.global )
{
redirector.globalRedirect(this.createSession, resolvedURI);
+ }
+ else if (this.permanent && redirector instanceof PermanentRedirector)
+ {
+ ((PermanentRedirector)redirector).permanentRedirect(this.createSession, resolvedURI);
}
else
{
1.4 +19 -3 cocoon-2.1/src/java/org/apache/cocoon/environment/ForwardRedirector.java
Index: ForwardRedirector.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/ForwardRedirector.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ForwardRedirector.java 10 Mar 2003 15:57:42 -0000 1.3
+++ ForwardRedirector.java 24 Jun 2003 15:20:28 -0000 1.4
@@ -70,7 +70,7 @@
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @version CVS $Id$
*/
-public class ForwardRedirector extends AbstractLogEnabled implements Redirector {
+public class ForwardRedirector extends AbstractLogEnabled implements Redirector, PermanentRedirector {
/** Was there a call to <code>redirect()</code> ? */
private boolean hasRedirected = false;
@@ -118,8 +118,24 @@
this.hasRedirected = true;
}
+ public void permanentRedirect(boolean sessionMode, String url) throws IOException, ProcessingException {
+ if (getLogger().isInfoEnabled()) {
+ getLogger().info("Redirecting to '" + url + "'");
+ }
+
+ if (url.startsWith("cocoon:")) {
+ cocoonRedirect(sessionMode, url);
+ } else if (env instanceof PermanentRedirector) {
+ ((PermanentRedirector)env).permanentRedirect(sessionMode, url);
+ } else {
+ env.redirect(sessionMode, url);
+ }
+ this.hasRedirected = true;
+
+ }
+
/**
- * Inconditionnaly redirects to a given URL, even it this redirector is part of a
+ * Unconditionally redirects to a given URL, even it this redirector is part of a
* subpipeline.
*/
public void globalRedirect(boolean sessionMode, String url) throws IOException, ProcessingException {
1.1 cocoon-2.1/src/java/org/apache/cocoon/environment/PermanentRedirector.java
Index: PermanentRedirector.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.environment;
import org.apache.cocoon.ProcessingException;
import java.io.IOException;
/**
* Interface for a permanent redirector abstraction
*
* @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
* @version CVS $Id: PermanentRedirector.java,v 1.1 2003/06/24 15:20:28 upayavira Exp $
*/
public interface PermanentRedirector {
/**
* Redirect to the given URL
*/
void permanentRedirect(boolean sessionmode, String url) throws IOException, ProcessingException;
}
1.10 +17 -8 cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java
Index: HttpEnvironment.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HttpEnvironment.java 26 May 2003 07:45:42 -0000 1.9
+++ HttpEnvironment.java 24 Jun 2003 15:20:28 -0000 1.10
@@ -60,17 +60,14 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.cocoon.Constants;
-import org.apache.cocoon.environment.AbstractEnvironment;
-import org.apache.cocoon.environment.ObjectModelHelper;
-import org.apache.cocoon.environment.Redirector;
-import org.apache.cocoon.environment.Session;
+import org.apache.cocoon.environment.*;
import org.apache.cocoon.util.NetUtils;
/**
* @author ?
* @version CVS $Id$
*/
-public class HttpEnvironment extends AbstractEnvironment implements Redirector {
+public class HttpEnvironment extends AbstractEnvironment implements Redirector, PermanentRedirector {
public static final String HTTP_REQUEST_OBJECT = "httprequest";
public static final String HTTP_RESPONSE_OBJECT= "httpresponse";
@@ -151,10 +148,18 @@
}
}
+ public void redirect(boolean sessionmode, String newURL) throws IOException {
+ doRedirect(sessionmode, newURL, false);
+ }
+
+ public void permanentRedirect(boolean sessionmode, String newURL) throws IOException {
+ doRedirect(sessionmode, newURL, true);
+ }
+
/**
* Redirect the client to new URL with session mode
*/
- public void redirect(boolean sessionmode, String newURL) throws IOException {
+ private void doRedirect(boolean sessionmode, String newURL, boolean permanent) throws IOException {
this.hasRedirected = true;
// check if session mode shall be activated
@@ -202,7 +207,11 @@
if (getLogger().isDebugEnabled()) {
getLogger().debug("Sending redirect to '" + redirect + "'");
}
- this.response.sendRedirect (redirect);
+ if (permanent) {
+ this.response.sendPermanentRedirect(redirect);
+ } else {
+ this.response.sendRedirect (redirect);
+ }
}
public boolean hasRedirected() {
1.2 +5 -1 cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpResponse.java
Index: HttpResponse.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpResponse.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpResponse.java 9 Mar 2003 00:09:30 -0000 1.1
+++ HttpResponse.java 24 Jun 2003 15:20:28 -0000 1.2
@@ -131,6 +131,10 @@
this.res.sendRedirect(location);
}
+ public void sendPermanentRedirect(String location) throws IOException {
+ this.res.setHeader("location", location);
+ this.res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
+ }
public void setDateHeader(String name, long date) {
this.res.setDateHeader(name, date);
}