diff --git a/pom.xml b/pom.xml index a2414c92c9598d283a9f0c05b22314012af691ee..2ce3239385f8d9cae655f95bd68a56865d6932ea 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.33</version> + </dependency> <dependency> <groupId>junit</groupId> diff --git a/src/main/java/fil/sr2/endpoints/ServiceFTPEndPoint.java b/src/main/java/fil/sr2/endpoints/ServiceFTPEndPoint.java index 23228e8d5e4358078f41c70d7978ab9914c3abb7..3cd7baf216626e63296111ebbfc54cc4e5cebf3b 100644 --- a/src/main/java/fil/sr2/endpoints/ServiceFTPEndPoint.java +++ b/src/main/java/fil/sr2/endpoints/ServiceFTPEndPoint.java @@ -174,7 +174,45 @@ public class ServiceFTPEndPoint { } return Response.ok(file).header("Content-Disposition", "attachment; filename=\"" + filename + "\"").build(); } + + + @GET + @Secured + @Path("{alias}/file/last-modified/{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + public Response getLastDateFile(@PathParam("alias") String alias, @PathParam("path") String path, + @DefaultValue("anonymous") @HeaderParam("username") String username, + @DefaultValue("anonymous") @HeaderParam("password") String psw, + @DefaultValue("pasv") @HeaderParam("mode") String mode, + @DefaultValue("FTPS") @HeaderParam("type") String type, @DefaultValue("21") @HeaderParam("port") int port) { + Response r = init(alias, type, username, psw, mode, port); + if (!(r.getStatus() == Response.Status.ACCEPTED.getStatusCode())) {// on verifie que l'init s'est bien passer + return r; + } + String date; + try { + + date = ftp.getModificationTime(path); + if(date ==null) { + return Response.status(Response.Status.FORBIDDEN) + .entity("file " + path + " not exist on the ftp server ") + .build(); + } + System.out.println(date); + + } catch (IOException e) { + return Response.status(Response.Status.FORBIDDEN) + .entity("file " + path + " not exist on the ftp server or you don't have the right to download it") + .build(); + } finally { + try { + ftp.disconnect(); + } catch (IOException e) { + } + } + return Response.ok(date).build(); + } /* * Méthode non fonctionnel qui permet de récuperer un dossier, de le zipper et * de l'envoyer au client @@ -352,6 +390,42 @@ public class ServiceFTPEndPoint { } return Response.ok("successfully renamed to " + rename).build(); } + + + + + + @PUT + @Secured + @Path("{alias}/relocate/{path: .*}") + @Produces(MediaType.TEXT_PLAIN) + public Response relocate(@PathParam("alias") String alias, @PathParam("path") String path, + @DefaultValue("anonymous") @HeaderParam("username") String username, + @DefaultValue("anonymous") @HeaderParam("password") String psw, + @DefaultValue("pasv") @HeaderParam("mode") String mode, + @DefaultValue("FTP") @HeaderParam("type") String type, @QueryParam("to") String rename, + @DefaultValue("21") @HeaderParam("port") int port) { + Response r = init(alias, type, username, psw, mode, port); + if (!(r.getStatus() == Response.Status.ACCEPTED.getStatusCode())) {// on verifie que l'init s'est bien passer + return r; + } +// if (path.endsWith("/")) { +// path = path.substring(0, path.length() - 1); +// } + try { + if (!ftp.rename(path, rename)) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } catch (IOException e) { + return Response.status(Response.Status.FORBIDDEN).entity("can't rename one file " + path + "!").build(); + } finally { + try { + ftp.disconnect(); + } catch (IOException e) { + } + } + return Response.ok("successfully renamed to " + rename).build(); + } /** * Methode qui permet d'upload un fichier sur le serveur, pour envoyer un diff --git a/src/main/java/fil/sr2/utils/Utils.java b/src/main/java/fil/sr2/utils/Utils.java index f206dfa4670bf0b054e40a5af7472650c0a0cc31..a5f435eda91297fe6d8d098671404be9cb997107 100644 --- a/src/main/java/fil/sr2/utils/Utils.java +++ b/src/main/java/fil/sr2/utils/Utils.java @@ -8,6 +8,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.PrintWriter; import java.net.SocketException; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -15,7 +16,7 @@ import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; - +import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; @@ -45,6 +46,8 @@ public class Utils { if (file.isDirectory()) { details = "[" + details + "]"; + }else { + details = "<" + details + ">"; } details += " " + file.getSize(); details += " " + file.getUser(); @@ -180,6 +183,7 @@ public class Utils { f.login(username, psw); f.sendCommand("PBSZ 0"); f.sendCommand("PROT P"); + f.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); if (mode.equals("pasv")) { f.enterLocalPassiveMode(); } else if (!mode.equals("port")) {