KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > RedirectTag


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import info.magnolia.cms.beans.config.Server;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.util.Resource;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32 /**
33  * <p>
34  * Redirects to the first child page. If the server is an authoring instance or magnolia and the preview mode is not
35  * active the tag will simply add to the pagecontext a variable named from the <code>var</code> attribute containing
36  * the path of the child page.
37  * </p>
38  * <p>
39  * A typical requirement is to have pages with no content which will simply redirect to a child page: using this tag you
40  * can easily build a "redirect" template and use it for empty pages:
41  * </p>
42  *
43  * <pre>
44  * Title Template Mod. Date
45  * -----------------------^----------------^-------^----------------------^---------------
46  * - siteroot - o redirect 05-01-01
47  * - it - o redirect 05-01-01
48  * + home Home page o home 05-01-01
49  *
50  * </pre>
51  *
52  * <p>
53  * This tag should be put <strong>before</strong> any other tag or include in the page, since response should not be
54  * committed yet for it to work.
55  * </p>
56  * <p>
57  * Example:
58  * </p>
59  *
60  * <pre>
61  * &lt;cmsu:redirect var="destpage" />
62  *
63  * This page has no content and it will redirect to
64  * &lt;a HREF="${pageContext.request.contextPath}${destpage}">${destpage}&lt;/a> in a public instance.
65  * </pre>
66  *
67  * @author Fabrizio Giustina
68  * @version $Id: RedirectTag.java 6341 2006-09-12 09:18:27Z philipp $
69  * @since 2.2
70  */

71 public class RedirectTag extends BodyTagSupport JavaDoc {
72
73     /**
74      * Stable serialVersionUID.
75      */

76     private static final long serialVersionUID = 222L;
77
78     /**
79      * Logger.
80      */

81     private static Logger log = LoggerFactory.getLogger(RedirectTag.class);
82
83     /**
84      * Name for the variable which will contain the URL of the page this tag will redirect to.
85      */

86     private String JavaDoc var;
87
88     /**
89      * Setter for the <code>var</code> tag parameter.
90      * @param var Name for the variable which will contain the URL of the page this tag will redirect to
91      */

92     public void setVar(String JavaDoc var) {
93         this.var = var;
94     }
95
96     /**
97      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
98      */

99     public int doStartTag() throws JspException JavaDoc {
100         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
101         String JavaDoc location = getRedirectLocation(request);
102         if (!Server.isAdmin() || Resource.showPreview(request)) {
103
104             if (location != null) {
105                 try {
106                     ((HttpServletResponse JavaDoc) pageContext.getResponse()).sendRedirect(request.getContextPath() + location);
107                 }
108                 catch (IOException JavaDoc e) {
109                     log.error("Could not redirect to first child HTML page: " + e.getMessage()); //$NON-NLS-1$
110
}
111             }
112         }
113         else if (StringUtils.isNotBlank(var)) {
114             request.setAttribute(var, location);
115         }
116         return super.doStartTag();
117     }
118
119     /**
120      * @see javax.servlet.jsp.tagext.Tag#release()
121      */

122     public void release() {
123         this.var = null;
124         super.release();
125     }
126
127     /**
128      * Returns the locationto which we intend to redirect.
129      * @param request The HTTP request.
130      * @return A URI if a child page is available, or null.
131      */

132     private String JavaDoc getRedirectLocation(HttpServletRequest JavaDoc request) {
133         Content page = Resource.getActivePage(request);
134         Iterator JavaDoc it = page.getChildren().iterator();
135         if (it.hasNext()) {
136             Content c = (Content) it.next();
137             return c.getHandle() + '.' + Server.getDefaultExtension();
138         }
139
140         return null;
141     }
142
143 }
144
Popular Tags