Java URL shortener service with Dropwizard and Redis

It never occurred to me I should write a Java URL shortener service with Dropwizard and Redis. Not because of Dropwizard or Redis, I just didn’t see why I should write a Java URL shortener at all.

That is until I read the blog post URL shortener service in 42 lines of code in… Java (?!) Spring Boot + Redis by Tomasz Nurkiewicz. Tomasz also references other interesting implementations of URL shortener services written in Scala, Clojure or Haskell.

Java URL shortener service with Dropwizard and Redis

Dropwizard logo

I knew from the start that an implementation based on Dropwizard would probably take a few more lines than one based on Spring Boot. Saving LoCs at all costs wasn’t the primary goal, though. I wanted to complete this exercise because I only recently wrote my first Dropwizard application – and because it was a very pleasant experience.

Somewhere I read a comment about Spring Boot which quite nicely expresses the thoughts I had when I first learned about its existence.

In short, this IS Dropwizard in every way. Spring 4 tries to become Dropwizard (and succeeds), JEE7 tries to become Spring 3 and kind of gets there. Everyone is becoming everyone else.

Eventhough Dropwizard could be tweaked to work with Spring (e.g. Spring Data Redis) this isn’t an option for me. If I want Spring, which I usually do, I’ll go with Spring Boot. Hence, this is pure JAX-RS with Dropwizard/Jersey and Jedis, a popular Java client for Redis.

The code

So, here’s the final solution on some 50 lines. Find a few comments below.

Line 17

The Jedis Wiki clearly states that “You shouldn’t use the same instance from different threads because you’ll have strange errors…To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections…You can store the pool somewhere statically, it is thread-safe.”

Line 22-23

This method is required by the Dropwizard Application class even though it does nothing. Also note that contrary to bigger Dropwizard applications which rely on configuration options there’s no need to implement your own Configuration sub class.

Line 33/47

For each Redis operation one should obtain a Jedis instance from the pool in a try-with-resources statement.

Line 44

Contrary to Tomasz solution I opted to POST the URL to shorten as a query parameter rather than a path parameter. I’m aware of a number of heated debates around the Internet how to do this “properly” with regards to REST principles and the HTTP spec.

Line 45

If you want to avoid the external dependency to UrlValidator you could do something like
instead. However, be aware that you’d then have to handle the checked MalformedURLException declared by the URL constructor.

The Maven build

The pom.xml declares only 4 dependencies

If you use the Maven Shade Plugin as recommended by Dropwizard you can build a single fat JAR which contains all dependencies listed above.


Building a Java URL shortener service with Dropwizard and Redis is not difficult and you won’t have to sacrifice readability. For me this has always been way more important than saving characters or lines while coding.

Denmark, an anti-whaling member nation of the EU, participates in slaughter of whales

Fourteen Sea Shepherd Conservation Society volunteer crew members of pilot whale defense campaign Operation GrindStop 2014 were arrested today on the Faroese island of Sandoy, as they entered the water to defend pilot whales from the brutal mass slaughter known as “grindadráp” or “grind” – putting themselves directly between a pod of 33 whales and their killers.

Despite being a member nation of the European Union and subject to laws prohibiting the slaughter of cetaceans, Denmark has officially shown its support of — and now direct collaboration with — the Faroese whalers by sending the Danish Navy to defend this archaic, mass slaughter of whales in the Faroe Islands alongside Faroese police.

Despite being a member nation of the European Union and subject to laws prohibiting the slaughter of cetaceans, Denmark has officially shown its support of — and now direct collaboration with — the Faroese whalers

Despite being a member nation of the European Union and subject to laws prohibiting the slaughter of cetaceans, Denmark has officially shown its support of — and now direct collaboration with — the Faroese whalers.

Follow the Sea Shepherd operation at @OpGrindStop.

Release to Maven Central

This guide shows you how to deploy artifacts to Maven Central with the help of Sonatype OSSRH (OSS Repository Hosting). It only takes few simple steps to release to Maven Central – if you follow the right documentation.

Brief intro

Release to Maven CentralIn January 2010 Maven Central stopped accepting rsync feeds from projects into their repository. It was an approach that just didn’t scale. See the official Maven Central documentation for details. In order to release to Maven Central your project either needs to be in an approved repository hosting location such as Apache or Codehaus or you need to publish to Sonatype OSSRH instead. Sonatype acts as an intermediary itself being an approved repository hosting location.

Why this guide

There’s extensive documentation by Sonatype about the whole process. However, at the end of March 2014 their documentation was completely overhauled. While being well written and easy to follow there are two problems with it:

  1. Not streamlined enough. It’s covers a lot of ground and talks about many possible build scenarios. I can be difficult to understand which sections actually apply to you.
  2. The old documentation wasn’t retired. The new docs link to the old ones for certain steps. This leads to inconsistencies and cost me a few hours because the old docs are just wrong sometimes i.e. their instructions don’t fit the current process anymore.

I’m going to show you how to configure your Mac and your pom.xml in order to do a plain Maven release and release to Maven Central. Where applicable I’ll reference the Sonatype docs of course. Let’s go!


I’ll walk you through the below tasks:

  • Sign up with Sonatype and request a new project
  • Include required meta data in POM
  • Sign artifacts with GPG/PGP
  • Configure build plugins in POM
  • Deploy release builds to Sonatype OSSRH
  • Release to Maven Central from Sonatype OSSRH

Request new project with Sonatype

Sonatype uses JIRA to manage requests.

  1. Create your JIRA account if you don’t have one yet
  2. Create a New Project ticket

This triggers creation of your repositories. Normally, the process takes less than 2 business days. In the meantime you can complete all other tasks.

Include required meta data in POM

Maven Central requires you to provide sufficient meta data. Sonatype will verify this and publishing will fail if your POM is not compliant. Read up on the details or copy/paste from the below bare minimal template:

There’s also an example POM in the Sonatype docs.

Sign artifacts with GPG/PGP

GPG Suite - generate a keySetting up artifact signing correctly was the only major obstacle for me the first time. It was all new to me and the Sonatype docs are not correct in certain areas. If you work with Windows or Linux the old Sonatype documentation on GPG/PGP may be ok. I couldn’t tell because I work on a Mac which makes everything a lot easier – as I know now. I’ll describe how to prepare OS X for GPG below. The integration in the POM is documented in the next chapter.

  • Download and install the GPG Suite
  • Generate a new key according to the docs on the GPG site
  • Publish your key to a key server, both these steps are a breeze, the GPG Suite is fantastic

Done, sweet.

Configure build plugins in POM

The Sonatype page on deployment with Maven has a wealth of information but it left me puzzled. I didn’t quite know what was actually applicable for me and what not. Again you may optionally read up on the details on the Sonatype page. However, I reckon it’s easier if copy/paste from my fully functional POM. The relevant section is below:

Note if you work on Mac no further configuration for the maven-gpg-plugin is required, neither in the POM nor in the settings.xml. It all just works out-of-the-box with the GPG Suite if you only generated a single key.

Deploy release builds to Sonatype OSSRH

In order to tell Maven where to deploy your artifacts to during a release build you need to add a distribution management section.

Both listed Sonatype repositories require authentication. Hence, your credentials for the server need to be added to your personal Maven settings.xml file in ~/.m2.

Puuhh, finally, all the tedious preparations are done! If you now run a regular Maven release build all artifacts will be created, signed, and deployed to Sonatype.

Release to Maven Central

OSSRH Nexus - build promotion menuAll that is left to do is login to the OSSRH Nexus, make a few clicks and sit back. Sonatype will release to Maven Central what you just deployed within a few minutes. This part is again nicely documented in the Sonatype guide.





Based on my own POM which may serve as a reference I guided you step-by-step to release to Maven Central. I hope I was able to demonstrate that once GPG is correctly configured on your system it’s really just a matter of pasting the right chunks of configuration into your POM.

Let me know how well this guide worked for you.

Reset Windows 7 passwords: a field report

Reset Windows 7 passwordsToday I spent way too much time to reset a Windows 7 password. What took time, however, wasn’t the actual reset process but digging up information on the Internet. I read lots of instructions and hints on some shady hacker/cracker forums which didn’t sound too trustworthy. Also, I looked at a number of reset/recovery tools my favorite search engine and those forums suggested.

It was only after I reset my Windows 7 password that I learned about the excellent but rather lengthy ‘How To Reset a Windows 7 Password‘ article. If you have a lot of time or like very detailed tutorials with lots of links to related content I highly recommend that article. If you prefer short to-the-point instructions I suggest you follow along here.

Continue reading →

Sencha Touch native packaging for iOS done right


Sencha Touch native packaging for iOSThere are a number of tutorials out there on the Internet that tell you how to do Sencha Touch native packaging for iOS. The subject is also covered pretty well in the Sencha Touch docs. The problem I have with all those resources: they’re either outdated, outright wrong or stop half-way.

I don’t need a tutorial whose title claims to describe how to create an .ipa file and then simply describes how to produce an .app file – something that’s much better explained in the Sencha docs. Also, those tutorials that tell you how to produce a binary for your own single device stop half-way. Why? Because I expect you usually want to have a more or less convenient setup for all your own and your beta-testers devices, don’t you?
Continue reading →

Paul Kalkbrenner’s Aaron vs Meshell Ndegeocello’s Late Petite Mort

Anyone ever noticed that the beat in Paul Kalkbrenner‘s Aaron is veeery similar to the base line in Meshell Ndegeocello‘s Late Petite Mort? Hear for yourself:

We should END captive dolphin facilities, not build more!

Please sign the petition today to end captive dolphin facilities!Dolphins and other marine mammals have been exploited in the name of entertainment for far too long. Two new marine mammal facilties have been preliminarily approved in The Bahamas. And what’s worse is, the public was not notified or given the chance to voice their opinion.

End captive dolphin facilities

These facilities are outdated and cruel. That’s why I signed a Care2 petition opposing the construction of any new marine mammal facilities in The Bahamas, and I’m hoping you will too.

iPhoto order language is wrong

iPhoto logoFor a very long time I was annoyed that the iPhoto order language is wrong for me each and every time I order print products from Apple. I live in Switzerland, speak mainly German and English, on my Mac I use English as the system language yet the iPhoto order language is always French (which I’m no longer fluent in) instead of German. Continue reading →

This movie requires QuickTime, which is not supported by this version of iTunes

If you found here looking for how to fix “This movie requires QuickTime, which is not supported by this version of iTunes.”, good it’s here:

  • run iTunes in 32bit mode
  • and/or update Flip4Mac to 3.x if you’re still on 2.x

Continue reading →

Techniques how to use ellipsis in table columns

The CSS3 text-oveflow property allows to have content not exceed a certain limit. Browsers can be asked to place ellipsis, the … “character”, after the last visible character rather than just cutting content off. It’s a nice visual indication for readers that there’s actually more content than what is shown. Later you’ll learn techniques how to use ellipsis in fixed-width or max-width table columns using HTML and CSS. Continue reading →