Java Reference
In-Depth Information
To go the other direction, first I use the decodeBase64 method that Groovy adds to
java.lang.String , which returns a byte [] again. Then String has a constructor
that takes a byte array, and I use the split method from Java to separate the username
fromthe passwordagain andverify that they haven'tbeen modified bythe transformations.
Other than showing how the Groovy JDK adds new methods to standard Java data types,
this example also demonstrates that encoded text isn't encrypted. Anyone who intercepts
the request and accesses the encoded header can extract the username and password. Using
basic authentication therefore is not at all secure if the requests are transmitted over an un-
encrypted connection, like HTTP. At a minimum the request should be sent over HTTPS
instead. [ 4 ]
4 For several years Twitter supported basic authentication as part of its RESTful API. Hopefully all the many Twitter
clients who used it transmitted their authentication over secure sockets. If not you might want to consider changing
your password. These days Twitter has switched to OAuth, which may be overly complicated but is much better than
basic authentication.
There are lots and lots of useful methods in the Groovy JDK. As another example, date ma-
nipulation is always painful in Java. [ 5 ] Groovy doesn't necessarily fix the many problems,
but the Groovy JDK adds several methods to make date-related classes more powerful.
Here's an example, which hopefully will be both interesting and at least mildly amusing to
some readers.
5 Java 8 is supposed to fix this, at long last. In the meantime, the open source date/time library of choice in the Java
world is Joda time: http://joda-time.sourceforge.net/ .
In the United States and Canada, February 2 is known as Groundhog Day. On Groundhog
Day, the groundhog is supposed to emerge from his hole and look for his shadow. If he
doesn't see it he'll stay out of the burrow, and winter is nearly over. If he sees his shadow,
he goes back to sleep in his burrow, and we'll sadly have to suffer through six more weeks
of winter.
Let's check the math on that, though, as shown in the next listing.
 
 
Search WWH ::




Custom Search