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;
}
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());
}
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;
}
Aggregations