Spring validations, resolving arguments for errors
Using spring web mvc and the spring tags. it is easy to show i18n errors. However, what if you want to pass in parameters to your messages? For instance if you have a general message for a required field that is not set, you don’t want to have a message for each required field, but a general message with a place holder for the name of the field that is missing.
Read more…
Webapplications with node.js – Jade
In my previous post I described how you can install node.js and how to use express.js to create a webapplication for the node.js server. Now it is time to look into the templating engine – Jade – that express uses for outputting html to the browser.
As I did not have any knowledge yet of jade, it was time for me to do a quick study of jade. As a lot of github projects, the initial documentation is rather sparse, since it only showed an example of the jade template language will be translated into html. But luckily clicking on the github fork ribbon showed some more documentation on github. Basically it looked pretty easy, it’s just html with indentation meaning something and with all the >’s and <’s. replaced by ‘(‘ and ‘)’. At least that makes it easier to type in html
Read more…
Webapplications with node.js
I really think that javascript is going to change the server side world just as it has changed the client side world. The web is so much richer today using html 5 and javascript that it is hard to imagine it otherwise. By moving a lot of functionality to the client, the users will have a much better experience using a site, then using simple html pages with only server interactions. However building all those richness only in the client, leaves the server open for hacking attempts. Therefore you will have to code many of the same validations and checks twice. Once on the client for immediate feedback to the users, and once on the server to keep bad requests outside. So if you can use the same validations and checks both on the client and on the server, you can probably develop a bit faster and more secure.
Read more…
Twitter 406
Integrating with social sites, such as twitter, seems to be so easy, and it actually can be, once you get the configuration set up correctly. Last day however, I spend most of my time trying to hunt down a 406 error code when trying to login using a twitter account. According to the twitter site I have to :
- Think of a name for my application, well that was not too hard
- Enter a description
- Tell on which domain your twitter application will be hosted, but you can enter a placeholder for this if you don’t know that yet, or want to test
And then you should be set and ready to go. Read more…
Legacy code: treat it with silk gloves
Legacy code is tough to deal with. It may be the toughest thing we have to deal with in our everyday jobs.
Before we start, let’s align our definition of what legacy code is. Legacy code is untested code. Legacy code may be old, but not necessarily. Unfortunately some projects are actively producing legacy code.
Most programmers have to work with legacy code frequently. Even worse, they often must make changes in the legacy code. They must fix bugs in the legacy code, or change it to implement new features. At the end of last October I started working on a project that at the time, fully consisted of legacy code. I won’t share any specific details about that client and their projects, but I like to share how we work with the legacy code. Read more…
Bye bye XMLGregorianCalendar (part 1)
Introduction.
We all know that working with dates in the Java language isn’t perfect, at least when you just look at the standard JDK. Not only is more then half of them methods in java.util.Date deprecated, but in order to get the Date of a java.util.Calendar you have to use the getTime() method. So when the base object aren’t that well to start with, things can only get worse when you start to mix in more technologies like XML.
When you are getting started with marshalling and unmarshalling java objects to XML you’re bound to run into the javax.xml.datatype.XMLGregorianCalendar. Did you ever try to get the Date value for a field unmarshalled from XML and wrote the following code:
Date date = xmlGregorianCalendar.toGregorianCalendar.getTime();
Why isn’t an XMLGregorianCalendar a GregorianCalendar just like a ArrayList is a List or a HashMap is a Map?
Oke, some smart people discovered these problems as well and started to specify a new Date and Time API (JSR 310).
Their idea are great although an anwser in the original JSR does give me some worries:
2.11 Are there any internationalization or localization issues?
No issues are expected.
In my opinion internationalization and localization are the major issues when working with dates and times, but hey, that’s propably just me.
So the expert group started on 13 februari 2007 and finished the early review on 28 march 2010.
I’m really wondering in which version of the JDK this well be included.
And of course once this has been introducted into the JDK: How long will it take to get it supported as the default datatype for XML dates?
Solution.
Fortunately there is already a solution available: Joda-Time.
With it’s DateTime, Period, Interval and Duration objects it provided a mature, easy to use and comprehensive feature set.
There is only one slight problem: JAXB doesn’t know anything about Joda-Time and it’s DateTime object.
By default fields in an XML-file with a xsd:datetime type will be unmarshalled to an XMLGregorianCalendar.
In various projects I have seen DateConverter utility classes for conversion from and to the Joda-Time DateTime object.
Wouldn’t it be nice if you just could use the DateTime field and have the conversion be done as some form of ‘custom marshalling‘?
But wait: you can! The JAXB API specifies an XMLAdapter, which “Adapts a Java type for custom marshaling“
What we need is an XMLAdapter, which allows us marshal and unmashal between a ValueType and the BoundType.
The ValueType is some class, which JAXB knows how to handle out of the box (Strings, Lists or an Calendar and the BoundType is some type that JAXB doesn’t know how to handle. The Adapter will specify how transform from one type to the other.
So I’ve written a DateTimeAdapter, which unmashals from a Calender to DateTime and marshals a DateTime to a Calendar.
public class DateTimeAdapter extends XmlAdapter {
@Override
public DateTime unmarshal(Calendar calendar) throws Exception {
return new DateTime(
calendar.get(Calendar.YEAR),
// Correct for the difference between Joda DateTime
// and JDK Calendar.
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DATE),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND),
calendar.get(Calendar.MILLISECOND),
DateTimeZone.forTimeZone(calendar.getTimeZone()));
}
@Override
public Calendar marshal(DateTime v) throws Exception {
Calendar calendar = Calendar.getInstance(v.getZone().toTimeZone());
calendar.setTime(v.toDate());
return calendar;
}
}
Now the tricky part: You have to tell the XML (un)marshaller that it should use the custom adapter for mashalling certain fields in your objects.
But as you propably could have guessed: the JAXB-API provides a solution for that as well:
Just add an XmlJavaTypeAdapter annotation to the field in your object and the Marshaller and Unmarshaller will invoke the Adapter during the process.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "book", propOrder = {
"author",
"title",
"publicationDate"
})
public class Book {
@XmlElement(required = true)
protected String author;
@XmlElement(required = true)
protected String title;
@XmlElement(required = true)
@XmlSchemaType(name = "date")
@XmlJavaTypeAdapter(nl.iprofs.util.jaxb.adapter.DateTimeAdapter.class)
private DateTime publicationDate;
// Getters and Setters
.....
}
Part 2: WsImport, Maven and XJC.
In the next blog about this topic I’ll dig into the topic of WsImport with Maven and XJC (XML to Java Compiler) which is being using in generating classes for webservices based upon a WSDL and Schema. If some generator is going to generate the java classes for the SOAP service, I want it to generate fields as DateTime objects and annotate them with my XMLAdapter.
Sound easy, but of course it isn’t just that.
Dependency Injection with Spring
Dependency injection is hot, and Spring Framework is one of the most popular frameworks that facilitates in dependency injection. Why is dependency injection so hot? If almost every programmer is using it, there must be a very good reason.
And there obviously is. The great thing about Object Orientation is polymorphism. Through polymorphism we can depend on abstraction at compile time, and at the concrete implementation at runtime. This is called dependency inversion; we invert the direction of our dependencies. It promotes decoupled systems and greatly reduces compile time, too some extend even in Java. And this is why everybody is so excited about dependency injection; it facilitates dependency inversion.
Read more…
HTML5 and CSS3 features and browser compatibility
HTML5 is the latest major milestone in HTML, XHTML, and the HTML DOM. It represents an enormous advance for modern web applications and promises to increase the capabilities of browsers and Web applications for years to come. The previous version of HTML – 4.01 – was released in 1999 and a lot has changed since then on the web. Therefor HTML5 has been brought to live in order to meet the demand of the users. When HTML5 is supported by a browser it will go hand in hand with CSS3. One fact is that it will improve the interaction with the user. This article will go into the features of HTML5 as well as CSS3 and the level of compatibility of the current browsers.
Read more…
Improving CountdownLatch example from JDK-apidocs
Some time ago I discussed a use case for CountDownLatch, which was a thread safety issue.
If you read that very closely, you might have noticed I did not follow the exact pattern of the API specification of CountDownLatch.
I did that on purpose, because the example from the documentation is not perfect.
Here I will prove this claim and explain my own version.
Read more…
Custom workflows in Hippo CMS 7
Customizing the CMS workflow is one of the things you eventually come across in almost every CMS implementation, be it minor tweaks or a complete rethink.
In Hippo CMS, the workflow configuration is part of, and stored in, the JCR content repository.
After some searching for workflows in Hippo CMS, you will ultimately find Hippo’s page about hooking into the default workflow.
It’s a fine step by step manual, and it serves as a good starting point, were it not for the fact that it’s quite outdated and does not function anymore.
Read along to find out how to make things work in Hippo CMS 7.6.
Read more…
Recent Comments