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 _log.warn("Unable to get groupId for path " + path);
056 }
057
058 return groupId;
059 }
060
061 public static String[] getPathArray(String path) {
062 path = HttpUtil.fixPath(path, true, true);
063
064 return StringUtil.split(path, CharPool.SLASH);
065 }
066
067 public static SharepointStorage getStorage(String path) {
068 String storageClass = null;
069
070 if (path == null) {
071 return null;
072 }
073
074 String[] pathArray = getPathArray(path);
075
076 if (pathArray.length == 0) {
077 storageClass = CompanySharepointStorageImpl.class.getName();
078 }
079 else if (pathArray.length == 1) {
080 storageClass = GroupSharepointStorageImpl.class.getName();
081 }
082 else if (pathArray.length >= 2) {
083 storageClass = getStorageClass(pathArray[1]);
084 }
085
086 if (_log.isInfoEnabled()) {
087 _log.info("Storage class for path " + path + " is " + storageClass);
088 }
089
090 return (SharepointStorage)InstancePool.get(storageClass);
091 }
092
093 public static String getStorageClass(String token) {
094 return _instance._getStorageClass(token);
095 }
096
097 public static String getStorageToken(String className) {
098 return _instance._getStorageToken(className);
099 }
100
101 public static Collection<String> getStorageTokens() {
102 return _instance._getStorageTokens();
103 }
104
105 public static String replaceBackSlashes(String value) {
106 return value.replaceAll("\\\\", StringPool.BLANK);
107 }
108
109 public static String stripService(String url, boolean trailingSlash) {
110 return _instance._stripService(url, trailingSlash);
111 }
112
113 private SharepointUtil() {
114 _storageMap = new HashMap<String, String>();
115
116 String[] tokens = PropsUtil.getArray(
117 PropsKeys.SHAREPOINT_STORAGE_TOKENS);
118
119 for (String token : tokens) {
120 Filter filter = new Filter(token);
121
122 String className = PropsUtil.get(
123 PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
124
125 if (Validator.isNotNull(className)) {
126 _storageMap.put(className, token);
127 }
128 }
129 }
130
131 private String _getStorageClass(String token) {
132 for (Map.Entry<String, String> entry : _storageMap.entrySet()) {
133 String value = entry.getValue();
134
135 if (value.equals(token)) {
136 return entry.getKey();
137 }
138 }
139
140 return null;
141 }
142
143 private String _getStorageToken(String className) {
144 return _storageMap.get(className);
145 }
146
147 private Collection<String> _getStorageTokens() {
148 return _storageMap.values();
149 }
150
151 private String _stripService(String url, boolean trailingSlash) {
152 url = _stripService(url, "sharepoint", trailingSlash);
153 url = _stripService(url, "webdav", trailingSlash);
154
155 return url;
156 }
157
158 private String _stripService(
159 String url, String service, boolean trailingSlash) {
160
161 if (trailingSlash) {
162 service = service + StringPool.SLASH;
163 }
164 else {
165 service = StringPool.SLASH + service;
166 }
167
168 int pos = url.lastIndexOf(service);
169
170 if (pos != -1) {
171 url = url.substring(pos + service.length());
172 }
173
174 return url;
175 }
176
177 private static Log _log = LogFactoryUtil.getLog(SharepointUtil.class);
178
179 private static SharepointUtil _instance = new SharepointUtil();
180
181 private final Map<String, String> _storageMap;
182
183 }