'Spring Reactive call router handler method from another router handler method

I have written a router function that as two API calls -

  1. Upload to s3
  2. download from s3

My Router function is as below -

 @Bean
public RouterFunction<ServerResponse> s3ApiRouter() {
    return route(POST("/s3/upload").and(accept(MediaType.APPLICATION_JSON)), s3RouterHandler::handleUpload)
            .andRoute(GET("/s3/download/{name}"), s3RouterHandler::downloadFromS3);
}

My objective here is to upload a file to S3 and download the file, process it, and reupload.

So, in the "/s3/download/{name}" (download API call) - I download the image, process it and I have to call the "/s3/upload" method to reupload the file.

I'm able to call the "/s3/upload" independently,

I'm not able to call "/s3/upload**" from the "/s3/download/{name}" method.**

my code for the - "/s3/upload"

  Mono<ServerResponse> handleUpload(ServerRequest serverRequest) {
    Mono<ServerResponse> response = uploadResource.filepartUploadHandler(serverRequest);
    return response;
}
  1. "/s3/download/{name}"

    Mono downloadFromS3(ServerRequest serverRequest) {

     log.info("handle request {} - {}", serverRequest.method(), serverRequest.path());
     String name = serverRequest.pathVariable("name");
     InputStreamResource inputStreamResource = null;
     File outBoundFile = null;
     try {
         InputStream is = uploadResource.downloadFileFromS3Bucket(name);
         if (!isNull(is)) {
             File file = File.createTempFile(FilenameUtils.removeExtension(name),
                     "." + FilenameUtils.getExtension(name)); // vitrual File
             FileUtils.copyInputStreamToFile(is, file);
             String urlFilePath = (String) rotationUtil.rotate(file, serverRequest);
             inputStreamResource = new InputStreamResource(is);
             if (!isNull(urlFilePath))
                 outBoundFile = new File(urlFilePath);
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
     return ServerResponse.created(URI.create("/s3/upload"))
             .bodyValue(BodyInserters.fromMultipartData(fromFile(outBoundFile)));
    

    }

    public MultiValueMap<String, HttpEntity<?>> fromFile(File file) {

     MultipartBodyBuilder builder = new MultipartBodyBuilder();
     builder.part("files", new FileSystemResource(file));
     return builder.build();
    

    }

How do I call handleUpload method from the downloadFromS3 ?.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source