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