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

File XWikiDavUtils.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

8
14
4
2
148
51
8
0.57
3.5
2
2

Classes

Class Line # Actions
XWikiDavUtils 30 14 0% 8 26
0.00%
XWikiDavUtils.BaseViews 60 0 - 0 0
-1.0 -
 

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.utils;
21   
22    import com.xpn.xwiki.api.Attachment;
23    import com.xpn.xwiki.api.Document;
24   
25    /**
26    * Holds all utility methods / variable for the webdav module.
27    *
28    * @version $Id: ee59aefb9c89588a11b5f95e6df9eaec2ffd3069 $.
29    */
 
30    public final class XWikiDavUtils
31    {
32    /**
33    * Path separator character.
34    */
35    public static final String URL_SEPARATOR = "/";
36   
37    /**
38    * Prefix used to indicate the beginning of a virtual grouping.
39    */
40    public static final String VIRTUAL_DIRECTORY_PREFIX = "_";
41   
42    /**
43    * Post-fix used to indicate the beginning of a virtual grouping.
44    */
45    public static final String VIRTUAL_DIRECTORY_POSTFIX = VIRTUAL_DIRECTORY_PREFIX;
46   
47    /**
48    * Signature used to identify an attachment url.
49    */
50    public static final String XWIKI_ATTACHMENT_SIGNATURE = "/xwiki/bin/download/";
51   
52    /**
53    * Signature used to identify a webdav url.
54    */
55    public static final String XWIKI_WEBDAV_SIGNATURE = "/xwiki/webdav/spaces/";
56   
57    /**
58    * An interface for collecting all base views.
59    */
 
60    public interface BaseViews
61    {
62    /**
63    * Root view.
64    */
65    String ROOT = "root";
66   
67    /**
68    * Pages view.
69    */
70    String PAGES = "spaces";
71   
72    /**
73    * Attachments view.
74    */
75    String ATTACHMENTS = "attachments";
76   
77    /**
78    * Home view.
79    */
80    String HOME = "home";
81   
82    /**
83    * Orphans view.
84    */
85    String ORPHANS = "orphans";
86   
87    /**
88    * Whatsnew view.
89    */
90    String WHATSNEW = "whatsnew";
91    }
92   
93    /**
94    * Forbidden constructor.
95    */
 
96  0 toggle private XWikiDavUtils()
97    {
98   
99    }
100   
101    /**
102    * Calculates the length of a subview (for groupings) given the total document count.
103    *
104    * @param totalDocumentCount Total document count.
105    * @return The calculated view name length.
106    */
 
107  0 toggle public static int getSubViewNameLength(int totalDocumentCount)
108    {
109    // We might want to change this logic later.
110  0 if (totalDocumentCount < 200) {
111  0 return 1;
112  0 } else if (totalDocumentCount < 5000) {
113  0 return 2;
114    } else {
115  0 return 3;
116    }
117    }
118   
119    /**
120    * @param doc The {@link Document} having the attachment.
121    * @param attachment The {@link Attachment}.
122    * @return The webdav url corresponding to the attachment.
123    */
 
124  0 toggle public static String getDavURL(Document doc, Attachment attachment)
125    {
126  0 String docDownloadURL = doc.getExternalURL("download");
127  0 String httpUrl = docDownloadURL.endsWith(URL_SEPARATOR) ? docDownloadURL + attachment.getFilename()
128    : docDownloadURL + URL_SEPARATOR + attachment.getFilename();
129  0 return getDavURL(httpUrl);
130    }
131   
132    /**
133    * @param httpUrl The http url of an attachment.
134    * @return The calculated webdav url.
135    */
 
136  0 toggle private static String getDavURL(String httpUrl)
137    {
138    // For the moment we'll only consider attachments.
139  0 String webDAVUrl = "";
140  0 if (httpUrl.contains(XWIKI_ATTACHMENT_SIGNATURE)) {
141  0 String[] parts = httpUrl.split(XWIKI_ATTACHMENT_SIGNATURE);
142  0 String[] elements = parts[1].split(URL_SEPARATOR);
143  0 webDAVUrl = parts[0] + XWIKI_WEBDAV_SIGNATURE + elements[0]
144    + URL_SEPARATOR + elements[1] + URL_SEPARATOR + elements[2];
145    }
146  0 return webDAVUrl;
147    }
148    }