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.NoSuchGroupException;
26  import com.liferay.portal.kernel.configuration.Filter;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.model.Group;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.service.CompanyLocalServiceUtil;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  import com.liferay.portal.util.PropsKeys;
38  import com.liferay.portal.util.PropsUtil;
39  
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.HashSet;
43  import java.util.Map;
44  import java.util.Set;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  import org.dom4j.Namespace;
52  
53  /**
54   * <a href="WebDAVUtil.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Alexander Chow
58   *
59   */
60  public class WebDAVUtil {
61  
62      public static final Namespace DAV_URI = Namespace.get("D", "DAV:");
63  
64      public static final int SC_MULTI_STATUS = 207;
65  
66      public static String fixPath(String path) {
67          if (path.endsWith(StringPool.SLASH)) {
68              path = path.substring(0, path.length() - 1);
69          }
70  
71          return path;
72      }
73  
74      public static long getCompanyId(String path) throws WebDAVException {
75          String[] pathArray = getPathArray(path);
76  
77          return getCompanyId(pathArray);
78      }
79  
80      public static long getCompanyId(String[] pathArray) throws WebDAVException {
81          try {
82              String webId = getWebId(pathArray);
83  
84              Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
85  
86              return company.getCompanyId();
87          }
88          catch (Exception e) {
89              throw new WebDAVException(e);
90          }
91      }
92  
93      public static long getDepth(HttpServletRequest request) {
94          String value = GetterUtil.getString(request.getHeader("Depth"));
95  
96          if (_log.isInfoEnabled()) {
97              _log.info("\"Depth\" header is " + value);
98          }
99  
100         if (value.equals("0")) {
101             return 0;
102         }
103         else {
104             return -1;
105         }
106     }
107 
108     public static String getDestination(
109         HttpServletRequest request, String rootPath) {
110 
111         String headerDestination = request.getHeader("Destination");
112         String[] pathSegments = StringUtil.split(headerDestination, rootPath);
113 
114         String destination = pathSegments[pathSegments.length - 1];
115 
116         if (_log.isDebugEnabled()) {
117             _log.debug("Destination " + destination);
118         }
119 
120         return destination;
121     }
122 
123     public static long getGroupId(String path) throws WebDAVException {
124         String[] pathArray = getPathArray(path);
125 
126         return getGroupId(pathArray);
127     }
128 
129     public static long getGroupId(String[] pathArray) throws WebDAVException {
130         try {
131             if (pathArray.length <= 1) {
132                 return 0;
133             }
134 
135             long companyId = getCompanyId(pathArray);
136 
137             String name = pathArray[1];
138 
139             try {
140                 Group group = GroupLocalServiceUtil.getGroup(companyId, name);
141 
142                 return group.getGroupId();
143             }
144             catch (NoSuchGroupException nsge) {
145             }
146 
147             try {
148                 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
149                     companyId, StringPool.SLASH + name);
150 
151                 return group.getGroupId();
152             }
153             catch (NoSuchGroupException nsge) {
154             }
155 
156             User user = UserLocalServiceUtil.getUserByScreenName(
157                 companyId, name);
158 
159             Group group = user.getGroup();
160 
161             return group.getGroupId();
162         }
163         catch (Exception e) {
164             throw new WebDAVException(e);
165         }
166     }
167 
168     public static String[] getPathArray(String path) {
169         return getPathArray(path, false);
170     }
171 
172     public static String[] getPathArray(String path, boolean fixPath) {
173         if (fixPath) {
174             path = fixPath(path);
175         }
176 
177         if (path.startsWith(StringPool.SLASH)) {
178             path = path.substring(1, path.length());
179         }
180 
181         return StringUtil.split(path, StringPool.SLASH);
182     }
183 
184     public static String getResourceName(String[] pathArray) {
185         if (pathArray.length <= 3) {
186             return StringPool.BLANK;
187         }
188         else {
189             return pathArray[pathArray.length - 1];
190         }
191     }
192 
193     public static String getStorageClass(String token) {
194         return _instance._getStorageClass(token);
195     }
196 
197     public static String getStorageToken(String className) {
198         return _instance._getStorageToken(className);
199     }
200 
201     public static Collection<String> getStorageTokens() {
202         return _instance._getStorageTokens();
203     }
204 
205     public static String getWebId(String path) throws WebDAVException {
206         String[] pathArray = getPathArray(path);
207 
208         return getWebId(pathArray);
209     }
210 
211     public static String getWebId(String[] pathArray) throws WebDAVException {
212         if (pathArray.length > 0) {
213             String webId = pathArray[0];
214 
215             return webId;
216         }
217         else {
218             throw new WebDAVException();
219         }
220     }
221 
222     public static boolean isEditEnabled(String className) {
223         return _instance._isEditEnabled(className);
224     }
225 
226     public static boolean isEnabled(String className) {
227         return _instance._isEnabled(className);
228     }
229 
230     public static boolean isOverwrite(HttpServletRequest request) {
231         return _instance._isOverwrite(request);
232     }
233 
234     public static boolean isViewEnabled(String className) {
235         return _instance._isViewEnabled(className);
236     }
237 
238     private WebDAVUtil() {
239         _storageMap = new HashMap<String, String>();
240         _storageEditUrls = new HashSet<String>();
241         _storageViewUrls = new HashSet<String>();
242 
243         String[] tokens = PropsUtil.getArray(PropsKeys.WEBDAV_STORAGE_TOKENS);
244 
245         for (String token: tokens) {
246             Filter filter = new Filter(token);
247 
248             String className = PropsUtil.get(
249                 PropsKeys.WEBDAV_STORAGE_CLASS, filter);
250 
251             if (Validator.isNotNull(className)) {
252                 _storageMap.put(className, token);
253 
254                 boolean editUrl = GetterUtil.getBoolean(PropsUtil.get(
255                     PropsKeys.WEBDAV_STORAGE_SHOW_EDIT_URL, filter));
256                 boolean viewUrl = GetterUtil.getBoolean(PropsUtil.get(
257                     PropsKeys.WEBDAV_STORAGE_SHOW_VIEW_URL, filter));
258 
259                 if (editUrl) {
260                     _storageEditUrls.add(className);
261                 }
262 
263                 if (viewUrl) {
264                     _storageViewUrls.add(className);
265                 }
266             }
267         }
268     }
269 
270     private String _getStorageClass(String token) {
271         if (_storageMap.containsValue(token)) {
272             for (String key : _storageMap.keySet()) {
273                 if (_storageMap.get(key).equals(token)) {
274                     return key;
275                 }
276             }
277         }
278 
279         return null;
280     }
281 
282     private String _getStorageToken(String className) {
283         return _storageMap.get(className);
284     }
285 
286     private Collection<String> _getStorageTokens() {
287         return _storageMap.values();
288     }
289 
290     private boolean _isEditEnabled(String className) {
291         return _isEnabled(className) && _storageEditUrls.contains(className);
292     }
293 
294     private boolean _isEnabled(String className) {
295         return _storageMap.containsKey(className);
296     }
297 
298     private boolean _isOverwrite(HttpServletRequest request) {
299         String value = GetterUtil.getString(request.getHeader("Overwrite"));
300 
301         if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
302             return false;
303         }
304         else {
305             return true;
306         }
307     }
308 
309     private boolean _isViewEnabled(String className) {
310         return _isEnabled(className) && _storageViewUrls.contains(className);
311     }
312 
313     private static Log _log = LogFactory.getLog(WebDAVUtil.class);
314 
315     private static WebDAVUtil _instance = new WebDAVUtil();
316 
317     private final Set<String> _storageEditUrls;
318 
319     private final Set<String> _storageViewUrls;
320 
321     private final Map<String, String> _storageMap;
322 
323 }