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