-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNfsFactory.java
171 lines (146 loc) · 6.13 KB
/
NfsFactory.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
package com.researchspace.netfiles;
import com.researchspace.model.UserKeyPair;
import com.researchspace.model.netfiles.NfsAuthenticationType;
import com.researchspace.model.netfiles.NfsClientType;
import com.researchspace.model.netfiles.NfsFileSystem;
import com.researchspace.model.netfiles.NfsFileSystemOption;
import com.researchspace.netfiles.irods.IRODSClient;
import com.researchspace.netfiles.irods.JargonFacade;
import com.researchspace.netfiles.samba.JcifsClient;
import com.researchspace.netfiles.samba.JcifsSmbjClient;
import com.researchspace.netfiles.samba.SmbjClient;
import com.researchspace.netfiles.sftp.SftpClient;
import org.apache.commons.lang.StringUtils;
import org.irods.jargon.core.connection.IRODSAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/** Class providing various Net File Store implementations depending on deployment.properties */
@Component
public class NfsFactory {
private static final int IRODS_DEFAULT_PORT = 1247;
private static final Logger log = LoggerFactory.getLogger(NfsFactory.class);
@Autowired private NfsUserPasswordAuthentication userPasswordAuthentication;
@Autowired private NfsPublicKeyAuthentication publicKeyAuthentication;
@Value("${netfilestores.auth.pubKey.passphrase}")
private String passphrase;
@Value("${netfilestores.smbj.download}")
private Boolean smbjDownload;
// this is for legacy systems only and doesn't need defining for new setups
@Value("${netfilestores.smbj.shareName}")
private String smbjShareName;
@Value("${netfilestores.smbj.withDfsEnabled}")
private Boolean smbjWithDfsEnabled = false;
@Value("${netfilestores.extraSystemProps}")
private String extraSystemProps;
@Value("${netfilestores.smbj.name.match.path}")
private String sambaNameMustMatchFilePath;
private boolean extraSystemPropsConfigured;
public NfsClient getNfsClient(String nfsusername, String nfspassword, NfsFileSystem fileSystem) {
verifyFileSystemProperties(fileSystem);
configureExtraSystemProperties();
NfsClientType clientType = fileSystem.getClientType();
if (NfsClientType.SAMBA.equals(clientType)) {
if (smbjDownload != null && smbjDownload) {
return new JcifsSmbjClient(
nfsusername,
nfspassword,
fileSystem.getClientOption(NfsFileSystemOption.SAMBA_DOMAIN),
fileSystem.getUrl(),
smbjShareName);
}
return new JcifsClient(
nfsusername,
nfspassword,
fileSystem.getClientOption(NfsFileSystemOption.SAMBA_DOMAIN),
fileSystem.getUrl());
}
if (NfsClientType.SMBJ.equals(clientType)) {
return new SmbjClient(
nfsusername,
nfspassword,
fileSystem.getClientOption(NfsFileSystemOption.SAMBA_DOMAIN),
fileSystem.getUrl(),
fileSystem.getClientOption(NfsFileSystemOption.SAMBA_SHARE_NAME),
smbjWithDfsEnabled,
Boolean.parseBoolean(sambaNameMustMatchFilePath));
}
if (NfsClientType.SFTP.equals(clientType)) {
return new SftpClient(
nfsusername,
nfspassword,
fileSystem.getUrl(),
fileSystem.getClientOption(NfsFileSystemOption.SFTP_SERVER_PUBLIC_KEY));
}
if (NfsClientType.IRODS.equals(clientType)) {
int irodsPort;
irodsPort = (StringUtils.isBlank(fileSystem.getClientOption(NfsFileSystemOption.IRODS_PORT))) ? IRODS_DEFAULT_PORT : Integer.parseInt(fileSystem.getClientOption(NfsFileSystemOption.IRODS_PORT));
return new IRODSClient(
new IRODSAccount(
fileSystem.getUrl(),
irodsPort,
nfsusername,
nfspassword,
fileSystem.getClientOption(NfsFileSystemOption.IRODS_HOME_DIR),
fileSystem.getClientOption(NfsFileSystemOption.IRODS_ZONE),
""),
new JargonFacade());
}
return null;
}
private void configureExtraSystemProperties() {
if (!extraSystemPropsConfigured) {
extraSystemPropsConfigured = true;
if (!StringUtils.isEmpty(extraSystemProps)) {
String[] props = StringUtils.split(extraSystemProps, ",");
for (String prop : props) {
String[] propNameValue = StringUtils.split(prop, "=");
log.info("Setting system property '{}' to '{}'", propNameValue[0], propNameValue[1]);
System.setProperty(propNameValue[0], propNameValue[1]);
}
}
}
}
public NfsClient getNfsClient(UserKeyPair userKeyPair, NfsFileSystem fileSystem) {
verifyFileSystemProperties(fileSystem);
NfsClientType clientType = fileSystem.getClientType();
if (NfsClientType.SAMBA.equals(clientType) || NfsClientType.SMBJ.equals(clientType)) {
throw new UnsupportedOperationException(
"samba connection not supported with pubkey authentication");
}
if (NfsClientType.SFTP.equals(clientType)) {
return new SftpClient(
userKeyPair,
passphrase,
fileSystem.getUrl(),
fileSystem.getClientOption(NfsFileSystemOption.SFTP_SERVER_PUBLIC_KEY));
}
return null;
}
public NfsAuthentication getNfsAuthentication(NfsFileSystem fileSystem) {
verifyFileSystemProperties(fileSystem);
return getNfsAuthentication(fileSystem.getAuthType());
}
public NfsAuthentication getNfsAuthentication(NfsAuthenticationType authType) {
if (NfsAuthenticationType.PUBKEY.equals(authType)) {
return publicKeyAuthentication;
}
if (NfsAuthenticationType.PASSWORD.equals(authType)) {
return userPasswordAuthentication;
}
return null;
}
private void verifyFileSystemProperties(NfsFileSystem fileSystem) {
if (StringUtils.isEmpty(fileSystem.getUrl())) {
throw new IllegalStateException("nfsFileSystem url is empty");
}
if (fileSystem.getClientType() == null) {
throw new IllegalStateException("nfsFileSystem client type is empty");
}
if (fileSystem.getAuthType() == null) {
throw new IllegalStateException("nfsFileSystem authentication type is empty");
}
}
}