001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.InstancePool;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.webdav.WebDAVException;
027    import com.liferay.portal.kernel.webdav.WebDAVUtil;
028    import com.liferay.portal.security.auth.CompanyThreadLocal;
029    import com.liferay.portal.util.PropsUtil;
030    
031    import java.util.Collection;
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    /**
036     * @author Bruno Farache
037     */
038    public class SharepointUtil {
039    
040            public static final String VEERMER_URLENCODED =
041                    "application/x-vermeer-urlencoded";
042    
043            public static final String VERSION = "6.0.2.8117";
044    
045            public static long getGroupId(String path) {
046                    long groupId = 0;
047    
048                    long companyId = CompanyThreadLocal.getCompanyId();
049    
050                    try {
051                            groupId = WebDAVUtil.getGroupId(companyId, path);
052                    }
053                    catch (WebDAVException wde) {
054                            _log.warn("Unable to get groupId for path " + path);
055                    }
056    
057                    return groupId;
058            }
059    
060            public static String[] getPathArray(String path) {
061                    return StringUtil.split(path, CharPool.SLASH);
062            }
063    
064            public static SharepointStorage getStorage(String path) {
065                    String storageClass = null;
066    
067                    if (path == null) {
068                            return null;
069                    }
070    
071                    String[] pathArray = getPathArray(path);
072    
073                    if (pathArray.length == 0) {
074                            storageClass = CompanySharepointStorageImpl.class.getName();
075                    }
076                    else if (pathArray.length == 1) {
077                            storageClass = GroupSharepointStorageImpl.class.getName();
078                    }
079                    else if (pathArray.length >= 2) {
080                            storageClass = getStorageClass(pathArray[1]);
081                    }
082    
083                    if (_log.isInfoEnabled()) {
084                            _log.info("Storage class for path " + path + " is " + storageClass);
085                    }
086    
087                    return (SharepointStorage)InstancePool.get(storageClass);
088            }
089    
090            public static String getStorageClass(String token) {
091                    return _instance._getStorageClass(token);
092            }
093    
094            public static String getStorageToken(String className) {
095                    return _instance._getStorageToken(className);
096            }
097    
098            public static Collection<String> getStorageTokens() {
099                    return _instance._getStorageTokens();
100            }
101    
102            public static String replaceBackSlashes(String value) {
103                    return value.replaceAll("\\\\", StringPool.BLANK);
104            }
105    
106            private SharepointUtil() {
107                    _storageMap = new HashMap<String, String>();
108    
109                    String[] tokens = PropsUtil.getArray(
110                            PropsKeys.SHAREPOINT_STORAGE_TOKENS);
111    
112                    for (String token : tokens) {
113                            Filter filter = new Filter(token);
114    
115                            String className = PropsUtil.get(
116                                    PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
117    
118                            if (Validator.isNotNull(className)) {
119                                    _storageMap.put(className, token);
120                            }
121                    }
122            }
123    
124            private String _getStorageClass(String token) {
125                    for (Map.Entry<String, String> entry : _storageMap.entrySet()) {
126                            String value = entry.getValue();
127    
128                            if (value.equals(token)) {
129                                    return entry.getKey();
130                            }
131                    }
132    
133                    return null;
134            }
135    
136            private String _getStorageToken(String className) {
137                    return _storageMap.get(className);
138            }
139    
140            private Collection<String> _getStorageTokens() {
141                    return _storageMap.values();
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(SharepointUtil.class);
145    
146            private static SharepointUtil _instance = new SharepointUtil();
147    
148            private final Map<String, String> _storageMap;
149    
150    }