1
22
23 package com.liferay.portal.webdav;
24
25 import com.liferay.portal.NoSuchGroupException;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.HttpUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Time;
33 import com.liferay.portal.kernel.xml.Namespace;
34 import com.liferay.portal.kernel.xml.SAXReaderUtil;
35 import com.liferay.portal.model.Company;
36 import com.liferay.portal.model.Group;
37 import com.liferay.portal.model.User;
38 import com.liferay.portal.service.CompanyLocalServiceUtil;
39 import com.liferay.portal.service.GroupLocalServiceUtil;
40 import com.liferay.portal.service.UserLocalServiceUtil;
41
42 import java.util.Collection;
43 import java.util.Map;
44 import java.util.TreeMap;
45
46 import javax.servlet.http.HttpServletRequest;
47
48
54 public class WebDAVUtil {
55
56 public static final Namespace DAV_URI = SAXReaderUtil.createNamespace(
57 "D", "DAV:");
58
59 public static final int SC_MULTI_STATUS = 207;
60
61 public static final int SC_LOCKED = 423;
62
63 public static final String TOKEN_PREFIX = "opaquelocktoken:";
64
65 public static void addStorage(WebDAVStorage storage) {
66 _instance._addStorage(storage);
67 }
68
69 public static void deleteStorage(WebDAVStorage storage) {
70 _instance._deleteStorage(storage);
71 }
72
73 public static String encodeURL(String url) {
74 url = HttpUtil.encodeURL(url);
75 url = StringUtil.replace(url, StringPool.PLUS, StringPool.SPACE);
76
77 return url;
78 }
79
80 public static String fixPath(String path) {
81 if (path.endsWith(StringPool.SLASH)) {
82 path = path.substring(0, path.length() - 1);
83 }
84
85 return path;
86 }
87
88 public static long getCompanyId(String path) throws WebDAVException {
89 String[] pathArray = getPathArray(path);
90
91 return getCompanyId(pathArray);
92 }
93
94 public static long getCompanyId(String[] pathArray) throws WebDAVException {
95 try {
96 String webId = getWebId(pathArray);
97
98 Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
99
100 return company.getCompanyId();
101 }
102 catch (Exception e) {
103 throw new WebDAVException(e);
104 }
105 }
106
107 public static long getDepth(HttpServletRequest request) {
108 String value = GetterUtil.getString(request.getHeader("Depth"));
109
110 if (_log.isDebugEnabled()) {
111 _log.debug("\"Depth\" header is " + value);
112 }
113
114 if (value.equals("0")) {
115 return 0;
116 }
117 else {
118 return -1;
119 }
120 }
121
122 public static String getDestination(
123 HttpServletRequest request, String rootPath) {
124
125 String headerDestination = request.getHeader("Destination");
126 String[] pathSegments = StringUtil.split(headerDestination, rootPath);
127
128 String destination = pathSegments[pathSegments.length - 1];
129
130 destination = StringUtil.replace(
131 destination, StringPool.SLASH, _TEMP_SLASH);
132 destination = HttpUtil.decodeURL(destination, true);
133 destination = StringUtil.replace(
134 destination, _TEMP_SLASH, StringPool.SLASH);
135
136 if (_log.isDebugEnabled()) {
137 _log.debug("Destination " + destination);
138 }
139
140 return destination;
141 }
142
143 public static long getGroupId(String path) throws WebDAVException {
144 String[] pathArray = getPathArray(path);
145
146 return getGroupId(pathArray);
147 }
148
149 public static long getGroupId(String[] pathArray) throws WebDAVException {
150 try {
151 if (pathArray.length <= 1) {
152 return 0;
153 }
154
155 long companyId = getCompanyId(pathArray);
156
157 String name = pathArray[1];
158
159 try {
160 Group group = GroupLocalServiceUtil.getGroup(companyId, name);
161
162 return group.getGroupId();
163 }
164 catch (NoSuchGroupException nsge) {
165 }
166
167 try {
168 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
169 companyId, StringPool.SLASH + name);
170
171 return group.getGroupId();
172 }
173 catch (NoSuchGroupException nsge) {
174 }
175
176 User user = UserLocalServiceUtil.getUserByScreenName(
177 companyId, name);
178
179 Group group = user.getGroup();
180
181 return group.getGroupId();
182 }
183 catch (Exception e) {
184 throw new WebDAVException(e);
185 }
186 }
187
188 public static String getLockUuid(HttpServletRequest request)
189 throws WebDAVException {
190
191 String token = StringPool.BLANK;
192
193 String value = GetterUtil.getString(request.getHeader("If"));
194
195 if (_log.isDebugEnabled()) {
196 _log.debug("\"If\" header is " + value);
197 }
198
199 if (value.contains("(<DAV:no-lock>)")) {
200 if (_log.isWarnEnabled()) {
201 _log.warn("Lock tokens can never be <DAV:no-lock>");
202 }
203
204 throw new WebDAVException();
205 }
206
207 int beg = value.indexOf(TOKEN_PREFIX);
208
209 if (beg >= 0) {
210 beg += TOKEN_PREFIX.length();
211
212 if (beg < value.length()) {
213 int end = value.indexOf(">", beg);
214
215 token = GetterUtil.getString(value.substring(beg, end));
216 }
217 }
218
219 return token;
220 }
221
222 public static String[] getPathArray(String path) {
223 return getPathArray(path, false);
224 }
225
226 public static String[] getPathArray(String path, boolean fixPath) {
227 if (fixPath) {
228 path = fixPath(path);
229 }
230
231 if (path.startsWith(StringPool.SLASH)) {
232 path = path.substring(1, path.length());
233 }
234
235 return StringUtil.split(path, StringPool.SLASH);
236 }
237
238 public static String getResourceName(String[] pathArray) {
239 if (pathArray.length <= 3) {
240 return StringPool.BLANK;
241 }
242 else {
243 return pathArray[pathArray.length - 1];
244 }
245 }
246
247 public static WebDAVStorage getStorage(String token) {
248 return _instance._getStorage(token);
249 }
250
251 public static Collection<String> getStorageTokens() {
252 return _instance._getStorageTokens();
253 }
254
255 public static long getTimeout(HttpServletRequest request) {
256 final String TIME_PREFIX = "Second-";
257
258 long timeout = 0;
259
260 String value = GetterUtil.getString(request.getHeader("Timeout"));
261
262 if (_log.isDebugEnabled()) {
263 _log.debug("\"Timeout\" header is " + value);
264 }
265
266 int index = value.indexOf(TIME_PREFIX);
267
268 if (index >= 0) {
269 index += TIME_PREFIX.length();
270
271 if (index < value.length()) {
272 timeout = GetterUtil.getLong(value.substring(index));
273 }
274 }
275
276 return timeout * Time.SECOND;
277 }
278
279 public static String getWebId(String path) throws WebDAVException {
280 String[] pathArray = getPathArray(path);
281
282 return getWebId(pathArray);
283 }
284
285 public static String getWebId(String[] pathArray) throws WebDAVException {
286 if (pathArray.length > 0) {
287 String webId = pathArray[0];
288
289 return webId;
290 }
291 else {
292 throw new WebDAVException();
293 }
294 }
295
296 public static boolean isOverwrite(HttpServletRequest request) {
297 return _instance._isOverwrite(request);
298 }
299
300 private WebDAVUtil() {
301 _storageMap = new TreeMap<String, WebDAVStorage>();
302 }
303
304 private void _addStorage(WebDAVStorage storage) {
305 _storageMap.put(storage.getToken(), storage);
306 }
307
308 private void _deleteStorage(WebDAVStorage storage) {
309 if (storage != null) {
310 _storageMap.remove(storage.getToken());
311 }
312 }
313
314 private WebDAVStorage _getStorage(String token) {
315 return _storageMap.get(token);
316 }
317
318 private Collection<String> _getStorageTokens() {
319 return _storageMap.keySet();
320 }
321
322 private boolean _isOverwrite(HttpServletRequest request) {
323 String value = GetterUtil.getString(request.getHeader("Overwrite"));
324
325 if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
326 return false;
327 }
328 else {
329 return true;
330 }
331 }
332
333 private static final String _TEMP_SLASH = "_LIFERAY_TEMP_SLASH_";
334
335 private static Log _log = LogFactoryUtil.getLog(WebDAVUtil.class);
336
337 private static WebDAVUtil _instance = new WebDAVUtil();
338
339 private Map<String, WebDAVStorage> _storageMap;
340
341 }