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