Skip to content

Recent Articles

28
Apr

A Blog Analyzer Project

In the coming days, I will be writing about a project I’m working on which will perform analysis on Andrew Sullivan’s The Dish blog, which is one of the most popular blogs on American politics.  The intent of the project, which will utilize such technologies as Spring 3, JSP/JSTL, JDBC, PostgreSQL, and jQuery/Ajax, is to web scrape the blog, extract key data elements, and reorganize and present this data in new and interesting ways.  Additionally, I will create a bookmarklet that will add value to the blog site itself.

Development tools used include Netbeans 7.0, Firefox with Firebug, and the always handy psql Postgres command line tool.

There are many interesting technical challenges involved, and I will write about them on this blog.  Additionally, there is the question of copyright law, which is an unavoidable concern when building off of content from a third party.  Copyright law was not meant to stifle innovation, though, provided certain criteria are met: the content originator must not be harmed in the marketplace, repurposed content must be transformed into a novel work, and small portions must be used.  I believe my project fits these criteria.

21
Apr

Profile SQL statements in Java / Spring

Wouldn’t it be nice if there were a way to time your application’s SQL statements unobtrusively? This information could give you insight into the performance of your queries and updates and help you identify slow, poorly-performing SQL. Of course, there is a way to add such SQL profiling to your Spring application, by using AspectJ.

I use Spring JDBC and wanted to identify slow SQL queries in my application so that I could tune them in order to improve overall performance.  Capturing the execution times whenever SQL is executed can be done by creating a pointcut on the methods of JdbcTemplate.  Here is what we need:

  1. aspectjrt.jar and aspectjweaver.jar from here.
  2. An aspect with a pointcut on the JdbcOperations interface, which JdbcTemplate implements.
  3. @Around advice that times the execution of JdbcTemplate methods and stores this data for later retrieval.
  4. Configuration of Spring applicationContext.xml to get it working.

The pointcut looks like this, with the String argument being the SQL statement:
Read moreRead more

4
Apr

Add custom annotation to Spring MVC controller

The question of how to add custom annotations to my Spring MVC controllers puzzled me for some time, because the documentation in this area is lacking. Even the bible of Spring 3.0, Spring Recipes by Gary Mak, et. al., did not address the topic.

But then I found this great blog post detailing exactly what I was interested in.  In a nutshell, you need to implement WebArgumentResolver and set your class as a customArgumentResolver of the AnnotationMethodHandlerAdapter bean.  What I was interested in was adding a @RequestAttribute annotation that would work like @RequestParam, but would obviously pull the value from a request attribute rather than a request parameter.
Read moreRead more

29
Mar

JSON / Ajax in Spring MVC

How do you configure your Spring MVC web application to serve JSON for Ajax? It is not difficult. You probably have a view resolver in your dispatcher-servlet.xml that looks like this:

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:viewClass="org.springframework.web.servlet.view.JstlView"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp"></bean>

In order to serve JSON content, you need to use the ContentNegotiatingViewResolver. The configuration looks like this:
Read moreRead more

24
Mar

Highlight words in text with jQuery

highlight words jqueryOn the right is an example of word-highlighting in a blog post, done with jQuery (my javascript library of choice).  I needed to highlight certain words in blocks of formatted text, so I went searching for an existing solution.  I found a pretty good starting point, posted by bjarlestam on Stackoverflow.  His highlighting function is below.  It wraps the word inside a span tag with a class of your choosing.

jQuery.fn.highlight = function (str, className) {
    var regex = new RegExp(str, "gi");
    return this.each(function () {
        this.innerHTML = this.innerHTML.replace(regex, function(matched) {
            return "<span class=\"" + className + "\">" + matched + "</span>";
        });
    });
};

This is nice, because it uses a case-insensitive regular expression to find the word to highlight.  However, there is a flaw.  If you call this function on an element with non-text child elements, and the tag/attribute names or attribute values match the word to highlight, this function will mess up your HTML.  We’d like it to apply only to text nodes.
Read moreRead more

22
Mar

Combine and cache multiple CSS files for performance

This is a simple way to get a performance boost if you’re using PHP + Apache. I am using it to boost performance on GoToQuiz. If you are not using Apache, you can still use the PHP portion of this performance enhancement.

Some quick background: maintaining your site’s CSS in multiple files is frequently useful and sometimes necessary. But including multiple CSS files results in a performance hit when your pages load, because each one requires a separate request from the user’s browser to your server. Each request has associated overhead, which can be minimized if you send all CSS in one request.

This is how you can combine your CSS files with PHP and cache them, relying on a clever bit of .htaccess modification:

Read moreRead more

19
Mar

How to extract titles from web pages in Java

Let’s say you have a set of URLs and you want the web page titles associated with them. Maybe you’ve data-mined a bunch of links from HTML pages, or acquired a flat file listing URLs. How would you go about getting the corresponding page titles, and associating them with the URLs using Java?

You could use an HTML parser such as Jsoup to request the HTML document associated with each URL and parse it into a DOM document.  Once obtained, you could navigate the document and select the text from the title tag, like so:

String titleText = document.select("title").first().text();

Elegant, but a lot of overhead for such a simple task. You’d be loading the whole page into memory and parsing it into a DOM structure just to extract the title.  Instead, you could use the Apache HTTP Client library, which provides a robust API for requesting resources over the HTTP protocol.  But it would be unnecessary in this case.  Let’s keep it simple and rely only on the java standard library.

Read moreRead more

17
Mar

Create thumbnails and avatars in Java

Avatars–icon-sized images used to represent people online–are usually square. Consider, Facebook avatars are 50 by 50 pixel images. Most source images are not square, however. Cameras typically take pictures with a 4:3 aspect ratio. So to create an avatar from an image, you must do two things: crop it to a square and resize it to your desired icon size.

Thumbnails present a similar challenge.  When displaying thumbnails in an image gallery, you typically want to enforce at least one of width|height to be the same for all images to avoid a very sloppy look.  The cleanest look is achieved with square thumbnails of equal size, though the necessary cropping may not be desireable in every case.
Read moreRead more

17
Mar

Handy RowMapper base class for Spring JDBC

RowMappers are needed all over the place in your Spring JDBC DAO classes. One challenge that I kept running into was that when I wanted to reuse a particular RowMapper class for numerous queries, there was an ever-present threat of an underlying SQLException if certain columns were not present in the ResultSet. Obviously, a reusable RowMapper will set every field on the object it maps for, however not every ResultSet will include every field. Calling rs.getString("column_name"); will result in an exception being thrown if column_name is not present in the particular ResultSet.

So to solve this problem, I wrote this base RowMapper class:
Read moreRead more

9
Mar

Define global lists, sets and maps in Spring configuration

Sometimes you may need to define stand-alone collections–lists, maps and sets–in your Spring XML configuration files, so that your beans can reference them as properties.  This is easy to do using the util schema.  For example, let’s say you want to define a set of credit scores in your applicationContext.xml file:

<util:set id="creditScores">
    <value>600</value>
    <value>710</value>
    <value>760</value>
</util:set>

Now you can reference the set in your beans, like so:

<bean id="creditBean" class="com.this.is.my.CreditBean">
    <property name="myScores">
        <ref local="creditScores"/>
    </property>
</bean>

Read moreRead more

8
Mar

Annoying: Netbeans code completion in comments

I don’t understand why Netbeans performs code completion when you are typing a comment.  If I am working in a java file, typing a comment, and come to a word like “if” or “for”, Netbeans automatically inserts the code completion templates for those keywords.  Shouldn’t it recognize that I am typing within a comment?  Is there a fix for this issue?

EDIT: Another thing that drives me mad: I use Alt+F S to save. Constantly. I’m so trained in doing this that I usually don’t even think about it. In Netbeans, however, this often results in the letter “s” being typed, either because the File menu didn’t drop down fast enough or the S key registered twice. I’ve never had this problem in any other program.

4
Mar

How to load an image from a URL in java

Assuming you want to load the image from the URL into memory in order to display or manipulate it:

try {
    URL imageUrl = new URL("http//example/image.jpg");
    InputStream in = imageUrl.openStream();
    BufferedImage image = ImageIO.read(in);
    in.close();
}
catch (IOException ioe) {
    //log the error
}

Loading an image from a URL is as simple as that.  Now you’ve got a BufferedImage.

It can be even simpler, and if all you are doing is cropping or resizing the image, you may be interested in a little java image utility I wrote.  Using my ImageLoader and Image classes, the code would look like:

try {
    Image image = ImageLoader.fromUrl("http//example/image.jpg");
}
catch (IOException ioe) {
    //log the error
}

The Image class is a wrapper on a BufferedImage and provides some useful methods for cropping, resizing, and writing (saving) the image.  Let me know in the comments below if you’ve found this helpful.