Programmer's Cookbook

Recipes for the practical programmer

Friday, June 27, 2008

 

Spring + ResourceBundleMessageSource + Arguments

Recently I had a request to move some hard-coded error messages from the source files to an external properties file. At first I ran into some issues since we were using parameters, but spring's taglib allows for that so here goes:

Spring config

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com.harryfwong.shared.messages">
</property>


com/harryfwong/shared/messages.properties

error.testingParameters=Are you passing in Parameters: {0}, {1}?


displayError.jsp

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code="error.testParameters" argumentseparator=";" arguments="param1;param2">
</spring:message>

Labels: , , , , ,


Friday, June 13, 2008

 

Parsing Comma Seperated Values (CSV) in Java

I suggest using Java CSV Library, it is very easy to use. If you use Maven 2, you can use this to load it as a dependancy.


<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>


The code below reads in a CSV file, parsing one line at a time, splitting each line into the vals array.


CsvReader rdr = new CsvReader("simeFile.csv");
while (rdr.readRecord()) {
String[] vals = rdr.getValues();
...
System.out.println(vals.length);
// process here
}

 

DB Schema for Spring-Security 2.0

Spring Security was only recently released, so the docs are fairly poor. So if you have been looking everywhere for the DB schema to use with <jdbc-user-service/>, look no further.

Here are the generic table create statements. The foreign-key columns are marked, which you can either ignore or translate them for your specific DB.


create table users (
username varchar(32) primary key,
password varchar(64) not null,
enabled bit not null
)

create table authorities (
username varchar(32) not null, [FK]
authority varchar(64) not null
)

create table groups (
id number(10) primary key,
group_name varchar(64)
)

create table group_members (
group_id number(10) not null, [FK]
username varchar(32) not null [FK]
)

create table group_authorities (
group_id number(10) not null, [FK]
authority varchar(64) not null
)

 

Creating an MD5 dogest of a Java String

You can generate MD5's using Java's own MessageDigest class. This class will generate the MD5 value as a set of bytes. Typically this isn't what you want. In most cases you will want to create a hex string from the bytes, so it looks something like this, "007868b95b02a639bed49adea41f266e". You can rectify this by downloading the commons-codec library, and running the bytes through the Hex class.

The code below does just this.


import java.security.MessageDigest;
import import org.apache.commons.codec.binary.Hex;

...

String myString = "The string to create a digest from";
String md5String = null;

try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(myString.getBytes());
md5String = new String(Hex.encodeHex(digest.digest()));
}
catch (Exception e) {}

This page is powered by Blogger. Isn't yours?