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.spi.browser;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
27  import org.apache.chemistry.opencmis.client.bindings.spi.http.Output;
28  import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
29  import org.apache.chemistry.opencmis.commons.data.Acl;
30  import org.apache.chemistry.opencmis.commons.data.ContentStream;
31  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
32  import org.apache.chemistry.opencmis.commons.data.ObjectData;
33  import org.apache.chemistry.opencmis.commons.data.Properties;
34  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
35  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
36  import org.apache.chemistry.opencmis.commons.impl.Constants;
37  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
38  import org.apache.chemistry.opencmis.commons.impl.ReturnVersion;
39  import org.apache.chemistry.opencmis.commons.impl.TypeCache;
40  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
41  import org.apache.chemistry.opencmis.commons.spi.Holder;
42  import org.apache.chemistry.opencmis.commons.spi.VersioningService;
43  
44  /**
45   * Versioning Service Browser Binding client.
46   */
47  public class VersioningServiceImpl extends AbstractBrowserBindingService implements VersioningService {
48  
49      /**
50       * Constructor.
51       */
52      public VersioningServiceImpl(BindingSession session) {
53          setSession(session);
54      }
55  
56      @Override
57      public void checkOut(String repositoryId, Holder<String> objectId, ExtensionsData extension,
58              Holder<Boolean> contentCopied) {
59          // we need an object id
60          if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
61              throw new CmisInvalidArgumentException("Object id must be set!");
62          }
63  
64          // build URL
65          UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
66  
67          // prepare form data
68          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CHECK_OUT);
69          formData.addSuccinctFlag(getSuccinct());
70  
71          // send and parse
72          Response resp = post(url, formData.getContentType(), new Output() {
73              @Override
74              public void write(OutputStream out) throws IOException {
75                  formData.write(out);
76              }
77          });
78  
79          Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
80  
81          TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
82  
83          ObjectData newObj = JSONConverter.convertObject(json, typeCache);
84  
85          objectId.setValue(newObj == null ? null : newObj.getId());
86      }
87  
88      @Override
89      public void cancelCheckOut(String repositoryId, String objectId, ExtensionsData extension) {
90          // build URL
91          UrlBuilder url = getObjectUrl(repositoryId, objectId);
92  
93          // prepare form data
94          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CANCEL_CHECK_OUT);
95  
96          // send
97          postAndConsume(url, formData.getContentType(), new Output() {
98              @Override
99              public void write(OutputStream out) throws IOException {
100                 formData.write(out);
101             }
102         });
103     }
104 
105     @Override
106     public void checkIn(String repositoryId, Holder<String> objectId, Boolean major, Properties properties,
107             ContentStream contentStream, String checkinComment, List<String> policies, Acl addAces, Acl removeAces,
108             ExtensionsData extension) {
109         // we need an object id
110         if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
111             throw new CmisInvalidArgumentException("Object id must be set!");
112         }
113 
114         // build URL
115         UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
116 
117         // prepare form data
118         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CHECK_IN, contentStream);
119         formData.addParameter(Constants.PARAM_MAJOR, major);
120         formData.addPropertiesParameters(properties, getDateTimeFormat());
121         formData.addParameter(Constants.PARAM_CHECKIN_COMMENT, checkinComment);
122         formData.addPoliciesParameters(policies);
123         formData.addAddAcesParameters(addAces);
124         formData.addRemoveAcesParameters(removeAces);
125         formData.addSuccinctFlag(getSuccinct());
126 
127         // send and parse
128         Response resp = post(url, formData.getContentType(), new Output() {
129             @Override
130             public void write(OutputStream out) throws IOException {
131                 formData.write(out);
132             }
133         });
134 
135         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
136 
137         TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
138 
139         ObjectData newObj = JSONConverter.convertObject(json, typeCache);
140 
141         objectId.setValue(newObj == null ? null : newObj.getId());
142     }
143 
144     @Override
145     public ObjectData getObjectOfLatestVersion(String repositoryId, String objectId, String versionSeriesId,
146             Boolean major, String filter, Boolean includeAllowableActions, IncludeRelationships includeRelationships,
147             String renditionFilter, Boolean includePolicyIds, Boolean includeAcl, ExtensionsData extension) {
148         // build URL
149         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_OBJECT);
150         url.addParameter(Constants.PARAM_FILTER, filter);
151         url.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
152         url.addParameter(Constants.PARAM_RELATIONSHIPS, includeRelationships);
153         url.addParameter(Constants.PARAM_RENDITION_FILTER, renditionFilter);
154         url.addParameter(Constants.PARAM_POLICY_IDS, includePolicyIds);
155         url.addParameter(Constants.PARAM_ACL, includeAcl);
156         url.addParameter(Constants.PARAM_RETURN_VERSION,
157                 (major == null || Boolean.FALSE.equals(major) ? ReturnVersion.LATEST : ReturnVersion.LASTESTMAJOR));
158         url.addParameter(Constants.PARAM_SUCCINCT, getSuccinctParameter());
159         url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
160 
161         // read and parse
162         Response resp = read(url);
163         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
164 
165         TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
166 
167         return JSONConverter.convertObject(json, typeCache);
168     }
169 
170     @Override
171     public Properties getPropertiesOfLatestVersion(String repositoryId, String objectId, String versionSeriesId,
172             Boolean major, String filter, ExtensionsData extension) {
173         // build URL
174         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_PROPERTIES);
175         url.addParameter(Constants.PARAM_FILTER, filter);
176         url.addParameter(Constants.PARAM_RETURN_VERSION,
177                 (major == null || Boolean.FALSE.equals(major) ? ReturnVersion.LATEST : ReturnVersion.LASTESTMAJOR));
178         url.addParameter(Constants.PARAM_SUCCINCT, getSuccinctParameter());
179         url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
180 
181         // read and parse
182         Response resp = read(url);
183         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
184 
185         if (getSuccinct()) {
186             TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
187             return JSONConverter.convertSuccinctProperties(json, null, typeCache);
188         } else {
189             return JSONConverter.convertProperties(json, null);
190         }
191     }
192 
193     @Override
194     public List<ObjectData> getAllVersions(String repositoryId, String objectId, String versionSeriesId, String filter,
195             Boolean includeAllowableActions, ExtensionsData extension) {
196         // build URL
197         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_VERSIONS);
198         url.addParameter(Constants.PARAM_FILTER, filter);
199         url.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
200         url.addParameter(Constants.PARAM_SUCCINCT, getSuccinctParameter());
201         url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
202 
203         // read and parse
204         Response resp = read(url);
205         List<Object> json = parseArray(resp.getStream(), resp.getCharset());
206 
207         TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
208 
209         return JSONConverter.convertObjects(json, typeCache);
210     }
211 }