This project has retired. For details please refer to its Attic page.
ClientSession xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench.model;
20  
21  import java.net.Authenticator;
22  import java.security.cert.X509Certificate;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.Set;
30  
31  import javax.net.ssl.HttpsURLConnection;
32  import javax.net.ssl.SSLContext;
33  import javax.net.ssl.TrustManager;
34  import javax.net.ssl.X509TrustManager;
35  
36  import org.apache.chemistry.opencmis.client.api.ObjectType;
37  import org.apache.chemistry.opencmis.client.api.OperationContext;
38  import org.apache.chemistry.opencmis.client.api.Repository;
39  import org.apache.chemistry.opencmis.client.api.Session;
40  import org.apache.chemistry.opencmis.client.bindings.CmisBindingFactory;
41  import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
42  import org.apache.chemistry.opencmis.commons.PropertyIds;
43  import org.apache.chemistry.opencmis.commons.SessionParameter;
44  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
45  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
46  import org.apache.chemistry.opencmis.commons.enums.BindingType;
47  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
48  
49  public class ClientSession {
50  
51      public static final String WORKBENCH_PREFIX = "cmis.workbench.";
52      public static final String OBJECT_PREFIX = WORKBENCH_PREFIX + "object.";
53      public static final String FOLDER_PREFIX = WORKBENCH_PREFIX + "folder.";
54      public static final String VERSION_PREFIX = WORKBENCH_PREFIX + "version.";
55      public static final String ACCEPT_SELF_SIGNED_CERTIFICATES = WORKBENCH_PREFIX + "acceptSelfSignedCertificates";
56  
57      public enum Authentication {
58          NONE, STANDARD, NTLM
59      }
60  
61      private static final Set<String> FOLDER_PROPERTY_SET = new HashSet<String>();
62      static {
63          FOLDER_PROPERTY_SET.add(PropertyIds.OBJECT_ID);
64          FOLDER_PROPERTY_SET.add(PropertyIds.OBJECT_TYPE_ID);
65          FOLDER_PROPERTY_SET.add(PropertyIds.NAME);
66          FOLDER_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_MIME_TYPE);
67          FOLDER_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_LENGTH);
68          FOLDER_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_FILE_NAME);
69          FOLDER_PROPERTY_SET.add(PropertyIds.CREATED_BY);
70          FOLDER_PROPERTY_SET.add(PropertyIds.CREATION_DATE);
71          FOLDER_PROPERTY_SET.add(PropertyIds.LAST_MODIFIED_BY);
72          FOLDER_PROPERTY_SET.add(PropertyIds.LAST_MODIFICATION_DATE);
73          FOLDER_PROPERTY_SET.add(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
74          FOLDER_PROPERTY_SET.add(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
75      }
76  
77      private static final Set<String> VERSION_PROPERTY_SET = new HashSet<String>();
78      static {
79          VERSION_PROPERTY_SET.add(PropertyIds.OBJECT_ID);
80          VERSION_PROPERTY_SET.add(PropertyIds.OBJECT_TYPE_ID);
81          VERSION_PROPERTY_SET.add(PropertyIds.NAME);
82          VERSION_PROPERTY_SET.add(PropertyIds.VERSION_LABEL);
83          VERSION_PROPERTY_SET.add(PropertyIds.IS_LATEST_VERSION);
84          VERSION_PROPERTY_SET.add(PropertyIds.IS_MAJOR_VERSION);
85          VERSION_PROPERTY_SET.add(PropertyIds.IS_LATEST_MAJOR_VERSION);
86          VERSION_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_MIME_TYPE);
87          VERSION_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_LENGTH);
88          VERSION_PROPERTY_SET.add(PropertyIds.CONTENT_STREAM_FILE_NAME);
89      }
90  
91      private Map<String, String> sessionParameters;
92      private List<Repository> repositories;
93      private Session session;
94      private OperationContext objectOperationContext;
95      private OperationContext folderOperationContext;
96      private OperationContext versionOperationContext;
97  
98      public ClientSession(Map<String, String> sessionParameters) {
99          if (sessionParameters == null) {
100             throw new IllegalArgumentException("Parameters must not be null!");
101         }
102 
103         connect(sessionParameters);
104     }
105 
106     public static Map<String, String> createSessionParameters(String url, BindingType binding, String username,
107             String password, Authentication authentication, boolean compression, boolean clientCompression,
108             boolean cookies) {
109         Map<String, String> parameters = new LinkedHashMap<String, String>();
110 
111         switch (binding) {
112         case WEBSERVICES:
113             parameters.put(SessionParameter.BINDING_TYPE, BindingType.WEBSERVICES.value());
114             parameters.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, url);
115             parameters.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, url);
116             parameters.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, url);
117             parameters.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, url);
118             parameters.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, url);
119             parameters.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, url);
120             parameters.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, url);
121             parameters.put(SessionParameter.WEBSERVICES_ACL_SERVICE, url);
122             parameters.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, url);
123             break;
124         case ATOMPUB:
125             parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
126             parameters.put(SessionParameter.ATOMPUB_URL, url);
127             break;
128         case BROWSER:
129             parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());
130             parameters.put(SessionParameter.BROWSER_URL, url);
131             break;
132         default:
133             parameters.put(SessionParameter.BINDING_TYPE, BindingType.CUSTOM.value());
134         }
135 
136         switch (authentication) {
137         case STANDARD:
138             parameters.put(SessionParameter.USER, username);
139             parameters.put(SessionParameter.PASSWORD, password);
140             break;
141         case NTLM:
142             parameters.put(SessionParameter.USER, username);
143             parameters.put(SessionParameter.PASSWORD, password);
144             parameters.put(SessionParameter.AUTHENTICATION_PROVIDER_CLASS,
145                     CmisBindingFactory.NTLM_AUTHENTICATION_PROVIDER);
146             break;
147         }
148 
149         if (compression) {
150             parameters.put(SessionParameter.COMPRESSION, "true");
151         }
152 
153         if (clientCompression) {
154             parameters.put(SessionParameter.CLIENT_COMPRESSION, "true");
155         }
156 
157         if (cookies) {
158             parameters.put(SessionParameter.COOKIES, "true");
159         }
160 
161         // get additional workbench properties from system properties
162         Properties sysProps = System.getProperties();
163         for (String key : sysProps.stringPropertyNames()) {
164             if (key.startsWith(WORKBENCH_PREFIX)) {
165                 parameters.put(key, sysProps.getProperty(key));
166             }
167         }
168 
169         return parameters;
170     }
171 
172     private void connect(Map<String, String> sessionParameters) {
173         this.sessionParameters = sessionParameters;
174 
175         // set a new dummy authenticator
176         // don't send previous credentials to another server
177         Authenticator.setDefault(new Authenticator() {
178         });
179 
180         if (Boolean.parseBoolean(sessionParameters.get(ACCEPT_SELF_SIGNED_CERTIFICATES))) {
181             acceptSelfSignedCertificates();
182         }
183 
184         repositories = SessionFactoryImpl.newInstance().getRepositories(sessionParameters);
185     }
186 
187     public List<Repository> getRepositories() {
188         return repositories;
189     }
190 
191     public Session createSession(int index) {
192         session = repositories.get(index).createSession();
193         createOperationContexts();
194         return getSession();
195     }
196 
197     public Session getSession() {
198         return session;
199     }
200 
201     public Map<String, String> getSessionParameters() {
202         return Collections.unmodifiableMap(sessionParameters);
203     }
204 
205     public OperationContext getObjectOperationContext() {
206         return objectOperationContext;
207     }
208 
209     public OperationContext getFolderOperationContext() {
210         return folderOperationContext;
211     }
212 
213     public OperationContext getVersionOperationContext() {
214         return versionOperationContext;
215     }
216 
217     private void createOperationContexts() {
218         // object operation context
219         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.FILTER, "*");
220         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ACLS, "true");
221         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ALLOWABLE_ACTIONS, "true");
222         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_POLICIES, "true");
223         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_RELATIONSHIPS,
224                 IncludeRelationships.BOTH.value());
225         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.RENDITION_FILTER, "*");
226         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.ORDER_BY, null);
227         setDefault(OBJECT_PREFIX, sessionParameters, ClientOperationContext.MAX_ITEMS_PER_PAGE, "1000");
228 
229         objectOperationContext = new ClientOperationContext(OBJECT_PREFIX, sessionParameters);
230 
231         // folder operation context
232         if (!sessionParameters.containsKey(FOLDER_PREFIX + ClientOperationContext.FILTER)) {
233             ObjectType type = session.getTypeDefinition(BaseTypeId.CMIS_DOCUMENT.value());
234 
235             StringBuilder filter = new StringBuilder();
236             for (String propId : FOLDER_PROPERTY_SET) {
237                 PropertyDefinition<?> propDef = type.getPropertyDefinitions().get(propId);
238                 if (propDef != null) {
239                     if (filter.length() > 0) {
240                         filter.append(",");
241                     }
242                     filter.append(propDef.getQueryName());
243                 }
244             }
245 
246             sessionParameters.put(FOLDER_PREFIX + ClientOperationContext.FILTER, filter.toString());
247         }
248 
249         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ACLS, "false");
250         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ALLOWABLE_ACTIONS, "false");
251         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_POLICIES, "false");
252         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_RELATIONSHIPS,
253                 IncludeRelationships.NONE.value());
254         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.RENDITION_FILTER, "cmis:none");
255         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.ORDER_BY, null);
256         setDefault(FOLDER_PREFIX, sessionParameters, ClientOperationContext.MAX_ITEMS_PER_PAGE, "10000");
257 
258         folderOperationContext = new ClientOperationContext(FOLDER_PREFIX, sessionParameters);
259 
260         if (!sessionParameters.containsKey(VERSION_PREFIX + ClientOperationContext.FILTER)) {
261             ObjectType type = session.getTypeDefinition(BaseTypeId.CMIS_DOCUMENT.value());
262 
263             StringBuilder filter = new StringBuilder();
264             for (String propId : VERSION_PROPERTY_SET) {
265                 PropertyDefinition<?> propDef = type.getPropertyDefinitions().get(propId);
266                 if (propDef != null) {
267                     if (filter.length() > 0) {
268                         filter.append(",");
269                     }
270                     filter.append(propDef.getQueryName());
271                 }
272             }
273 
274             sessionParameters.put(VERSION_PREFIX + ClientOperationContext.FILTER, filter.toString());
275         }
276 
277         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ACLS, "false");
278         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_ALLOWABLE_ACTIONS, "false");
279         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_POLICIES, "false");
280         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.INCLUDE_RELATIONSHIPS,
281                 IncludeRelationships.NONE.value());
282         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.RENDITION_FILTER, "cmis:none");
283         setDefault(VERSION_PREFIX, sessionParameters, ClientOperationContext.MAX_ITEMS_PER_PAGE, "10000");
284 
285         versionOperationContext = new ClientOperationContext(VERSION_PREFIX, sessionParameters);
286     }
287 
288     private void setDefault(String prefix, Map<String, String> map, String key, String value) {
289         if (!map.containsKey(prefix + key)) {
290             map.put(prefix + key, value);
291         }
292     }
293 
294     private void acceptSelfSignedCertificates() {
295         TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
296             public X509Certificate[] getAcceptedIssuers() {
297                 return null;
298             }
299 
300             public void checkClientTrusted(X509Certificate[] certs, String authType) {
301             }
302 
303             public void checkServerTrusted(X509Certificate[] certs, String authType) {
304             }
305         } };
306 
307         try {
308             SSLContext sc = SSLContext.getInstance("SSL");
309             sc.init(null, trustAllCerts, new java.security.SecureRandom());
310             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
311         } catch (Exception e) {
312         }
313     }
314 }