1
22
23 package com.liferay.portal.webdav;
24
25 import com.germinus.easyconf.Filter;
26
27 import com.liferay.portal.NoSuchGroupException;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.Company;
33 import com.liferay.portal.model.Group;
34 import com.liferay.portal.model.User;
35 import com.liferay.portal.service.CompanyLocalServiceUtil;
36 import com.liferay.portal.service.GroupLocalServiceUtil;
37 import com.liferay.portal.service.UserLocalServiceUtil;
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 req) {
92 String value = GetterUtil.getString(req.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 req, String rootPath) {
108
109 String headerDestination = req.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, 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 req) {
225 String value = GetterUtil.getString(req.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(PropsUtil.WEBDAV_STORAGE_TOKENS);
239
240 for (String token: tokens) {
241 String className =
242 PropsUtil.getComponentProperties().getString(
243 PropsUtil.WEBDAV_STORAGE_CLASS, Filter.by(token));
244
245 if (Validator.isNotNull(className)) {
246 _storageMap.put(className, token);
247 }
248 }
249 }
250
251 private String _getStorageClass(String token) {
252 if (_storageMap.containsValue(token)) {
253 for (String key : _storageMap.keySet()) {
254 if (_storageMap.get(key).equals(token)) {
255 return key;
256 }
257 }
258 }
259
260 return null;
261 }
262
263 private String _getStorageToken(String className) {
264 return _storageMap.get(className);
265 }
266
267 private Collection<String> _getStorageTokens() {
268 return _storageMap.values();
269 }
270
271 private boolean _isEnabled(String storageClassName) {
272 return _storageMap.containsKey(storageClassName);
273 }
274
275 private static Log _log = LogFactory.getLog(WebDAVUtil.class);
276
277 private static WebDAVUtil _instance = new WebDAVUtil();
278
279 private final Map<String, String> _storageMap;
280
281 }