blob: 56735fde01493b4c6560cfa63b1280380d7b47af (
plain)
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
|
package org.onap.a1pesimulator.service.fileready;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.onap.a1pesimulator.data.fileready.FileData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@Service
@PropertySource("classpath:application.properties")
public class FtpServerService {
private static final Logger log = LoggerFactory.getLogger(FtpServerService.class);
@Value("${ftp.server.url}")
private String ftpServerUrl;
@Value("${ftp.server.protocol}")
private String ftpServerProtocol;
@Value("${ftp.server.port}")
private String ftpServerPort;
@Value("${ftp.server.filepath}")
private String ftpServerFilepath;
@Value("${ftp.server.username}")
private String ftpServerUsername;
@Value("${ftp.server.password}")
private String ftpServerPassword;
public Mono<FileData> uploadFileToFtp(FileData fileData) {
return Mono.just(fileData)
.flatMap(this::tryToCompressFile)
.flatMap(this::tryToUploadFileToFtp)
.onErrorResume(throwable -> resumeError(throwable, fileData))
.doOnNext(file -> deletePMBulkFile(file.getPmBulkFile()));
}
/**
* Trying to compress file into .gz
*
* @param fileData file to be archived
* @return archived file
*/
private Mono<FileData> tryToCompressFile(FileData fileData) {
fileData.setArchivedPmBulkFile(fileData.getPmBulkFile());
return Mono.just(fileData);
}
/**
* Upload file to FTP
*
* @param fileData archived file in Mono
* @return archived file for fileReadyEvent
*/
private Mono<FileData> tryToUploadFileToFtp(FileData fileData) {
return Mono.just(fileData);
}
/**
* Deletes created PM Bulk File xml from temp storage after successful upload to FTP
*
* @param file file which we gonna delete
*/
public static void deletePMBulkFile(File file) {
try {
log.trace("Deleting file: {}", file.getAbsoluteFile());
Files.delete(file.toPath());
} catch (IOException e) {
log.warn("Could not delete file: {}", file.getName(), e);
}
}
/**
* Get path to FTP server
*
* @return for example: "sftp://foo:pass@106.120.119.170:2222/upload/"
*/
public String getFtpPath() {
return ftpServerProtocol + "://" + ftpServerUsername + ":" + ftpServerPassword + "@" + ftpServerUrl + ":" + ftpServerPort + "/" + ftpServerFilepath
+ "/";
}
/**
* Try to clean up things after an exception
*/
private Mono<FileData> resumeError(Throwable throwable, FileData fileData) {
log.error("Error occurs while uploading file to FTP server", throwable);
deletePMBulkFile(fileData.getPmBulkFile());
deletePMBulkFile(fileData.getArchivedPmBulkFile());
return Mono.empty();
}
}
|