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