Quick and dirty web service testing
For command line geeks (on Windows the easiest way to get these utilities is to install Cygwin):
curl -s -d @request.xml -H "Content-type: text/xml" \
http://example.com/endpoint | xmllint --format -
Create a file request.xml with your XML input, pretty XML output will appear on the standard output. Curl is an HTTP swiss army knife, xmllint is a XML verification and manipulation tool.
SWI-Prolog has a sense of humor
Deep Thought jokes just never get old! SWI-Prolog, when asked an open-ended question for which no rules are defined, answers like so:
?- A. % ... 1,000,000 ............ 10,000,000 years later % % >> 42 << (last release gives the question) ?-
That's just awesome
Reminded me that I haven't (re)read HHGG in recent years - maybe I'll put it on me phone. Seems like the perfect book to read a chapter here and there when bored.
Annotation-based dispatch for Spring ApplicationEvents
I created a little extension that allows you to dispatch Spring application context events in a clean and type-safe way to POJO beans. Instead of implementing the ApplicationListener interface, one can simply create a handler as follows:
public class EventListener {
@EventHandler
public void onAppEvent(MyCustomEvent event) {
// ...
}
}
The code is available as a zipped up Maven project or via Git:
git clone http://pavel.tcholakov.net/code/eventhandler-annotation.git/
Quick and easy image resizing with Java ImageIO
There are quite a lot of examples of image resizing floating around the web but they all seem to access all sort of hidden features and classes deep inside the JDK to achieve their goal. Here is the most straightforward method that I could come up with - all in all just 5 lines of code. It requires ImageIO and requires Java 1.4 or later to run. If the ImageIO.write() method accepted plain ol' AWT Image arguments it could have been reduced further still to three lines, but you have to give it a BufferedImage instance as input.
The snippet below assumes that there exist the following variables (coloured green in the code):
- an input stream to read from
- a desired width variable (specifying the height as -1 tells the toolkit to preserve the original aspect ratio)
- and an output stream to write the encoded image to.
Sample code:
import java.awt.Image; import java.awt.image.BufferedImage; import javax.imageio.ImageIO;
// ... BufferedImage sourceImage = ImageIO.read(inputStream); Image thumbnail = sourceImage.getScaledInstance(width, -1, Image.SCALE_SMOOTH); BufferedImage bufferedThumbnail = new BufferedImage(thumbnail.getWidth(null), thumbnail.getHeight(null), BufferedImage.TYPE_INT_RGB); bufferedThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null); ImageIO.write(bufferedThumbnail, "jpeg", outputStream);
Effective Java Updated
Brush up on your Java 5 bag of tricks with this talk by Joshua Bloch based on his updated Effective Java book (can't recommend it highly enough).
A colleague of mine commented that this book has about the same information density as maths textbooks, so don't be mislead by the low number of pages - it is excellent value for money