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