View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   *
20   */
21  package org.apache.chemistry.opencmis.client.bindings.spi.local;
22  
23  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
24  import org.apache.chemistry.opencmis.commons.SessionParameter;
25  import org.apache.chemistry.opencmis.commons.server.CallContext;
26  import org.apache.chemistry.opencmis.commons.server.CmisService;
27  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
28  import org.apache.chemistry.opencmis.commons.server.ProgressControlCmisService;
29  import org.apache.chemistry.opencmis.commons.server.ProgressControlCmisService.Progress;
30  
31  /**
32   * Base class for all local clients.
33   */
34  public abstract class AbstractLocalService {
35  
36      private BindingSession session;
37      private CmisServiceFactory factory;
38  
39      private String user;
40      private String password;
41      private String language;
42      private String country;
43  
44      /**
45       * Sets the current session.
46       */
47      protected void setSession(BindingSession session) {
48          this.session = session;
49  
50          Object userObj = session.get(SessionParameter.USER);
51          user = userObj instanceof String ? userObj.toString() : null;
52  
53          Object passwordObj = session.get(SessionParameter.PASSWORD);
54          password = passwordObj instanceof String ? passwordObj.toString() : null;
55  
56          Object localeLanguageObj = session.get(SessionParameter.LOCALE_ISO639_LANGUAGE);
57          language = localeLanguageObj instanceof String ? localeLanguageObj.toString() : null;
58  
59          Object localeCountryObj = session.get(SessionParameter.LOCALE_ISO3166_COUNTRY);
60          country = localeCountryObj instanceof String ? localeCountryObj.toString() : null;
61      }
62  
63      /**
64       * Gets the current session.
65       */
66      protected BindingSession getSession() {
67          return session;
68      }
69  
70      /**
71       * Sets the service factory.
72       */
73      protected void setServiceFactory(CmisServiceFactory factory) {
74          this.factory = factory;
75      }
76  
77      /**
78       * Gets the service factory.
79       */
80      protected CmisServiceFactory getServiceFactory() {
81          return factory;
82      }
83  
84      /**
85       * Determines if the processing should be stopped before the service method
86       * is called.
87       * 
88       * @return {@code true} if the processing should be stopped, {@code false}
89       *         otherwise
90       */
91      protected boolean stopBeforeService(CmisService service) {
92          if (!(service instanceof ProgressControlCmisService)) {
93              return false;
94          }
95  
96          return ((ProgressControlCmisService) service).beforeServiceCall() == Progress.STOP;
97      }
98  
99      /**
100      * Determines if the processing should be stopped after the service method
101      * is called.
102      * 
103      * @return {@code true} if the processing should be stopped, {@code false}
104      *         otherwise
105      */
106     protected boolean stopAfterService(CmisService service) {
107         if (!(service instanceof ProgressControlCmisService)) {
108             return false;
109         }
110 
111         return ((ProgressControlCmisService) service).afterServiceCall() == Progress.STOP;
112     }
113 
114     /**
115      * creates a local call context.
116      */
117     protected CallContext createCallContext(String repositoryId) {
118         return new LocalCallContext(repositoryId, user, password, language, country);
119     }
120 
121     protected CmisService getService(String repositoryId) {
122         return factory.getService(createCallContext(repositoryId));
123     }
124 }