Skip to content
Snippets Groups Projects
Commit 1e4e7f1f authored by fabiovandewaeter's avatar fabiovandewaeter
Browse files

refactoring

parent c88cd315
Branches
Tags
No related merge requests found
......@@ -79,26 +79,11 @@ Puis ouvrir le fichier `target/site/apidocs/index.html`
**Note comprise entre 10 et 11 si le code compile et peut être lancé pour afficher l'arborescence d'un serveur FTP via le proxy FlopBox:**
```shell
curl -X GET -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: anonymous" -H "X-FTP-Pass: anonymous" localhost:8080/ftps/mon-ftp/folder
Note: Unnecessary use of -X or --request, GET is already inferred.
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /ftps/mon-ftp/folder HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> Authorization: Bearer valid-token-1
> X-FTP-User: anonymous
> X-FTP-Pass: anonymous
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 264
<
* Connection #0 to host localhost left intact
{"name":"folder","isDirectory":true,"children":[{"name":"sousdossier","isDirectory":true,"children":[{"name":"test12","isDirectory":false,"children":null}]},{"name":"test11","isDirectory":false,"children":null},{"name":"tree","isDirectory":false,"children":null}]}
curl -X GET -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: anonymous" -H "X-FTP-Pass: anonymous" localhost:8080/ftps/list/mon-ftp/
```
```shell
curl -X GET -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: anonymous" -H "X-FTP-Pass: anonymous" localhost:8080/ftps/list/mon-ftp/dossier1
```
**Note comprise entre 11 et 12 si—en plus—le proxy FlopBox, permet de télécharger (download) et téléverser (upload) un petit fichier texte:**
......
FICHIER11
FICHIER1
package fil.sr2.flopbox;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
public class FTPClientFactory {
public static FTPClient createClient(String alias) throws FTPException {
FTPServerConfig config = FTPServerRepository.getInstance().getServer(alias);
if (config == null) {
throw new FTPException("Serveur FTP non trouvé", 404);
}
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(config.getHost(), config.getPort());
} catch (IOException e) {
throw new FTPException("Erreur de connexion FTP: " + e.getMessage(), 500);
}
return ftpClient;
}
public static void disconnect(FTPClient ftpClient) {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package fil.sr2.flopbox;
public class FTPException extends Exception {
private final int status;
public FTPException(String message, int status) {
super(message);
this.status = status;
}
public int getStatus() {
return status;
}
}
This diff is collapsed.
package fil.sr2.flopbox;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FTPService {
public FtpNode getResourceTree(String alias, String path, String user, String pass)
throws IOException, FTPException {
FTPClient ftpClient = FTPClientFactory.createClient(alias);
try {
if (!ftpClient.login(user, pass)) {
throw new FTPException("Authentification FTP échouée", 401);
}
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// Normaliser le chemin en retirant le slash final s'il y en a un
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
FTPFile[] files = ftpClient.listFiles(path);
if (files == null || files.length == 0) {
throw new FTPException("Ressource non trouvée : " + path, 404);
}
return buildFtpTree(ftpClient, path);
} finally {
FTPClientFactory.disconnect(ftpClient);
}
}
// Méthode récursive pour construire l'arborescence
private FtpNode buildFtpTree(FTPClient ftpClient, String path) throws IOException {
FTPFile[] files = ftpClient.listFiles(path);
FtpNode node = new FtpNode(getFileName(path), true);
if (files != null) {
for (FTPFile file : files) {
String fullPath = path + "/" + file.getName();
if (file.isDirectory()) {
node.children.add(buildFtpTree(ftpClient, fullPath));
} else {
node.children.add(new FtpNode(file.getName(), false));
}
}
}
return node;
}
public void uploadFile(String alias, String path, String user, String pass, InputStream fileStream)
throws IOException, FTPException {
FTPClient ftp = FTPClientFactory.createClient(alias);
try {
if (!ftp.login(user, pass)) {
throw new FTPException("Authentification FTP échouée", 401);
}
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean success = ftp.storeFile(path, fileStream);
if (!success) {
throw new FTPException("Erreur lors de l'upload", 500);
}
} finally {
FTPClientFactory.disconnect(ftp);
}
}
public void createResource(String alias, String path, String user, String pass, String resourceType,
InputStream inputStream) throws IOException, FTPException {
FTPClient ftp = FTPClientFactory.createClient(alias);
try {
if (!ftp.login(user, pass)) {
throw new FTPException("Authentification FTP échouée", 401);
}
ftp.enterLocalPassiveMode();
if ("directory".equalsIgnoreCase(resourceType)) {
boolean created = ftp.makeDirectory(path);
if (!created) {
throw new FTPException("Erreur création répertoire", 400);
}
} else {
boolean created = ftp.storeFile(path, inputStream);
if (!created) {
throw new FTPException("Erreur création fichier", 400);
}
}
} finally {
FTPClientFactory.disconnect(ftp);
}
}
public void deleteResource(String alias, String path, String user, String pass) throws IOException, FTPException {
FTPClient ftp = FTPClientFactory.createClient(alias);
try {
if (!ftp.login(user, pass)) {
throw new FTPException("Authentification FTP échouée", 401);
}
ftp.enterLocalPassiveMode();
boolean deleted = deleteRecursive(ftp, path);
if (!deleted) {
throw new FTPException("Ressource non trouvée", 404);
}
} finally {
FTPClientFactory.disconnect(ftp);
}
}
// Méthode récursive de suppression
private boolean deleteRecursive(FTPClient ftp, String path) throws IOException {
if (ftp.deleteFile(path)) {
return true;
}
FTPFile[] files = ftp.listFiles(path);
if (files == null) {
return false;
}
for (FTPFile file : files) {
String fullPath = path + "/" + file.getName();
if (file.isDirectory()) {
if (!deleteRecursive(ftp, fullPath)) {
return false;
}
} else {
if (!ftp.deleteFile(fullPath)) {
return false;
}
}
}
return ftp.removeDirectory(path);
}
public void renameResource(String alias, String oldPath, String newPath, String user, String pass)
throws IOException, FTPException {
FTPClient ftp = FTPClientFactory.createClient(alias);
try {
if (!ftp.login(user, pass)) {
throw new FTPException("Authentification FTP échouée", 401);
}
ftp.enterLocalPassiveMode();
boolean success = ftp.rename(oldPath, newPath);
if (!success) {
throw new FTPException("Échec du renommage", 400);
}
} finally {
FTPClientFactory.disconnect(ftp);
}
}
// Méthode utilitaire pour extraire le nom du fichier/dossier à partir du chemin
private String getFileName(String path) {
return path.substring(path.lastIndexOf('/') + 1);
}
// Classe représentant un nœud de l'arborescence FTP
public static class FtpNode {
public String name;
public boolean isDirectory;
public List<FtpNode> children;
public FtpNode(String name, boolean isDirectory) {
this.name = name;
this.isDirectory = isDirectory;
if (isDirectory) {
this.children = new ArrayList<>();
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment