KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > ForwardRedirector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.cocoon.ProcessingException;
22 import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
23 import org.apache.cocoon.environment.wrapper.MutableEnvironmentFacade;
24
25 /**
26  * A base class for <code>Redirector</code>s that handle forward redirects, i.e. internal
27  * redirects using the "cocoon:" pseudo-protocol.
28  * <p>
29  * Concrete subclasses have to define the <code>cocoonRedirect()</code> method.
30  *
31  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
32  * @version CVS $Id: ForwardRedirector.java 57427 2004-11-11 12:28:57Z cziegeler $
33  */

34 public abstract class ForwardRedirector extends AbstractLogEnabled implements Redirector, PermanentRedirector {
35
36     /**
37      * Was there a call to <code>redirect()</code> ?
38      */

39     private boolean hasRedirected = false;
40     
41     /** The <code>Environment to use for redirection (either internal or external) */
42     protected Environment env;
43
44     public ForwardRedirector(Environment env) {
45         this.env = env;
46     }
47
48     /**
49      * Redirects to a given URL. If this URL starts with "cocoon:", then an internal
50      * redirect is performed. Otherwise, an external redirect is send to the
51      * environment.
52      */

53     public void redirect(boolean sessionMode, String JavaDoc url) throws IOException JavaDoc, ProcessingException {
54         if (getLogger().isInfoEnabled()) {
55             getLogger().info("Redirecting to '" + url + "'");
56         }
57
58         if (url.startsWith("cocoon:")) {
59             cocoonRedirect(url);
60         } else {
61             this.env.redirect(sessionMode, url);
62         }
63
64         this.hasRedirected = true;
65     }
66
67     public void permanentRedirect(boolean sessionMode, String JavaDoc url) throws IOException JavaDoc, ProcessingException {
68         if (getLogger().isInfoEnabled()) {
69             getLogger().info("Redirecting to '" + url + "'");
70         }
71
72         if (url.startsWith("cocoon:")) {
73             cocoonRedirect(url);
74         } else if (env instanceof PermanentRedirector) {
75             ((PermanentRedirector)env).permanentRedirect(sessionMode, url);
76         } else {
77             this.env.redirect(sessionMode, url);
78         }
79
80         this.hasRedirected = true;
81     }
82
83     /**
84      * Unconditionally redirects to a given URL, even it this redirector is part of a
85      * subpipeline.
86      */

87     public void globalRedirect(boolean sessionMode, String JavaDoc url) throws IOException JavaDoc, ProcessingException {
88         if (getLogger().isInfoEnabled()) {
89             getLogger().info("Redirecting to '" + url + "'");
90         }
91
92         // FIXME : how to handle global redirect to cocoon: ?
93
if (url.startsWith("cocoon:")) {
94             cocoonRedirect(url);
95         } else if (env instanceof MutableEnvironmentFacade ) {
96             ((MutableEnvironmentFacade)env).getDelegate().globalRedirect(sessionMode, url);
97         } else if (env instanceof EnvironmentWrapper) {
98             ((EnvironmentWrapper)env).globalRedirect(sessionMode,url);
99         } else {
100             this.env.redirect(sessionMode, url);
101         }
102         this.hasRedirected = true;
103     }
104
105     protected abstract void cocoonRedirect(String JavaDoc uri) throws IOException JavaDoc, ProcessingException;
106
107     /**
108      * Perform check on whether redirection has occured or not
109      */

110     public boolean hasRedirected() {
111         return this.hasRedirected;
112     }
113     
114     /* (non-Javadoc)
115      * @see org.apache.cocoon.environment.Redirector#sendStatus(int)
116      */

117     public void sendStatus(int sc) {
118         env.setStatus(sc);
119         this.hasRedirected = true;
120     }
121 }
122
Popular Tags