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.module.admininterface.pages;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.util.ContentUtil;
38  import info.magnolia.cms.util.DumperUtil;
39  import info.magnolia.cms.util.QueryUtil;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.module.admininterface.TemplatedMVCHandler;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.repository.RepositoryConstants;
44  import info.magnolia.repository.RepositoryManager;
45  
46  import java.util.ArrayList;
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  import javax.jcr.Node;
52  import javax.jcr.NodeIterator;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.query.Query;
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.apache.commons.lang.ArrayUtils;
59  import org.apache.commons.lang.StringEscapeUtils;
60  import org.apache.commons.lang.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * UI for launching JCR queries.
66   * @version $Id$
67   *
68   */
69  public class JCRUtilsPage extends TemplatedMVCHandler {
70      private static final Logger log = LoggerFactory.getLogger(JCRUtilsPage.class);
71  
72      private String repository = "";
73  
74      private int level = 1;
75  
76      private String path = "/";
77  
78      private String result = "";
79  
80      private String statement = "";
81  
82      private String language = Query.JCR_SQL2;
83  
84      private String[] supportedLanguages = new String[]{};
85  
86      private String itemType = "nt:base";
87  
88      private final List<String> repositories = new ArrayList<String>();
89  
90      public JCRUtilsPage(String name, HttpServletRequest request, HttpServletResponse response) {
91          super(name, request, response);
92          try {
93              supportedLanguages = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE).getWorkspace().getQueryManager().getSupportedQueryLanguages();
94              supportedLanguages = (String[]) ArrayUtils.removeElement(supportedLanguages, Query.JCR_JQOM) ;
95  
96              final Iterator<String> iter = Components.getComponent(RepositoryManager.class).getWorkspaceNames().iterator();
97              while(iter.hasNext()) {
98                  repositories.add(iter.next());
99              }
100             Collections.sort(repositories, String.CASE_INSENSITIVE_ORDER);
101 
102         } catch (RepositoryException e) {
103             this.result = e.getMessage();
104             log.error("An error occurred while retrieving supported query languages.", e);
105         }
106     }
107 
108 
109     public String dump() {
110         if (StringUtils.isNotEmpty(repository) && StringUtils.isNotEmpty(path)) {
111             Content node = ContentUtil.getContent(repository, path);
112             if (node == null) {
113                 return "path not found: " + this.path;
114             }
115             result = DumperUtil.dump(node, level);
116         }
117         return VIEW_SHOW;
118     }
119 
120     public String query() {
121         final long start = System.currentTimeMillis();
122         final NodeIterator iterator;
123         try {
124             iterator = QueryUtil.search(repository, statement, language, this.itemType);
125         } catch (Throwable e) {
126             this.result = e.getMessage() != null ? e.getMessage() : e.toString();
127             log.error("Error in JCR query:", e);
128             return VIEW_SHOW;
129         }
130         final StringBuilder sb = new StringBuilder();
131 
132         int count = 0;
133         while(iterator.hasNext()) {
134             Node node = iterator.nextNode();
135             count++;
136             try {
137                 sb.append(node.getPath());
138             } catch (RepositoryException e) {
139                 this.result = e.getMessage() != null ? e.getMessage() : e.toString();
140                 log.error("Error in JCR query:", e);
141                 return VIEW_SHOW;
142             }
143             sb.append("\n");
144         }
145 
146         sb.insert(0, Integer.toString(count) + " nodes returned in " + Long.toString((System.currentTimeMillis() - start)) + "ms\n");
147 
148         this.result = sb.toString();
149         return VIEW_SHOW;
150     }
151 
152     public String delete() {
153         try {
154             MgnlContext.getHierarchyManager(repository).delete(path);
155         }
156         catch (Exception e) {
157             result = e.toString();
158         }
159         return VIEW_SHOW;
160     }
161 
162     public Iterator getRepositories() {
163         return repositories.iterator();
164     }
165 
166     public String[] getLanguages() throws RepositoryException {
167         return supportedLanguages;
168     }
169 
170     public int getLevel() {
171         return level;
172     }
173 
174     public void setLevel(int level) {
175         this.level = level;
176     }
177 
178 
179     public String getPath() {
180         return path;
181     }
182 
183 
184     public void setPath(String path) {
185         this.path = StringEscapeUtils.escapeHtml(path);
186     }
187 
188 
189     public String getRepository() {
190         return repository;
191     }
192 
193 
194     public void setRepository(String repositroy) {
195         this.repository = repositroy;
196     }
197 
198 
199     public String getResult() {
200         return result;
201     }
202 
203 
204     public String getStatement() {
205         return statement;
206     }
207 
208 
209     public void setStatement(String statement) {
210         this.statement = statement;
211     }
212 
213 
214     public String getLanguage() {
215         return language;
216     }
217 
218 
219     public void setLanguage(String language) {
220         this.language = language;
221     }
222 
223 
224     public String getItemType() {
225         return itemType;
226     }
227 
228 
229     public void setItemType(String itemType) {
230         this.itemType = itemType;
231     }
232 
233 }