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