/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vn.mobile.id.fms;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.apache.log4j.Logger;
import vn.mobile.id.objects.FmsUuidListJsonObject;
import vn.mobile.id.utils.Configuration;
import vn.mobile.id.utils.Utils;
import vn.mobileid.fms.client.JCRConfig;
import vn.mobileid.fms.client.JCRException;
import vn.mobileid.fms.client.JCRFile;
import vn.mobileid.fms.client.JCRManager;
import vn.mobileid.fms.client.loadbalancing.HealcheckConfig;
/**
*
* @author TuoiCM
*/
public class FmsClientManager {
//
private static final Logger logger = Logger.getLogger(FmsClientManager.class);
private String uri;
private String username;
private String password;
private int maxSession;
private int maxFileInFolder;
private String workspaceName; // relying party name
private String folderPrefix; // hostname + @ + IP
private HealcheckConfig healcheckConfig;
private JCRConfig jcrConfig;
final private int DOWNLOAD_TIMEOUT = 5 * 60; // second
final private int UPLOAD_TIMEOUT = 5 * 60; //second
private String rpName;
//
//
public FmsClientManager(String rpName) {
try {
this.rpName = rpName.toUpperCase();
this.uri = Configuration.getInstance(this.rpName).getFmsUrl();
this.username = Configuration.getInstance(this.rpName).getFmsUsername();
this.password = Configuration.getInstance(this.rpName).getFmsPassword();
this.maxSession = Integer.parseInt(Configuration.getInstance(this.rpName).getFmsMaxSession());
this.maxFileInFolder = Integer.parseInt(Configuration.getInstance(this.rpName).getFmsMaxFileInFolder());
this.workspaceName = Configuration.getInstance(this.rpName).getFmsWorkSpace();
healcheckConfig = new HealcheckConfig();
} catch (Exception ex) {
logger.error("CTOR FmsClientManager " + Utils.printStackTrace(ex));
}
}
//
//
public void condifFms() {
try {
if (Utils.isNullOrEmpty(this.uri)) {
jcrConfig = null;
} else {
logger.info("----------- RP " + this.rpName + " FMS CONFIG -----------");
logger.debug("URL " + (Utils.isNullOrEmpty(this.uri) ? "NULL" : this.uri));
logger.debug("USERNAME " + (Utils.isNullOrEmpty(this.username) ? "NULL" : this.username));
logger.debug("PASSWORD " + (Utils.isNullOrEmpty(this.password) ? "NULL" : this.password));
logger.debug("MAX SESSION " + this.maxSession);
logger.debug("MAX FILE IN FOLDER " + this.maxFileInFolder);
logger.debug("WORKSPACE NAME " + (Utils.isNullOrEmpty(this.workspaceName) ? "NULL" : this.workspaceName));
logger.debug("DOWLOAD TIMEOUT " + this.DOWNLOAD_TIMEOUT);
logger.info("----------- RP " + this.rpName + " FMS CONFIG -----------");
jcrConfig = new JCRConfig();
jcrConfig.setHost(this.uri);
jcrConfig.setUserID(this.username);
jcrConfig.setPassword(this.password);
jcrConfig.setMaxSession(this.maxSession);
jcrConfig.setMaxFileInFolder(this.maxFileInFolder);
jcrConfig.setWorkSpaceName(this.workspaceName);
jcrConfig.setFolderPrefix(this.folderPrefix);
jcrConfig.setTimeoutUpload(this.UPLOAD_TIMEOUT);
jcrConfig.setTimeoutDownload(this.DOWNLOAD_TIMEOUT);
}
} catch (Exception ex) {
logger.error("CONFIG FMS FAIL " + Utils.printStackTrace(ex));
}
}
//
//
public void uploadFileFms(String filePathNeedUpload, String fileName, String filePathUploaded) {
try {
if (null == this.jcrConfig) {
logger.error("CAN NOT CONFIG FMS");
return;
}
Path path = Paths.get(filePathNeedUpload + fileName);
InputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(path));
JCRFile uploadFile = new JCRFile();
uploadFile.setFilePath(filePathUploaded);
uploadFile.setMimeType(Files.probeContentType(path));
uploadFile.setStream(inputStream);
JCRFile referFile = JCRManager.getInstance(jcrConfig, healcheckConfig).uploadJCR(uploadFile);
logger.debug("=> " + referFile.getUuid());
String fullDownloadUri = "/" + URLEncoder.encode(workspaceName, "UTF-8")
+ "/" + URLEncoder.encode(referFile.getFolderName(), "UTF-8")
+ "/" + URLEncoder.encode(fileName, "UTF-8");
logger.debug("FULL DOWNLOAD URI " + fullDownloadUri);
logger.info("UPLOAD FILE TO FMS SERVER SUCCESS");
} catch (Exception ex) {
logger.error(Utils.printStackTrace(ex));
}
}
//
//
public void downloadFileFms(String filePathDownloadSave, String fileNameDownloadSave, String uuid) {
try {
if (null == this.jcrConfig) {
logger.error("CAN NOT CONFIG FMS");
return;
}
JCRManager jcrManager = JCRManager.getInstance(jcrConfig, healcheckConfig);
JCRFile downloadFile = jcrManager.downloadJCR(uuid);
logger.debug("FILE NAME DOWNLOAD " + downloadFile.getFileName());
logger.debug("MIME TYPE DOWNLOAD " + downloadFile.getMimeType());
logger.debug("FOLDER NAME DOWNLOAD " + downloadFile.getFolderName());
logger.debug("FILE PATH DOWNLOAD " + downloadFile.getFilePath());
File folderSaveFiles = new File(filePathDownloadSave);
boolean isCreatedFolderSaveFiles = folderSaveFiles.mkdir();
if (isCreatedFolderSaveFiles) {
logger.warn("CREATED NEW FOLDER SAVE FILE " + folderSaveFiles);
} else {
logger.warn("FOLDER SAVE FILE ALREADY EXISTS " + folderSaveFiles);
}
String fullPathFileToSave = filePathDownloadSave + fileNameDownloadSave;
File file = new File(fullPathFileToSave);
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
int read;
byte[] bytes = new byte[1024 * 5];
while ((read = downloadFile.getStream().read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
logger.info("DOWNLOAD FILE FMS SERVER SUCCESS " + fullPathFileToSave);
}
} catch (IOException | JCRException ex) {
logger.error("DOWNLOAD FILE TO FMS SERVER FAIL " + Utils.printStackTrace(ex));
}
}
//
//
public void downloadFileFmsUsingUuidListFile(String filePathDownloadSave, String pathUuidListFile) {
try {
if (null == this.jcrConfig) {
logger.error("CAN NOT CONFIG FMS");
return;
}
File folderSaveFiles = new File(filePathDownloadSave);
boolean isCreatedFolderSaveFiles = folderSaveFiles.mkdir();
if (isCreatedFolderSaveFiles) {
logger.warn("CREATED NEW FOLDER SAVE FILE " + folderSaveFiles);
} else {
logger.warn("FOLDER SAVE FILE ALREADY EXISTS " + folderSaveFiles);
}
JCRManager jcrManager = JCRManager.getInstance(jcrConfig, healcheckConfig);
FmsUuidListJsonObject uuidListJsonObject = Utils.getUuidListJson(pathUuidListFile);
List listUuidFrames = null;
List listUuidVideos = null;
if (null == uuidListJsonObject) {
logger.error("CAN NOT GET UUID LIST FROM FILE");
return;
} else {
if (null != uuidListJsonObject.getFrames()) {
if (null != uuidListJsonObject.getFrames().getUuids()) {
listUuidFrames = uuidListJsonObject.getFrames().getUuids();
}
}
if (null != uuidListJsonObject.getVideos()) {
if (null != uuidListJsonObject.getVideos().getUuids()) {
listUuidVideos = uuidListJsonObject.getVideos().getUuids();
}
}
}
//Download frames
int countFrames = 1;
if (null != listUuidFrames && !listUuidFrames.isEmpty()) {
logger.info("START DOWNLOAD FRAMES...");
for (String uuidFrame : listUuidFrames) {
JCRFile downloadFile = jcrManager.downloadJCR(uuidFrame);
// logger.debug("FILE NAME DOWNLOAD " + downloadFile.getFileName());
// logger.debug("MIME TYPE DOWNLOAD " + downloadFile.getMimeType());
// logger.debug("FOLDER NAME DOWNLOAD " + downloadFile.getFolderName());
// logger.debug("FILE PATH DOWNLOAD " + downloadFile.getFilePath());
String fullPathFileToSave = filePathDownloadSave + "frame" + countFrames + "_" + uuidFrame + ".jpg";
File file = new File(fullPathFileToSave);
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
int read;
byte[] bytes = new byte[1024 * 5];
while ((read = downloadFile.getStream().read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
logger.info("DOWNLOAD FILE FMS SERVER SUCCESS " + fullPathFileToSave);
}
countFrames++;
}
} else {
logger.warn("NOT DOWNLOAD FRAMES CAUSE LIST UUID IS NULL OR EMPTY");
}
//Dowload videos
int countVideos = 1;
if (null != listUuidVideos && !listUuidVideos.isEmpty()) {
logger.info("START DOWNLOAD VIDEOS...");
for (String uuidVideo : listUuidVideos) {
JCRFile downloadFile = jcrManager.downloadJCR(uuidVideo);
// logger.debug("FILE NAME DOWNLOAD " + downloadFile.getFileName());
// logger.debug("MIME TYPE DOWNLOAD " + downloadFile.getMimeType());
// logger.debug("FOLDER NAME DOWNLOAD " + downloadFile.getFolderName());
// logger.debug("FILE PATH DOWNLOAD " + downloadFile.getFilePath());
String fullPathFileToSave = filePathDownloadSave + "video" + countVideos + "_" + uuidVideo + ".mp4";
File file = new File(fullPathFileToSave);
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
int read;
byte[] bytes = new byte[1024 * 5];
while ((read = downloadFile.getStream().read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
logger.info("DOWNLOAD FILE FMS SERVER SUCCESS " + fullPathFileToSave);
}
countVideos++;
}
} else {
logger.warn("NOT DOWNLOAD VIDEOS CAUSE LIST UUID IS NULL OR EMPTY");
}
} catch (IOException | JCRException ex) {
logger.error("DOWNLOAD FILE TO FMS SERVER FAIL " + Utils.printStackTrace(ex));
}
}
//
//
public static Logger getLogger() {
return logger;
}
public String getUri() {
return uri;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getMaxSession() {
return maxSession;
}
public int getMaxFileInFolder() {
return maxFileInFolder;
}
public String getWorkspaceName() {
return workspaceName;
}
public String getFolderPrefix() {
return folderPrefix;
}
public HealcheckConfig getHealcheckConfig() {
return healcheckConfig;
}
public JCRConfig getJcrConfig() {
return jcrConfig;
}
public int getDOWNLOAD_TIMEOUT() {
return DOWNLOAD_TIMEOUT;
}
public int getUPLOAD_TIMEOUT() {
return UPLOAD_TIMEOUT;
}
//
}