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.runtime;
20  
21  import java.io.Serializable;
22  import java.util.Collections;
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  import org.apache.chemistry.opencmis.client.api.OperationContext;
27  import org.apache.chemistry.opencmis.commons.PropertyIds;
28  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
29  
30  /**
31   * {@link OperationContext} implementation.
32   */
33  public class OperationContextImpl implements OperationContext, Serializable {
34  
35      public static final String PROPERTIES_STAR = "*";
36      public static final String RENDITION_NONE = "cmis:none";
37  
38      private static final long serialVersionUID = 1L;
39  
40      private TreeSet<String> filter;
41      private boolean loadSecondaryTypeProperties;
42      private boolean includeAcls;
43      private boolean includeAllowableActions;
44      private boolean includePolicies;
45      private IncludeRelationships includeRelationships;
46      private TreeSet<String> renditionFilter;
47      private boolean includePathSegments;
48      private String orderBy;
49      private boolean cacheEnabled;
50      private String cacheKey;
51      private int maxItemsPerPage;
52  
53      /**
54       * Default constructor.
55       */
56      public OperationContextImpl() {
57          setFilter(null);
58          setLoadSecondaryTypeProperties(false);
59          setIncludeAcls(false);
60          setIncludeAllowableActions(true);
61          setIncludePolicies(false);
62          setIncludeRelationships(IncludeRelationships.NONE);
63          setRenditionFilter(null);
64          setIncludePathSegments(true);
65          setOrderBy(null);
66          setCacheEnabled(false);
67          generateCacheKey();
68  
69          // default page size is 100
70          setMaxItemsPerPage(100);
71      }
72  
73      /**
74       * Copy constructor.
75       */
76      public OperationContextImpl(OperationContext source) {
77          setFilter(source.getFilter());
78          setLoadSecondaryTypeProperties(source.loadSecondaryTypeProperties());
79          setIncludeAcls(source.isIncludeAcls());
80          setIncludeAllowableActions(source.isIncludeAllowableActions());
81          setIncludePolicies(source.isIncludePolicies());
82          setIncludeRelationships(source.getIncludeRelationships());
83          setRenditionFilter(source.getRenditionFilter());
84          setIncludePathSegments(source.isIncludePathSegments());
85          setOrderBy(source.getOrderBy());
86          setCacheEnabled(source.isCacheEnabled());
87          generateCacheKey();
88  
89          setMaxItemsPerPage(source.getMaxItemsPerPage());
90      }
91  
92      /**
93       * Constructor with parameters.
94       */
95      public OperationContextImpl(Set<String> propertyFilter, boolean includeAcls, boolean includeAllowableActions,
96              boolean includePolicies, IncludeRelationships includeRelationships, Set<String> renditionFilter,
97              boolean includePathSegments, String orderBy, boolean cacheEnabled, int maxItemsPerPage) {
98          setFilter(propertyFilter);
99          setIncludeAcls(includeAcls);
100         setIncludeAllowableActions(includeAllowableActions);
101         setIncludePolicies(includePolicies);
102         setIncludeRelationships(includeRelationships);
103         setRenditionFilter(renditionFilter);
104         setIncludePathSegments(includePathSegments);
105         setOrderBy(orderBy);
106         setCacheEnabled(cacheEnabled);
107         generateCacheKey();
108 
109         setMaxItemsPerPage(maxItemsPerPage);
110     }
111 
112     @Override
113     public final Set<String> getFilter() {
114         if (filter == null) {
115             return null;
116         }
117 
118         return Collections.unmodifiableSet(filter);
119     }
120 
121     @Override
122     public final void setFilter(Set<String> propertyFilter) {
123         if (propertyFilter != null) {
124             TreeSet<String> tempSet = new TreeSet<String>();
125 
126             for (String oid : propertyFilter) {
127                 if (oid == null) {
128                     continue;
129                 }
130 
131                 String toid = oid.trim();
132                 if (toid.length() == 0) {
133                     continue;
134                 }
135                 if (toid.equals(PROPERTIES_STAR)) {
136                     tempSet = new TreeSet<String>();
137                     tempSet.add(PROPERTIES_STAR);
138                     break;
139                 }
140                 if (toid.indexOf(',') > -1) {
141                     throw new IllegalArgumentException("Query id must not contain a comma!");
142                 }
143 
144                 tempSet.add(toid);
145             }
146 
147             if (tempSet.isEmpty()) {
148                 filter = null;
149             } else {
150                 filter = tempSet;
151             }
152         } else {
153             filter = null;
154         }
155 
156         generateCacheKey();
157     }
158 
159     @Override
160     public final void setFilterString(String propertyFilter) {
161         if (propertyFilter == null || propertyFilter.trim().length() == 0) {
162             setFilter(null);
163             return;
164         }
165 
166         String[] propertyIds = propertyFilter.split(",");
167         TreeSet<String> tempSet = new TreeSet<String>();
168         for (String pid : propertyIds) {
169             tempSet.add(pid);
170         }
171 
172         setFilter(tempSet);
173     }
174 
175     @Override
176     public final String getFilterString() {
177         if (filter == null) {
178             return null;
179         }
180 
181         if (filter.contains(PROPERTIES_STAR)) {
182             return PROPERTIES_STAR;
183         }
184 
185         filter.add(PropertyIds.OBJECT_ID);
186         filter.add(PropertyIds.BASE_TYPE_ID);
187         filter.add(PropertyIds.OBJECT_TYPE_ID);
188         if (loadSecondaryTypeProperties) {
189             filter.add(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
190         }
191 
192         StringBuilder sb = new StringBuilder(128);
193 
194         for (String oid : filter) {
195             if (sb.length() > 0) {
196                 sb.append(',');
197             }
198 
199             sb.append(oid);
200         }
201 
202         return sb.toString();
203     }
204 
205     @Override
206     public final void setLoadSecondaryTypeProperties(boolean load) {
207         loadSecondaryTypeProperties = load;
208     }
209 
210     @Override
211     public final boolean loadSecondaryTypeProperties() {
212         return loadSecondaryTypeProperties;
213     }
214 
215     @Override
216     public final boolean isIncludeAcls() {
217         return includeAcls;
218     }
219 
220     @Override
221     public final void setIncludeAcls(boolean include) {
222         includeAcls = include;
223         generateCacheKey();
224     }
225 
226     @Override
227     public final boolean isIncludeAllowableActions() {
228         return includeAllowableActions;
229     }
230 
231     @Override
232     public final void setIncludeAllowableActions(boolean include) {
233         includeAllowableActions = include;
234         generateCacheKey();
235     }
236 
237     @Override
238     public final boolean isIncludePolicies() {
239         return includePolicies;
240     }
241 
242     @Override
243     public final void setIncludePolicies(boolean include) {
244         includePolicies = include;
245         generateCacheKey();
246     }
247 
248     @Override
249     public final IncludeRelationships getIncludeRelationships() {
250         return includeRelationships;
251     }
252 
253     @Override
254     public final void setIncludeRelationships(IncludeRelationships include) {
255         includeRelationships = include;
256         generateCacheKey();
257     }
258 
259     @Override
260     public final Set<String> getRenditionFilter() {
261         if (renditionFilter == null) {
262             return null;
263         }
264 
265         return Collections.unmodifiableSet(renditionFilter);
266     }
267 
268     @Override
269     public final void setRenditionFilter(Set<String> renditionFilter) {
270         TreeSet<String> tempSet = new TreeSet<String>();
271 
272         if (renditionFilter != null) {
273             for (String rf : renditionFilter) {
274                 if (rf == null) {
275                     continue;
276                 }
277 
278                 String trf = rf.trim();
279                 if (trf.length() == 0) {
280                     continue;
281                 }
282                 if (trf.indexOf(',') > -1) {
283                     throw new IllegalArgumentException("Rendition must not contain a comma!");
284                 }
285 
286                 tempSet.add(trf);
287             }
288 
289             if (tempSet.isEmpty()) {
290                 tempSet.add(RENDITION_NONE);
291             }
292         } else {
293             tempSet.add(RENDITION_NONE);
294         }
295 
296         this.renditionFilter = tempSet;
297         generateCacheKey();
298     }
299 
300     @Override
301     public final void setRenditionFilterString(String renditionFilter) {
302         if (renditionFilter == null || renditionFilter.trim().length() == 0) {
303             setRenditionFilter(null);
304             return;
305         }
306 
307         String[] renditions = renditionFilter.split(",");
308         TreeSet<String> tempSet = new TreeSet<String>();
309         for (String rend : renditions) {
310             tempSet.add(rend);
311         }
312 
313         setRenditionFilter(tempSet);
314     }
315 
316     @Override
317     public final String getRenditionFilterString() {
318         if (renditionFilter == null) {
319             return null;
320         }
321 
322         StringBuilder sb = new StringBuilder(128);
323 
324         for (String rf : renditionFilter) {
325             if (sb.length() > 0) {
326                 sb.append(',');
327             }
328 
329             sb.append(rf);
330         }
331 
332         return sb.toString();
333     }
334 
335     @Override
336     public final boolean isIncludePathSegments() {
337         return includePathSegments;
338     }
339 
340     @Override
341     public final void setIncludePathSegments(boolean include) {
342         includePathSegments = include;
343     }
344 
345     @Override
346     public final String getOrderBy() {
347         return orderBy;
348     }
349 
350     @Override
351     public final void setOrderBy(String orderBy) {
352         this.orderBy = orderBy;
353     }
354 
355     @Override
356     public final boolean isCacheEnabled() {
357         return cacheEnabled;
358     }
359 
360     @Override
361     public final void setCacheEnabled(boolean cacheEnabled) {
362         this.cacheEnabled = cacheEnabled;
363     }
364 
365     @Override
366     public final String getCacheKey() {
367         return cacheKey;
368     }
369 
370     /**
371      * Generates a new cache key from all parameters that are relevant for
372      * caching.
373      */
374     protected final void generateCacheKey() {
375         if (!cacheEnabled) {
376             cacheKey = null;
377         }
378 
379         StringBuilder sb = new StringBuilder(128);
380 
381         int bits = 0;
382         if (includeAcls) {
383             bits += 1;
384         }
385         if (includeAllowableActions) {
386             bits += 2;
387         }
388         if (includePolicies) {
389             bits += 4;
390         }
391 
392         sb.append((char) ('0' + bits));
393         sb.append(includeRelationships == null ? '-' : (char) ('a' + includeRelationships.ordinal()));
394         sb.append(filter == null ? "" : getFilterString());
395         if (renditionFilter != null && renditionFilter.size() > 0) {
396             sb.append('\\');
397             sb.append(getRenditionFilterString());
398         }
399 
400         cacheKey = sb.toString();
401     }
402 
403     @Override
404     public final int getMaxItemsPerPage() {
405         return maxItemsPerPage;
406     }
407 
408     @Override
409     public final void setMaxItemsPerPage(int maxItemsPerPage) {
410         if (maxItemsPerPage < 1) {
411             throw new IllegalArgumentException("itemsPerPage must be > 0!");
412         }
413 
414         this.maxItemsPerPage = maxItemsPerPage;
415     }
416 }