Search code examples
javajax-rsminio

jax-rs how can I implement a webhook listener in REST server?


I'm working on a migration project in Java that migrates BLOB files from table to a minio storage server. It operates in such a manner that a client reads from source table and sends certain data as POST values to a REST server. Those values are then written to a new table and an uploadlink is returned, through which the BLOB(converted to file) gets uploaded to minio server. This is the POST handler from resource class

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UploadLink postFile(@Context UriInfo uriInfo, Attachment attachment) throws Exception {
    Integer id = attachmentService.createNew(attachment);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    String uploadLinkForFile = minioFileServer.getUploadLinkForFile("test", attachment.getUuid(), attachment.getName());
    UploadLink uploadLink = new UploadLink();
    uploadLink.setUploadLink(uploadLinkForFile);
    uploadLink.setLocation(builder.path(Integer.toString(id)).build());
    return uploadLink;
}

When called by a client, this method returns the uploadlink and using a PUT on the uploadlink, the client then uploads this file to minio server. What I want to do now is to implement a webhook listener in the server that would listen to the events published by minio server on every successful upload. How can I achieve this? Do I need to create an API that would act as the endpoint on minio's configuration? I would really appreciate any kind of advice/help with this.


Solution

  • Turns out Minio has webhook feature which can be used to send event notifications to an endpoint. HTTP POST is used to send those notifications. So, what I did to tackle this issue was - I created an API that would accept POST requests like this.

    @POST
    @Path("webhook")
    @Produces(MediaType.APPLICATION_JSON)
    public Response webhookListener(NotificationConfiguration nc) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(nc);        
        return Response.ok().entity(jsonString).build();
    }
    

    So, basically what this API does is, it receives webhook notification from Minio which is of NotificationConfiguration data type and I've simply converted it to JSON using Jackson and returned it as an entity. The JSON data contains the event information.