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