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.UniqueList;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.xml.Namespace;
032 import com.liferay.portal.kernel.xml.SAXReaderUtil;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.model.GroupConstants;
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
041 import java.util.ArrayList;
042 import java.util.Collection;
043 import java.util.Collections;
044 import java.util.LinkedHashMap;
045 import java.util.List;
046 import java.util.Map;
047 import java.util.TreeMap;
048
049 import javax.servlet.http.HttpServletRequest;
050
051
056 public class WebDAVUtil {
057
058 public static final Namespace DAV_URI = SAXReaderUtil.createNamespace(
059 "D", "DAV:");
060
061 public static final int SC_LOCKED = 423;
062
063 public static final int SC_MULTI_STATUS = 207;
064
065 public static final String TOKEN_PREFIX = "opaquelocktoken:";
066
067 public static void addStorage(WebDAVStorage storage) {
068 getInstance()._addStorage(storage);
069 }
070
071 public static Namespace createNamespace(String prefix, String uri) {
072 Namespace namespace = null;
073
074 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
075 namespace = WebDAVUtil.DAV_URI;
076 }
077 else if (Validator.isNull(prefix)) {
078 namespace = SAXReaderUtil.createNamespace(uri);
079 }
080 else {
081 namespace = SAXReaderUtil.createNamespace(prefix, uri);
082 }
083
084 return namespace;
085 }
086
087 public static void deleteStorage(WebDAVStorage storage) {
088 getInstance()._deleteStorage(storage);
089 }
090
091 public static long getDepth(HttpServletRequest request) {
092 String value = GetterUtil.getString(request.getHeader("Depth"));
093
094 if (_log.isDebugEnabled()) {
095 _log.debug("\"Depth\" header is " + value);
096 }
097
098 if (value.equals("0")) {
099 return 0;
100 }
101 else {
102 return -1;
103 }
104 }
105
106 public static String getDestination(
107 HttpServletRequest request, String rootPath) {
108
109 String headerDestination = request.getHeader("Destination");
110 String[] pathSegments = StringUtil.split(headerDestination, rootPath);
111
112 String destination = pathSegments[pathSegments.length - 1];
113
114 destination = HttpUtil.decodePath(destination);
115
116 if (_log.isDebugEnabled()) {
117 _log.debug("Destination " + destination);
118 }
119
120 return destination;
121 }
122
123 public static long getGroupId(long companyId, String path)
124 throws WebDAVException {
125
126 String[] pathArray = getPathArray(path);
127
128 return getGroupId(companyId, pathArray);
129 }
130
131 public static long getGroupId(long companyId, String[] pathArray)
132 throws WebDAVException {
133
134 try {
135 if (pathArray.length == 0) {
136 return 0;
137 }
138
139 String name = pathArray[0];
140
141 Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(
142 companyId, StringPool.SLASH + name);
143
144 if (group != null) {
145 return group.getGroupId();
146 }
147
148 User user = UserLocalServiceUtil.fetchUserByScreenName(
149 companyId, name);
150
151 if (user != null) {
152 group = user.getGroup();
153
154 return group.getGroupId();
155 }
156 }
157 catch (Exception e) {
158 throw new WebDAVException(e);
159 }
160
161 return 0;
162 }
163
164 public static List<Group> getGroups(long userId) throws Exception {
165 User user = UserLocalServiceUtil.getUser(userId);
166
167 return getGroups(user);
168 }
169
170 public static List<Group> getGroups(User user) throws Exception {
171
172
173
174 if (user.isDefaultUser()) {
175 List<Group> groups = new ArrayList<Group>();
176
177 Group group = GroupLocalServiceUtil.getGroup(
178 user.getCompanyId(), GroupConstants.GUEST);
179
180 groups.add(group);
181
182 return groups;
183 }
184
185
186
187 List<Group> groups = new UniqueList<Group>();
188
189 LinkedHashMap<String, Object> params =
190 new LinkedHashMap<String, Object>();
191
192 params.put("usersGroups", user.getUserId());
193
194 OrderByComparator orderByComparator = new GroupFriendlyURLComparator(
195 true);
196
197 groups.addAll(
198 GroupLocalServiceUtil.search(
199 user.getCompanyId(), null, null, params, QueryUtil.ALL_POS,
200 QueryUtil.ALL_POS, orderByComparator));
201
202
203
204 groups.addAll(
205 GroupLocalServiceUtil.getUserOrganizationsGroups(
206 user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));
207
208
209
210 if (!user.isDefaultUser()) {
211 groups.add(user.getGroup());
212 }
213
214 Collections.sort(groups, orderByComparator);
215
216 return groups;
217 }
218
219 public static WebDAVUtil getInstance() {
220 PortalRuntimePermission.checkGetBeanProperty(WebDAVUtil.class);
221
222 return _instance;
223 }
224
225 public static String getLockUuid(HttpServletRequest request)
226 throws WebDAVException {
227
228 String token = StringPool.BLANK;
229
230 String value = GetterUtil.getString(request.getHeader("If"));
231
232 if (_log.isDebugEnabled()) {
233 _log.debug("\"If\" header is " + value);
234 }
235
236 if (value.contains("(<DAV:no-lock>)")) {
237 if (_log.isWarnEnabled()) {
238 _log.warn("Lock tokens can never be <DAV:no-lock>");
239 }
240
241 throw new WebDAVException();
242 }
243
244 int beg = value.indexOf(TOKEN_PREFIX);
245
246 if (beg >= 0) {
247 beg += TOKEN_PREFIX.length();
248
249 if (beg < value.length()) {
250 int end = value.indexOf(CharPool.GREATER_THAN, beg);
251
252 token = GetterUtil.getString(value.substring(beg, end));
253 }
254 }
255
256 return token;
257 }
258
259 public static String[] getPathArray(String path) {
260 return getPathArray(path, false);
261 }
262
263 public static String[] getPathArray(String path, boolean fixTrailing) {
264 path = HttpUtil.fixPath(path, true, fixTrailing);
265
266 return StringUtil.split(path, CharPool.SLASH);
267 }
268
269 public static String getResourceName(String[] pathArray) {
270 if (pathArray.length <= 2) {
271 return StringPool.BLANK;
272 }
273 else {
274 return pathArray[pathArray.length - 1];
275 }
276 }
277
278 public static WebDAVStorage getStorage(String token) {
279 return getInstance()._getStorage(token);
280 }
281
282 public static Collection<String> getStorageTokens() {
283 return getInstance()._getStorageTokens();
284 }
285
286 public static long getTimeout(HttpServletRequest request) {
287 final String TIME_PREFIX = "Second-";
288
289 long timeout = 0;
290
291 String value = GetterUtil.getString(request.getHeader("Timeout"));
292
293 if (_log.isDebugEnabled()) {
294 _log.debug("\"Timeout\" header is " + value);
295 }
296
297 int index = value.indexOf(TIME_PREFIX);
298
299 if (index >= 0) {
300 index += TIME_PREFIX.length();
301
302 if (index < value.length()) {
303 timeout = GetterUtil.getLong(value.substring(index));
304 }
305 }
306
307 return timeout * Time.SECOND;
308 }
309
310 public static boolean isOverwrite(HttpServletRequest request) {
311 return getInstance()._isOverwrite(request);
312 }
313
314 public static String stripManualCheckInRequiredPath(String url) {
315 return stripToken(url, DL.MANUAL_CHECK_IN_REQUIRED_PATH);
316 }
317
318 public static String stripOfficeExtension(String url) {
319 String strippedUrl = stripToken(url, DL.OFFICE_EXTENSION_PATH);
320
321 if (strippedUrl.length() != url.length()) {
322 strippedUrl = FileUtil.stripExtension(strippedUrl);
323 }
324
325 return strippedUrl;
326 }
327
328 public static String stripToken(String url, String token) {
329 if (Validator.isNull(url)) {
330 return StringPool.BLANK;
331 }
332
333 int index = url.indexOf(token);
334
335 if (index >= 0) {
336 url =
337 url.substring(0, index) + url.substring(index + token.length());
338 }
339
340 return url;
341 }
342
343 private WebDAVUtil() {
344 _storageMap = new TreeMap<String, WebDAVStorage>();
345 }
346
347 private void _addStorage(WebDAVStorage storage) {
348 _storageMap.put(storage.getToken(), storage);
349 }
350
351 private void _deleteStorage(WebDAVStorage storage) {
352 if (storage != null) {
353 _storageMap.remove(storage.getToken());
354 }
355 }
356
357 private WebDAVStorage _getStorage(String token) {
358 return _storageMap.get(token);
359 }
360
361 private Collection<String> _getStorageTokens() {
362 return _storageMap.keySet();
363 }
364
365 private boolean _isOverwrite(HttpServletRequest request) {
366 String value = GetterUtil.getString(request.getHeader("Overwrite"));
367
368 if (StringUtil.equalsIgnoreCase(value, "F") ||
369 !GetterUtil.getBoolean(value)) {
370
371 return false;
372 }
373 else {
374 return true;
375 }
376 }
377
378 private static Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);
379
380 private static WebDAVUtil _instance = new WebDAVUtil();
381
382 private Map<String, WebDAVStorage> _storageMap;
383
384 }