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