paint-brush
Simple Mailgun Webhook handling in Java with Jerseyby@igor_98383
879 reads
879 reads

Simple Mailgun Webhook handling in Java with Jersey

by Igor RendulicDecember 3rd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Handling a WebHook is a straight and forward process in most cases. I’ve stumbled upon a bit of a problem when trying to consume <a href="https://documentation.mailgun.com/en/latest/api-webhooks.html" target="_blank">Mailgun Webhooks</a> in Embedded Jersey. My goal was to keep Jersey embedded configuration as light as possible and MultipartConfig annotation wasn’t working out of the box.

Company Mentioned

Mention Thumbnail
featured image - Simple Mailgun Webhook handling in Java with Jersey
Igor Rendulic HackerNoon profile picture

credit: blog.mailchimp.com

Handling a WebHook is a straight and forward process in most cases. I’ve stumbled upon a bit of a problem when trying to consume Mailgun Webhooks in Embedded Jersey. My goal was to keep Jersey embedded configuration as light as possible and MultipartConfig annotation wasn’t working out of the box.

In my case the need for handling the events is no more than 10 lines of code. Its job is basically to count the number of bounced, delivered and dropped emails. Thats it.

Inspecting what is being sent from a webhook with requestb.in I figured there are 2 types Content-Type that need to be handled:

  • application/x-www-form-urlencoded
  • multipart/form-data

Checking what is being sent from MailGun Webhooks

Apache Commons FileUpload library provides the parsing of multipart/form content type without the need of additional configuration and native servlet handles the application/x-www-form-urlencoded.

That way i can have 1 servlet and all required parsing in one simple method. The code could be shortened a bit but it does the job without wasting too much time on this.

All we need we’re left to do is Signature Validation and we’re done.

This code is used in www.sixthmass.com project for Automated Funnel Insights in conjunction with email campaigns. In this case Automated Funnels provide insight of conversion paths users chose to take after clicking on a link from email campaign.