Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SR2-projet1-VANDEWAETER
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabio Vandewaeter
SR2-projet1-VANDEWAETER
Commits
913b9285
Commit
913b9285
authored
2 months ago
by
Fabio Vandewaeter
Browse files
Options
Downloads
Patches
Plain Diff
pom + chemin download
parent
482bbe71
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pom.xml
+2
-0
2 additions, 0 deletions
pom.xml
src/main/java/fil/sr2/flopbox/AuthFilter.java
+0
-29
0 additions, 29 deletions
src/main/java/fil/sr2/flopbox/AuthFilter.java
src/main/java/fil/sr2/flopbox/FTPResource.java
+61
-12
61 additions, 12 deletions
src/main/java/fil/sr2/flopbox/FTPResource.java
with
63 additions
and
41 deletions
pom.xml
+
2
−
0
View file @
913b9285
...
...
@@ -116,5 +116,7 @@
<properties>
<jersey.version>
3.0.2
</jersey.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.release>
17
</maven.compiler.release>
</properties>
</project>
This diff is collapsed.
Click to expand it.
src/main/java/fil/sr2/flopbox/AuthFilter.java
+
0
−
29
View file @
913b9285
...
...
@@ -14,35 +14,6 @@ import jakarta.ws.rs.ext.Provider;
public
class
AuthFilter
implements
ContainerRequestFilter
{
private
static
final
Map
<
String
,
String
>
USERS
=
loadUsers
();
/*
* @Override
* public void filter(ContainerRequestContext requestContext) throws IOException
* {
* // Récupération de l'en-tête Authorization (ex.
* "Bearer flopbox-secret-token")
* String authHeader =
* requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
* if (authHeader == null || !authHeader.startsWith("Bearer ")) {
* requestContext.abortWith(
* Response.status(Response.Status.UNAUTHORIZED)
* .entity("En-tête Authorization manquant ou invalide")
* .build());
* return;
* }
* String token = authHeader.substring("Bearer ".length());
* // Vérification du token (dans une implémentation réelle, vérification par
* // rapport à une liste de comptes stockés)
* if (!"flopbox-secret-token".equals(token)) {
* requestContext.abortWith(
* Response.status(Response.Status.UNAUTHORIZED)
* .entity("Token invalide")
* .build());
* return;
* }
* // Si le token est valide, la requête continue.
* }
*/
@Override
public
void
filter
(
ContainerRequestContext
ctx
)
throws
IOException
{
String
token
=
extractToken
(
ctx
);
...
...
This diff is collapsed.
Click to expand it.
src/main/java/fil/sr2/flopbox/FTPResource.java
+
61
−
12
View file @
913b9285
...
...
@@ -2,14 +2,18 @@ package fil.sr2.flopbox;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.core.*
;
import
java.io.Closeable
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.SocketException
;
import
java.net.URI
;
import
java.util.List
;
import
org.apache.commons.net.ftp.FTP
;
import
org.apache.commons.net.ftp.FTPClient
;
import
org.apache.commons.net.ftp.FTPFile
;
@Path
(
"/ftps"
)
public
class
FTPResource
{
...
...
@@ -98,29 +102,74 @@ public class FTPResource {
@HeaderParam
(
"X-FTP-Pass"
)
String
pass
)
{
FTPServerConfig
config
=
FTPServerRepository
.
getInstance
().
getServer
(
alias
);
if
(
config
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Serveur FTP non trouvé"
).
build
();
}
FTPClient
ftp
=
new
FTPClient
();
try
{
// Connexion au serveur FTP
ftp
.
connect
(
config
.
getHost
(),
config
.
getPort
());
ftp
.
login
(
user
,
pass
);
// path = "/home/m1gl/fabio.vandewaeter.etu/M1/SR2/serveur-ftp-test/" + path;
if
(!
ftp
.
login
(
user
,
pass
))
{
return
Response
.
status
(
Response
.
Status
.
UNAUTHORIZED
)
.
entity
(
"Authentification FTP échouée"
).
build
();
}
// Configuration du transfert
ftp
.
enterLocalPassiveMode
();
ftp
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
// Vérification de l'existence du fichier
FTPFile
[]
files
=
ftp
.
listFiles
(
path
);
if
(
files
.
length
==
0
||
files
[
0
].
isDirectory
())
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Fichier non trouvé : "
+
path
).
build
();
}
// Récupération du flux FTP
InputStream
is
=
ftp
.
retrieveFileStream
(
path
);
if
(
is
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"
Could not retrieve file : "
+
path
).
build
();
.
entity
(
"
Échec de la récupération du fichier"
).
build
();
}
return
Response
.
ok
(
is
)
// Création de la réponse avec fermeture automatique des ressources
return
Response
.
ok
(
new
InputStreamResource
(
is
,
ftp
))
.
header
(
"Content-Disposition"
,
"attachment; filename=\""
+
getFileName
(
path
)
+
"\""
)
.
build
();
}
catch
(
IOException
e
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
).
build
();
}
finally
{
try
{
if
(
ftp
.
isConnected
())
{
ftp
.
logout
();
ftp
.
disconnect
();
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Erreur FTP : "
+
e
.
getMessage
()).
build
();
}
}
// Classe utilitaire pour gérer le flux et fermer les ressources
private
static
class
InputStreamResource
implements
StreamingOutput
{
private
final
InputStream
inputStream
;
private
final
FTPClient
ftpClient
;
public
InputStreamResource
(
InputStream
inputStream
,
FTPClient
ftpClient
)
{
this
.
inputStream
=
inputStream
;
this
.
ftpClient
=
ftpClient
;
}
@Override
public
void
write
(
OutputStream
output
)
throws
IOException
,
WebApplicationException
{
try
(
inputStream
)
{
byte
[]
buffer
=
new
byte
[
4096
];
int
bytesRead
;
while
((
bytesRead
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
output
.
write
(
buffer
,
0
,
bytesRead
);
}
}
finally
{
if
(
ftpClient
.
isConnected
())
{
ftpClient
.
completePendingCommand
();
ftpClient
.
logout
();
ftpClient
.
disconnect
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment