1
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.Time;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.kernel.xml.Namespace;
33 import com.liferay.portal.kernel.xml.SAXReaderUtil;
34 import com.liferay.portal.model.Company;
35 import com.liferay.portal.model.Group;
36 import com.liferay.portal.model.User;
37 import com.liferay.portal.service.CompanyLocalServiceUtil;
38 import com.liferay.portal.service.GroupLocalServiceUtil;
39 import com.liferay.portal.service.UserLocalServiceUtil;
40 import com.liferay.portal.util.PropsKeys;
41 import com.liferay.portal.util.PropsUtil;
42
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Map;
47 import java.util.Set;
48
49 import javax.servlet.http.HttpServletRequest;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54
61 public class WebDAVUtil {
62
63 public static final Namespace DAV_URI = SAXReaderUtil.createNamespace(
64 "D", "DAV:");
65
66 public static final int SC_MULTI_STATUS = 207;
67
68 public static final int SC_LOCKED = 423;
69
70 public static final String TOKEN_PREFIX = "opaquelocktoken:";
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.isInfoEnabled()) {
103 _log.info("\"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 if (_log.isDebugEnabled()) {
123 _log.debug("Destination " + destination);
124 }
125
126 return destination;
127 }
128
129 public static long getGroupId(String path) throws WebDAVException {
130 String[] pathArray = getPathArray(path);
131
132 return getGroupId(pathArray);
133 }
134
135 public static long getGroupId(String[] pathArray) throws WebDAVException {
136 try {
137 if (pathArray.length <= 1) {
138 return 0;
139 }
140
141 long companyId = getCompanyId(pathArray);
142
143 String name = pathArray[1];
144
145 try {
146 Group group = GroupLocalServiceUtil.getGroup(companyId, name);
147
148 return group.getGroupId();
149 }
150 catch (NoSuchGroupException nsge) {
151 }
152
153 try {
154 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
155 companyId, StringPool.SLASH + name);
156
157 return group.getGroupId();
158 }
159 catch (NoSuchGroupException nsge) {
160 }
161
162 User user = UserLocalServiceUtil.getUserByScreenName(
163 companyId, name);
164
165 Group group = user.getGroup();
166
167 return group.getGroupId();
168 }
169 catch (Exception e) {
170 throw new WebDAVException(e);
171 }
172 }
173
174 public static String getLockUuid(HttpServletRequest request)
175 throws WebDAVException {
176
177 String token = StringPool.BLANK;
178
179 String value = GetterUtil.getString(request.getHeader("If"));
180
181 if (_log.isInfoEnabled()) {
182 _log.info("\"If\" header is " + value);
183 }
184
185 if (value.contains("(<DAV:no-lock>)")) {
186 if (_log.isWarnEnabled()) {
187 _log.warn("Lock tokens can never be <DAV:no-lock>");
188 }
189
190 throw new WebDAVException();
191 }
192
193 int beg = value.indexOf(TOKEN_PREFIX);
194
195 if (beg >= 0) {
196 beg += TOKEN_PREFIX.length();
197
198 if (beg < value.length()) {
199 int end = value.indexOf(">", beg);
200
201 token = GetterUtil.getString(value.substring(beg, end));
202 }
203 }
204
205 return token;
206 }
207
208 public static String[] getPathArray(String path) {
209 return getPathArray(path, false);
210 }
211
212 public static String[] getPathArray(String path, boolean fixPath) {
213 if (fixPath) {
214 path = fixPath(path);
215 }
216
217 if (path.startsWith(StringPool.SLASH)) {
218 path = path.substring(1, path.length());
219 }
220
221 return StringUtil.split(path, StringPool.SLASH);
222 }
223
224 public static String getResourceName(String[] pathArray) {
225 if (pathArray.length <= 3) {
226 return StringPool.BLANK;
227 }
228 else {
229 return pathArray[pathArray.length - 1];
230 }
231 }
232
233 public static String getStorageClass(String token) {
234 return _instance._getStorageClass(token);
235 }
236
237 public static String getStorageToken(String className) {
238 return _instance._getStorageToken(className);
239 }
240
241 public static Collection<String> getStorageTokens() {
242 return _instance._getStorageTokens();
243 }
244
245 public static long getTimeout(HttpServletRequest request) {
246 final String TIME_PREFIX = "Second-";
247
248 long timeout = 0;
249
250 String value = GetterUtil.getString(request.getHeader("Timeout"));
251
252 if (_log.isInfoEnabled()) {
253 _log.info("\"Timeout\" header is " + value);
254 }
255
256 int index = value.indexOf(TIME_PREFIX);
257
258 if (index >= 0) {
259 index += TIME_PREFIX.length();
260
261 if (index < value.length()) {
262 timeout = GetterUtil.getLong(value.substring(index));
263 }
264 }
265
266 return timeout * Time.SECOND;
267 }
268
269 public static String getWebId(String path) throws WebDAVException {
270 String[] pathArray = getPathArray(path);
271
272 return getWebId(pathArray);
273 }
274
275 public static String getWebId(String[] pathArray) throws WebDAVException {
276 if (pathArray.length > 0) {
277 String webId = pathArray[0];
278
279 return webId;
280 }
281 else {
282 throw new WebDAVException();
283 }
284 }
285
286 public static boolean isEditEnabled(String className) {
287 return _instance._isEditEnabled(className);
288 }
289
290 public static boolean isEnabled(String className) {
291 return _instance._isEnabled(className);
292 }
293
294 public static boolean isOverwrite(HttpServletRequest request) {
295 return _instance._isOverwrite(request);
296 }
297
298 public static boolean isViewEnabled(String className) {
299 return _instance._isViewEnabled(className);
300 }
301
302 private WebDAVUtil() {
303 _storageMap = new HashMap<String, String>();
304 _storageEditUrls = new HashSet<String>();
305 _storageViewUrls = new HashSet<String>();
306
307 String[] tokens = PropsUtil.getArray(PropsKeys.WEBDAV_STORAGE_TOKENS);
308
309 for (String token: tokens) {
310 Filter filter = new Filter(token);
311
312 String className = PropsUtil.get(
313 PropsKeys.WEBDAV_STORAGE_CLASS, filter);
314
315 if (Validator.isNotNull(className)) {
316 _storageMap.put(className, token);
317
318 boolean editUrl = GetterUtil.getBoolean(PropsUtil.get(
319 PropsKeys.WEBDAV_STORAGE_SHOW_EDIT_URL, filter));
320 boolean viewUrl = GetterUtil.getBoolean(PropsUtil.get(
321 PropsKeys.WEBDAV_STORAGE_SHOW_VIEW_URL, filter));
322
323 if (editUrl) {
324 _storageEditUrls.add(className);
325 }
326
327 if (viewUrl) {
328 _storageViewUrls.add(className);
329 }
330 }
331 }
332 }
333
334 private String _getStorageClass(String token) {
335 if (_storageMap.containsValue(token)) {
336 for (String key : _storageMap.keySet()) {
337 if (_storageMap.get(key).equals(token)) {
338 return key;
339 }
340 }
341 }
342
343 return null;
344 }
345
346 private String _getStorageToken(String className) {
347 return _storageMap.get(className);
348 }
349
350 private Collection<String> _getStorageTokens() {
351 return _storageMap.values();
352 }
353
354 private boolean _isEditEnabled(String className) {
355 return _isEnabled(className) && _storageEditUrls.contains(className);
356 }
357
358 private boolean _isEnabled(String className) {
359 return _storageMap.containsKey(className);
360 }
361
362 private boolean _isOverwrite(HttpServletRequest request) {
363 String value = GetterUtil.getString(request.getHeader("Overwrite"));
364
365 if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
366 return false;
367 }
368 else {
369 return true;
370 }
371 }
372
373 private boolean _isViewEnabled(String className) {
374 return _isEnabled(className) && _storageViewUrls.contains(className);
375 }
376
377 private static Log _log = LogFactory.getLog(WebDAVUtil.class);
378
379 private static WebDAVUtil _instance = new WebDAVUtil();
380
381 private final Set<String> _storageEditUrls;
382
383 private final Set<String> _storageViewUrls;
384
385 private final Map<String, String> _storageMap;
386
387 }