001
014
015 package com.liferay.portal.kernel.webdav;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.HttpUtil;
025 import com.liferay.portal.kernel.util.OrderByComparator;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Time;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.kernel.xml.Namespace;
031 import com.liferay.portal.kernel.xml.SAXReaderUtil;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.GroupConstants;
034 import com.liferay.portal.model.Portlet;
035 import com.liferay.portal.model.User;
036 import com.liferay.portal.service.GroupLocalServiceUtil;
037 import com.liferay.portal.service.UserLocalServiceUtil;
038 import com.liferay.portal.util.comparator.GroupFriendlyURLComparator;
039 import com.liferay.portlet.documentlibrary.util.DL;
040 import com.liferay.registry.Registry;
041 import com.liferay.registry.RegistryUtil;
042 import com.liferay.registry.ServiceReference;
043 import com.liferay.registry.ServiceRegistration;
044 import com.liferay.registry.ServiceTrackerCustomizer;
045 import com.liferay.registry.collections.ServiceRegistrationMap;
046 import com.liferay.registry.collections.ServiceRegistrationMapImpl;
047 import com.liferay.registry.collections.ServiceTrackerCollections;
048 import com.liferay.registry.collections.ServiceTrackerMap;
049
050 import java.util.ArrayList;
051 import java.util.Collection;
052 import java.util.Collections;
053 import java.util.HashSet;
054 import java.util.LinkedHashMap;
055 import java.util.List;
056 import java.util.Set;
057
058 import javax.servlet.http.HttpServletRequest;
059
060
065 public class WebDAVUtil {
066
067 public static final Namespace DAV_URI = SAXReaderUtil.createNamespace(
068 "D", "DAV:");
069
070 public static final int SC_LOCKED = 423;
071
072 public static final int SC_MULTI_STATUS = 207;
073
074 public static final String TOKEN_PREFIX = "opaquelocktoken:";
075
076 public static void addStorage(WebDAVStorage storage) {
077 getInstance()._addStorage(storage);
078 }
079
080 public static Namespace createNamespace(String prefix, String uri) {
081 Namespace namespace = null;
082
083 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
084 namespace = WebDAVUtil.DAV_URI;
085 }
086 else if (Validator.isNull(prefix)) {
087 namespace = SAXReaderUtil.createNamespace(uri);
088 }
089 else {
090 namespace = SAXReaderUtil.createNamespace(prefix, uri);
091 }
092
093 return namespace;
094 }
095
096 public static void deleteStorage(WebDAVStorage storage) {
097 getInstance()._deleteStorage(storage);
098 }
099
100 public static long getDepth(HttpServletRequest request) {
101 String value = GetterUtil.getString(request.getHeader("Depth"));
102
103 if (_log.isDebugEnabled()) {
104 _log.debug("\"Depth\" header is " + value);
105 }
106
107 if (value.equals("0")) {
108 return 0;
109 }
110 else {
111 return -1;
112 }
113 }
114
115 public static String getDestination(
116 HttpServletRequest request, String rootPath) {
117
118 String headerDestination = request.getHeader("Destination");
119 String[] pathSegments = StringUtil.split(headerDestination, rootPath);
120
121 String destination = pathSegments[pathSegments.length - 1];
122
123 destination = HttpUtil.decodePath(destination);
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("Destination " + destination);
127 }
128
129 return destination;
130 }
131
132 public static long getGroupId(long companyId, String path)
133 throws WebDAVException {
134
135 String[] pathArray = getPathArray(path);
136
137 return getGroupId(companyId, pathArray);
138 }
139
140 public static long getGroupId(long companyId, String[] pathArray)
141 throws WebDAVException {
142
143 try {
144 if (pathArray.length == 0) {
145 return 0;
146 }
147
148 String name = pathArray[0];
149
150 Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(
151 companyId, StringPool.SLASH + name);
152
153 if (group != null) {
154 return group.getGroupId();
155 }
156
157 User user = UserLocalServiceUtil.fetchUserByScreenName(
158 companyId, name);
159
160 if (user != null) {
161 group = user.getGroup();
162
163 return group.getGroupId();
164 }
165 }
166 catch (Exception e) {
167 throw new WebDAVException(e);
168 }
169
170 return 0;
171 }
172
173 public static List<Group> getGroups(long userId) throws Exception {
174 User user = UserLocalServiceUtil.getUser(userId);
175
176 return getGroups(user);
177 }
178
179 public static List<Group> getGroups(User user) throws Exception {
180
181
182
183 if (user.isDefaultUser()) {
184 List<Group> groups = new ArrayList<>();
185
186 Group group = GroupLocalServiceUtil.getGroup(
187 user.getCompanyId(), GroupConstants.GUEST);
188
189 groups.add(group);
190
191 return groups;
192 }
193
194
195
196 Set<Group> groups = new HashSet<>();
197
198 LinkedHashMap<String, Object> params = new LinkedHashMap<>();
199
200 params.put("usersGroups", user.getUserId());
201
202 OrderByComparator<Group> orderByComparator =
203 new GroupFriendlyURLComparator(true);
204
205 groups.addAll(
206 GroupLocalServiceUtil.search(
207 user.getCompanyId(), null, null, params, QueryUtil.ALL_POS,
208 QueryUtil.ALL_POS, orderByComparator));
209
210
211
212 groups.addAll(
213 GroupLocalServiceUtil.getUserOrganizationsGroups(
214 user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));
215
216
217
218 if (!user.isDefaultUser()) {
219 groups.add(user.getGroup());
220 }
221
222 List<Group> groupsList = new ArrayList<>(groups);
223
224 Collections.sort(groupsList, orderByComparator);
225
226 return groupsList;
227 }
228
229 public static WebDAVUtil getInstance() {
230 PortalRuntimePermission.checkGetBeanProperty(WebDAVUtil.class);
231
232 return _instance;
233 }
234
235 public static String getLockUuid(HttpServletRequest request)
236 throws WebDAVException {
237
238 String token = StringPool.BLANK;
239
240 String value = GetterUtil.getString(request.getHeader("If"));
241
242 if (_log.isDebugEnabled()) {
243 _log.debug("\"If\" header is " + value);
244 }
245
246 if (value.contains("(<DAV:no-lock>)")) {
247 if (_log.isWarnEnabled()) {
248 _log.warn("Lock tokens can never be <DAV:no-lock>");
249 }
250
251 throw new WebDAVException();
252 }
253
254 int beg = value.indexOf(TOKEN_PREFIX);
255
256 if (beg >= 0) {
257 beg += TOKEN_PREFIX.length();
258
259 if (beg < value.length()) {
260 int end = value.indexOf(CharPool.GREATER_THAN, beg);
261
262 token = GetterUtil.getString(value.substring(beg, end));
263 }
264 }
265
266 return token;
267 }
268
269 public static String[] getPathArray(String path) {
270 return getPathArray(path, false);
271 }
272
273 public static String[] getPathArray(String path, boolean fixTrailing) {
274 path = HttpUtil.fixPath(path, true, fixTrailing);
275
276 return StringUtil.split(path, CharPool.SLASH);
277 }
278
279 public static String getResourceName(String[] pathArray) {
280 if (pathArray.length <= 2) {
281 return StringPool.BLANK;
282 }
283 else {
284 return pathArray[pathArray.length - 1];
285 }
286 }
287
288 public static WebDAVStorage getStorage(String token) {
289 return getInstance()._getStorage(token);
290 }
291
292 public static String getStorageToken(Portlet portlet) {
293 WebDAVStorage webDAVStorageInstance =
294 portlet.getWebDAVStorageInstance();
295
296 if (webDAVStorageInstance == null) {
297 return null;
298 }
299
300 return webDAVStorageInstance.getToken();
301 }
302
303 public static Collection<String> getStorageTokens() {
304 return getInstance()._getStorageTokens();
305 }
306
307 public static long getTimeout(HttpServletRequest request) {
308 final String TIME_PREFIX = "Second-";
309
310 long timeout = 0;
311
312 String value = GetterUtil.getString(request.getHeader("Timeout"));
313
314 if (_log.isDebugEnabled()) {
315 _log.debug("\"Timeout\" header is " + value);
316 }
317
318 int index = value.indexOf(TIME_PREFIX);
319
320 if (index >= 0) {
321 index += TIME_PREFIX.length();
322
323 if (index < value.length()) {
324 timeout = GetterUtil.getLong(value.substring(index));
325 }
326 }
327
328 return timeout * Time.SECOND;
329 }
330
331 public static boolean isOverwrite(HttpServletRequest request) {
332 return getInstance()._isOverwrite(request);
333 }
334
335 public static String stripManualCheckInRequiredPath(String url) {
336 return stripToken(url, DL.MANUAL_CHECK_IN_REQUIRED_PATH);
337 }
338
339 public static String stripOfficeExtension(String url) {
340 String strippedUrl = stripToken(url, DL.OFFICE_EXTENSION_PATH);
341
342 if (strippedUrl.length() != url.length()) {
343 strippedUrl = FileUtil.stripExtension(strippedUrl);
344 }
345
346 return strippedUrl;
347 }
348
349 public static String stripToken(String url, String token) {
350 if (Validator.isNull(url)) {
351 return StringPool.BLANK;
352 }
353
354 int index = url.indexOf(token);
355
356 if (index >= 0) {
357 url =
358 url.substring(0, index) + url.substring(index + token.length());
359 }
360
361 return url;
362 }
363
364 private WebDAVUtil() {
365 _storages = ServiceTrackerCollections.openSingleValueMap(
366 WebDAVStorage.class, "webdav.storage.token",
367 new ServiceTrackerCustomizer<WebDAVStorage, WebDAVStorage>() {
368
369 @Override
370 public WebDAVStorage addingService(
371 ServiceReference<WebDAVStorage> serviceReference) {
372
373 Registry registry = RegistryUtil.getRegistry();
374
375 WebDAVStorage webDAVStorage = registry.getService(
376 serviceReference);
377
378 setToken(serviceReference, webDAVStorage);
379
380 return webDAVStorage;
381 }
382
383 @Override
384 public void modifiedService(
385 ServiceReference<WebDAVStorage> serviceReference,
386 WebDAVStorage webDAVStorage) {
387
388 setToken(serviceReference, webDAVStorage);
389 }
390
391 @Override
392 public void removedService(
393 ServiceReference<WebDAVStorage> serviceReference,
394 WebDAVStorage webDAVStorage) {
395 }
396
397 protected void setToken(
398 ServiceReference<WebDAVStorage> serviceReference,
399 WebDAVStorage webDAVStorage) {
400
401 String token = (String)serviceReference.getProperty(
402 "webdav.storage.token");
403
404 webDAVStorage.setToken(token);
405 }
406
407 });
408 }
409
410 private void _addStorage(WebDAVStorage storage) {
411 Registry registry = RegistryUtil.getRegistry();
412
413 ServiceRegistration<WebDAVStorage> serviceRegistration =
414 registry.registerService(WebDAVStorage.class, storage);
415
416 _serviceRegistrations.put(storage, serviceRegistration);
417 }
418
419 private void _deleteStorage(WebDAVStorage storage) {
420 ServiceRegistration<WebDAVStorage> serviceRegistration =
421 _serviceRegistrations.remove(storage);
422
423 if (serviceRegistration != null) {
424 serviceRegistration.unregister();
425 }
426 }
427
428 private WebDAVStorage _getStorage(String token) {
429 return _storages.getService(token);
430 }
431
432 private Collection<String> _getStorageTokens() {
433 return _storages.keySet();
434 }
435
436 private boolean _isOverwrite(HttpServletRequest request) {
437 String value = GetterUtil.getString(request.getHeader("Overwrite"));
438
439 if (StringUtil.equalsIgnoreCase(value, "F") ||
440 !GetterUtil.getBoolean(value)) {
441
442 return false;
443 }
444 else {
445 return true;
446 }
447 }
448
449 private static final Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);
450
451 private static final WebDAVUtil _instance = new WebDAVUtil();
452
453 private final ServiceRegistrationMap<WebDAVStorage> _serviceRegistrations =
454 new ServiceRegistrationMapImpl<>();
455 private final ServiceTrackerMap<String, WebDAVStorage> _storages;
456
457 }