added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / common / utility / sftp / SftpUtility.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.common.utility.sftp;\r
18 \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
23 \r
24 import java.io.File;\r
25 import java.io.InputStream;\r
26 \r
27 \r
28 public class SftpUtility {\r
29 \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
32 \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
46         }\r
47         InputStream is = remoteFileObject.getContent().getInputStream();\r
48         bytes = IOUtils.toByteArray(is);\r
49         remoteFileObject.close();\r
50         return bytes;\r
51 \r
52     }\r
53 \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
56 \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
67             return fileObject;\r
68         }\r
69         fileObject.close();\r
70         throw new Exception("Expected a folder, but supplied filePath was not a folder.");\r
71     }\r
72 \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
75 \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
90         sourceFile.close();\r
91     }\r
92 \r
93 \r
94 }\r