1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.util.FileUtil;
29  
30  import java.io.IOException;
31  
32  import javax.servlet.http.HttpServletRequest;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="WebDAVUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class WebDAVUtil {
44  
45      public static final int SC_MULTI_STATUS = 207;
46  
47      public static String fixPath(String path) {
48          if (path.endsWith(StringPool.SLASH)) {
49              path = path.substring(0, path.length() - 1);
50          }
51  
52          return path;
53      }
54  
55      public static long getCompanyId(String path) {
56          String[] pathArray = getPathArray(path);
57  
58          if (pathArray.length <= 0) {
59              return 0;
60          }
61          else {
62              return GetterUtil.getLong(pathArray[0]);
63          }
64      }
65  
66      public static String getDestination(HttpServletRequest req) {
67          String destination = req.getHeader("Destination");
68  
69          if (_log.isDebugEnabled()) {
70              _log.debug("Destination " + destination);
71          }
72  
73          return destination;
74      }
75  
76      public static long getGroupId(String path) {
77          String[] pathArray = getPathArray(path);
78  
79          if (pathArray.length <= 1) {
80              return 0;
81          }
82          else {
83              return GetterUtil.getLong(pathArray[1]);
84          }
85      }
86  
87      public static String[] getPathArray(String path) {
88          if (path.startsWith(StringPool.SLASH)) {
89              path = path.substring(1, path.length());
90          }
91  
92          return StringUtil.split(path, StringPool.SLASH);
93      }
94  
95      public static String getRequestXML(HttpServletRequest req)
96          throws IOException {
97  
98          String xml = new String(FileUtil.getBytes(req.getInputStream()));
99  
100         if (_log.isDebugEnabled()) {
101             _log.debug("Request XML\n" + xml);
102         }
103 
104         return xml;
105     }
106 
107     public static boolean isGroupPath(String path) {
108         String[] pathArray = getPathArray(path);
109 
110         if (pathArray.length == 2) {
111             return true;
112         }
113         else {
114             return false;
115         }
116     }
117 
118     private static Log _log = LogFactory.getLog(WebDAVUtil.class);
119 
120 }