1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.resources.pages

File PagesResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

16
31
1
1
118
77
11
0.35
31
1
11

Classes

Class Line # Actions
PagesResourceImpl 44 31 0% 11 19
0.604166760.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rest.internal.resources.pages;
21   
22    import java.util.List;
23    import java.util.regex.Pattern;
24   
25    import javax.inject.Named;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.query.Query;
29    import org.xwiki.query.QueryFilter;
30    import org.xwiki.rest.XWikiResource;
31    import org.xwiki.rest.XWikiRestException;
32    import org.xwiki.rest.internal.DomainObjectFactory;
33    import org.xwiki.rest.internal.Utils;
34    import org.xwiki.rest.model.jaxb.Pages;
35    import org.xwiki.rest.resources.pages.PagesResource;
36   
37    import com.xpn.xwiki.api.Document;
38   
39    /**
40    * @version $Id: 5ed70e71bec912eb6fe92f301c5dd42a88b0aabf $
41    */
42    @Component
43    @Named("org.xwiki.rest.internal.resources.pages.PagesResourceImpl")
 
44    public class PagesResourceImpl extends XWikiResource implements PagesResource
45    {
 
46  17 toggle @Override
47    public Pages getPages(String wikiName, String spaceName, Integer start, Integer number,
48    String parentFilterExpression, String order, Boolean withPrettyNames)
49    throws XWikiRestException
50    {
51  17 String database = Utils.getXWikiContext(componentManager).getWikiId();
52  17 List<String> spaces = parseSpaceSegments(spaceName);
53  17 String spaceId = Utils.getLocalSpaceId(spaces);
54   
55  17 Pages pages = objectFactory.createPages();
56   
57  17 try {
58  17 Utils.getXWikiContext(componentManager).setWikiId(wikiName);
59   
60  17 Query query = ("date".equals(order)) ? queryManager.createQuery(
61    "select doc.name from Document doc where doc.space=:space and language='' order by doc.date desc",
62    "xwql") : queryManager.getNamedQuery("getSpaceDocsName");
63   
64    /* Use an explicit query to improve performance */
65  17 List<String> pageNames =
66    query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "hidden"))
67    .bindValue("space", spaceId).setOffset(start).setLimit(number).execute();
68   
69  17 Pattern parentFilter = null;
70  17 if (parentFilterExpression != null) {
71  0 if (parentFilterExpression.equals("null")) {
72  0 parentFilter = Pattern.compile("");
73    } else {
74  0 parentFilter = Pattern.compile(parentFilterExpression);
75    }
76    }
77   
78  17 for (String pageName : pageNames) {
79  134 String pageFullName = Utils.getPageId(wikiName, spaces, pageName);
80   
81  134 if (!Utils.getXWikiApi(componentManager).exists(pageFullName)) {
82  0 logger.warning(String
83    .format("[Page '%s' appears to be in space '%s' but no information is available.]",
84    pageName,
85    spaceId));
86    } else {
87  134 Document doc = Utils.getXWikiApi(componentManager).getDocument(pageFullName);
88   
89    /* We only add pages we have the right to access */
90  134 if (doc != null) {
91  134 boolean add = true;
92   
93  134 Document parent = Utils.getParentDocument(doc, Utils.getXWikiApi(componentManager));
94   
95  134 if (parentFilter != null) {
96  0 String parentId = "";
97  0 if (parent != null && !parent.isNew()) {
98  0 parentId = parent.getPrefixedFullName();
99    }
100  0 add = parentFilter.matcher(parentId).matches();
101    }
102   
103  134 if (add) {
104  134 pages.getPageSummaries().add(DomainObjectFactory.createPageSummary(objectFactory,
105    uriInfo.getBaseUri(), doc, Utils.getXWikiApi(componentManager), withPrettyNames));
106    }
107    }
108    }
109    }
110    } catch (Exception e) {
111  0 throw new XWikiRestException(e);
112    } finally {
113  17 Utils.getXWikiContext(componentManager).setWikiId(database);
114    }
115   
116  17 return pages;
117    }
118    }