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

File AbstractDatabaseSearchSource.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
51
2
1
183
123
10
0.2
25.5
2
5

Classes

Class Line # Actions
AbstractDatabaseSearchSource 60 51 0% 10 9
0.8615384786.2%
 

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.search;
21   
22    import java.util.ArrayList;
23    import java.util.Calendar;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Provider;
30    import javax.ws.rs.core.UriInfo;
31   
32    import org.apache.commons.lang3.StringUtils;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.query.Query;
37    import org.xwiki.query.QueryFilter;
38    import org.xwiki.query.QueryManager;
39    import org.xwiki.query.internal.HiddenDocumentFilter;
40    import org.xwiki.rest.Relations;
41    import org.xwiki.rest.internal.ModelFactory;
42    import org.xwiki.rest.internal.Utils;
43    import org.xwiki.rest.model.jaxb.Link;
44    import org.xwiki.rest.model.jaxb.SearchResult;
45    import org.xwiki.rest.resources.pages.PageResource;
46    import org.xwiki.rest.resources.pages.PageTranslationResource;
47    import org.xwiki.security.authorization.ContextualAuthorizationManager;
48    import org.xwiki.security.authorization.Right;
49   
50    import com.xpn.xwiki.XWikiContext;
51    import com.xpn.xwiki.api.Document;
52    import com.xpn.xwiki.api.XWiki;
53    import com.xpn.xwiki.doc.XWikiDocument;
54    import com.xpn.xwiki.objects.BaseObject;
55   
56    /**
57    * @version $Id: 8bd0d3ca33f572a01eee9f38441a7c9928d21db2 $
58    * @since 6.1M2
59    */
 
60    public abstract class AbstractDatabaseSearchSource extends AbstractSearchSource
61    {
62    @Inject
63    protected Provider<XWikiContext> xcontextProvider;
64   
65    @Inject
66    protected QueryManager queryManager;
67   
68    @Inject
69    @Named(HiddenDocumentFilter.HINT)
70    protected QueryFilter hiddenFilter;
71   
72    @Inject
73    protected ModelFactory modelFactory;
74   
75    @Inject
76    protected DocumentReferenceResolver<String> resolver;
77   
78    @Inject
79    protected ContextualAuthorizationManager authorization;
80   
81    protected final String queryLanguage;
82   
 
83  1 toggle public AbstractDatabaseSearchSource(String queryLanguage)
84    {
85  1 this.queryLanguage = queryLanguage;
86    }
87   
88    protected abstract String resolveQuery(boolean distinct, String query);
89   
 
90  3 toggle @Override
91    public List<SearchResult> search(String partialQueryString, String wikiName, String wikis,
92    boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start,
93    Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception
94    {
95  3 XWikiContext xwikiContext = this.xcontextProvider.get();
96  3 XWiki xwikiApi = new XWiki(xwikiContext.getWiki(), xwikiContext);
97   
98  3 if (partialQueryString == null || StringUtils.startsWithIgnoreCase(partialQueryString, "select")) {
99  0 return Collections.emptyList();
100    }
101   
102  3 String queryString = resolveQuery(distinct, partialQueryString);
103   
104  3 Query query = this.queryManager.createQuery(queryString, this.queryLanguage);
105   
106  3 query.setLimit(number).setOffset(start);
107  3 query.setWiki(wikiName);
108   
109  3 List<Object> queryResult = query.execute();
110   
111  3 WikiReference wikiReference = new WikiReference(wikiName);
112   
113    /* Build the result. */
114  3 List<SearchResult> result = new ArrayList<>();
115  3 for (Object object : queryResult) {
116  3 Object[] fields = (Object[]) object;
117   
118  3 String fullName = (String) fields[0];
119  3 String language = (String) fields[3];
120   
121  3 DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference);
122   
123    /* Check if the user has the right to see the found document */
124  3 if (this.authorization.hasAccess(Right.VIEW, documentReference)) {
125  3 Document doc = xwikiApi.getDocument(documentReference);
126  3 String title = doc.getDisplayTitle();
127   
128  3 SearchResult searchResult = this.objectFactory.createSearchResult();
129  3 searchResult.setType("page");
130  3 searchResult.setId(doc.getPrefixedFullName());
131  3 searchResult.setPageFullName(doc.getFullName());
132  3 searchResult.setTitle(title);
133  3 searchResult.setWiki(wikiName);
134  3 searchResult.setSpace(doc.getSpace());
135  3 searchResult.setPageName(doc.getName());
136  3 searchResult.setVersion(doc.getVersion());
137  3 searchResult.setAuthor(doc.getAuthor());
138  3 Calendar calendar = Calendar.getInstance();
139  3 calendar.setTime(doc.getDate());
140  3 searchResult.setModified(calendar);
141   
142  3 if (withPrettyNames) {
143  0 searchResult.setAuthorName(xwikiApi.getUserName(doc.getAuthor(), false));
144    }
145   
146    /*
147    * Avoid to return object information if the user is not authenticated. This will prevent crawlers to
148    * retrieve information such as email addresses and passwords from user's profiles.
149    */
150  3 if (StringUtils.isNotEmpty(className) && xwikiContext.getUserReference() != null) {
151  1 XWikiDocument xdocument =
152    xwikiContext.getWiki().getDocument(doc.getDocumentReference(), xwikiContext);
153  1 BaseObject baseObject = xdocument.getObject(className);
154  1 if (baseObject != null) {
155  1 searchResult.setObject(
156    this.modelFactory.toRestObject(uriInfo.getBaseUri(), doc, baseObject, false, false));
157    }
158    }
159   
160  3 String pageUri;
161  3 if (StringUtils.isBlank(language)) {
162  3 pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName,
163    Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()),
164    documentReference.getName()).toString();
165    } else {
166  0 searchResult.setLanguage(language);
167  0 pageUri = Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, wikiName,
168    Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()),
169    documentReference.getName(), language).toString();
170    }
171   
172  3 Link pageLink = new Link();
173  3 pageLink.setHref(pageUri);
174  3 pageLink.setRel(Relations.PAGE);
175  3 searchResult.getLinks().add(pageLink);
176   
177  3 result.add(searchResult);
178    }
179    }
180   
181  3 return result;
182    }
183    }