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