View Javadoc

1   /*
2    * #%L
3    * Bitrepository Webclient
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.webservice;
23  
24  import java.util.List;
25  
26  import org.bitrepository.BasicClient;
27  import org.bitrepository.BasicClientFactory;
28  import org.bitrepository.common.settings.Settings;
29  import org.bitrepository.common.utils.SettingsUtils;
30  import org.bitrepository.settings.repositorysettings.Collection;
31  import org.bitrepository.settings.repositorysettings.ProtocolSettings;
32  import org.json.JSONArray;
33  import org.json.JSONException;
34  import org.json.JSONObject;
35  
36  import javax.ws.rs.GET;
37  import javax.ws.rs.Path;
38  import javax.ws.rs.Produces;
39  import javax.ws.rs.QueryParam;
40  import javax.ws.rs.core.MediaType;
41  
42  /**
43   * The class exposes the REST webservices provided by the Bitrepository-webclient using Jersey. 
44   */
45  
46  @Path("/reposervice")
47  public class Reposervice {
48  
49      private BasicClient client;
50  
51  
52      public Reposervice() {
53          client = BasicClientFactory.getInstance();
54      }
55      
56      @GET
57      @Path("/getCollectionIDs")
58      @Produces(MediaType.APPLICATION_JSON)
59      public String getCollectionIDs() {
60          JSONArray array = new JSONArray();
61          for(String collectionID : client.getCollectionIDs()) {
62              array.put(collectionID);
63          }
64          
65          return array.toString();
66      }
67      
68      @GET
69      @Path("/getCollectionName")
70      @Produces(MediaType.APPLICATION_JSON)
71      public String getCollectionName(@QueryParam("collectionID") String collectionID) {
72          return SettingsUtils.getCollectionName(collectionID);
73      }
74      
75      @GET
76      @Path("/getCollections")
77      @Produces(MediaType.APPLICATION_JSON)
78      public String getCollections() throws JSONException {
79          JSONArray array = new JSONArray();
80          JSONObject obj;
81          for(String collectionID : client.getCollectionIDs()) {
82              obj = new JSONObject();
83              obj.put("collectionID", collectionID);
84              obj.put("collectionName", SettingsUtils.getCollectionName(collectionID));
85              array.put((JSONObject) obj);
86          }
87          
88          return array.toString();
89      }
90      
91      @GET
92      @Path("/getRepositoryName")
93      @Produces(MediaType.APPLICATION_JSON)
94      public String getRepositoryName() {
95          return SettingsUtils.getRepositoryName();
96      }
97      
98      @GET
99      @Path("/getConfigurationOverview")
100     @Produces(MediaType.APPLICATION_JSON)
101     public String getConfigurationOverview() throws JSONException {
102         JSONObject config = new JSONObject();
103         config.put("repositoryName", SettingsUtils.getRepositoryName());
104         config.put("collections", makeCollectionsArray());
105         config.put("protocolSettings", makeProtocolSettingsObj());
106         return config.toString();
107     }
108     
109     private JSONObject makeProtocolSettingsObj() throws JSONException {
110         JSONObject obj = new JSONObject();
111         ProtocolSettings protocolSettings = client.getSettings().getRepositorySettings().getProtocolSettings();
112         obj.put("Allowed fileID pattern", protocolSettings.getAllowedFileIDPattern());
113         obj.put("Default checksum type", protocolSettings.getDefaultChecksumType());
114         obj.put("Require message authentication", protocolSettings.isRequireMessageAuthentication());
115         obj.put("Require operation authutorization", protocolSettings.isRequireOperationAuthorization());
116         obj.put("Require checksum for destructive reqests", protocolSettings.isRequireChecksumForDestructiveRequests());
117         obj.put("Require checksum for new file", protocolSettings.isRequireChecksumForNewFileRequests());
118         return obj; 
119     }
120 
121     private JSONArray makeCollectionsArray() throws JSONException {
122         JSONArray array = new JSONArray();
123         Settings settings = client.getSettings();
124         List<Collection> collections = settings.getRepositorySettings().getCollections().getCollection();
125         for(Collection c : collections) {
126             JSONObject obj = new JSONObject();
127             obj.put("collectionID", c.getID());
128             obj.put("collectionName", SettingsUtils.getCollectionName(c.getID()));
129             JSONArray pillarArray = new JSONArray();
130             List<String> pillars = c.getPillarIDs().getPillarID();
131             for(String p : pillars) {
132                 pillarArray.put(p);
133             }
134             obj.put("pillars", pillarArray);
135             array.put(obj);
136         }
137         return array;
138     }
139     
140     /**
141      * getSettingsSummary provides a summary of some important settings of the Bitrepository collection, herein:
142      * - The message bus which that is communicated with
143      * - The Pillars in the collection
144      * - The Bitrepository collection ID
145      * @return The current settings formatted as HTML 
146      */
147     @GET
148     @Path("/getSettingsSummary")
149     @Produces("text/plain")
150     public String getSummarySettings() {
151         return client.getSettingsSummary();
152     }
153 }