1   /**
2    * Copyright (c) 2000-2008 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  
29  import javax.servlet.http.HttpServletRequest;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import org.dom4j.Namespace;
35  
36  /**
37   * <a href="WebDAVUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Alexander Chow
41   *
42   */
43  public class WebDAVUtil {
44  
45      public static final Namespace DAV_URI = Namespace.get("D", "DAV:");
46  
47      public static final int SC_MULTI_STATUS = 207;
48  
49      public static String fixPath(String path) {
50          if (path.endsWith(StringPool.SLASH)) {
51              path = path.substring(0, path.length() - 1);
52          }
53  
54          return path;
55      }
56  
57      public static long getCompanyId(String path) {
58          return getCompanyId(path, false);
59      }
60  
61      public static long getCompanyId(String path, boolean fixPath) {
62          if (fixPath) {
63              path = fixPath(path);
64          }
65  
66          String[] pathArray = getPathArray(path);
67  
68          if (pathArray.length <= 0) {
69              return 0;
70          }
71          else {
72              return GetterUtil.getLong(pathArray[0]);
73          }
74      }
75  
76      public static long getDepth(HttpServletRequest req) {
77          String value = GetterUtil.getString(req.getHeader("Depth"));
78  
79          if (_log.isInfoEnabled()) {
80              _log.info("\"Depth\" header is " + value);
81          }
82  
83          if (value.equals("0")) {
84              return 0;
85          }
86          else {
87              return -1;
88          }
89      }
90  
91      public static String getDestination(
92          HttpServletRequest req, String rootPath) {
93  
94          String headerDestination = req.getHeader("Destination");
95          String[] pathSegments = StringUtil.split(headerDestination, rootPath);
96  
97          String destination = pathSegments[pathSegments.length - 1];
98  
99          if (_log.isDebugEnabled()) {
100             _log.debug("Destination " + destination);
101         }
102 
103         return destination;
104     }
105 
106     public static String getEntryName(String[] pathArray) {
107         if (pathArray.length <= 2) {
108             return StringPool.BLANK;
109         }
110         else {
111             return pathArray[pathArray.length - 1];
112         }
113     }
114 
115     public static long getGroupId(String path) {
116         return getGroupId(path, false);
117     }
118 
119     public static long getGroupId(String[] pathArray) {
120         if (pathArray.length <= 1) {
121             return 0;
122         }
123         else {
124             return GetterUtil.getLong(pathArray[1]);
125         }
126     }
127 
128     public static long getGroupId(String path, boolean fixPath) {
129         if (fixPath) {
130             path = fixPath(path);
131         }
132 
133         String[] pathArray = getPathArray(path);
134 
135         return getGroupId(pathArray);
136     }
137 
138     public static String[] getPathArray(String path) {
139         return getPathArray(path, false);
140     }
141 
142     public static String[] getPathArray(String path, boolean fixPath) {
143         if (fixPath) {
144             path = fixPath(path);
145         }
146 
147         if (path.startsWith(StringPool.SLASH)) {
148             path = path.substring(1, path.length());
149         }
150 
151         return StringUtil.split(path, StringPool.SLASH);
152     }
153 
154     public static boolean isGroupPath(String path) {
155         return isGroupPath(path, false);
156     }
157 
158     public static boolean isGroupPath(String path, boolean fixPath) {
159         if (fixPath) {
160             path = fixPath(path);
161         }
162 
163         String[] pathArray = getPathArray(path);
164 
165         if (pathArray.length == 2) {
166             return true;
167         }
168         else {
169             return false;
170         }
171     }
172 
173     public static boolean isOverwrite(HttpServletRequest req) {
174         String value = GetterUtil.getString(req.getHeader("Overwrite"));
175 
176         if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
177             return false;
178         }
179         else {
180             return true;
181         }
182     }
183 
184     private static Log _log = LogFactory.getLog(WebDAVUtil.class);
185 
186 }