Search in sources :

Example 1 with EntityPart

use of jakarta.ws.rs.core.EntityPart in project jaxrs-api by eclipse-ee4j.

the class MultipartClient method sendPdfs.

public boolean sendPdfs(Path dir) throws IOException {
    List<EntityPart> parts = Files.list(dir).map(this::toPart).collect(Collectors.toList());
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:9080/multipart?dirName=abc");
    Entity<List<EntityPart>> entity = Entity.entity(parts, MediaType.MULTIPART_FORM_DATA);
    Response response = target.request().post(entity);
    return response.getStatus() == 200;
}
Also used : Response(jakarta.ws.rs.core.Response) List(java.util.List) WebTarget(jakarta.ws.rs.client.WebTarget) EntityPart(jakarta.ws.rs.core.EntityPart) Client(jakarta.ws.rs.client.Client)

Example 2 with EntityPart

use of jakarta.ws.rs.core.EntityPart in project jaxrs-api by eclipse-ee4j.

the class MultipartClient method retrievePdfs.

public List<Path> retrievePdfs(String remoteDirName) throws IOException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:9080/multipart").queryParam("dirName", remoteDirName);
    Response response = target.request(MediaType.MULTIPART_FORM_DATA).get();
    List<EntityPart> parts = response.readEntity(new GenericType<List<EntityPart>>() {
    });
    return parts.stream().map(part -> {
        try (InputStream is = part.getContent()) {
            Path file = Files.createFile(Paths.get(part.getFileName().orElse(part.getName() + ".pdf")));
            Files.copy(is, file);
            return file;
        } catch (IOException ioex) {
            LOG.log(Level.WARNING, "Failed to process attachment part {0}", part);
            return null;
        }
    }).collect(Collectors.toList());
}
Also used : Response(jakarta.ws.rs.core.Response) Client(jakarta.ws.rs.client.Client) Files(java.nio.file.Files) EntityPart(jakarta.ws.rs.core.EntityPart) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) Entity(jakarta.ws.rs.client.Entity) GenericType(jakarta.ws.rs.core.GenericType) List(java.util.List) Response(jakarta.ws.rs.core.Response) MediaType(jakarta.ws.rs.core.MediaType) Paths(java.nio.file.Paths) Path(java.nio.file.Path) InputStream(java.io.InputStream) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) Path(java.nio.file.Path) InputStream(java.io.InputStream) List(java.util.List) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Client(jakarta.ws.rs.client.Client) EntityPart(jakarta.ws.rs.core.EntityPart)

Example 3 with EntityPart

use of jakarta.ws.rs.core.EntityPart in project jaxrs-api by eclipse-ee4j.

the class MultipartResource method getAllPdfFilesInDirectory.

@GET
@Produces(MediaType.MULTIPART_FORM_DATA)
public List<EntityPart> getAllPdfFilesInDirectory(@QueryParam("dirName") String dirName) throws IOException {
    File dir = getDirectoryIfExists(dirName);
    List<EntityPart> parts = new ArrayList<>();
    for (File f : dir.listFiles()) {
        parts.add(EntityPart.withFileName(f.getName()).content(new FileInputStream(f)).mediaType("application/pdf").build());
    }
    return parts;
}
Also used : ArrayList(java.util.ArrayList) EntityPart(jakarta.ws.rs.core.EntityPart) File(java.io.File) FileInputStream(java.io.FileInputStream) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Aggregations

EntityPart (jakarta.ws.rs.core.EntityPart)3 Client (jakarta.ws.rs.client.Client)2 WebTarget (jakarta.ws.rs.client.WebTarget)2 Response (jakarta.ws.rs.core.Response)2 List (java.util.List)2 GET (jakarta.ws.rs.GET)1 Produces (jakarta.ws.rs.Produces)1 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)1 Entity (jakarta.ws.rs.client.Entity)1 GenericType (jakarta.ws.rs.core.GenericType)1 MediaType (jakarta.ws.rs.core.MediaType)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Level (java.util.logging.Level)1