1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.webdav.resources.views.pages

File PagesBySpaceNameSubView.java

 

Coverage histogram

../../../../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

34
91
6
1
223
174
31
0.34
15.17
6
5.17

Classes

Class Line # Actions
PagesBySpaceNameSubView 47 91 0% 31 69
0.4732824647.3%
 

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 com.xpn.xwiki.plugin.webdav.resources.views.pages;
21   
22    import java.util.ArrayList;
23    import java.util.HashSet;
24    import java.util.List;
25    import java.util.Set;
26   
27    import org.apache.jackrabbit.webdav.DavException;
28    import org.apache.jackrabbit.webdav.DavResource;
29    import org.apache.jackrabbit.webdav.DavResourceIterator;
30    import org.apache.jackrabbit.webdav.DavResourceIteratorImpl;
31    import org.apache.jackrabbit.webdav.DavServletResponse;
32    import org.apache.jackrabbit.webdav.io.InputContext;
33    import org.slf4j.Logger;
34    import org.slf4j.LoggerFactory;
35   
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.plugin.webdav.resources.XWikiDavResource;
38    import com.xpn.xwiki.plugin.webdav.resources.domain.DavPage;
39    import com.xpn.xwiki.plugin.webdav.resources.partial.AbstractDavView;
40    import com.xpn.xwiki.plugin.webdav.utils.XWikiDavUtils;
41   
42    /**
43    * This view groups all pages according to their space name.
44    *
45    * @version $Id: fbd8bca8b274ee4af838aac970cc05d48e3134d3 $
46    */
 
47    public class PagesBySpaceNameSubView extends AbstractDavView
48    {
49    /**
50    * Logger instance.
51    */
52    private static final Logger logger = LoggerFactory.getLogger(PagesBySpaceNameSubView.class);
53   
 
54  17 toggle @Override
55    public XWikiDavResource decode(String[] tokens, int next) throws DavException
56    {
57  17 String nextToken = tokens[next];
58  17 boolean last = (next == tokens.length - 1);
59  17 XWikiDavResource resource = null;
60  17 if (isTempResource(nextToken)) {
61  0 return super.decode(tokens, next);
62  17 } else if ((nextToken.startsWith(XWikiDavUtils.VIRTUAL_DIRECTORY_PREFIX) && nextToken
63    .endsWith(XWikiDavUtils.VIRTUAL_DIRECTORY_POSTFIX))
64    && !(last && getContext().isCreateOrMoveRequest())) {
65  0 resource = new PagesByFirstLettersSubView();
66  0 resource.init(this, nextToken.toUpperCase(), "/" + nextToken.toUpperCase());
67  17 } else if (getContext().isCreateCollectionRequest() || getContext().exists(this.name + "." + nextToken)) {
68  17 resource = new DavPage();
69  17 resource.init(this, this.name + "." + nextToken, "/" + nextToken);
70  0 } else if (nextToken.startsWith(this.name + ".") && getContext().exists(nextToken)) {
71    // For compatibility with FoXWiki
72  0 resource = new DavPage();
73  0 resource.init(this, nextToken, "/" + nextToken);
74    } else {
75  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
76    }
77  17 return last ? resource : resource.decode(tokens, next + 1);
78    }
79   
 
80  32 toggle @Override
81    public boolean exists()
82    {
83  32 try {
84  32 List<String> spaces = getContext().getSpaces();
85  32 if (spaces.contains(name)) {
86  11 return true;
87    }
88    } catch (DavException ex) {
89  0 logger.error("Unexpected Error : ", ex);
90    }
91  21 return false;
92    }
93   
 
94  0 toggle @Override
95    public DavResourceIterator getMembers()
96    {
97  0 List<DavResource> children = new ArrayList<DavResource>();
98  0 try {
99  0 String sql = "where doc.web='" + this.name + "'";
100  0 List<String> docNames = getContext().searchDocumentsNames(sql);
101  0 Set<String> subViewNames = new HashSet<String>();
102  0 int subViewNameLength = XWikiDavUtils.getSubViewNameLength(docNames.size());
103  0 for (String docName : docNames) {
104  0 if (getContext().hasAccess("view", docName)) {
105  0 int dot = docName.lastIndexOf('.');
106  0 String pageName = docName.substring(dot + 1);
107  0 if (subViewNameLength < pageName.length()) {
108  0 subViewNames.add(pageName.substring(0, subViewNameLength).toUpperCase());
109    } else {
110    // This is not good.
111  0 subViewNames.add(pageName.toUpperCase());
112    }
113    }
114    }
115  0 for (String subViewName : subViewNames) {
116  0 try {
117  0 String modName =
118    XWikiDavUtils.VIRTUAL_DIRECTORY_PREFIX + subViewName + XWikiDavUtils.VIRTUAL_DIRECTORY_POSTFIX;
119  0 PagesByFirstLettersSubView subView = new PagesByFirstLettersSubView();
120  0 subView.init(this, modName, "/" + modName);
121  0 children.add(subView);
122    } catch (DavException e) {
123  0 logger.error("Unexpected Error : ", e);
124    }
125    }
126    } catch (DavException ex) {
127  0 logger.error("Unexpected Error : ", ex);
128    }
129  0 children.addAll(getVirtualMembers());
130  0 return new DavResourceIteratorImpl(children);
131    }
132   
 
133  4 toggle @Override
134    public void addMember(DavResource resource, InputContext inputContext) throws DavException
135    {
136  4 if (resource instanceof DavPage) {
137  4 String pName = resource.getDisplayName();
138  4 if (getContext().hasAccess("edit", pName)) {
139  4 XWikiDocument childDoc = getContext().getDocument(pName);
140  4 childDoc.setContent("This page was created through the WebDAV interface.");
141  4 getContext().saveDocument(childDoc);
142    }
143    } else {
144  0 super.addMember(resource, inputContext);
145    }
146    }
147   
 
148  4 toggle @Override
149    public void removeMember(DavResource member) throws DavException
150    {
151  4 XWikiDavResource davResource = (XWikiDavResource) member;
152  4 if (davResource instanceof DavPage) {
153  4 String pName = davResource.getDisplayName();
154  4 getContext().checkAccess("delete", pName);
155  4 XWikiDocument childDoc = getContext().getDocument(pName);
156  4 if (!childDoc.isNew()) {
157  4 getContext().deleteDocument(childDoc);
158    }
159  0 } else if (member instanceof PagesByFirstLettersSubView) {
160    // We are going to force a recursive delete.
161  0 String filter =
162    member.getDisplayName().substring(XWikiDavUtils.VIRTUAL_DIRECTORY_PREFIX.length(),
163    member.getDisplayName().length() - XWikiDavUtils.VIRTUAL_DIRECTORY_POSTFIX.length());
164  0 String sql = "where doc.web='" + this.name + "'";
165  0 List<String> docNames = getContext().searchDocumentsNames(sql);
166  0 List<String> filteredDocNames = new ArrayList<String>();
167  0 for (String docName : docNames) {
168  0 if (docName.toUpperCase().startsWith(filter)) {
169  0 filteredDocNames.add(docName);
170    }
171    }
172    // Verify delete rights on all the documents to be removed.
173  0 for (String docName : filteredDocNames) {
174  0 getContext().checkAccess("delete", docName);
175    }
176    // Delete the documents.
177  0 for (String docName : filteredDocNames) {
178  0 getContext().deleteDocument(getContext().getDocument(docName));
179    }
180    } else {
181  0 super.removeMember(member);
182    }
183  4 davResource.clearCache();
184    }
185   
 
186  1 toggle @Override
187    public void move(DavResource destination) throws DavException
188    {
189    // We only support rename operation for the moment.
190  1 if (destination instanceof PagesBySpaceNameSubView) {
191  1 PagesBySpaceNameSubView dSpace = (PagesBySpaceNameSubView) destination;
192  1 if (!dSpace.exists()) {
193    // Now check whether this is a rename operation.
194  1 if (getCollection().equals(dSpace.getCollection())) {
195  1 String sql = "where doc.web='" + this.name + "'";
196  1 List<String> docNames = getContext().searchDocumentsNames(sql);
197    // To rename an entire space, user should have edit rights on all the
198    // documents in the current space and delete rights on all the documents that
199    // will be replaced (if they exist).
200  1 for (String docName : docNames) {
201  1 String newDocName = dSpace.getDisplayName() + "." + docName;
202  1 getContext().checkAccess("edit", docName);
203  1 getContext().checkAccess("overwrite", newDocName);
204    }
205  1 for (String docName : docNames) {
206  1 XWikiDocument doc = getContext().getDocument(docName);
207  1 String newDocName = dSpace.getDisplayName() + "." + doc.getName();
208  1 getContext().renameDocument(doc, newDocName);
209    }
210    } else {
211    // Actual moves (perhaps from one view to another) is not
212    // allowed.
213  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
214    }
215    } else {
216  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
217    }
218    } else {
219  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
220    }
221  1 clearCache();
222    }
223    }