added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / common / utility / sftp / SftpUtility.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/common/utility/sftp/SftpUtility.java b/otf-camunda/src/main/java/org/oran/otf/common/utility/sftp/SftpUtility.java
new file mode 100644 (file)
index 0000000..cdaf8fd
--- /dev/null
@@ -0,0 +1,94 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.common.utility.sftp;\r
+\r
+import org.apache.commons.io.IOUtils;\r
+import org.apache.commons.vfs2.*;\r
+import org.apache.commons.vfs2.provider.sftp.IdentityInfo;\r
+import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;\r
+\r
+import java.io.File;\r
+import java.io.InputStream;\r
+\r
+\r
+public class SftpUtility {\r
+\r
+    public static byte[] getFile(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase) throws Exception {\r
+        String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;\r
+\r
+        FileSystemOptions fsOptions = new FileSystemOptions();\r
+        FileSystemManager fsManager = null;\r
+        byte[] bytes = null;\r
+        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();\r
+        builder.setUserDirIsRoot(fsOptions, false);\r
+        builder.setStrictHostKeyChecking(fsOptions, "no");\r
+        IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());\r
+        builder.setIdentityInfo(fsOptions, identityInfo);\r
+        fsManager = VFS.getManager();\r
+        FileObject remoteFileObject = fsManager.resolveFile(remoteURI, fsOptions);\r
+        if(!remoteFileObject.isFile()) {\r
+            remoteFileObject.close();\r
+            throw new Exception("Expected a file, but supplied filePath was not a file.");\r
+        }\r
+        InputStream is = remoteFileObject.getContent().getInputStream();\r
+        bytes = IOUtils.toByteArray(is);\r
+        remoteFileObject.close();\r
+        return bytes;\r
+\r
+    }\r
+\r
+    public static FileObject getDirectory(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase) throws Exception {\r
+        String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;\r
+\r
+        FileSystemOptions fsOptions = new FileSystemOptions();\r
+        FileSystemManager fsManager = null;\r
+        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();\r
+        builder.setUserDirIsRoot(fsOptions, false);\r
+        builder.setStrictHostKeyChecking(fsOptions, "no");\r
+        IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());\r
+        builder.setIdentityInfo(fsOptions, identityInfo);\r
+        fsManager = VFS.getManager();\r
+        FileObject fileObject = fsManager.resolveFile(remoteURI, fsOptions);\r
+        if(fileObject.isFolder()) {\r
+            return fileObject;\r
+        }\r
+        fileObject.close();\r
+        throw new Exception("Expected a folder, but supplied filePath was not a folder.");\r
+    }\r
+\r
+    public static void uploadFile(String host, String artifactPath, String privateKeyPath, String privateKeyUsername, String privateKeyPasspharase, File tempFile) throws Exception {\r
+        String remoteURI = "sftp://" + privateKeyUsername + "@" + host + "/" + artifactPath;\r
+\r
+        FileSystemOptions fsOptions = new FileSystemOptions();\r
+        FileSystemManager fsManager = null;\r
+        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();\r
+        builder.setUserDirIsRoot(fsOptions, false);\r
+        builder.setStrictHostKeyChecking(fsOptions, "no");\r
+        IdentityInfo identityInfo = new IdentityInfo(new File(privateKeyPath), privateKeyPasspharase.getBytes());\r
+        builder.setIdentityInfo(fsOptions, identityInfo);\r
+        fsManager = VFS.getManager();\r
+        //resolve file location\r
+        FileObject remoteFileObject = fsManager.resolveFile(remoteURI, fsOptions);\r
+        FileObject sourceFile = fsManager.resolveFile(tempFile.getAbsolutePath());\r
+        //if file exists then override, else create file\r
+        remoteFileObject.copyFrom(sourceFile, Selectors.SELECT_SELF);\r
+        remoteFileObject.close();\r
+        sourceFile.close();\r
+    }\r
+\r
+\r
+}\r