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.atompub;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import javax.xml.stream.XMLStreamException;
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.commons.data.ExtensionsData;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
31  import org.apache.chemistry.opencmis.commons.impl.Constants;
32  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
33  import org.apache.chemistry.opencmis.commons.spi.MultiFilingService;
34  
35  /**
36   * MultiFiling Service AtomPub client.
37   */
38  public class MultiFilingServiceImpl extends AbstractAtomPubService implements MultiFilingService {
39  
40      /**
41       * Constructor.
42       */
43      public MultiFilingServiceImpl(BindingSession session) {
44          setSession(session);
45      }
46  
47      @Override
48      public void addObjectToFolder(String repositoryId, String objectId, String folderId, Boolean allVersions,
49              ExtensionsData extension) {
50          if (objectId == null) {
51              throw new CmisInvalidArgumentException("Object id must be set!");
52          }
53  
54          // find the link
55          String link = loadLink(repositoryId, folderId, Constants.REL_DOWN, Constants.MEDIATYPE_CHILDREN);
56  
57          if (link == null) {
58              throwLinkException(repositoryId, folderId, Constants.REL_DOWN, Constants.MEDIATYPE_CHILDREN);
59          }
60  
61          UrlBuilder url = new UrlBuilder(link);
62          url.addParameter(Constants.PARAM_ALL_VERSIONS, allVersions);
63  
64          // set up object and writer
65          final AtomEntryWriter entryWriter = new AtomEntryWriter(createIdObject(objectId), getCmisVersion(repositoryId));
66  
67          // post addObjectToFolder request
68          postAndConsume(url, Constants.MEDIATYPE_ENTRY, new Output() {
69              @Override
70              public void write(OutputStream out) throws XMLStreamException, IOException {
71                  entryWriter.write(out);
72              }
73          });
74      }
75  
76      @Override
77      public void removeObjectFromFolder(String repositoryId, String objectId, String folderId, ExtensionsData extension) {
78          if (objectId == null) {
79              throw new CmisInvalidArgumentException("Object id must be set!");
80          }
81  
82          // find the link
83          String link = loadCollection(repositoryId, Constants.COLLECTION_UNFILED);
84  
85          if (link == null) {
86              throw new CmisObjectNotFoundException("Unknown repository or unfiling not supported!");
87          }
88  
89          UrlBuilder url = new UrlBuilder(link);
90          url.addParameter(Constants.PARAM_REMOVE_FROM, folderId);
91  
92          // set up object and writer
93          final AtomEntryWriter entryWriter = new AtomEntryWriter(createIdObject(objectId), getCmisVersion(repositoryId));
94  
95          // post removeObjectFromFolder request
96          postAndConsume(url, Constants.MEDIATYPE_ENTRY, new Output() {
97              @Override
98              public void write(OutputStream out) throws XMLStreamException, IOException {
99                  entryWriter.write(out);
100             }
101         });
102     }
103 }