001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment;
018:
019: import java.io.IOException;
020:
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.cocoon.ProcessingException;
023: import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
024: import org.apache.cocoon.environment.wrapper.MutableEnvironmentFacade;
025:
026: /**
027: * A base class for <code>Redirector</code>s that handle forward redirects, i.e. internal
028: * redirects using the "cocoon:" pseudo-protocol.
029: * <p>
030: * Concrete subclasses have to define the <code>cocoonRedirect()</code> method.
031: *
032: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
033: * @version CVS $Id: ForwardRedirector.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public abstract class ForwardRedirector extends AbstractLogEnabled
036: implements Redirector, PermanentRedirector {
037:
038: /**
039: * Was there a call to <code>redirect()</code> ?
040: */
041: private boolean hasRedirected = false;
042:
043: /** The <code>Environment to use for redirection (either internal or external) */
044: protected Environment env;
045:
046: public ForwardRedirector(Environment env) {
047: this .env = env;
048: }
049:
050: /**
051: * Redirects to a given URL. If this URL starts with "cocoon:", then an internal
052: * redirect is performed. Otherwise, an external redirect is send to the
053: * environment.
054: */
055: public void redirect(boolean sessionMode, String url)
056: throws IOException, ProcessingException {
057: if (getLogger().isInfoEnabled()) {
058: getLogger().info("Redirecting to '" + url + "'");
059: }
060:
061: if (url.startsWith("cocoon:")) {
062: cocoonRedirect(url);
063: } else {
064: this .env.redirect(sessionMode, url);
065: }
066:
067: this .hasRedirected = true;
068: }
069:
070: public void permanentRedirect(boolean sessionMode, String url)
071: throws IOException, ProcessingException {
072: if (getLogger().isInfoEnabled()) {
073: getLogger().info("Redirecting to '" + url + "'");
074: }
075:
076: if (url.startsWith("cocoon:")) {
077: cocoonRedirect(url);
078: } else if (env instanceof PermanentRedirector) {
079: ((PermanentRedirector) env).permanentRedirect(sessionMode,
080: url);
081: } else {
082: this .env.redirect(sessionMode, url);
083: }
084:
085: this .hasRedirected = true;
086: }
087:
088: /**
089: * Unconditionally redirects to a given URL, even it this redirector is part of a
090: * subpipeline.
091: */
092: public void globalRedirect(boolean sessionMode, String url)
093: throws IOException, ProcessingException {
094: if (getLogger().isInfoEnabled()) {
095: getLogger().info("Redirecting to '" + url + "'");
096: }
097:
098: // FIXME : how to handle global redirect to cocoon: ?
099: if (url.startsWith("cocoon:")) {
100: cocoonRedirect(url);
101: } else if (env instanceof MutableEnvironmentFacade) {
102: ((MutableEnvironmentFacade) env).getDelegate()
103: .globalRedirect(sessionMode, url);
104: } else if (env instanceof EnvironmentWrapper) {
105: ((EnvironmentWrapper) env).globalRedirect(sessionMode, url);
106: } else {
107: this .env.redirect(sessionMode, url);
108: }
109: this .hasRedirected = true;
110: }
111:
112: protected abstract void cocoonRedirect(String uri)
113: throws IOException, ProcessingException;
114:
115: /**
116: * Perform check on whether redirection has occured or not
117: */
118: public boolean hasRedirected() {
119: return this .hasRedirected;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.apache.cocoon.environment.Redirector#sendStatus(int)
124: */
125: public void sendStatus(int sc) {
126: env.setStatus(sc);
127: this .hasRedirected = true;
128: }
129: }
|