1
22
23 package com.liferay.portal.webdav;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29
30 import javax.servlet.http.HttpServletRequest;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.dom4j.Namespace;
36
37
44 public class WebDAVUtil {
45
46 public static final Namespace DAV_URI = Namespace.get("D", "DAV:");
47
48 public static final int SC_MULTI_STATUS = 207;
49
50 public static String fixPath(String path) {
51 if (path.endsWith(StringPool.SLASH)) {
52 path = path.substring(0, path.length() - 1);
53 }
54
55 return path;
56 }
57
58 public static long getCompanyId(String path) {
59 return getCompanyId(path, false);
60 }
61
62 public static long getCompanyId(String path, boolean fixPath) {
63 if (fixPath) {
64 path = fixPath(path);
65 }
66
67 String[] pathArray = getPathArray(path);
68
69 if (pathArray.length <= 0) {
70 return 0;
71 }
72 else {
73 return GetterUtil.getLong(pathArray[0]);
74 }
75 }
76
77 public static long getDepth(HttpServletRequest req)
78 throws InvalidRequestException {
79
80 String value = GetterUtil.getString(req.getHeader("Depth"));
81
82 if (value.equals("0")) {
83 return 0;
84 }
85 else if (value.equalsIgnoreCase("infinity") ||
86 Validator.isNull(value)) {
87
88 return -1;
89 }
90 else {
91 throw new InvalidRequestException(value);
92 }
93 }
94
95 public static String getDestination(
96 HttpServletRequest req, String rootPath) {
97
98 String headerDestination = req.getHeader("Destination");
99 String[] pathSegments = StringUtil.split(headerDestination, rootPath);
100
101 String destination = pathSegments[pathSegments.length - 1];
102
103 if (_log.isDebugEnabled()) {
104 _log.debug("Destination " + destination);
105 }
106
107 return destination;
108 }
109
110 public static String getEntryName(String[] pathArray) {
111 if (pathArray.length <= 2) {
112 return StringPool.BLANK;
113 }
114 else {
115 return pathArray[pathArray.length - 1];
116 }
117 }
118
119 public static long getGroupId(String path) {
120 return getGroupId(path, false);
121 }
122
123 public static long getGroupId(String[] pathArray) {
124 if (pathArray.length <= 1) {
125 return 0;
126 }
127 else {
128 return GetterUtil.getLong(pathArray[1]);
129 }
130 }
131
132 public static long getGroupId(String path, boolean fixPath) {
133 if (fixPath) {
134 path = fixPath(path);
135 }
136
137 String[] pathArray = getPathArray(path);
138
139 return getGroupId(pathArray);
140 }
141
142 public static String[] getPathArray(String path) {
143 return getPathArray(path, false);
144 }
145
146 public static String[] getPathArray(String path, boolean fixPath) {
147 if (fixPath) {
148 path = fixPath(path);
149 }
150
151 if (path.startsWith(StringPool.SLASH)) {
152 path = path.substring(1, path.length());
153 }
154
155 return StringUtil.split(path, StringPool.SLASH);
156 }
157
158 public static boolean isGroupPath(String path) {
159 return isGroupPath(path, false);
160 }
161
162 public static boolean isGroupPath(String path, boolean fixPath) {
163 if (fixPath) {
164 path = fixPath(path);
165 }
166
167 String[] pathArray = getPathArray(path);
168
169 if (pathArray.length == 2) {
170 return true;
171 }
172 else {
173 return false;
174 }
175 }
176
177 public static boolean isOverwrite(HttpServletRequest req) {
178 String value = GetterUtil.getString(req.getHeader("Overwrite"));
179
180 if (value.equalsIgnoreCase("F") || !GetterUtil.getBoolean(value)) {
181 return false;
182 }
183 else {
184 return true;
185 }
186 }
187
188 private static Log _log = LogFactory.getLog(WebDAVUtil.class);
189
190 }