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 info.magnolia.jcr.util.NodeUtil;
41
42 import java.io.IOException;
43 import java.util.Iterator;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.jsp.JspException;
48 import javax.servlet.jsp.tagext.BodyTagSupport;
49 import javax.servlet.jsp.tagext.Tag;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56 /**
57 * Redirects to the first child page. If the server is an authoring instance or magnolia and the preview mode is not
58 * active the tag will simply add to the pagecontext a variable named from the <code>var</code> attribute containing
59 * the path of the child page.
60 * <p>
61 * A typical requirement is to have pages with no content which will simply redirect to a child page: using this tag you
62 * can easily build a "redirect" template and use it for empty pages:
63 * </p>
64 *
65 * <pre>
66 * Title Template Mod. Date
67 * -----------------------^----------------^-------^----------------------^---------------
68 * - siteroot - o redirect 05-01-01
69 * - it - o redirect 05-01-01
70 * + home Home page o home 05-01-01
71 *
72 * </pre>
73 *
74 * <p>
75 * This tag should be put <strong>before</strong> any other tag or include in the page, since response should not be
76 * committed yet for it to work.
77 * </p>
78 * <p>
79 * Example:
80 * </p>
81 *
82 * <pre>
83 * <cmsu:redirect var="destpage" />
84 *
85 * This page has no content and it will redirect to
86 * <a href="${pageContext.request.contextPath}${destpage}">${destpage}</a> in a public instance.
87 * </pre>
88 *
89 * @jsp.tag name="redirect" body-content="empty"
90 * @jsp.tag-example
91 * <cmsu:redirect var="destpage" />
92 * This page has no content and it will redirect to
93 * <a href="${pageContext.request.contextPath}${destpage}">${destpage}</a> in a public instance.
94 *
95 * @author Fabrizio Giustina
96 * @version $Id: RedirectTag.java 48832 2011-08-31 08:22:48Z had $
97 * @since 2.2
98 */
99 public class RedirectTag extends BodyTagSupport {
100
101 /**
102 * Stable serialVersionUID.
103 */
104 private static final long serialVersionUID = 222L;
105
106 /**
107 * Logger.
108 */
109 private static Logger log = LoggerFactory.getLogger(RedirectTag.class);
110
111 private String var;
112
113 /**
114 * Name for the variable which will contain the URL of the page this tag will redirect to.
115 * @jsp.attribute required="false" rtexprvalue="true"
116 */
117 public void setVar(String var) {
118 this.var = var;
119 }
120
121 /**
122 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
123 */
124 @Override
125 public int doStartTag() throws JspException {
126 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
127 String location = getRedirectLocation(request);
128
129 Content activePage = MgnlContext.getAggregationState().getMainContent();
130
131 // on public servers, during preview or when the user can't edit the page, just send the redirect
132 if (!ServerConfiguration.getInstance().isAdmin() || MgnlContext.getAggregationState().isPreviewMode() || !NodeUtil.isGranted(activePage.getJCRNode(), Permission.SET)) {
133 if (location != null) {
134 try {
135 ((HttpServletResponse) pageContext.getResponse()).sendRedirect(request.getContextPath() + location);
136 }
137 catch (IOException e) {
138 log.error("Could not redirect to first child HTML page: " + e.getMessage()); //$NON-NLS-1$
139 }
140 return Tag.SKIP_PAGE;
141 }
142 }
143 else if (StringUtils.isNotBlank(var)) {
144 request.setAttribute(var, location);
145 }
146 return super.doStartTag();
147 }
148
149 /**
150 * @see javax.servlet.jsp.tagext.Tag#release()
151 */
152 @Override
153 public void release() {
154 this.var = null;
155 super.release();
156 }
157
158 /**
159 * Returns the locationto which we intend to redirect.
160 * @param request The HTTP request.
161 * @return A URI if a child page is available, or null.
162 */
163 private String getRedirectLocation(HttpServletRequest request) {
164 Content page = MgnlContext.getAggregationState().getMainContent();
165 Iterator<Content> it = page.getChildren().iterator();
166 if (it.hasNext()) {
167 Content c = it.next();
168 return c.getHandle() + '.' + ServerConfiguration.getInstance().getDefaultExtension();
169 }
170
171 return null;
172 }
173
174 }