1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.common.utility.sftp;
\r
19 import org.apache.commons.io.IOUtils;
\r
20 import org.apache.commons.vfs2.*;
\r
21 import org.apache.commons.vfs2.provider.sftp.IdentityInfo;
\r
22 import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
\r
24 import java.io.File;
\r
25 import java.io.InputStream;
\r
28 public class SftpUtility {
\r
30 public static byte[] getFile(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase) throws Exception {
\r
31 String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;
\r
33 FileSystemOptions fsOptions = new FileSystemOptions();
\r
34 FileSystemManager fsManager = null;
\r
35 byte[] bytes = null;
\r
36 SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
\r
37 builder.setUserDirIsRoot(fsOptions, false);
\r
38 builder.setStrictHostKeyChecking(fsOptions, "no");
\r
39 IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());
\r
40 builder.setIdentityInfo(fsOptions, identityInfo);
\r
41 fsManager = VFS.getManager();
\r
42 FileObject remoteFileObject = fsManager.resolveFile(remoteURI, fsOptions);
\r
43 if(!remoteFileObject.isFile()) {
\r
44 remoteFileObject.close();
\r
45 throw new Exception("Expected a file, but supplied filePath was not a file.");
\r
47 InputStream is = remoteFileObject.getContent().getInputStream();
\r
48 bytes = IOUtils.toByteArray(is);
\r
49 remoteFileObject.close();
\r
54 public static FileObject getDirectory(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase) throws Exception {
\r
55 String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;
\r
57 FileSystemOptions fsOptions = new FileSystemOptions();
\r
58 FileSystemManager fsManager = null;
\r
59 SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
\r
60 builder.setUserDirIsRoot(fsOptions, false);
\r
61 builder.setStrictHostKeyChecking(fsOptions, "no");
\r
62 IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());
\r
63 builder.setIdentityInfo(fsOptions, identityInfo);
\r
64 fsManager = VFS.getManager();
\r
65 FileObject fileObject = fsManager.resolveFile(remoteURI, fsOptions);
\r
66 if(fileObject.isFolder()) {
\r
70 throw new Exception("Expected a folder, but supplied filePath was not a folder.");
\r
73 public static void uploadFile(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase, File tempFile) throws Exception {
\r
74 String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;
\r
76 FileSystemOptions fsOptions = new FileSystemOptions();
\r
77 FileSystemManager fsManager = null;
\r
78 SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
\r
79 builder.setUserDirIsRoot(fsOptions, false);
\r
80 builder.setStrictHostKeyChecking(fsOptions, "no");
\r
81 IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());
\r
82 builder.setIdentityInfo(fsOptions, identityInfo);
\r
83 fsManager = VFS.getManager();
\r
84 //resolve file location
\r
85 FileObject remoteFileObject = fsManager.resolveFile(remoteURI, fsOptions);
\r
86 FileObject sourceFile = fsManager.resolveFile(tempFile.getAbsolutePath());
\r
87 //if file exists then override, else create file
\r
88 remoteFileObject.copyFrom(sourceFile, Selectors.SELECT_SELF);
\r
89 remoteFileObject.close();
\r