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