1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.webdav;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Time;
33  import com.liferay.portal.kernel.xml.Namespace;
34  import com.liferay.portal.kernel.xml.SAXReaderUtil;
35  import com.liferay.portal.model.Company;
36  import com.liferay.portal.model.Group;
37  import com.liferay.portal.model.User;
38  import com.liferay.portal.service.CompanyLocalServiceUtil;
39  import com.liferay.portal.service.GroupLocalServiceUtil;
40  import com.liferay.portal.service.UserLocalServiceUtil;
41  
42  import java.util.Collection;
43  import java.util.Map;
44  import java.util.TreeMap;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  /**
49   * <a href="WebDAVUtil.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Alexander Chow
53   */
54  public class WebDAVUtil {
55  
56      public static final Namespace DAV_URI = SAXReaderUtil.createNamespace(
57          "D", "DAV:");
58  
59      public static final int SC_MULTI_STATUS = 207;
60  
61      public static final int SC_LOCKED = 423;
62  
63      public static final String TOKEN_PREFIX = "opaquelocktoken:";
64  
65      public static void addStorage(WebDAVStorage storage) {
66          _instance._addStorage(storage);
67      }
68  
69      public static void deleteStorage(WebDAVStorage storage) {
70          _instance._deleteStorage(storage);
71      }
72  
73      public static String encodeURL(String url) {
74          url = HttpUtil.encodeURL(url);
75          url = StringUtil.replace(url, StringPool.PLUS, StringPool.SPACE);
76  
77          return url;
78      }
79  
80      public static String fixPath(String path) {
81          if (path.endsWith(StringPool.SLASH)) {
82              path = path.substring(0, path.length() - 1);
83          }
84  
85          return path;
86      }
87  
88      public static long getCompanyId(String path) throws WebDAVException {
89          String[] pathArray = getPathArray(path);
90  
91          return getCompanyId(pathArray);
92      }
93  
94      public static long getCompanyId(String[] pathArray) throws WebDAVException {
95          try {
96              String webId = getWebId(pathArray);
97  
98              Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
99  
100             return company.getCompanyId();
101         }
102         catch (Exception e) {
103             throw new WebDAVException(e);
104         }
105     }
106 
107     public static long getDepth(HttpServletRequest request) {
108         String value = GetterUtil.getString(request.getHeader("Depth"));
109 
110         if (_log.isDebugEnabled()) {
111             _log.debug("\"Depth\" header is " + value);
112         }
113 
114         if (value.equals("0")) {
115             return 0;
116         }
117         else {
118             return -1;
119         }
120     }
121 
122     public static String getDestination(
123         HttpServletRequest request, String rootPath) {
124 
125         String headerDestination = request.getHeader("Destination");
126         String[] pathSegments = StringUtil.split(headerDestination, rootPath);
127 
128         String destination = pathSegments[pathSegments.length - 1];
129 
130         if (_log.isDebugEnabled()) {
131             _log.debug("Destination " + destination);
132         }
133 
134         return destination;
135     }
136 
137     public static long getGroupId(String path) throws WebDAVException {
138         String[] pathArray = getPathArray(path);
139 
140         return getGroupId(pathArray);
141     }
142 
143     public static long getGroupId(String[] pathArray) throws WebDAVException {
144         try {
145             if (pathArray.length <= 1) {
146                 return 0;
147             }
148 
149             long companyId = getCompanyId(pathArray);
150 
151             String name = pathArray[1];
152 
153             try {
154                 Group group = GroupLocalServiceUtil.getGroup(companyId, name);
155 
156                 return group.getGroupId();
157             }
158             catch (NoSuchGroupException nsge) {
159             }
160 
161             try {
162                 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
163                     companyId, StringPool.SLASH + name);
164 
165                 return group.getGroupId();
166             }
167             catch (NoSuchGroupException nsge) {
168             }
169 
170             User user = UserLocalServiceUtil.getUserByScreenName(
171                 companyId, name);
172 
173             Group group = user.getGroup();
174 
175             return group.getGroupId();
176         }
177         catch (Exception e) {
178             throw new WebDAVException(e);
179         }
180     }
181 
182     public static String getLockUuid(HttpServletRequest request)
183         throws WebDAVException {
184 
185         String token = StringPool.BLANK;
186 
187         String value = GetterUtil.getString(request.getHeader("If"));
188 
189         if (_log.isDebugEnabled()) {
190             _log.debug("\"If\" header is " + value);
191         }
192 
193         if (value.contains("(<DAV:no-lock>)")) {
194             if (_log.isWarnEnabled()) {
195                 _log.warn("Lock tokens can never be <DAV:no-lock>");
196             }
197 
198             throw new WebDAVException();
199         }
200 
201         int beg = value.indexOf(TOKEN_PREFIX);
202 
203         if (beg >= 0) {
204             beg += TOKEN_PREFIX.length();
205 
206             if (beg < value.length()) {
207                 int end = value.indexOf(">", beg);
208 
209                 token = GetterUtil.getString(value.substring(beg, end));
210             }
211         }
212 
213         return token;
214     }
215 
216     public static String[] getPathArray(String path) {
217         return getPathArray(path, false);
218     }
219 
220     public static String[] getPathArray(String path, boolean fixPath) {
221         if (fixPath) {
222             path = fixPath(path);
223         }
224 
225         if (path.startsWith(StringPool.SLASH)) {
226             path = path.substring(1, path.length());
227         }
228 
229         return StringUtil.split(path, StringPool.SLASH);
230     }
231 
232     public static String getResourceName(String[] pathArray) {
233         if (pathArray.length <= 3) {
234             return StringPool.BLANK;
235         }
236         else {
237             return pathArray[pathArray.length - 1];
238         }
239     }
240 
241     public static WebDAVStorage getStorage(String token) {
242         return _instance._getStorage(token);
243     }
244 
245     public static Collection<String> getStorageTokens() {
246         return _instance._getStorageTokens();
247     }
248 
249     public static long getTimeout(HttpServletRequest request) {
250         final String TIME_PREFIX = "Second-";
251 
252         long timeout = 0;
253 
254         String value = GetterUtil.getString(request.getHeader("Timeout"));
255 
256         if (_log.isDebugEnabled()) {
257             _log.debug("\"Timeout\" header is " + value);
258         }
259 
260         int index = value.indexOf(TIME_PREFIX);
261 
262         if (index >= 0) {
263             index += TIME_PREFIX.length();
264 
265             if (index < value.length()) {
266                 timeout = GetterUtil.getLong(value.substring(index));
267             }
268         }
269 
270         return timeout * Time.SECOND;
271     }
272 
273     public static String getWebId(String path) throws WebDAVException {
274         String[] pathArray = getPathArray(path);
275 
276         return getWebId(pathArray);
277     }
278 
279     public static String getWebId(String[] pathArray) throws WebDAVException {
280         if (pathArray.length > 0) {
281             String webId = pathArray[0];
282 
283             return webId;
284         }
285         else {
286             throw new WebDAVException();
287         }
288     }
289 
290     public static boolean isOverwrite(HttpServletRequest request) {
291         return _instance._isOverwrite(request);
292     }
293 
294     private WebDAVUtil() {
295         _storageMap = new TreeMap<String, WebDAVStorage>();
296     }
297 
298     private void _addStorage(WebDAVStorage storage) {
299         _storageMap.put(storage.getToken(), storage);
300     }
301 
302     private void _deleteStorage(WebDAVStorage storage) {
303         if (storage != null) {
304             _storageMap.remove(storage.getToken());
305         }
306     }
307 
308     private WebDAVStorage _getStorage(String token) {
309         return _storageMap.get(token);
310     }
311 
312     private Collection<String> _getStorageTokens() {
313         return _storageMap.keySet();
314     }
315 
316     private boolean _isOverwrite(HttpServletRequest request) {
317         String value = GetterUtil.getString(request.getHeader("Overwrite"));
318 
319         if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
320             return false;
321         }
322         else {
323             return true;
324         }
325     }
326 
327     private static Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);
328 
329     private static WebDAVUtil _instance = new WebDAVUtil();
330 
331     private Map<String, WebDAVStorage> _storageMap;
332 
333 }