This project has retired. For details please refer to its Attic page.
RepositoryServiceImpl 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.client.bindings.spi.atompub;
20  
21  import static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
22  
23  import java.math.BigInteger;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomElement;
29  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomEntry;
30  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomFeed;
31  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomLink;
32  import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils;
33  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
34  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
35  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
36  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
37  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
39  import org.apache.chemistry.opencmis.commons.impl.Constants;
40  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
41  import org.apache.chemistry.opencmis.commons.impl.dataobjects.TypeDefinitionContainerImpl;
42  import org.apache.chemistry.opencmis.commons.impl.dataobjects.TypeDefinitionListImpl;
43  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
44  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
45  
46  /**
47   * Repository Service AtomPub client.
48   */
49  public class RepositoryServiceImpl extends AbstractAtomPubService implements RepositoryService {
50  
51      /**
52       * Constructor.
53       */
54      public RepositoryServiceImpl(BindingSession session) {
55          setSession(session);
56      }
57  
58      public List<RepositoryInfo> getRepositoryInfos(ExtensionsData extension) {
59          return getRepositoriesInternal(null);
60      }
61  
62      public RepositoryInfo getRepositoryInfo(String repositoryId, ExtensionsData extension) {
63          List<RepositoryInfo> repositoryInfos = getRepositoriesInternal(repositoryId);
64  
65          // find the repository
66          for (RepositoryInfo info : repositoryInfos) {
67              if (info.getId() == null) {
68                  continue;
69              }
70  
71              if (info.getId().equals(repositoryId)) {
72                  return info;
73              }
74          }
75  
76          throw new CmisObjectNotFoundException("Repository not found!");
77      }
78  
79      public TypeDefinition getTypeDefinition(String repositoryId, String typeId, ExtensionsData extension) {
80          return getTypeDefinitionInternal(repositoryId, typeId);
81      }
82  
83      public TypeDefinitionList getTypeChildren(String repositoryId, String typeId, Boolean includePropertyDefinitions,
84              BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
85          TypeDefinitionListImpl result = new TypeDefinitionListImpl();
86  
87          // find the link
88          String link = null;
89          if (typeId == null) {
90              link = loadCollection(repositoryId, Constants.COLLECTION_TYPES);
91          } else {
92              link = loadTypeLink(repositoryId, typeId, Constants.REL_DOWN, Constants.MEDIATYPE_CHILDREN);
93          }
94  
95          if (link == null) {
96              throw new CmisObjectNotFoundException("Unknown repository or type!");
97          }
98  
99          UrlBuilder url = new UrlBuilder(link);
100         url.addParameter(Constants.PARAM_PROPERTY_DEFINITIONS, includePropertyDefinitions);
101         url.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
102         url.addParameter(Constants.PARAM_SKIP_COUNT, skipCount);
103 
104         // read and parse
105         HttpUtils.Response resp = read(url);
106         AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
107 
108         // handle top level
109         for (AtomElement element : feed.getElements()) {
110             if (element.getObject() instanceof AtomLink) {
111                 if (isNextLink(element)) {
112                     result.setHasMoreItems(Boolean.TRUE);
113                 }
114             } else if (isInt(NAME_NUM_ITEMS, element)) {
115                 result.setNumItems((BigInteger) element.getObject());
116             }
117         }
118 
119         result.setList(new ArrayList<TypeDefinition>(feed.getEntries().size()));
120 
121         // get the children
122         if (!feed.getEntries().isEmpty()) {
123             for (AtomEntry entry : feed.getEntries()) {
124                 TypeDefinition child = null;
125 
126                 lockTypeLinks();
127                 try {
128                     // walk through the entry
129                     for (AtomElement element : entry.getElements()) {
130                         if (element.getObject() instanceof AtomLink) {
131                             addTypeLink(repositoryId, entry.getId(), (AtomLink) element.getObject());
132                         } else if (element.getObject() instanceof CmisTypeDefinitionType) {
133                             child = convert((CmisTypeDefinitionType) element.getObject());
134                         }
135                     }
136                 } finally {
137                     unlockTypeLinks();
138                 }
139 
140                 if (child != null) {
141                     result.getList().add(child);
142                 }
143             }
144         }
145 
146         return result;
147     }
148 
149     public List<TypeDefinitionContainer> getTypeDescendants(String repositoryId, String typeId, BigInteger depth,
150             Boolean includePropertyDefinitions, ExtensionsData extension) {
151         List<TypeDefinitionContainer> result = new ArrayList<TypeDefinitionContainer>();
152 
153         // find the link
154         String link = null;
155         if (typeId == null) {
156             link = loadRepositoryLink(repositoryId, Constants.REP_REL_TYPEDESC);
157         } else {
158             link = loadTypeLink(repositoryId, typeId, Constants.REL_DOWN, Constants.MEDIATYPE_DESCENDANTS);
159         }
160 
161         if (link == null) {
162             throw new CmisObjectNotFoundException("Unknown repository or type!");
163         }
164 
165         UrlBuilder url = new UrlBuilder(link);
166         url.addParameter(Constants.PARAM_DEPTH, depth);
167         url.addParameter(Constants.PARAM_PROPERTY_DEFINITIONS, includePropertyDefinitions);
168 
169         // read and parse
170         HttpUtils.Response resp = read(url);
171         AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
172 
173         // process tree
174         addTypeDescendantsLevel(repositoryId, feed, result);
175 
176         return result;
177     }
178 
179     /**
180      * Adds type descendants level recursively.
181      */
182     private void addTypeDescendantsLevel(String repositoryId, AtomFeed feed, List<TypeDefinitionContainer> containerList) {
183         if ((feed == null) || (feed.getEntries().isEmpty())) {
184             return;
185         }
186 
187         // walk through the feed
188         for (AtomEntry entry : feed.getEntries()) {
189             TypeDefinitionContainerImpl childContainer = null;
190             List<TypeDefinitionContainer> childContainerList = new ArrayList<TypeDefinitionContainer>();
191 
192             // walk through the entry
193             lockTypeLinks();
194             try {
195                 for (AtomElement element : entry.getElements()) {
196                     if (element.getObject() instanceof AtomLink) {
197                         addTypeLink(repositoryId, entry.getId(), (AtomLink) element.getObject());
198                     } else if (element.getObject() instanceof CmisTypeDefinitionType) {
199                         childContainer = new TypeDefinitionContainerImpl(
200                                 convert((CmisTypeDefinitionType) element.getObject()));
201                     } else if (element.getObject() instanceof AtomFeed) {
202                         addTypeDescendantsLevel(repositoryId, (AtomFeed) element.getObject(), childContainerList);
203                     }
204                 }
205             } finally {
206                 unlockTypeLinks();
207             }
208 
209             if (childContainer != null) {
210                 childContainer.setChildren(childContainerList);
211                 containerList.add(childContainer);
212             }
213         }
214     }
215 }