View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.taglibs.util;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.security.Permission;
39  import info.magnolia.context.MgnlContext;
40  import org.apache.commons.lang.StringUtils;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.jsp.JspException;
47  import javax.servlet.jsp.tagext.BodyTagSupport;
48  import javax.servlet.jsp.tagext.Tag;
49  import java.io.IOException;
50  import java.util.Iterator;
51  
52  
53  /**
54   * Redirects to the first child page. If the server is an authoring instance or magnolia and the preview mode is not
55   * active the tag will simply add to the pagecontext a variable named from the <code>var</code> attribute containing
56   * the path of the child page.
57   * <p>
58   * A typical requirement is to have pages with no content which will simply redirect to a child page: using this tag you
59   * can easily build a "redirect" template and use it for empty pages:
60   * </p>
61   *
62   * <pre>
63   *                         Title                    Template               Mod. Date
64   * -----------------------^----------------^-------^----------------------^---------------
65   * - siteroot             -                o       redirect                05-01-01
66   *   - it                 -                o       redirect                05-01-01
67   *     + home             Home page        o       home                    05-01-01
68   *
69   * </pre>
70   *
71   * <p>
72   * This tag should be put <strong>before</strong> any other tag or include in the page, since response should not be
73   * committed yet for it to work.
74   * </p>
75   * <p>
76   * Example:
77   * </p>
78   *
79   * <pre>
80   * &lt;cmsu:redirect var="destpage" />
81   *
82   * This page has no content and it will redirect to
83   * &lt;a href="${pageContext.request.contextPath}${destpage}">${destpage}&lt;/a> in a public instance.
84   * </pre>
85   *
86   * @jsp.tag name="redirect" body-content="empty"
87   * @jsp.tag-example
88   * <cmsu:redirect var="destpage" />
89   * This page has no content and it will redirect to
90   * <a href="${pageContext.request.contextPath}${destpage}">${destpage}</a> in a public instance.
91   *
92   * @author Fabrizio Giustina
93   * @version $Id: RedirectTag.java 41137 2011-01-06 18:19:25Z gjoseph $
94   * @since 2.2
95   */
96  public class RedirectTag extends BodyTagSupport {
97  
98      /**
99       * Stable serialVersionUID.
100      */
101     private static final long serialVersionUID = 222L;
102 
103     /**
104      * Logger.
105      */
106     private static Logger log = LoggerFactory.getLogger(RedirectTag.class);
107 
108     private String var;
109 
110     /**
111      * Name for the variable which will contain the URL of the page this tag will redirect to.
112      * @jsp.attribute required="false" rtexprvalue="true"
113      */
114     public void setVar(String var) {
115         this.var = var;
116     }
117 
118     /**
119      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
120      */
121     public int doStartTag() throws JspException {
122         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
123         String location = getRedirectLocation(request);
124 
125         Content activePage = MgnlContext.getAggregationState().getMainContent();
126 
127         // on public servers, during preview or when the user can't edit the page, just send the redirect
128         if (!ServerConfiguration.getInstance().isAdmin() || MgnlContext.getAggregationState().isPreviewMode() || !activePage.isGranted(Permission.SET)) {
129             if (location != null) {
130                 try {
131                     ((HttpServletResponse) pageContext.getResponse()).sendRedirect(request.getContextPath() + location);
132                 }
133                 catch (IOException e) {
134                     log.error("Could not redirect to first child HTML page: " + e.getMessage()); //$NON-NLS-1$
135                 }
136                 return Tag.SKIP_PAGE;
137             }
138         }
139         else if (StringUtils.isNotBlank(var)) {
140             request.setAttribute(var, location);
141         }
142         return super.doStartTag();
143     }
144 
145     /**
146      * @see javax.servlet.jsp.tagext.Tag#release()
147      */
148     public void release() {
149         this.var = null;
150         super.release();
151     }
152 
153     /**
154      * Returns the locationto which we intend to redirect.
155      * @param request The HTTP request.
156      * @return A URI if a child page is available, or null.
157      */
158     private String getRedirectLocation(HttpServletRequest request) {
159         Content page = MgnlContext.getAggregationState().getMainContent();
160         Iterator it = page.getChildren().iterator();
161         if (it.hasNext()) {
162             Content c = (Content) it.next();
163             return c.getHandle() + '.' + ServerConfiguration.getInstance().getDefaultExtension();
164         }
165 
166         return null;
167     }
168 
169 }