View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.core.search;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.DefaultContent;
38  import info.magnolia.cms.core.HierarchyManager;
39  import info.magnolia.cms.core.ItemType;
40  import info.magnolia.cms.security.PermissionUtil;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.Hashtable;
45  import java.util.Map;
46  
47  import javax.jcr.Node;
48  import javax.jcr.NodeIterator;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.Session;
51  
52  import org.apache.commons.lang3.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  
57  /**
58   * Wrapping a JCR {@link javax.jcr.query.QueryResult}. This class will filter the result according
59   * to the user's ACLs. You can use {@link #getContent(String)} to retrieve nodes of a certain type.
60   * If the node's type doesn't match the nearest matching ancestors is add instead. This allows to
61   * search in paragraph content while retrieving a list of pages.
62   *
63   * @deprecated Since 4.5.4 we are using JCR query API.
64   */
65  @Deprecated
66  public class QueryResultImpl implements QueryResult {
67  
68      private static Logger log = LoggerFactory.getLogger(QueryResultImpl.class);
69  
70      /**
71       * Unfiltered result object.
72       */
73      protected javax.jcr.query.QueryResult result;
74  
75      /**
76       * caches all previously queried objects.
77       */
78      protected Map<String, Collection<Content>> objectStore = new Hashtable<String, Collection<Content>>();
79  
80      /**
81       * TODO dlipp: Remove - it's not used internally.
82       */
83      protected HierarchyManager hm;
84  
85      protected final long maxResultSize;
86  
87      protected Map<String, String> dirtyHandles = new Hashtable<String, String>();
88  
89      protected QueryResultImpl(javax.jcr.query.QueryResult result, HierarchyManager hm) {
90          this(result, hm, Long.MAX_VALUE);
91      }
92  
93      protected QueryResultImpl(javax.jcr.query.QueryResult result, HierarchyManager hm, long maxResultSize) {
94          this.result = result;
95          this.hm = hm;
96          this.maxResultSize = maxResultSize;
97      }
98  
99      public javax.jcr.query.QueryResult getJcrResult() {
100         return result;
101     }
102 
103     /**
104      * Adds all found nodes of a certain type. If the type doesn't match it will traverse the
105      * ancestors and add them instead.
106      */
107     protected void build(String nodeType, Collection<Content> collection) throws RepositoryException {
108         this.objectStore.put(nodeType, collection);
109         NodeIterator nodeIterator = this.result.getNodes();
110 
111         // whitespace separated list (can't hurt since a single nodetype name can't contain a space)
112         String[] nodeTypes = StringUtils.split(nodeType);
113 
114         while (collection.size() < maxResultSize && nodeIterator.hasNext()) {
115             Node node = nodeIterator.nextNode();
116             try {
117                 build(node, nodeTypes, collection);
118             } catch (RepositoryException re) {
119                 log.error("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage());
120                 log.debug("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage(), re);
121             }
122         }
123     }
124 
125     /**
126      * Traverses the hierarchy from the current node to the root until the node's type matches.
127      */
128     protected void build(Node node, String[] nodeType, Collection<Content> collection) throws RepositoryException {
129         /**
130          * All custom node types
131          */
132         if ((nodeType == null || nodeType.length == 0) || isNodeType(node, nodeType) && !node.isNodeType(ItemType.NT_RESOURCE)) {
133             if (this.dirtyHandles.get(node.getPath()) == null) {
134                 // TODO: yet another of those silly checks ... if we were not allowed, we would not have the node here
135                 boolean isAllowed = PermissionUtil.isGranted(node.getSession(), node.getPath(), Session.ACTION_READ);
136                 if (isAllowed) {
137                     collection.add(new DefaultContent(node));
138                     this.dirtyHandles.put(node.getPath(), StringUtils.EMPTY);
139                 }
140             }
141             return;
142         }
143         if (node.getDepth() > 0) {
144             this.build(node.getParent(), nodeType, collection);
145         }
146     }
147 
148     @Override
149     public Collection<Content> getContent() {
150         return getContent(ItemType.CONTENT.getSystemName());
151     }
152 
153     @Override
154     public Collection<Content> getContent(String nodeType) {
155         Collection<Content> resultSet = this.objectStore.get(nodeType);
156         if (resultSet == null) {
157             /* build it first time */
158             resultSet = new ArrayList<Content>();
159             try {
160                 this.build(nodeType, resultSet);
161             } catch (RepositoryException re) {
162                 log.error(re.getMessage());
163             }
164         }
165         return resultSet;
166     }
167 
168     private boolean isNodeType(Node node, String[] nodeType) throws RepositoryException {
169 
170         for (String nt : nodeType) {
171             if (node.isNodeType(nt)) {
172                 return true;
173             }
174         }
175         return false;
176     }
177 }