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.math.BigInteger;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.client.bindings.spi.http.Output;
29  import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
30  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
31  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
32  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
33  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
34  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
35  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
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.UrlBuilder;
39  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
40  
41  /**
42   * Repository Service Browser Binding client.
43   */
44  public class RepositoryServiceImpl extends AbstractBrowserBindingService implements RepositoryService {
45  
46      /**
47       * Constructor.
48       */
49      public RepositoryServiceImpl(BindingSession session) {
50          setSession(session);
51      }
52  
53      @Override
54      public List<RepositoryInfo> getRepositoryInfos(ExtensionsData extension) {
55          return getRepositoriesInternal(null);
56      }
57  
58      @Override
59      public RepositoryInfo getRepositoryInfo(String repositoryId, ExtensionsData extension) {
60          List<RepositoryInfo> repositoryInfos = getRepositoriesInternal(repositoryId);
61  
62          if (repositoryInfos.isEmpty()) {
63              throw new CmisObjectNotFoundException("Repository '" + repositoryId + "' not found!");
64          }
65  
66          // find the repository
67          for (RepositoryInfo info : repositoryInfos) {
68              if (info.getId() == null) {
69                  continue;
70              }
71  
72              if (info.getId().equals(repositoryId)) {
73                  return info;
74              }
75          }
76  
77          throw new CmisObjectNotFoundException("Repository '" + repositoryId + "' not found!");
78      }
79  
80      @Override
81      public TypeDefinition getTypeDefinition(String repositoryId, String typeId, ExtensionsData extension) {
82          return getTypeDefinitionInternal(repositoryId, typeId);
83      }
84  
85      @Override
86      public TypeDefinitionList getTypeChildren(String repositoryId, String typeId, Boolean includePropertyDefinitions,
87              BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
88          // build URL
89          UrlBuilder url = getRepositoryUrl(repositoryId, Constants.SELECTOR_TYPE_CHILDREN);
90          url.addParameter(Constants.PARAM_TYPE_ID, typeId);
91          url.addParameter(Constants.PARAM_PROPERTY_DEFINITIONS, includePropertyDefinitions);
92          url.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
93          url.addParameter(Constants.PARAM_SKIP_COUNT, skipCount);
94          url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
95  
96          // read and parse
97          Response resp = read(url);
98          Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
99  
100         return JSONConverter.convertTypeChildren(json);
101     }
102 
103     @Override
104     public List<TypeDefinitionContainer> getTypeDescendants(String repositoryId, String typeId, BigInteger depth,
105             Boolean includePropertyDefinitions, ExtensionsData extension) {
106         // build URL
107         UrlBuilder url = getRepositoryUrl(repositoryId, Constants.SELECTOR_TYPE_DESCENDANTS);
108         url.addParameter(Constants.PARAM_TYPE_ID, typeId);
109         url.addParameter(Constants.PARAM_DEPTH, depth);
110         url.addParameter(Constants.PARAM_PROPERTY_DEFINITIONS, includePropertyDefinitions);
111         url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
112 
113         // read and parse
114         Response resp = read(url);
115         List<Object> json = parseArray(resp.getStream(), resp.getCharset());
116 
117         return JSONConverter.convertTypeDescendants(json);
118     }
119 
120     @Override
121     public TypeDefinition createType(String repositoryId, TypeDefinition type, ExtensionsData extension) {
122         // build URL
123         UrlBuilder url = getRepositoryUrl(repositoryId);
124 
125         // prepare form data
126         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_TYPE);
127         if (type != null) {
128             formData.addParameter(Constants.CONTROL_TYPE, JSONConverter.convert(type, getDateTimeFormat())
129                     .toJSONString());
130         }
131 
132         // send
133         Response resp = post(url, formData.getContentType(), new Output() {
134             @Override
135             public void write(OutputStream out) throws IOException {
136                 formData.write(out);
137             }
138         });
139 
140         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
141 
142         return JSONConverter.convertTypeDefinition(json);
143     }
144 
145     @Override
146     public TypeDefinition updateType(String repositoryId, TypeDefinition type, ExtensionsData extension) {
147         // build URL
148         UrlBuilder url = getRepositoryUrl(repositoryId);
149 
150         // prepare form data
151         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_UPDATE_TYPE);
152         if (type != null) {
153             formData.addParameter(Constants.CONTROL_TYPE, JSONConverter.convert(type, getDateTimeFormat())
154                     .toJSONString());
155         }
156 
157         // send
158         Response resp = post(url, formData.getContentType(), new Output() {
159             @Override
160             public void write(OutputStream out) throws IOException {
161                 formData.write(out);
162             }
163         });
164 
165         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
166 
167         return JSONConverter.convertTypeDefinition(json);
168     }
169 
170     @Override
171     public void deleteType(String repositoryId, String typeId, ExtensionsData extension) {
172         // build URL
173         UrlBuilder url = getRepositoryUrl(repositoryId);
174 
175         // prepare form data
176         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_DELETE_TYPE);
177         formData.addParameter(Constants.CONTROL_TYPE_ID, typeId);
178 
179         // send
180         postAndConsume(url, formData.getContentType(), new Output() {
181             @Override
182             public void write(OutputStream out) throws IOException {
183                 formData.write(out);
184             }
185         });
186     }
187 }