HTTP Basic Authentication with Spring 4 and the RestTemplate

So, due to some misuse of HTTP authentication in the Apache Commons HTTP Client (3.x), the developers totally changed how authentication works in 4.x. Given that Spring 4′s RestTemplate requires the HTTP Client 4.x, many hoops need to be jumped through. After digging through a bunch of posts on stackoverflow, I came up with the following approach to wiring authentication in – hat tip to Baeldung for providing guidance.

First, we create the new factory:

Then, we instantiate it and wire it into the RestTemplate – in my case I’m using it to talk to Mailgun:

Please ignore my blatantly terrible string concatenation in creating the URL for the campaigns – I’m just using this to test things out. And no, I haven’t posted the model objects; you can go ahead and create your own!

Hopefully this helps somebody out; I had to wade through so many different posts before finding a mechanism that worked.


This isn’t really about technology, but it’s a topic that I thought was pretty interesting. Unless you’re living under a rock, you are aware that the olympics are taking place in Sochi, Russia. Similarly, unless you’re a luddite, you are aware that there are a lot of problems with the various venues – construction still underway, poor quality control, incomplete setup, the list goes on and on. There’s a twitter account dedicated to tracking exactly what is going on – @SochiFails. There may have been something similar for the 2010 olympics held in Vancouver, but the worst I can recall is the unexpected weather and lack of snow. No doubt some of that is related to the popularity and reach of twitter today than four years ago, but a lot still has to be attributed to the location itself.

Of course, not everybody agrees that #SochiProblems is actually a problem, but is more of a reflection on the people going to Sochi and their expectations – Sarah Kaufman’s article #SochiProblems Is More of An Embarrassment For America Than It Is For Russia is probably the best one I’ve come across. Some of her points are certainly valid – it seems that people have never travelled outside of the US, Canada and Western Europe, their expectations are so outlandish. Yes folks, in lots of countries you cannot flush toilet paper, you put it in a bin beside the toilet. For a reporter to be so flabbergasted by this suggests that they’ve never travelled anywhere, ever. I also disagree with everybody complaining about the translations – literal translations from one language to another are nothing new, as evidenced by the amusing instructions we all get when we assemble furniture.

I think the actual issue is related to the expectations as defined by the amount of money spent – for $51 billion, you’d almost expect the streets to be paved in gold. The fact that workers aren’t paid, that promises made years ago to upgrade or create infrastructure came to nothing – these are the things to get up in arms about. The question should be: why are things incomplete when somebody obviously made outrageous sums of money? In light of the horrendous political climate in Russia, the LGBT issues, the outright corruption – why did the IOC decide to proceed, or not put safeguards in place to ensure that everything would be delivered on time, and smoothly? All blame should go to the IOC, not to the host country, and certainly not to the locals.

Apparently I felt it was the fault of the IOA, not the IOC. I guess I was too fixated on them after reading about how the Indian competitors were there under the “Olympic” flag as the IOA had serious issues.

On Using a BB Z10 as the Daily Driver

TL;DR: You can use a Z10 instead of an Android phone, but you won’t really like it.

Ana and I both have HTC One phones that we got when we combined our separate accounts (me from Rogers, Ana from Mobilicity) to Bell in May. I’ve been an Android user since my original HTC Nexus One, whereas Ana was coming from BlackBerry land. BBM and a keyboard were really a killer feature, though the lack of any real new hardware for three years made the switch easy for me to sell.
Unfortunately her phone stopped charging a few days back, so we went to a Bell store to get it looked at. The phone is a sealed unit, so we can’t just swap out the battery – Bell arranged to get it sent for service and we were told it’d come back in two to three weeks. The service rep then offered a loaner phone – some nasty old Samsung phone that probably ran ICS, if not FroYo. I politely declined – $20 to use that thing? Pass.

Fall-back plan – use the BB Z10 that work provided me that had been sitting in a drawer. So, for the last few days that’s what I’ve been using. Now I get to describe what it has been like! The last BB I owned was a Bold, back in 2009. I liked the device and the keyboard, but the OS was getting very stale, the lack of touch was a bother, and the app scene was non-existent. Overall, the Z10 is a generic device – the hardware specs are mediocre and the screen resolution is decent. The device itself isn’t really growing on me, though – the huge bezel at the top and bottom feel like a waste of space, and the plastic back just rubs me the wrong way. I am probably spoiled after using the HTC One; the aluminium back is just a great feel. Same with the generic brick form factor of the Z10 – absolutely nothing outstanding about the device in any way.

On the OS side, it’s a very noticeable difference between the OS 7 of the Bold I used to have and the OS 10 on the Z10. It is certainly more advanced and snappy, and is pretty solid for regular use. However, there are still a few gripes, mostly about the gestures. The ‘swipe up’ thing is just a freaking pain in the ass – I’m very used to just flicking things; swiping from that stupid BB logo halfway up the screen (if not more) just to unlock it is a pain. Similarly, having to swipe from the very edge in a left-to-right or right-to-left motion to get from one screen to the next is onerous – the ‘flicking’ in Android is much easier/faster.

The apps themselves are relatively solid, if unremarkable – Twitter, FourSquare, FB, email, WhatsApp, Subsonic – all function in the same way as on Android. Dropbox is notably less useful – I’ve grown very used to the photos that I take on my phone syncing to my Dropbox account and I’m missing that terribly. Yes, I can go to the photo in the gallery, then share it and export it, but damn, wasted time/steps. The one pro: when I select an item in my calendar that has a phone number for dialling into a conference call and then click the dial button will not just dial the phone number, but also do the conference code entry. Yay saving that “read the number in my calendar, try to memorize, then use the dial pad to type it in” or “let me open the calendar invite on my laptop/screen, then type it in.”

I have *no* idea what this ‘balance’ feature is about – something to do with work/life balance? Not a clue – just something that the IT folks decided we needed, I guess.

The biggest complaint is regarding the HCI guidelines, or lack thereof, when it comes to configuring an application. Some apps you have to get that strange left-bar to show up and the configuration option is there. Some you have to tug from top to bottom (and then some) to expose the application settings. Others have have it randomly placed in the app. I suspect this is a natural byproduct of the bizarre gesture scheme that somebody in BB foisted off on us.

Finally, we come to the ‘hub’ that BB seems to use as the centre for collecting notifications/details about your accounts. I suspect they wanted something like Android’s app drawer (or iOS’s terrible rip off) or Microsoft’s Live Tiles, but with their own feel. They just didn’t get it right. I’m used to having my weather notification sitting in the app drawer, so I can wake up and figure out how brutally cold today might be, along with some notifications about emails, tweets, or whatever FB spam I got. I can then simply swipe the notifications away, and move on with it. No such feature with the weather app in BB10, nor can I just swipe away notifications. I can’t even multi-select a bunch of stuff by long-pressing; instead I have to hit the ‘more’ menu icon, hit ‘select more’, then select items. Basic functionality that other touch-centric OSs have had for years are still lacking and lacking in a noticeable way.

Verdict: I can use this damn thing, and it gets things done, but it has rough edges and just doesn’t feel remarkable in any way. I’d give this a 7/10. It’s average, I don’t want to smash it to bits, but I can’t think of a reason as to why I’d recommend it to anybody. It just exists.

Q and $q – Promises in JavaScript

One of the biggest hurdles that I’ve had to overcome when working in JavaScript, whether client- or server-side, has been dealing with the endlessly nested nature of the code. This really rears it’s head when working with node.js – everything is designed to be asynchronous and requires callbacks. This heads to the following type of nightmare scenario:

That’s a bit of a nightmare! Nested callbacks AND the fun fact that the doIt function will complete without returning any value – that happens in that nested pyramid of doom. This means that if you were to try var result = doIt('michael'); you’d have a whopping null. So, you’d update the function to include another parameter, another function(err, value) callback that can then handle the value which then leads to… More callbacks.

This is tiresome and frustrating. While you can make the code cleaner by naming the functions and moving them into external blocks for easier reading, it doesn’t cut down on the boilerplate error checking and nested callbacks. So, I went searching for a better way to do this – my first stab at this located the package fibrous – a package to mix synchronous and asynchronous code together. The examples look great and got me really excited, but the documentation was a bit lacking. Basically, strange errors kept cropping up, and I had no idea if it was my inexperience, inability to understand how to use fibrous, or just a bug. After smashing my head on the wall for a while, I started looking around for something else to use.

Enter Q – a promise framework. A promise is just that – your function returns a promise and at a later date you’ll fulfil that promise with a value or an error. This allows for the easy chaining of asynchronous functions:

You then get to call the function using doIt(firstName).then(function(value) { /* do stuff here */ });. Much easier to read, easier to handle errors in one place (if desired), and gets rid of the nested callback hell.

This gets even more interesting, since I found out a few days ago that AngularJS contains a cutdown implementation called $q that allows for far better interactions between controllers and services. How? By removing those pesky nested callbacks! Just a bunch of happy .then chains that are far more readable.

Fighting the Desire to Quit Rdio

I had originally signed up for Rdio in August of 2010 – I had my free trial for a month but didn’t consider it compelling enough for me to sign up for a paid account. In January of 2011 I signed up for a month of the unlimited service, but again cancelled it – while their product was great, their catalogue wasn’t really up to par. In December of 2012 I re-visited that decision and signed up for an unlimited subscription. At the end of January 2013 I upgraded to the family plan and added Ana to my account. A few days later, since it worked out to be something like $30, I added Jamie to the family plan for a year. For a few months, it was great – I created a playlist for my crunk needs, and had fun collaborating with Jamie on adding music to it. Now that I’ve been using the service for six months, I feel I can voice my feelings on the downsides to Rdio and why I’m considering nuking my plan. I’m not sure what I’d replace it with – I refuse to use iTunes because it’s a 150mb virus (also made by Apple) and Spotify isn’t available in Canada. I want to pay for my music, but record labels sure don’t seem to want to make it easy for me to give money to the artists. However, that’s a completely separate rant, for now I’ll focus on Rdio.

My first issue with Rdio is how I can “discover” music – I’m forced to do it the way Rdio wants me to. I can see what the most recent releases are, but I cannot organize them in any way. I just get a huge lump of “this week”, “last week” and “two weeks ago.” I can see what is “heavy rotation” according to the people I follow and I can see what the “top charts” are. I can then search by artist and find related music. However, the one thing I REALLY WANT is to be able to organize and search by genre. Hell, I’d just like to view things in a list and group/sort by different fields. How about I search for things in EDM released in the last year? Or EDM by artist? It’s incredibly frustrating to be given access to what may be a huge pool of music but only get to see what the last few weeks has released or whatever mind numbing crap is currently listed as the top of the chart.

The other issue is the concept of previews – I’m not sure if this is a feature Rdio introduced to get labels on board, or labels forced on Rdio to sign a deal, but it is incredibly frustrating. I find that Dim Mak is a huge abuser of this “feature”. Rather than putting the music up for streaming, they put snippets of it and let me choose to buy full albums through Rdio, for the typically extortionate pricing. I don’t want to buy the album; I’m paying $24/mo for my streaming plan for a reason. If I wanted media, I’m sure I’d find another way to get it (*cough*torrent*cough), one that was more economically viable. The most recent straw that has come close to breaking the camel’s back is the fact that Warner/Interscope released The Great Gatsby soundtrack, which I was digging. After a few days they went in and made a whole bunch of tracks previews – what the hell is that?! As Pete Lada wrote:

Pete Lada
New record release strategy:
1) release full tracks for a few days
2) album rockets to top of top charts
3) limit songs to preview
4) ????
5) Profit

I just don’t understand what the hell record labels are even thinking these days – how did #3-5 even occur to these wankers? Were they busy playing “how can I best shoot myself in the foot” followed up by “let’s see how we can make everybody love us less, a day at a time”? I understand this may not be Rdio’s fault, but they are complicit in it, and I’m really questioning the value of giving them my money.