001
014
015 package com.liferay.portal.sharepoint;
016
017 import com.liferay.portal.kernel.configuration.Filter;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.InstancePool;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.kernel.webdav.WebDAVException;
028 import com.liferay.portal.kernel.webdav.WebDAVUtil;
029 import com.liferay.portal.security.auth.CompanyThreadLocal;
030 import com.liferay.portal.util.PropsUtil;
031
032 import java.util.Collection;
033 import java.util.HashMap;
034 import java.util.Map;
035
036
039 public class SharepointUtil {
040
041 public static final String VEERMER_URLENCODED =
042 "application/x-vermeer-urlencoded";
043
044 public static final String VERSION = "6.0.2.8117";
045
046 public static long getGroupId(String path) {
047 long groupId = 0;
048
049 long companyId = CompanyThreadLocal.getCompanyId();
050
051 try {
052 groupId = WebDAVUtil.getGroupId(companyId, path);
053 }
054 catch (WebDAVException wde) {
055 if (_log.isWarnEnabled()) {
056 _log.warn("Unable to get groupId for path " + path);
057 }
058 }
059
060 return groupId;
061 }
062
063 public static String[] getPathArray(String path) {
064 path = HttpUtil.fixPath(path, true, true);
065
066 return StringUtil.split(path, CharPool.SLASH);
067 }
068
069 public static SharepointStorage getStorage(String path) {
070 String storageClass = null;
071
072 if (path == null) {
073 return null;
074 }
075
076 String[] pathArray = getPathArray(path);
077
078 if (pathArray.length == 0) {
079 storageClass = CompanySharepointStorageImpl.class.getName();
080 }
081 else if (pathArray.length == 1) {
082 storageClass = GroupSharepointStorageImpl.class.getName();
083 }
084 else if (pathArray.length >= 2) {
085 storageClass = getStorageClass(pathArray[1]);
086 }
087
088 if (_log.isInfoEnabled()) {
089 _log.info("Storage class for path " + path + " is " + storageClass);
090 }
091
092 return (SharepointStorage)InstancePool.get(storageClass);
093 }
094
095 public static String getStorageClass(String token) {
096 return _instance._getStorageClass(token);
097 }
098
099 public static String getStorageToken(String className) {
100 return _instance._getStorageToken(className);
101 }
102
103 public static Collection<String> getStorageTokens() {
104 return _instance._getStorageTokens();
105 }
106
107 public static String replaceBackSlashes(String value) {
108 return value.replaceAll("\\\\", StringPool.BLANK);
109 }
110
111 public static String stripService(String url, boolean trailingSlash) {
112 return _instance._stripService(url, trailingSlash);
113 }
114
115 private SharepointUtil() {
116 _storageMap = new HashMap<String, String>();
117
118 String[] tokens = PropsUtil.getArray(
119 PropsKeys.SHAREPOINT_STORAGE_TOKENS);
120
121 for (String token : tokens) {
122 Filter filter = new Filter(token);
123
124 String className = PropsUtil.get(
125 PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
126
127 if (Validator.isNotNull(className)) {
128 _storageMap.put(className, token);
129 }
130 }
131 }
132
133 private String _getStorageClass(String token) {
134 for (Map.Entry<String, String> entry : _storageMap.entrySet()) {
135 String value = entry.getValue();
136
137 if (value.equals(token)) {
138 return entry.getKey();
139 }
140 }
141
142 return null;
143 }
144
145 private String _getStorageToken(String className) {
146 return _storageMap.get(className);
147 }
148
149 private Collection<String> _getStorageTokens() {
150 return _storageMap.values();
151 }
152
153 private String _stripService(String url, boolean trailingSlash) {
154 url = _stripService(url, "sharepoint", trailingSlash);
155 url = _stripService(url, "webdav", trailingSlash);
156
157 return url;
158 }
159
160 private String _stripService(
161 String url, String service, boolean trailingSlash) {
162
163 if (trailingSlash) {
164 service = service + StringPool.SLASH;
165 }
166 else {
167 service = StringPool.SLASH + service;
168 }
169
170 int pos = url.lastIndexOf(service);
171
172 if (pos != -1) {
173 url = url.substring(pos + service.length());
174 }
175
176 return url;
177 }
178
179 private static Log _log = LogFactoryUtil.getLog(SharepointUtil.class);
180
181 private static SharepointUtil _instance = new SharepointUtil();
182
183 private final Map<String, String> _storageMap;
184
185 }