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