use of org.apache.commons.vfs2.FileSystemOptions in project agileway by fangjinuo.
the class SftpFileProvider method doCreateFileSystem.
@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions) throws FileSystemException {
// Create the file system
final GenericFileName root = (GenericFileName) rootName;
SshConnectionFactory sshConnectionFactory = new SshConnectionFactoryRegistry().getDefault();
SshConnectionConfig connectionConfig = sshConnectionFactory.newConfig();
connectionConfig.setHost(root.getHostName());
connectionConfig.setPort(root.getPort());
connectionConfig.setUser(root.getUserName());
connectionConfig.setPassword(root.getPassword());
SftpFileSystemConfigBuilder configBuilder = (SftpFileSystemConfigBuilder) getConfigBuilder();
final File knownHostsFile = configBuilder.getKnownHosts(fileSystemOptions);
if (knownHostsFile != null) {
connectionConfig.setKnownHostsPath(knownHostsFile.getAbsolutePath());
}
// JSCH 特有属性:
Integer timout = configBuilder.getTimeout(fileSystemOptions);
if (timout != null) {
connectionConfig.setProperty("ConnectTimeout", timout);
}
String strictHostKeyChecking = configBuilder.getStrictHostKeyChecking(fileSystemOptions);
if (Strings.isNotEmpty(strictHostKeyChecking)) {
connectionConfig.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
}
final String preferredAuthentications = configBuilder.getPreferredAuthentications(fileSystemOptions);
if (preferredAuthentications != null) {
connectionConfig.setProperty("PreferredAuthentications", preferredAuthentications);
}
// set compression property
final String compression = configBuilder.getCompression(fileSystemOptions);
if (compression != null) {
connectionConfig.setProperty("compression.s2c", compression);
connectionConfig.setProperty("compression.c2s", compression);
}
SshConnection sshConnection = sshConnectionFactory.get(connectionConfig);
SftpSession session = sshConnection.openSftpSession();
return new SftpFileSystem(root, session, null, fileSystemOptions);
}
use of org.apache.commons.vfs2.FileSystemOptions in project agileway by fangjinuo.
the class AgilewaySftpProviderTests method testListChildren.
@Test
public void testListChildren() throws Throwable {
DefaultFileSystemManager fileSystemManager = (DefaultFileSystemManager) VFS.getManager();
String url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test";
FileSystemOptions fileSystemOptions = new FileSystemOptions();
SftpFileSystemConfigBuilder configBuilder = SftpFileSystemConfigBuilder.getInstance();
configBuilder.setUserDirIsRoot(fileSystemOptions, true);
FileObject fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
System.out.println("===============Show remote directory==============");
showFile(0, fileObject);
System.out.println("===============Copy remote files to local==============");
url = "file://d:/tmp002";
FileObject localFileObject = fileSystemManager.resolveFile(url);
if (fileObject.isFolder()) {
if (!localFileObject.exists()) {
localFileObject.createFolder();
} else {
localFileObject.delete(Selectors.EXCLUDE_SELF);
}
localFileObject.copyFrom(fileObject, Selectors.EXCLUDE_SELF);
} else {
// 单独测试 文件时,将上面的 url 改成一个 文件的url即可
long writeSize = fileObject.getContent().write(localFileObject);
long expectedSize = fileObject.getContent().getSize();
Preconditions.checkTrue(writeSize == expectedSize);
localFileObject.copyFrom(fileObject, Selectors.SELECT_SELF);
}
System.out.println("================Copy local files to remote=============");
url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test2";
fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
if (!fileObject.exists()) {
fileObject.createFolder();
} else {
fileObject.delete(Selectors.EXCLUDE_SELF);
}
fileObject.copyFrom(localFileObject, Selectors.EXCLUDE_SELF);
}
use of org.apache.commons.vfs2.FileSystemOptions in project commons-vfs by apache.
the class HttpClientFactory method createConnection.
/**
* Creates a new connection to the server.
*
* @param builder The HttpFileSystemConfigBuilder.
* @param scheme The protocol.
* @param hostname The hostname.
* @param port The port number.
* @param username The username.
* @param password The password
* @param fileSystemOptions The file system options.
* @return a new HttpClient connection.
* @throws FileSystemException if an error occurs.
* @since 2.0
*/
public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme, final String hostname, final int port, final String username, final String password, final FileSystemOptions fileSystemOptions) throws FileSystemException {
final HttpClient client;
try {
final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();
client = new HttpClient(mgr);
final HostConfiguration config = new HostConfiguration();
config.setHost(hostname, port, scheme);
if (fileSystemOptions != null) {
final String proxyHost = builder.getProxyHost(fileSystemOptions);
final int proxyPort = builder.getProxyPort(fileSystemOptions);
if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
config.setProxy(proxyHost, proxyPort);
}
final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
if (proxyAuth != null) {
final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD });
if (authData != null) {
final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
client.getState().setProxyCredentials(scope, proxyCreds);
}
if (builder.isPreemptiveAuth(fileSystemOptions)) {
final HttpClientParams httpClientParams = new HttpClientParams();
httpClientParams.setAuthenticationPreemptive(true);
client.setParams(httpClientParams);
}
}
final Cookie[] cookies = builder.getCookies(fileSystemOptions);
if (cookies != null) {
client.getState().addCookies(cookies);
}
}
/*
* ConnectionManager set methods must be called after the host & port and proxy host & port are set in the
* HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams tries to locate
* the host configuration.
*/
connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions));
connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));
connectionMgrParams.setConnectionTimeout(DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)));
connectionMgrParams.setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)));
client.setHostConfiguration(config);
if (username != null) {
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
client.getState().setCredentials(scope, creds);
}
} catch (final Exception exc) {
throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
}
return client;
}
use of org.apache.commons.vfs2.FileSystemOptions in project commons-vfs by apache.
the class Http4FileProvider method doCreateFileSystem.
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException {
final GenericFileName rootName = (GenericFileName) name;
UserAuthenticationData authData = null;
HttpClient httpClient;
HttpClientContext httpClientContext;
try {
final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
httpClient = createHttpClient(builder, rootName, fileSystemOptions);
} finally {
UserAuthenticatorUtils.cleanup(authData);
}
return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
use of org.apache.commons.vfs2.FileSystemOptions in project commons-vfs by apache.
the class DefaultURLStreamHandler method parseURL.
@Override
protected void parseURL(final URL u, final String spec, final int start, final int limit) {
try {
final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
final FileObject newURL;
if (start > 0 && spec.charAt(start - 1) == ':') {
newURL = context.resolveFile(old, spec, fileSystemOptions);
} else if (old.isFile() && old.getParent() != null) {
// for files we have to resolve relative
newURL = old.getParent().resolveFile(spec);
} else {
newURL = old.resolveFile(spec);
}
final String url = newURL.getName().getURI();
final StringBuilder filePart = new StringBuilder();
final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
} catch (final FileSystemException fse) {
// This is rethrown to MalformedURLException in URL anyway
throw new RuntimeException(fse.getMessage());
}
}
Aggregations