View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.sword.manager;
20  
21  import java.util.AbstractMap;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.mycore.frontend.MCRFrontendUtil;
26  import org.mycore.sword.MCRSword;
27  import org.mycore.sword.MCRSwordConstants;
28  import org.mycore.sword.application.MCRSwordCollectionProvider;
29  import org.swordapp.server.AuthCredentials;
30  import org.swordapp.server.ServiceDocument;
31  import org.swordapp.server.ServiceDocumentManager;
32  import org.swordapp.server.SwordAuthException;
33  import org.swordapp.server.SwordCollection;
34  import org.swordapp.server.SwordConfiguration;
35  import org.swordapp.server.SwordError;
36  import org.swordapp.server.SwordServerException;
37  import org.swordapp.server.SwordWorkspace;
38  
39  /**
40   * @author Sebastian Hofmann (mcrshofm)
41   */
42  public class MCRSwordServiceDocumentManager implements ServiceDocumentManager {
43  
44      @Override
45      public ServiceDocument getServiceDocument(String sdUri, AuthCredentials auth, SwordConfiguration config)
46          throws SwordError, SwordServerException, SwordAuthException {
47          ServiceDocument serviceDocument = new ServiceDocument();
48  
49          MCRSword.getWorkspaces().forEach(workspaceName -> {
50              SwordWorkspace workspace = buildSwordWorkspace(workspaceName, auth);
51              serviceDocument.addWorkspace(workspace);
52          });
53  
54          return serviceDocument;
55      }
56  
57      private SwordWorkspace buildSwordWorkspace(String workspaceName, AuthCredentials auth) {
58          SwordWorkspace workspace = new SwordWorkspace();
59          workspace.setTitle(workspaceName);
60  
61          buildSwordCollectionList(workspaceName, auth).forEach(workspace::addCollection);
62  
63          return workspace;
64      }
65  
66      private List<SwordCollection> buildSwordCollectionList(String workspaceName, AuthCredentials auth) {
67          String baseURL = MCRFrontendUtil.getBaseURL();
68          List<SwordCollection> swordCollections = new ArrayList<>();
69  
70          MCRSword.getCollectionsOfWorkspace(workspaceName).stream()
71              .map(collection -> new AbstractMap.SimpleEntry<>(collection, MCRSword.getCollection(collection)))
72              .filter(collectionEntry -> collectionEntry.getValue().isVisible())
73              .forEach(collection -> {
74                  SwordCollection swordCollection = new SwordCollection();
75                  final String collectionTitle = collection.getKey();
76                  swordCollection.setTitle(collectionTitle);
77  
78                  // add the supported packaging to the collection Provider
79                  final MCRSwordCollectionProvider collectionProvider = collection.getValue();
80                  collectionProvider.getSupportedPagacking().forEach(swordCollection::addAcceptPackaging);
81  
82                  swordCollection.setHref(baseURL + MCRSwordConstants.SWORD2_COL_IRI + collectionTitle + "/");
83                  swordCollections.add(swordCollection);
84              });
85  
86          return swordCollections;
87      }
88  }