From 80a50e94aa83cd6311756c4f98e7577155f2ca13 Mon Sep 17 00:00:00 2001 From: Arimihanta Date: Tue, 16 Nov 2021 10:05:33 +0100 Subject: [PATCH 1/2] endpoint document (carto/basic) --- .../fr/lirmm/aren/service/DocumentService.java | 15 +++++++++++++++ .../fr/lirmm/aren/ws/rest/DocumentRESTFacade.java | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/lirmm/aren/service/DocumentService.java b/src/main/java/fr/lirmm/aren/service/DocumentService.java index 48719b3..bdb0f19 100644 --- a/src/main/java/fr/lirmm/aren/service/DocumentService.java +++ b/src/main/java/fr/lirmm/aren/service/DocumentService.java @@ -82,6 +82,21 @@ public class DocumentService extends AbstractService { return new HashSet(generateQuery(null, withDebates).getResultList()); } + public Set findAllByType(boolean withDebates, String type){ + TypedQuery query = getEntityManager().createQuery("SELECT do " + + "FROM Document do " + + (withDebates + ? "LEFT JOIN FETCH do.debates d " + : "") + + (type.equalsIgnoreCase("CARTO") + ?"WHERE do.type = :type " + :"WHERE do.type = :type OR do.type = NULL "), + Document.class) + .setParameter("type",type); + + return new HashSet(query.getResultList()); + } + /** * * @param document diff --git a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java index 505f095..0a04f3b 100644 --- a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java +++ b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java @@ -43,6 +43,13 @@ public class DocumentRESTFacade extends AbstractRESTFacade { return documentService.findAll(withDebates); } + @Path("{type}") + @RolesAllowed({"MODO"}) + public Set findAllByType(@PathParam("type") String type) { + boolean withDebates = this.overview == null; + return documentService.findAllByType(withDebates, type); + } + /** * * @param id @@ -57,7 +64,7 @@ public class DocumentRESTFacade extends AbstractRESTFacade { /** * - * @param id + * @param doc * @return */ @Override From 69e07a47c89179304c82209fd7b767360343b7db Mon Sep 17 00:00:00 2001 From: Arimihanta Date: Thu, 25 Nov 2021 17:03:48 +0300 Subject: [PATCH 2/2] endpoint upload file --- .../fr/lirmm/aren/model/aaf/UploadedFile.java | 13 ++++++ .../aren/ws/rest/DocumentRESTFacade.java | 46 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java diff --git a/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java b/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java new file mode 100644 index 0000000..41a979d --- /dev/null +++ b/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java @@ -0,0 +1,13 @@ +package fr.lirmm.aren.model.aaf; + +public class UploadedFile { + private String name ; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java index 0a04f3b..01e059d 100644 --- a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java +++ b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java @@ -1,14 +1,20 @@ package fr.lirmm.aren.ws.rest; +import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import fr.lirmm.aren.model.aaf.UploadedFile; import fr.lirmm.aren.service.DocumentService; import fr.lirmm.aren.model.Document; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; + +import java.io.*; import java.util.Set; /** @@ -88,4 +94,38 @@ public class DocumentRESTFacade extends AbstractRESTFacade { return this.create(document); } + + @POST + @Path("/map") + @PermitAll + @Consumes({MediaType.MULTIPART_FORM_DATA}) + public Response uploadPdfFile(@FormDataParam("file") InputStream fileInputStream, + @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception + { + File UPLOAD_PATH = new File("/tmp/img/"); + if (! UPLOAD_PATH.exists()){ + UPLOAD_PATH.mkdir(); + } + UploadedFile uploadedFile=new UploadedFile() ; + try + { + int read = 0; + byte[] bytes = new byte[1024]; + String filename=UPLOAD_PATH +"carto"+System.currentTimeMillis()+ fileMetaData.getFileName().substring(fileMetaData.getFileName().lastIndexOf(".")) ; + File file=new File(filename) ; + OutputStream out = new FileOutputStream(file); + while ((read = fileInputStream.read(bytes)) != -1) + { + out.write(bytes, 0, read); + } + out.flush(); + out.close(); + + uploadedFile.setName(file.getAbsolutePath()); + } catch (IOException e) + { + throw new WebApplicationException("Error while uploading file. Please try again !!"); + } + return Response.status(200).entity(uploadedFile).build(); + } }