1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PropsKeys;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
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  public class SharepointUtil {
44  
45      public static final String VEERMER_URLENCODED =
46          "application/x-vermeer-urlencoded";
47  
48      public static final String VERSION = "6.0.2.8117";
49  
50      public static void addBottom(StringBuilder sb) {
51          sb.append("</body>");
52          sb.append(StringPool.NEW_LINE);
53          sb.append("</html>");
54      }
55  
56      public static void addTop(StringBuilder sb, String methodName) {
57          sb.append("<html><head><title>vermeer RPC packet</title></head>");
58          sb.append(StringPool.NEW_LINE);
59          sb.append("<body>");
60          sb.append(StringPool.NEW_LINE);
61  
62          Property method = new Property("method", methodName + ":" + VERSION);
63  
64          sb.append(method.parse());
65      }
66  
67      public static long getGroupId(String path) {
68          long groupId = 0;
69  
70          String[] pathArray = getPathArray(path);
71  
72          String groupFolderName = pathArray[0];
73  
74          if (groupFolderName != null) {
75              int pos = groupFolderName.lastIndexOf(StringPool.OPEN_BRACKET);
76  
77              if (pos != -1) {
78                   groupId = GetterUtil.getLong(
79                      groupFolderName.substring(
80                          pos, groupFolderName.length() - 1));
81              }
82  
83          }
84  
85          return groupId;
86      }
87  
88      public static String[] getPathArray(String path) {
89          return StringUtil.split(path, StringPool.SLASH);
90      }
91  
92      public static SharepointStorage getStorage(String path) {
93          String storageClass = null;
94  
95          if (path == null) {
96              return null;
97          }
98  
99          String[] pathArray = getPathArray(path);
100 
101         if (pathArray.length == 0) {
102             storageClass = CompanySharepointStorageImpl.class.getName();
103         }
104         else if (pathArray.length == 1) {
105             storageClass = GroupSharepointStorageImpl.class.getName();
106         }
107         else if (pathArray.length >= 2) {
108             storageClass = getStorageClass(pathArray[1]);
109         }
110 
111         return (SharepointStorage)InstancePool.get(storageClass);
112     }
113 
114     public static String getStorageClass(String token) {
115         return _instance._getStorageClass(token);
116     }
117 
118     public static String getStorageToken(String className) {
119         return _instance._getStorageToken(className);
120     }
121 
122     public static Collection<String> getStorageTokens() {
123         return _instance._getStorageTokens();
124     }
125 
126     public static String replaceBackSlashes(String value) {
127         return value.replaceAll("\\\\", StringPool.BLANK);
128     }
129 
130     private SharepointUtil() {
131         _storageMap = new HashMap<String, String>();
132 
133         String[] tokens = PropsUtil.getArray(
134             PropsKeys.SHAREPOINT_STORAGE_TOKENS);
135 
136         for (String token: tokens) {
137             Filter filter = new Filter(token);
138 
139             String className = PropsUtil.get(
140                 PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
141 
142             if (Validator.isNotNull(className)) {
143                 _storageMap.put(className, token);
144             }
145         }
146     }
147 
148     private String _getStorageClass(String token) {
149         if (_storageMap.containsValue(token)) {
150             for (String key : _storageMap.keySet()) {
151                 if (_storageMap.get(key).equals(token)) {
152                     return key;
153                 }
154             }
155         }
156 
157         return null;
158     }
159 
160     private String _getStorageToken(String className) {
161         return _storageMap.get(className);
162     }
163 
164     private Collection<String> _getStorageTokens() {
165         return _storageMap.values();
166     }
167 
168     private static SharepointUtil _instance = new SharepointUtil();
169 
170     private final Map<String, String> _storageMap;
171 
172 }