View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.module.templatingkit.redirection.validator;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.PropertyUtil;
40  import info.magnolia.repository.RepositoryConstants;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
42  
43  import javax.jcr.Node;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.Session;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.data.Item;
52  import com.vaadin.data.validator.AbstractValidator;
53  
54  /**
55   * A validator to prevent the user to select invalid redirect path.
56   * Assuming both nodes current page and target page are both on website repository (which also indicates unique node paths).<br/>
57   * A redirect path is considered invalid, if:<br/>
58   * - It is the path of the redirecting page itself (pointing to itself).<br/>
59   * - It leads to circular redirection leading to infinite redirection.<br/>
60   * - It leads to a redirection cascade where the last redirection page has no path set and no child page.<br/>
61   */
62  public class RedirectTargetValidator extends AbstractValidator<String> {
63  
64      private final static Logger log = LoggerFactory.getLogger(RedirectTargetValidator.class);
65  
66      private static final String indirectRedirectPathLeadingTo404 = "indirectRedirectPathLeadingTo404";
67      private static final String circularRedirection = "circularRedirection";
68      private static final String redirectTo404 = "redirectTo404";
69  
70      private final Item item;
71      private final RedirectTargetValidatorDefinition definition;
72      private String errorMessageType = circularRedirection;
73  
74      public RedirectTargetValidator(Item item, RedirectTargetValidatorDefinition definition) {
75          super(null);
76          this.item = item;
77          this.definition = definition;
78      }
79  
80      @Override
81      public Class<String> getType() {
82          return String.class;
83      }
84  
85      @Override
86      public String getErrorMessage() {
87          if (circularRedirection.equals(errorMessageType)) {
88              return definition.getErrorMessage();
89          } else {
90              return definition.getErrorMessageFor404();
91          }
92      }
93  
94      @Override
95      protected boolean isValidValue(String targetPagePath) {
96          if (StringUtils.isEmpty(targetPagePath)) {
97              return true;
98          }
99  
100         try {
101             errorMessageType = circularRedirection;
102             final Session session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
103             final Node currentPage = (Node) ((JcrItemAdapter) item).getJcrItem();
104             final String currentPageLocation = currentPage.getPath();
105 
106             // 1st check whether the targetPage is the same as the currentPage
107             if (targetPagePath.equals(currentPageLocation)) {
108                 return false;
109             }
110 
111             // 2nd check: make sure that we have no redirection to redirection ... to redirection again to the currentPage.
112             // and no indirect redirection which would lead to 404 (pointing to another redirect with not target path set and with no child page)
113             final String indirectRedirectionPath = findCascadedRedirectionTarget(targetPagePath, session, currentPageLocation);
114             if (indirectRedirectPathLeadingTo404.equals(indirectRedirectionPath)) {
115                 errorMessageType = redirectTo404;
116                 return false;
117             } else if (circularRedirection.equals(indirectRedirectionPath)) {
118                 return false;
119             } else if (indirectRedirectionPath == null) {
120                 return true;
121             }
122         } catch (RepositoryException e) {
123             log.error("Failed to fetch jcr data.", e);
124         }
125 
126         // returning false here is not "ideal", but will occur only if a RepositoryException was thrown.
127         return false;
128     }
129 
130     /**
131      * Returns the "final" path to a redirection page. Final means, the final path after a cascade of redirects. Return value is:<br/>
132      * - null, if the "final" targetNode is not a redirection page.<br/>
133      * - The path to the further redirectionPage if the pageNode redirects. (In this case, the function is triggered again.)<br/>
134      * - "indirectRedirectPathLeadingTo404" if the final redirect in the cascade has no target path defined and has no child page.<br/>
135      * - "circularRedirection" if the final redirect leads to the current node (which leads to circular redirection).<br/>
136      * - Method is applied recursive until it returns null , "circularRedirection" or  "indirectRedirectPathLeadingTo404".
137      */
138     private String findCascadedRedirectionTarget(String redirectPath, Session session, String currentNodePath) throws RepositoryException {
139         final Node redirectionTargetNode = session.getNode(redirectPath);
140 
141         // redirection target is again a redirection
142         if (isRedirectPage(redirectionTargetNode)) {
143             final String nextRedirectionPath = PropertyUtil.getString(redirectionTargetNode, "path");
144 
145             // if no redirection path defined on indirectRedirectionPage, per default it would be the 1st child of indirectRedirectionPage
146             if (StringUtils.isEmpty(nextRedirectionPath)) {
147                 final Iterable<Node> children = NodeUtil.getNodes(redirectionTargetNode, NodeTypes.Page.NAME);
148 
149                 if (children != null && children.iterator().hasNext()) {
150                     final Node firstChild = children.iterator().next();
151 
152                     // 1st child is not a redirection again, that's fine
153                     if (!isRedirectPage(firstChild)) {
154                         return null;
155                     } else {
156                         return findCascadedRedirectionTarget(firstChild.getPath(), session, currentNodePath);
157                     }
158                 // no child page found
159                 } else {
160                     return indirectRedirectPathLeadingTo404;
161                 }
162             } else {
163                 if (currentNodePath.equals(nextRedirectionPath)) {
164                     return circularRedirection;
165                 } else {
166                     return findCascadedRedirectionTarget(nextRedirectionPath, session, currentNodePath);
167                 }
168             }
169         }
170         return null;
171     }
172 
173     private boolean isRedirectPage(Node pageNode) throws RepositoryException {
174         final String templateId = PropertyUtil.getString(pageNode, NodeTypes.Renderable.TEMPLATE);
175         return StringUtils.equals(definition.getTemplateId(), templateId);
176     }
177 
178 }