0

I have a java method, that reads from Table A (with BLOB), writes certain data to Table B and finally uploads the BLOB to a file-server. This is the code:

public void insertAndUploadService() throws Exception {

    List<Tiedosto> tiedostoList = new ArrayList<>();
    Integer newRow = 0;
    boolean uploaded;

    try {
        tiedostoList = tiedostoService.getAllFiles();
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (Tiedosto tiedosto : tiedostoList){
        Attachment attachment = new Attachment();
        attachment.setCustomerId(tiedosto.getCustomerId());
        attachment.setSize(tiedosto.getFileSize());

        try {
            newRow = attachmentService.createNew(attachment);
            System.out.println("************ NEW ROW CREATED IN attachments ******************************");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (newRow == 1 && tiedosto.getContent() != null){
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tiedosto.getContent());
            minioFileServer.uploadFile("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1), byteArrayInputStream,
                    byteArrayInputStream.available(), tiedostoService.getMimeType(tiedosto.getContent()));
            System.out.println("********************** Generating Upload Link ************************************ "+"\n"+
                    minioFileServer.getUploadLinkForFile("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1) ));
            uploaded = minioFileServer.fileExists("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1));

            if (uploaded == true){
                System.out.println("*********** UPLOAD SUCCESSFUL ***************");
                tiedostoService.updateService(tiedosto.getCustomerId(), tiedosto.getId());
                attachmentService.update(attachment.getUuid());
            }
        }

    }
}

If I send POST request to an API endpoint, something like:

@POST
@Path("attachments")
public void moveToMinio() throws Exception {
    MigrationService migrationService = new MigrationService();
    migrationService.insertAndUploadService();
}

It does the work as required and prints out the details. Is it by any chance possible to send multiple responses from insertAndUploadService when POST request is sent? For example when newRow is created and uploaded == true, is it possible to return HTTP 202? I'm new to Java and would really appreciate any sort of help/suggestions!

EDIT: "Multiple responses" wasn't probably the right term. I'd like the option to return one of several HTTP status codes, chosen based on the request actions.

Roshan Upreti
  • 1,782
  • 3
  • 21
  • 34

0 Answers0