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