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