Simple Mailgun Webhook handling in Java with Jersey

Igor Rendulic
Dec 3, 2017 · 2 min read
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.

HackerNoon.com

how hackers start their afternoons.

Igor Rendulic

Written by

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade