001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.GetterUtil;
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    /**
037     * @author Bruno Farache
038     */
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 void addBottom(StringBuilder sb) {
047                    sb.append("</body>");
048                    sb.append(StringPool.NEW_LINE);
049                    sb.append("</html>");
050            }
051    
052            public static void addTop(StringBuilder sb, String methodName) {
053                    sb.append("<html><head><title>vermeer RPC packet</title></head>");
054                    sb.append(StringPool.NEW_LINE);
055                    sb.append("<body>");
056                    sb.append(StringPool.NEW_LINE);
057    
058                    Property method = new Property("method", methodName + ":" + VERSION);
059    
060                    sb.append(method.parse());
061            }
062    
063            public static long getGroupId(String path) {
064                    long groupId = 0;
065    
066                    String[] pathArray = getPathArray(path);
067    
068                    String groupFolderName = pathArray[0];
069    
070                    if (groupFolderName != null) {
071                            int pos = groupFolderName.lastIndexOf(CharPool.OPEN_BRACKET);
072    
073                            if (pos != -1) {
074                                     groupId = GetterUtil.getLong(
075                                            groupFolderName.substring(
076                                                    pos, groupFolderName.length() - 1));
077                            }
078                            else {
079                                    long companyId = CompanyThreadLocal.getCompanyId();
080    
081                                    try {
082                                            groupId = WebDAVUtil.getGroupId(companyId, path);
083                                    }
084                                    catch (WebDAVException wde) {
085                                            _log.warn("Unable to get groupId for path " + path);
086                                    }
087                            }
088    
089                    }
090    
091                    return groupId;
092            }
093    
094            public static String[] getPathArray(String path) {
095                    return StringUtil.split(path, CharPool.SLASH);
096            }
097    
098            public static SharepointStorage getStorage(String path) {
099                    String storageClass = null;
100    
101                    if (path == null) {
102                            return null;
103                    }
104    
105                    String[] pathArray = getPathArray(path);
106    
107                    if (pathArray.length == 0) {
108                            storageClass = CompanySharepointStorageImpl.class.getName();
109                    }
110                    else if (pathArray.length == 1) {
111                            storageClass = GroupSharepointStorageImpl.class.getName();
112                    }
113                    else if (pathArray.length >= 2) {
114                            storageClass = getStorageClass(pathArray[1]);
115                    }
116    
117                    return (SharepointStorage)InstancePool.get(storageClass);
118            }
119    
120            public static String getStorageClass(String token) {
121                    return _instance._getStorageClass(token);
122            }
123    
124            public static String getStorageToken(String className) {
125                    return _instance._getStorageToken(className);
126            }
127    
128            public static Collection<String> getStorageTokens() {
129                    return _instance._getStorageTokens();
130            }
131    
132            public static String replaceBackSlashes(String value) {
133                    return value.replaceAll("\\\\", StringPool.BLANK);
134            }
135    
136            private SharepointUtil() {
137                    _storageMap = new HashMap<String, String>();
138    
139                    String[] tokens = PropsUtil.getArray(
140                            PropsKeys.SHAREPOINT_STORAGE_TOKENS);
141    
142                    for (String token: tokens) {
143                            Filter filter = new Filter(token);
144    
145                            String className = PropsUtil.get(
146                                    PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
147    
148                            if (Validator.isNotNull(className)) {
149                                    _storageMap.put(className, token);
150                            }
151                    }
152            }
153    
154            private String _getStorageClass(String token) {
155                    for (Map.Entry<String, String> entry : _storageMap.entrySet()) {
156                            String value = entry.getValue();
157    
158                            if (value.equals(token)) {
159                                    return entry.getKey();
160                            }
161                    }
162    
163                    return null;
164            }
165    
166            private String _getStorageToken(String className) {
167                    return _storageMap.get(className);
168            }
169    
170            private Collection<String> _getStorageTokens() {
171                    return _storageMap.values();
172            }
173    
174            private static Log _log = LogFactoryUtil.getLog(SharepointUtil.class);
175    
176            private static SharepointUtil _instance = new SharepointUtil();
177    
178            private final Map<String, String> _storageMap;
179    
180    }