1
22
23 package com.liferay.portal.webdav;
24
25 import com.liferay.portal.NoSuchGroupException;
26 import com.liferay.portal.kernel.configuration.Filter;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.Company;
32 import com.liferay.portal.model.Group;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.service.CompanyLocalServiceUtil;
35 import com.liferay.portal.service.GroupLocalServiceUtil;
36 import com.liferay.portal.service.UserLocalServiceUtil;
37 import com.liferay.portal.util.PropsKeys;
38 import com.liferay.portal.util.PropsUtil;
39
40 import java.util.Collection;
41 import java.util.HashMap;
42 import java.util.Map;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 import org.dom4j.Namespace;
50
51
58 public class WebDAVUtil {
59
60 public static final Namespace DAV_URI = Namespace.get("D", "DAV:");
61
62 public static final int SC_MULTI_STATUS = 207;
63
64 public static String fixPath(String path) {
65 if (path.endsWith(StringPool.SLASH)) {
66 path = path.substring(0, path.length() - 1);
67 }
68
69 return path;
70 }
71
72 public static long getCompanyId(String path) throws WebDAVException {
73 String[] pathArray = getPathArray(path);
74
75 return getCompanyId(pathArray);
76 }
77
78 public static long getCompanyId(String[] pathArray) throws WebDAVException {
79 try {
80 String webId = getWebId(pathArray);
81
82 Company company = CompanyLocalServiceUtil.getCompanyByWebId(webId);
83
84 return company.getCompanyId();
85 }
86 catch (Exception e) {
87 throw new WebDAVException(e);
88 }
89 }
90
91 public static long getDepth(HttpServletRequest request) {
92 String value = GetterUtil.getString(request.getHeader("Depth"));
93
94 if (_log.isInfoEnabled()) {
95 _log.info("\"Depth\" header is " + value);
96 }
97
98 if (value.equals("0")) {
99 return 0;
100 }
101 else {
102 return -1;
103 }
104 }
105
106 public static String getDestination(
107 HttpServletRequest request, String rootPath) {
108
109 String headerDestination = request.getHeader("Destination");
110 String[] pathSegments = StringUtil.split(headerDestination, rootPath);
111
112 String destination = pathSegments[pathSegments.length - 1];
113
114 if (_log.isDebugEnabled()) {
115 _log.debug("Destination " + destination);
116 }
117
118 return destination;
119 }
120
121 public static long getGroupId(String path) throws WebDAVException {
122 String[] pathArray = getPathArray(path);
123
124 return getGroupId(pathArray);
125 }
126
127 public static long getGroupId(String[] pathArray) throws WebDAVException {
128 try {
129 if (pathArray.length <= 1) {
130 return 0;
131 }
132
133 long companyId = getCompanyId(pathArray);
134
135 String name = pathArray[1];
136
137 try {
138 Group group = GroupLocalServiceUtil.getGroup(companyId, name);
139
140 return group.getGroupId();
141 }
142 catch (NoSuchGroupException nsge) {
143 }
144
145 try {
146 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
147 companyId, StringPool.SLASH + name);
148
149 return group.getGroupId();
150 }
151 catch (NoSuchGroupException nsge) {
152 }
153
154 User user = UserLocalServiceUtil.getUserByScreenName(
155 companyId, name);
156
157 Group group = user.getGroup();
158
159 return group.getGroupId();
160 }
161 catch (Exception e) {
162 throw new WebDAVException(e);
163 }
164 }
165
166 public static String[] getPathArray(String path) {
167 return getPathArray(path, false);
168 }
169
170 public static String[] getPathArray(String path, boolean fixPath) {
171 if (fixPath) {
172 path = fixPath(path);
173 }
174
175 if (path.startsWith(StringPool.SLASH)) {
176 path = path.substring(1, path.length());
177 }
178
179 return StringUtil.split(path, StringPool.SLASH);
180 }
181
182 public static String getResourceName(String[] pathArray) {
183 if (pathArray.length <= 3) {
184 return StringPool.BLANK;
185 }
186 else {
187 return pathArray[pathArray.length - 1];
188 }
189 }
190
191 public static String getStorageClass(String token) {
192 return _instance._getStorageClass(token);
193 }
194
195 public static String getStorageToken(String className) {
196 return _instance._getStorageToken(className);
197 }
198
199 public static Collection<String> getStorageTokens() {
200 return _instance._getStorageTokens();
201 }
202
203 public static String getWebId(String path) throws WebDAVException {
204 String[] pathArray = getPathArray(path);
205
206 return getWebId(pathArray);
207 }
208
209 public static String getWebId(String[] pathArray) throws WebDAVException {
210 if (pathArray.length > 0) {
211 String webId = pathArray[0];
212
213 return webId;
214 }
215 else {
216 throw new WebDAVException();
217 }
218 }
219
220 public static boolean isEnabled(String storageClassName) {
221 return _instance._isEnabled(storageClassName);
222 }
223
224 public static boolean isOverwrite(HttpServletRequest request) {
225 String value = GetterUtil.getString(request.getHeader("Overwrite"));
226
227 if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
228 return false;
229 }
230 else {
231 return true;
232 }
233 }
234
235 private WebDAVUtil() {
236 _storageMap = new HashMap<String, String>();
237
238 String[] tokens = PropsUtil.getArray(PropsKeys.WEBDAV_STORAGE_TOKENS);
239
240 for (String token: tokens) {
241 String className = PropsUtil.get(
242 PropsKeys.WEBDAV_STORAGE_CLASS, new Filter(token));
243
244 if (Validator.isNotNull(className)) {
245 _storageMap.put(className, token);
246 }
247 }
248 }
249
250 private String _getStorageClass(String token) {
251 if (_storageMap.containsValue(token)) {
252 for (String key : _storageMap.keySet()) {
253 if (_storageMap.get(key).equals(token)) {
254 return key;
255 }
256 }
257 }
258
259 return null;
260 }
261
262 private String _getStorageToken(String className) {
263 return _storageMap.get(className);
264 }
265
266 private Collection<String> _getStorageTokens() {
267 return _storageMap.values();
268 }
269
270 private boolean _isEnabled(String storageClassName) {
271 return _storageMap.containsKey(storageClassName);
272 }
273
274 private static Log _log = LogFactory.getLog(WebDAVUtil.class);
275
276 private static WebDAVUtil _instance = new WebDAVUtil();
277
278 private final Map<String, String> _storageMap;
279
280 }