View Javadoc

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.client.bindings;
20  
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.chemistry.opencmis.client.bindings.cache.TypeDefinitionCache;
26  import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingImpl;
27  import org.apache.chemistry.opencmis.commons.SessionParameter;
28  import org.apache.chemistry.opencmis.commons.SessionParameterDefaults;
29  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
30  import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
31  
32  /**
33   * Default factory for a CMIS binding instance.
34   */
35  public class CmisBindingFactory {
36  
37      /** Default CMIS AtomPub binding SPI implementation. */
38      public static final String BINDING_SPI_ATOMPUB = "org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubSpi";
39      /** Default CMIS Web Services binding SPI implementation. */
40      public static final String BINDING_SPI_WEBSERVICES = "org.apache.chemistry.opencmis.client.bindings.spi.webservices.CmisWebServicesSpi";
41      /** Default CMIS Browser binding SPI implementation. */
42      public static final String BINDING_SPI_BROWSER = "org.apache.chemistry.opencmis.client.bindings.spi.browser.CmisBrowserBindingSpi";
43      /** Default CMIS local binding SPI implementation. */
44      public static final String BINDING_SPI_LOCAL = "org.apache.chemistry.opencmis.client.bindings.spi.local.CmisLocalSpi";
45  
46      /** Default type definition cache class */
47      public static final String DEFAULT_TYPE_DEFINITION_CACHE_CLASS = "org.apache.chemistry.opencmis.client.bindings.impl.TypeDefinitionCacheImpl";
48      /** Default HTTP invoker class */
49      public static final String DEFAULT_HTTP_INVOKER = "org.apache.chemistry.opencmis.client.bindings.spi.http.DefaultHttpInvoker";
50      /** Standard authentication provider class. */
51      public static final String STANDARD_AUTHENTICATION_PROVIDER = "org.apache.chemistry.opencmis.client.bindings.spi.StandardAuthenticationProvider";
52      /** NTLM authentication provider class. */
53      public static final String NTLM_AUTHENTICATION_PROVIDER = "org.apache.chemistry.opencmis.client.bindings.spi.NTLMAuthenticationProvider";
54  
55      private Map<String, String> defaults;
56  
57      /**
58       * Constructor.
59       */
60      public CmisBindingFactory() {
61          defaults = createNewDefaultParameters();
62      }
63  
64      /**
65       * Creates a new factory instance.
66       * 
67       * @return a new factory instance
68       */
69      public static CmisBindingFactory newInstance() {
70          return new CmisBindingFactory();
71      }
72  
73      /**
74       * Returns the default session parameters.
75       * 
76       * @return the default session parameters
77       */
78      public Map<String, String> getDefaultSessionParameters() {
79          return defaults;
80      }
81  
82      /**
83       * Sets the default session parameters.
84       * 
85       * @param sessionParameters
86       *            the session parameters
87       */
88      public void setDefaultSessionParameters(Map<String, String> sessionParameters) {
89          if (sessionParameters == null) {
90              defaults = createNewDefaultParameters();
91          } else {
92              defaults = sessionParameters;
93          }
94      }
95  
96      /**
97       * Creates a CMIS binding instance. A binding class has to be provided in
98       * the session parameters.
99       * 
100      * @param sessionParameters
101      *            the session parameters
102      * @return the binding object
103      */
104     public CmisBinding createCmisBinding(Map<String, String> sessionParameters) {
105         return createCmisBinding(sessionParameters, null, null);
106     }
107 
108     /**
109      * Creates a CMIS binding instance. A binding class has to be provided in
110      * the session parameters.
111      * 
112      * @param sessionParameters
113      *            the session parameters
114      * @return the binding object
115      */
116     public CmisBinding createCmisBinding(Map<String, String> sessionParameters,
117             AuthenticationProvider authenticationProvider, TypeDefinitionCache typeDefCache) {
118         checkSessionParameters(sessionParameters, true);
119 
120         addDefaultParameters(sessionParameters);
121 
122         return new CmisBindingImpl(sessionParameters, authenticationProvider, typeDefCache);
123     }
124 
125     /**
126      * Creates a default CMIS AtomPub binding instance.
127      * 
128      * @param sessionParameters
129      *            the session parameters
130      * @return the binding object
131      */
132     public CmisBinding createCmisAtomPubBinding(Map<String, String> sessionParameters) {
133         return createCmisAtomPubBinding(sessionParameters, null, null);
134     }
135 
136     /**
137      * Creates a default CMIS AtomPub binding instance with a custom
138      * authentication provider.
139      * 
140      * @param sessionParameters
141      *            the session parameters
142      * @return the binding object
143      */
144     public CmisBinding createCmisAtomPubBinding(Map<String, String> sessionParameters,
145             AuthenticationProvider authenticationProvider, TypeDefinitionCache typeDefCache) {
146         checkSessionParameters(sessionParameters, false);
147 
148         sessionParameters.put(SessionParameter.BINDING_SPI_CLASS, BINDING_SPI_ATOMPUB);
149         if (!sessionParameters.containsKey(SessionParameter.HTTP_INVOKER_CLASS)) {
150             sessionParameters.put(SessionParameter.HTTP_INVOKER_CLASS, DEFAULT_HTTP_INVOKER);
151         }
152         if (authenticationProvider == null) {
153             if (!sessionParameters.containsKey(SessionParameter.AUTHENTICATION_PROVIDER_CLASS)) {
154                 sessionParameters.put(SessionParameter.AUTHENTICATION_PROVIDER_CLASS, STANDARD_AUTHENTICATION_PROVIDER);
155             }
156         }
157         if (typeDefCache == null) {
158             if (!sessionParameters.containsKey(SessionParameter.TYPE_DEFINITION_CACHE_CLASS)) {
159                 sessionParameters
160                         .put(SessionParameter.TYPE_DEFINITION_CACHE_CLASS, DEFAULT_TYPE_DEFINITION_CACHE_CLASS);
161             }
162         }
163         if (!sessionParameters.containsKey(SessionParameter.AUTH_HTTP_BASIC)) {
164             sessionParameters.put(SessionParameter.AUTH_HTTP_BASIC, "true");
165         }
166         sessionParameters.put(SessionParameter.AUTH_SOAP_USERNAMETOKEN, "false");
167         addDefaultParameters(sessionParameters);
168 
169         check(sessionParameters, SessionParameter.ATOMPUB_URL);
170 
171         return new CmisBindingImpl(sessionParameters, authenticationProvider, typeDefCache);
172     }
173 
174     /**
175      * Creates a default CMIS Web Services binding instance.
176      * 
177      * @param sessionParameters
178      *            the session parameters
179      * @return the binding object
180      */
181     public CmisBinding createCmisWebServicesBinding(Map<String, String> sessionParameters) {
182         return createCmisWebServicesBinding(sessionParameters, null, null);
183     }
184 
185     /**
186      * Creates a default CMIS Web Services binding instance with a custom
187      * authentication provider.
188      * 
189      * @param sessionParameters
190      *            the session parameters
191      * @return the binding object
192      */
193     public CmisBinding createCmisWebServicesBinding(Map<String, String> sessionParameters,
194             AuthenticationProvider authenticationProvider, TypeDefinitionCache typeDefCache) {
195         checkSessionParameters(sessionParameters, false);
196 
197         sessionParameters.put(SessionParameter.BINDING_SPI_CLASS, BINDING_SPI_WEBSERVICES);
198         if (!sessionParameters.containsKey(SessionParameter.HTTP_INVOKER_CLASS)) {
199             sessionParameters.put(SessionParameter.HTTP_INVOKER_CLASS, DEFAULT_HTTP_INVOKER);
200         }
201         if (authenticationProvider == null) {
202             if (!sessionParameters.containsKey(SessionParameter.AUTHENTICATION_PROVIDER_CLASS)) {
203                 sessionParameters.put(SessionParameter.AUTHENTICATION_PROVIDER_CLASS, STANDARD_AUTHENTICATION_PROVIDER);
204             }
205         }
206         if (typeDefCache == null) {
207             if (!sessionParameters.containsKey(SessionParameter.TYPE_DEFINITION_CACHE_CLASS)) {
208                 sessionParameters
209                         .put(SessionParameter.TYPE_DEFINITION_CACHE_CLASS, DEFAULT_TYPE_DEFINITION_CACHE_CLASS);
210             }
211         }
212         if (!sessionParameters.containsKey(SessionParameter.AUTH_SOAP_USERNAMETOKEN)) {
213             sessionParameters.put(SessionParameter.AUTH_SOAP_USERNAMETOKEN, "true");
214         }
215         if (!sessionParameters.containsKey(SessionParameter.AUTH_HTTP_BASIC)) {
216             sessionParameters.put(SessionParameter.AUTH_HTTP_BASIC, "true");
217         }
218         addDefaultParameters(sessionParameters);
219 
220         check(sessionParameters, SessionParameter.WEBSERVICES_ACL_SERVICE,
221                 SessionParameter.WEBSERVICES_ACL_SERVICE_ENDPOINT);
222         check(sessionParameters, SessionParameter.WEBSERVICES_DISCOVERY_SERVICE,
223                 SessionParameter.WEBSERVICES_DISCOVERY_SERVICE_ENDPOINT);
224         check(sessionParameters, SessionParameter.WEBSERVICES_MULTIFILING_SERVICE,
225                 SessionParameter.WEBSERVICES_MULTIFILING_SERVICE_ENDPOINT);
226         check(sessionParameters, SessionParameter.WEBSERVICES_NAVIGATION_SERVICE,
227                 SessionParameter.WEBSERVICES_NAVIGATION_SERVICE_ENDPOINT);
228         check(sessionParameters, SessionParameter.WEBSERVICES_OBJECT_SERVICE,
229                 SessionParameter.WEBSERVICES_OBJECT_SERVICE_ENDPOINT);
230         check(sessionParameters, SessionParameter.WEBSERVICES_POLICY_SERVICE,
231                 SessionParameter.WEBSERVICES_POLICY_SERVICE_ENDPOINT);
232         check(sessionParameters, SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE,
233                 SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE_ENDPOINT);
234         check(sessionParameters, SessionParameter.WEBSERVICES_REPOSITORY_SERVICE,
235                 SessionParameter.WEBSERVICES_REPOSITORY_SERVICE_ENDPOINT);
236         check(sessionParameters, SessionParameter.WEBSERVICES_VERSIONING_SERVICE,
237                 SessionParameter.WEBSERVICES_VERSIONING_SERVICE_ENDPOINT);
238 
239         return new CmisBindingImpl(sessionParameters, authenticationProvider, typeDefCache);
240     }
241 
242     /**
243      * Creates a default CMIS Browser binding instance.
244      * 
245      * @param sessionParameters
246      *            the session parameters
247      * @return the binding object
248      */
249     public CmisBinding createCmisBrowserBinding(Map<String, String> sessionParameters) {
250         return createCmisBrowserBinding(sessionParameters, null, null);
251     }
252 
253     /**
254      * Creates a default CMIS Browser binding instance with a custom
255      * authentication provider.
256      * 
257      * @param sessionParameters
258      *            the session parameters
259      * @return the binding object
260      */
261     public CmisBinding createCmisBrowserBinding(Map<String, String> sessionParameters,
262             AuthenticationProvider authenticationProvider, TypeDefinitionCache typeDefCache) {
263         checkSessionParameters(sessionParameters, false);
264 
265         sessionParameters.put(SessionParameter.BINDING_SPI_CLASS, BINDING_SPI_BROWSER);
266         if (!sessionParameters.containsKey(SessionParameter.HTTP_INVOKER_CLASS)) {
267             sessionParameters.put(SessionParameter.HTTP_INVOKER_CLASS, DEFAULT_HTTP_INVOKER);
268         }
269         if (authenticationProvider == null) {
270             if (!sessionParameters.containsKey(SessionParameter.AUTHENTICATION_PROVIDER_CLASS)) {
271                 sessionParameters.put(SessionParameter.AUTHENTICATION_PROVIDER_CLASS, STANDARD_AUTHENTICATION_PROVIDER);
272             }
273         }
274         if (typeDefCache == null) {
275             if (!sessionParameters.containsKey(SessionParameter.TYPE_DEFINITION_CACHE_CLASS)) {
276                 sessionParameters
277                         .put(SessionParameter.TYPE_DEFINITION_CACHE_CLASS, DEFAULT_TYPE_DEFINITION_CACHE_CLASS);
278             }
279         }
280         if (!sessionParameters.containsKey(SessionParameter.BROWSER_SUCCINCT)) {
281             sessionParameters.put(SessionParameter.BROWSER_SUCCINCT, "true");
282         }
283         if (!sessionParameters.containsKey(SessionParameter.AUTH_HTTP_BASIC)) {
284             sessionParameters.put(SessionParameter.AUTH_HTTP_BASIC, "true");
285         }
286         sessionParameters.put(SessionParameter.AUTH_SOAP_USERNAMETOKEN, "false");
287         addDefaultParameters(sessionParameters);
288 
289         check(sessionParameters, SessionParameter.BROWSER_URL);
290 
291         return new CmisBindingImpl(sessionParameters, authenticationProvider, typeDefCache);
292     }
293 
294     /**
295      * Creates a default CMIS local binding instance.
296      * 
297      * @param sessionParameters
298      *            the session parameters
299      * @return the binding object
300      */
301     public CmisBinding createCmisLocalBinding(Map<String, String> sessionParameters) {
302         return createCmisLocalBinding(sessionParameters, null);
303     }
304 
305     /**
306      * Creates a default CMIS local binding instance.
307      * 
308      * @param sessionParameters
309      *            the session parameters
310      * @return the binding object
311      */
312     public CmisBinding createCmisLocalBinding(Map<String, String> sessionParameters, TypeDefinitionCache typeDefCache) {
313         checkSessionParameters(sessionParameters, false);
314 
315         sessionParameters.put(SessionParameter.BINDING_SPI_CLASS, BINDING_SPI_LOCAL);
316         if (typeDefCache == null) {
317             if (!sessionParameters.containsKey(SessionParameter.TYPE_DEFINITION_CACHE_CLASS)) {
318                 sessionParameters
319                         .put(SessionParameter.TYPE_DEFINITION_CACHE_CLASS, DEFAULT_TYPE_DEFINITION_CACHE_CLASS);
320             }
321         }
322         addDefaultParameters(sessionParameters);
323 
324         check(sessionParameters, SessionParameter.LOCAL_FACTORY);
325 
326         return new CmisBindingImpl(sessionParameters);
327     }
328 
329     // ---- internal ----
330 
331     /**
332      * Checks the passed session parameters.
333      */
334     private static void checkSessionParameters(Map<String, String> sessionParameters, boolean mustContainSPI) {
335         // don't accept null
336         if (sessionParameters == null) {
337             throw new IllegalArgumentException("Session parameter map not set!");
338         }
339 
340         // check binding entry
341         final String spiClass = sessionParameters.get(SessionParameter.BINDING_SPI_CLASS);
342         if (mustContainSPI) {
343             if ((spiClass == null) || (spiClass.trim().length() == 0)) {
344                 throw new IllegalArgumentException("SPI class entry (" + SessionParameter.BINDING_SPI_CLASS
345                         + ") is missing!");
346             }
347         }
348     }
349 
350     /**
351      * Checks if the given parameter is present. If not, throw an
352      * <code>IllegalArgumentException</code>.
353      */
354     private static void check(Map<String, String> sessionParameters, String... parameters) {
355         for (String parameter : parameters) {
356             if (sessionParameters.containsKey(parameter)) {
357                 return;
358             }
359         }
360 
361         if (parameters.length == 1) {
362             throw new IllegalArgumentException("Parameter '" + parameters[0] + "' is missing!");
363         } else {
364             throw new IllegalArgumentException("One of the following parameters must be set: "
365                     + Arrays.asList(parameters).toString());
366         }
367     }
368 
369     /**
370      * Add the default session parameters to the given map without override
371      * existing entries.
372      */
373     private void addDefaultParameters(Map<String, String> sessionParameters) {
374         for (String key : defaults.keySet()) {
375             if (!sessionParameters.containsKey(key)) {
376                 sessionParameters.put(key, defaults.get(key));
377             }
378         }
379     }
380 
381     /**
382      * Creates a default session parameters map with some reasonable defaults.
383      */
384     private static Map<String, String> createNewDefaultParameters() {
385         Map<String, String> result = new HashMap<String, String>();
386 
387         result.put(SessionParameter.CACHE_SIZE_REPOSITORIES,
388                 String.valueOf(SessionParameterDefaults.CACHE_SIZE_REPOSITORIES));
389         result.put(SessionParameter.CACHE_SIZE_TYPES, String.valueOf(SessionParameterDefaults.CACHE_SIZE_TYPES));
390         result.put(SessionParameter.CACHE_SIZE_LINKS, String.valueOf(SessionParameterDefaults.CACHE_SIZE_LINKS));
391 
392         return result;
393     }
394 }