1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.sharepoint;
24  
25  import com.liferay.portal.kernel.configuration.Filter;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.InstancePool;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsUtil;
33  
34  import java.util.Collection;
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  /**
39   * <a href="SharepointUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Bruno Farache
42   *
43   */
44  public class SharepointUtil {
45  
46      public static final String VEERMER_URLENCODED =
47          "application/x-vermeer-urlencoded";
48  
49      public static final String VERSION = "6.0.2.8117";
50  
51      public static void addBottom(StringBuilder sb) {
52          sb.append("</body>");
53          sb.append(StringPool.NEW_LINE);
54          sb.append("</html>");
55      }
56  
57      public static void addTop(StringBuilder sb, String methodName) {
58          sb.append("<html><head><title>vermeer RPC packet</title></head>");
59          sb.append(StringPool.NEW_LINE);
60          sb.append("<body>");
61          sb.append(StringPool.NEW_LINE);
62  
63          Property method = new Property("method", methodName + ":" + VERSION);
64  
65          sb.append(method.parse());
66      }
67  
68      public static long getGroupId(String path) {
69          long groupId = 0;
70  
71          String[] pathArray = getPathArray(path);
72  
73          String groupFolderName = pathArray[0];
74  
75          if (groupFolderName != null) {
76              int pos = groupFolderName.lastIndexOf(StringPool.OPEN_BRACKET);
77  
78              if (pos != -1) {
79                   groupId = GetterUtil.getLong(
80                      groupFolderName.substring(
81                          pos, groupFolderName.length() - 1));
82              }
83  
84          }
85  
86          return groupId;
87      }
88  
89      public static String[] getPathArray(String path) {
90          return StringUtil.split(path, StringPool.SLASH);
91      }
92  
93      public static SharepointStorage getStorage(String path) {
94          String storageClass = null;
95  
96          if (path == null) {
97              return null;
98          }
99  
100         String[] pathArray = getPathArray(path);
101 
102         if (pathArray.length == 0) {
103             storageClass = CompanySharepointStorageImpl.class.getName();
104         }
105         else if (pathArray.length == 1) {
106             storageClass = GroupSharepointStorageImpl.class.getName();
107         }
108         else if (pathArray.length >= 2) {
109             storageClass = getStorageClass(pathArray[1]);
110         }
111 
112         return (SharepointStorage)InstancePool.get(storageClass);
113     }
114 
115     public static String getStorageClass(String token) {
116         return _instance._getStorageClass(token);
117     }
118 
119     public static String getStorageToken(String className) {
120         return _instance._getStorageToken(className);
121     }
122 
123     public static Collection<String> getStorageTokens() {
124         return _instance._getStorageTokens();
125     }
126 
127     public static String replaceBackSlashes(String value) {
128         return value.replaceAll("\\\\", StringPool.BLANK);
129     }
130 
131     private SharepointUtil() {
132         _storageMap = new HashMap<String, String>();
133 
134         String[] tokens = PropsUtil.getArray(
135             PropsKeys.SHAREPOINT_STORAGE_TOKENS);
136 
137         for (String token: tokens) {
138             Filter filter = new Filter(token);
139 
140             String className = PropsUtil.get(
141                 PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
142 
143             if (Validator.isNotNull(className)) {
144                 _storageMap.put(className, token);
145             }
146         }
147     }
148 
149     private String _getStorageClass(String token) {
150         if (_storageMap.containsValue(token)) {
151             for (String key : _storageMap.keySet()) {
152                 if (_storageMap.get(key).equals(token)) {
153                     return key;
154                 }
155             }
156         }
157 
158         return null;
159     }
160 
161     private String _getStorageToken(String className) {
162         return _storageMap.get(className);
163     }
164 
165     private Collection<String> _getStorageTokens() {
166         return _storageMap.values();
167     }
168 
169     private static SharepointUtil _instance = new SharepointUtil();
170 
171     private final Map<String, String> _storageMap;
172 
173 }