-
Notifications
You must be signed in to change notification settings - Fork 23
/
BitmagUtils.java
206 lines (186 loc) · 8.82 KB
/
BitmagUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dk.netarkivet.common.distribute.bitrepository;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
import javax.jms.JMSException;
import org.bitrepository.access.AccessComponentFactory;
import org.bitrepository.access.getchecksums.GetChecksumsClient;
import org.bitrepository.access.getfile.GetFileClient;
import org.bitrepository.access.getfileids.GetFileIDsClient;
import org.bitrepository.bitrepositoryelements.ChecksumDataForFileTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumType;
import org.bitrepository.common.ArgumentValidator;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.settings.SettingsProvider;
import org.bitrepository.common.settings.XMLFileSettingsLoader;
import org.bitrepository.common.utils.Base16Utils;
import org.bitrepository.common.utils.CalendarUtils;
import org.bitrepository.common.utils.SettingsUtils;
import org.bitrepository.modify.ModifyComponentFactory;
import org.bitrepository.modify.putfile.PutFileClient;
import org.bitrepository.protocol.FileExchange;
import org.bitrepository.protocol.ProtocolComponentFactory;
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.BasicMessageAuthenticator;
import org.bitrepository.protocol.security.BasicMessageSigner;
import org.bitrepository.protocol.security.BasicOperationAuthorizor;
import org.bitrepository.protocol.security.BasicSecurityManager;
import org.bitrepository.protocol.security.MessageAuthenticator;
import org.bitrepository.protocol.security.MessageSigner;
import org.bitrepository.protocol.security.OperationAuthorizor;
import org.bitrepository.protocol.security.PermissionStore;
import org.bitrepository.protocol.security.SecurityManager;
import org.bitrepository.settings.referencesettings.FileExchangeSettings;
/**
* Utility class to abstract away the specifics of setting up and obtaining bitrepository.org clients.
*/
public class BitmagUtils {
public static final String BITREPOSITORY_GETFILEIDS_MAX_RESULTS = "settings.common.arcrepositoryClient.bitrepository.getFileIDsMaxResults";
private static Settings settings;
private static SecurityManager securityManager;
private static Path certificate;
/**
* Method to initialize the utility class. Must be called prior to use of any other method
* as it initializes internal state.
* @param configurationDir Path to the configuration base directory, this is the directory where
* RepositorySettings.xml and ReferenceSettings.xml is expected to be found.
* @param clientCertificate Path to the clients certificate.
*/
public static void initialize(Path configurationDir, Path clientCertificate) {
certificate = clientCertificate;
settings = loadSettings(configurationDir);
securityManager = loadSecurityManager();
}
/**
* Load settings based on configuration found in configurationDir.
* Use CommandLineSettingsProvider as that will help give a convenient clientID.
*/
private static Settings loadSettings(Path configurationDir) {
SettingsProvider settingsLoader =
new SettingsProvider(new XMLFileSettingsLoader(configurationDir.toString()), generateComponentID());
Settings settings = settingsLoader.getSettings();
SettingsUtils.initialize(settings);
return settings;
}
/**
* Boiler plate for loading permission model.
*/
private static SecurityManager loadSecurityManager() {
ArgumentValidator.checkNotNull(settings, "Settings settings");
PermissionStore permissionStore = new PermissionStore();
MessageAuthenticator authenticator = new BasicMessageAuthenticator(permissionStore);
MessageSigner signer = new BasicMessageSigner();
OperationAuthorizor authorizer = new BasicOperationAuthorizor(permissionStore);
return new BasicSecurityManager(settings.getRepositorySettings(), certificate.toString(),
authenticator, signer, authorizer, permissionStore,
settings.getComponentID());
}
/**
* Method to get the list of known pillars in a collection
* @param collectionID The ID of the collection to obtain pillars for
* @return the list of known pillarIDs
*/
public static List<String> getKnownPillars(String collectionID) {
return SettingsUtils.getPillarIDsForCollection(collectionID);
}
/**
* Method to get the list of known collections
* @return the list of known collectionIDs
*/
public static List<String> getKnownCollections() {
return SettingsUtils.getAllCollectionsIDs();
}
/**
* Generates a component id, which includes the hostname and a random UUID.
* @return The Bitrepository component id for this NetarchiveSuite application.
*/
public static String generateComponentID() {
String hn = HostNameUtils.getHostName();
return "NetarchivesuiteClient-" + hn + "-" + UUID.randomUUID();
}
/**
* Method to get the base part of the URL to the file exchange server.
* @return {@link URL} URL with the base part of the file exchange server.
* @throws MalformedURLException if the file exchange configuration results in an invalid url
*/
public static URL getFileExchangeBaseURL() throws MalformedURLException {
FileExchangeSettings feSettings = settings.getReferenceSettings().getFileExchangeSettings();
return new URL(feSettings.getProtocolType().value(), feSettings.getServerName(),
feSettings.getPort().intValue(), feSettings.getPath() + "/");
}
/**
* Method to retrieve the instance of the FileExchange.
* To be used for transferring files to and from the FileExchange server
* @return {@link FileExchange} The FileExchange instance
*/
public static FileExchange getFileExchange() {
return ProtocolComponentFactory.getInstance().getFileExchange(settings);
}
/**
* Method to build the datastructure to transport checksums in
* @param checksum The string form of the checksum
* @return {@link ChecksumDataForFileTYPE} The bitrepository.org data structure
* to transport the checksum
*/
public static ChecksumDataForFileTYPE getChecksum(String checksum) {
ChecksumDataForFileTYPE checksumData = new ChecksumDataForFileTYPE();
checksumData.setChecksumValue(Base16Utils.encodeBase16(checksum));
checksumData.setCalculationTimestamp(CalendarUtils.getNow());
ChecksumSpecTYPE checksumSpec = new ChecksumSpecTYPE();
checksumSpec.setChecksumType(ChecksumType.MD5);
checksumData.setChecksumSpec(checksumSpec);
return checksumData;
}
public static ChecksumSpecTYPE getChecksumSpec(ChecksumType checksumType) {
ChecksumSpecTYPE checksumSpec = new ChecksumSpecTYPE();
checksumSpec.setChecksumType(checksumType);
return checksumSpec;
}
/**
* Retreive a PutFileClient
* @return {@link PutFileClient} The PutFileClient
*/
public static PutFileClient getPutFileClient() {
return ModifyComponentFactory.getInstance().retrievePutClient(settings,
securityManager, settings.getComponentID());
}
/**
* Retreive a GetFileIDsClient
* @return {@link GetFileIDsClient} The GetFileIDsClient
*/
public static GetFileIDsClient getFileIDsClient() {
return AccessComponentFactory.getInstance().createGetFileIDsClient(settings, securityManager,
settings.getComponentID());
}
/**
* Retreive a GetChecksumsClient
* @return {@link GetChecksumsClient} The GetChecksumsClient
*/
public static GetChecksumsClient getChecksumsClient() {
return AccessComponentFactory.getInstance().createGetChecksumsClient(settings, securityManager,
settings.getComponentID());
}
/**
* Retreive a GetFileClient
* @return {@link GetFileClient} The GetFileClient
*/
public static GetFileClient getFileClient() {
return AccessComponentFactory.getInstance().createGetFileClient(settings, securityManager,
settings.getComponentID());
}
/**
* Method to shutdown the bitrepository connections if such exists.
* Should only be called when all interactions with the bitrepository from this client is done.
* @throws JMSException if there is trouble shutting down the messagebus connection
*/
public static void shutdown() throws JMSException {
MessageBus messageBus = MessageBusManager.getMessageBus();
if (messageBus != null) {
messageBus.close();
}
}
}