This project has retired. For details please refer to its Attic page.
DiscoveryServiceImpl 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.io.OutputStream;
24  import java.math.BigInteger;
25  import java.util.ArrayList;
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.ObjectData;
35  import org.apache.chemistry.opencmis.commons.data.ObjectList;
36  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
37  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
38  import org.apache.chemistry.opencmis.commons.impl.Constants;
39  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
40  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
41  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectListImpl;
42  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
43  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisQueryType;
44  import org.apache.chemistry.opencmis.commons.impl.jaxb.EnumIncludeRelationships;
45  import org.apache.chemistry.opencmis.commons.spi.DiscoveryService;
46  import org.apache.chemistry.opencmis.commons.spi.Holder;
47  
48  /**
49   * Discovery Service AtomPub client.
50   */
51  public class DiscoveryServiceImpl extends AbstractAtomPubService implements DiscoveryService {
52  
53      /**
54       * Constructor.
55       */
56      public DiscoveryServiceImpl(BindingSession session) {
57          setSession(session);
58      }
59  
60      public ObjectList getContentChanges(String repositoryId, Holder<String> changeLogToken, Boolean includeProperties,
61              String filter, Boolean includePolicyIds, Boolean includeACL, BigInteger maxItems, ExtensionsData extension) {
62          ObjectListImpl result = new ObjectListImpl();
63  
64          // find the link
65          String link = loadRepositoryLink(repositoryId, Constants.REP_REL_CHANGES);
66  
67          if (link == null) {
68              throw new CmisObjectNotFoundException("Unknown repository or content changes not supported!");
69          }
70  
71          UrlBuilder url = new UrlBuilder(link);
72          url.addParameter(Constants.PARAM_CHANGE_LOG_TOKEN, (changeLogToken == null ? null : changeLogToken.getValue()));
73          url.addParameter(Constants.PARAM_PROPERTIES, includeProperties);
74          url.addParameter(Constants.PARAM_FILTER, filter);
75          url.addParameter(Constants.PARAM_POLICY_IDS, includePolicyIds);
76          url.addParameter(Constants.PARAM_ACL, includeACL);
77          url.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
78  
79          // read and parse
80          HttpUtils.Response resp = read(url);
81          AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
82  
83          // handle top level
84          for (AtomElement element : feed.getElements()) {
85              if (element.getObject() instanceof AtomLink) {
86                  if (isNextLink(element)) {
87                      result.setHasMoreItems(Boolean.TRUE);
88                  }
89              } else if (isInt(NAME_NUM_ITEMS, element)) {
90                  result.setNumItems((BigInteger) element.getObject());
91              }
92          }
93  
94          // get the changes
95          if (!feed.getEntries().isEmpty()) {
96              result.setObjects(new ArrayList<ObjectData>(feed.getEntries().size()));
97  
98              for (AtomEntry entry : feed.getEntries()) {
99                  ObjectData hit = null;
100 
101                 // walk through the entry
102                 for (AtomElement element : entry.getElements()) {
103                     if (element.getObject() instanceof CmisObjectType) {
104                         hit = convert((CmisObjectType) element.getObject());
105                     }
106                 }
107 
108                 if (hit != null) {
109                     result.getObjects().add(hit);
110                 }
111             }
112         }
113 
114         return result;
115     }
116 
117     public ObjectList query(String repositoryId, String statement, Boolean searchAllVersions,
118             Boolean includeAllowableActions, IncludeRelationships includeRelationships, String renditionFilter,
119             BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
120         ObjectListImpl result = new ObjectListImpl();
121 
122         // find the link
123         String link = loadCollection(repositoryId, Constants.COLLECTION_QUERY);
124 
125         if (link == null) {
126             throw new CmisObjectNotFoundException("Unknown repository or query not supported!");
127         }
128 
129         UrlBuilder url = new UrlBuilder(link);
130 
131         // compile query request
132         final CmisQueryType query = new CmisQueryType();
133         query.setStatement(statement);
134         query.setSearchAllVersions(searchAllVersions);
135         query.setIncludeAllowableActions(includeAllowableActions);
136         query.setIncludeRelationships(convert(EnumIncludeRelationships.class, includeRelationships));
137         query.setRenditionFilter(renditionFilter);
138         query.setMaxItems(maxItems);
139         query.setSkipCount(skipCount);
140 
141         // post the query and parse results
142         HttpUtils.Response resp = post(url, Constants.MEDIATYPE_QUERY, new HttpUtils.Output() {
143             public void write(OutputStream out) throws Exception {
144                 JaxBHelper.marshal(JaxBHelper.CMIS_OBJECT_FACTORY.createQuery(query), out, false);
145             }
146         });
147         AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
148 
149         // handle top level
150         for (AtomElement element : feed.getElements()) {
151             if (element.getObject() instanceof AtomLink) {
152                 if (isNextLink(element)) {
153                     result.setHasMoreItems(Boolean.TRUE);
154                 }
155             } else if (isInt(NAME_NUM_ITEMS, element)) {
156                 result.setNumItems((BigInteger) element.getObject());
157             }
158         }
159 
160         // get the result set
161         if (!feed.getEntries().isEmpty()) {
162             result.setObjects(new ArrayList<ObjectData>(feed.getEntries().size()));
163 
164             for (AtomEntry entry : feed.getEntries()) {
165                 ObjectData hit = null;
166 
167                 // walk through the entry
168                 for (AtomElement element : entry.getElements()) {
169                     if (element.getObject() instanceof CmisObjectType) {
170                         hit = convert((CmisObjectType) element.getObject());
171                     }
172                 }
173 
174                 if (hit != null) {
175                     result.getObjects().add(hit);
176                 }
177             }
178         }
179 
180         return result;
181     }
182 }