Posts

Checked exceptions, type parameters, and higher-order functions

Image
One of the peculiar features of Java, in what's otherwise a rather mundane language, is checked exceptions. What do checked exceptions do? The short explanation is: They make information about what exceptions a method may throw part of its signature, and force you to actually handle all exceptions that may arise. A better way to explain it is through a few small examples... Checked exceptions Let's try to implement a trivial method that may throw an exception: class QuickMaths { class ZeroDivisionException extends Exception { } // Divide x by y, making sure y isn't zero. Double divide(Number x, Number y) { if (y == 0) { throw new ZeroDivisionException(); } else { return x * 1.0 / y; } } } The method divide is going to cause a compiler error, because it may throw an exception but we haven't encoded this information in its signature. (Sure, the compiler could infer this, but it doesn'

Printing HTML from Android via ZPL

Image
My main professional activity is developing an Android app nowadays (and before that it was developing iOS apps) but that’s not a very exciting topic as you can find gazillions of guides on it on the web and most of the time it’s just… normal programming. But every now and then I have to develop something rather obscure, like a server application based on the outdated SOAP protocol (in this year of 2020) , or in this case, printing warehouse/product/shipping barcode labels from an Android app. Printing is so easy on Android! … Right? When I first looked into how to support printing from my app, considering that we want our customers to be able to define their own label designs, I thought hey, why not just let people import HTML files into the app, then use Android’s WebView to print it. Super easy to implement, super easy to design labels. I defined a bunch of special placeholder values that one can insert in the HTML (like for product code, product name, etc.), which the a

Making a SOAP server from WSDL files

Image
What I spent the last two days learning is so under-documented on the Internet that I just had to write about it and publish it somewhere. So here goes. Step 0: Introduction to SOAP So you’ve been tasked with writing a server implementing some dinosaur communication protocol that uses SOAP instead of defining a REST API. Maybe something like MSV3, the German standard for the pharma industry that allows pharmacies to send digital orders to their suppliers. Let’s say there’s a working reference implementation, but it uses an ancient version of Java and you’re really not sure if trying to bend the reference implementation to your will is the best way forward. We’re talking about a standardized protocol, so how hard can it be to write your own compliant server? Besides, you really want to learn how to implement a SOAP application server from scratch… If you’ve never touched SOAP before, the first thing you might notice is that the documentation of whatever protocol you want t