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