001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.webdav;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.dom4j.Namespace;
033:
034: /**
035: * <a href="WebDAVUtil.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: * @author Alexander Chow
039: *
040: */
041: public class WebDAVUtil {
042:
043: public static final Namespace DAV_URI = Namespace.get("D", "DAV:");
044:
045: public static final int SC_MULTI_STATUS = 207;
046:
047: public static String fixPath(String path) {
048: if (path.endsWith(StringPool.SLASH)) {
049: path = path.substring(0, path.length() - 1);
050: }
051:
052: return path;
053: }
054:
055: public static long getCompanyId(String path) {
056: return getCompanyId(path, false);
057: }
058:
059: public static long getCompanyId(String path, boolean fixPath) {
060: if (fixPath) {
061: path = fixPath(path);
062: }
063:
064: String[] pathArray = getPathArray(path);
065:
066: if (pathArray.length <= 0) {
067: return 0;
068: } else {
069: return GetterUtil.getLong(pathArray[0]);
070: }
071: }
072:
073: public static long getDepth(HttpServletRequest req) {
074: String value = GetterUtil.getString(req.getHeader("Depth"));
075:
076: if (_log.isInfoEnabled()) {
077: _log.info("\"Depth\" header is " + value);
078: }
079:
080: if (value.equals("0")) {
081: return 0;
082: } else {
083: return -1;
084: }
085: }
086:
087: public static String getDestination(HttpServletRequest req,
088: String rootPath) {
089:
090: String headerDestination = req.getHeader("Destination");
091: String[] pathSegments = StringUtil.split(headerDestination,
092: rootPath);
093:
094: String destination = pathSegments[pathSegments.length - 1];
095:
096: if (_log.isDebugEnabled()) {
097: _log.debug("Destination " + destination);
098: }
099:
100: return destination;
101: }
102:
103: public static String getEntryName(String[] pathArray) {
104: if (pathArray.length <= 2) {
105: return StringPool.BLANK;
106: } else {
107: return pathArray[pathArray.length - 1];
108: }
109: }
110:
111: public static long getGroupId(String path) {
112: return getGroupId(path, false);
113: }
114:
115: public static long getGroupId(String[] pathArray) {
116: if (pathArray.length <= 1) {
117: return 0;
118: } else {
119: return GetterUtil.getLong(pathArray[1]);
120: }
121: }
122:
123: public static long getGroupId(String path, boolean fixPath) {
124: if (fixPath) {
125: path = fixPath(path);
126: }
127:
128: String[] pathArray = getPathArray(path);
129:
130: return getGroupId(pathArray);
131: }
132:
133: public static String[] getPathArray(String path) {
134: return getPathArray(path, false);
135: }
136:
137: public static String[] getPathArray(String path, boolean fixPath) {
138: if (fixPath) {
139: path = fixPath(path);
140: }
141:
142: if (path.startsWith(StringPool.SLASH)) {
143: path = path.substring(1, path.length());
144: }
145:
146: return StringUtil.split(path, StringPool.SLASH);
147: }
148:
149: public static boolean isGroupPath(String path) {
150: return isGroupPath(path, false);
151: }
152:
153: public static boolean isGroupPath(String path, boolean fixPath) {
154: if (fixPath) {
155: path = fixPath(path);
156: }
157:
158: String[] pathArray = getPathArray(path);
159:
160: if (pathArray.length == 2) {
161: return true;
162: } else {
163: return false;
164: }
165: }
166:
167: public static boolean isOverwrite(HttpServletRequest req) {
168: String value = GetterUtil.getString(req.getHeader("Overwrite"));
169:
170: if (value.equalsIgnoreCase("F")
171: || !GetterUtil.getBoolean(value)) {
172: return false;
173: } else {
174: return true;
175: }
176: }
177:
178: private static Log _log = LogFactory.getLog(WebDAVUtil.class);
179:
180: }
|