<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Addictive Software &#187; Java</title>
	<atom:link href="http://blog.addictivesoftware.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.addictivesoftware.net</link>
	<description>ramblings of a chaotic mind</description>
	<lastBuildDate>Tue, 06 Jul 2010 09:04:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rest easy</title>
		<link>http://blog.addictivesoftware.net/2009/12/rest-easy/</link>
		<comments>http://blog.addictivesoftware.net/2009/12/rest-easy/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:10:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=49</guid>
		<description><![CDATA[I’ve been playing around with REST (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects
Advantages of rest

Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall
Every resource is [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been playing around with <a href="http://en.wikipedia.org/wiki/REST">REST</a> (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects</p>
<p>Advantages of rest</p>
<ul>
<li>Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall</li>
<li>Every resource is uniquely identified by a URI</li>
<li>Can do CRUD on those resources (they are just called POST, GET, PUT, DELETE)</li>
<li>used by all the major players for cloud applications (<a href="http://kenai.com/projects/suncloudapis/pages/Home">SUN's cloudApi</a>, <a href="http://www.microsoft.com/windowsazure/">Microsoft's Azure services platform</a>)</li>
<li>we might finally get rid of webdav</li>
</ul>
<p>I'll be using the <a href="https://jersey.dev.java.net/">Jersey</a> Framework for this.<br />
As i’ve been working on a backend that will allow you to manage <a href="http://en.wikipedia.org/wiki/Agile_development">agile development</a> processes (Scrum, Kanban etc) I thought I use that.</p>
<p>In this application every object is represented by a Card for instance a Story, Task, Bug, work Item, etc.<br />
Cards have a hierarchical structure and can each have custom properties<br />
The class looks something like this (removed methods for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then for instance the Story class with some default values set in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Story <span style="color: #000000; font-weight: bold;">extends</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Story<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Title&quot;</span>, <span style="color: #0000ff;">&quot;A new story&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Description&quot;</span>, <span style="color: #0000ff;">&quot;as a .. I want .. for the benefit of ..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Storypoints&quot;</span>, <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To be able to return these objects through REST they need to be in a format that can be distributed over HTTP, in this case we will be using XML and JSON.<br />
The <a href="https://jaxb.dev.java.net/">Java XML Bindings</a> are perfectly suited to create xml representations of our classes</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Card<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Entry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>* the Entry class is a simple class with a name and value property, I choose this approach as JAXB has an issue in representing HashMaps() in XML<br />
this will produce the following xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>for JSON we don’t need to do anything special then include the jersey-json.jar to the project.</p>
<p>Now to configure and hookup the rest servlet<br />
I added the jersey servlet to my web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rest Web Service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.sun.jersey.spi.container.servlet.ServletContainer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rest Web Service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/services/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And create the service provider class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@PerRequest
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/agileassistant&quot;</span><span style="color: #009900;">&#41;</span>
@Consumes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Produces<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AgileAssistantService <span style="color: #009900;">&#123;</span>
 ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will tell Jersey that this is a provider that will respond to requests to /rest/agileassistant, accept JSON and XML and can output JSON or XML depending on the accept headers sent by the client</p>
<p>Now if we for instance want to return the first card (rootCard) and all its children we add this method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Card rootCard <span style="color: #339933;">=</span> Card.<span style="color: #006633;">createCard</span><span style="color: #009900;">&#40;</span>Root.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@GET
	<span style="color: #000000; font-weight: bold;">public</span> Card getRoot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rootCard<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(in my data model the rootCard is always present and functions as a starting point to add the rest of the cards in a hierarchical structure)<br />
So requesting this card will effectively return the entire structure for my test data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Root&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Story&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Convince King&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;to let the prince go on an adventure&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Storypoints&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Story&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Rescue Princess&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;She'll be the one screaming from the tower window&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Once upon a time&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;in a land far far away&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Storypoints&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Doing a parameterized query is almost as easy</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@GET
	@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/card/{id}&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Card getCard<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> getCardById<span style="color: #009900;">&#40;</span>rootCard, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>here the id that you specify will be injected into the parameter<br />
giving the following output</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;card</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Task&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;nobody&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;assigned&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Slay dragon&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;his weak spot seems to be a small spot on his belly&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Description&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hours&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/card<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To conclude enabling rest for your applications is pretty easy, writing this blog took longer than writing the code.<br />
In a next post I will add POST/PUT/DELETE requests to store data in your application</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/12/rest-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaFX data binding to Java</title>
		<link>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/</link>
		<comments>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:01:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=3</guid>
		<description><![CDATA[Data binding in JavaFX is pretty straightforward.
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing

var someObject : bind myJavaclass.getProperty&#40;&#41;;

JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for [...]]]></description>
			<content:encoded><![CDATA[<p>Data binding in JavaFX is pretty straightforward.<br />
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var someObject <span style="color: #339933;">:</span> bind myJavaclass.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for all javafx objects.<br />
It seems JavaFX uses the Observer/Observable pattern for all its internal objects, well we can do the same to bind with JavaObjects.<br />
To get myself familiar with JavaFX i decided to write a photoviewer/editor. This application has a java backend with in particular a Photo class which contains all the information of a certain picture, filename, path, tags, exif information etcetera</p>
<p>To get changes in the Java class to appear in the gui I create a PhotoAdapter javafx class that extends Observer and implemented the properties I need in my GUI.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PhotoAdapter <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observer</span> <span style="color: #009900;">&#123;</span>
    public<span style="color: #339933;">-</span>read var filename<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var tags<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var exif<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    public<span style="color: #339933;">-</span>init var photo<span style="color: #339933;">:</span> Photo
         on replace <span style="color: #009900;">&#123;</span>
            photo.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            filename<span style="color: #339933;">=</span>photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    override function update<span style="color: #009900;">&#40;</span>observable<span style="color: #339933;">:</span> <span style="color: #003399;">Observable</span>, arg<span style="color: #339933;">:</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        filename <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>after that I had my existing Java Photo class extend Observable and in every method that made changes I added the methods setChanged() and NotifyObservers() to inform the Observers about the change</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Photo <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> exifInformation<span style="color: #339933;">;</span> 
    ....
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setExifInformation<span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span> exifInfo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">exifInformation</span> <span style="color: #339933;">=</span> exifInfo<span style="color: #339933;">;</span>
        setChanged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        notifyObservers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>in my main javafx class I can then use it as follows</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    var photoAdapter <span style="color: #339933;">=</span> PhotoAdapter <span style="color: #009900;">&#123;</span>
        photo <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> Photo<span style="color: #009900;">&#40;</span>startImage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and use to fill a ListView swing object with for instance the tags:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">ListView</span> <span style="color: #009900;">&#123;</span>
        items<span style="color: #339933;">:</span>     bind photoAdapter.<span style="color: #006633;">tags</span><span style="color: #339933;">;</span>
        width<span style="color: #339933;">:</span>     <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
        height<span style="color: #339933;">:</span>    <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>et viola, thats all that there is to it</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.Net&#8217;s using() for Java</title>
		<link>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/</link>
		<comments>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:22:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java7]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=39</guid>
		<description><![CDATA[As I'm working as a Java developer in a microsoft oriented company this means I sometime have to do .Net programming.
As there are some things I don't like about .Net (no checked exceptions is one) there is one thing I find very good and useful.
In .Net an object that implements the IDisposable interface can be [...]]]></description>
			<content:encoded><![CDATA[<p>As I'm working as a Java developer in a microsoft oriented company this means I sometime have to do .Net programming.<br />
As there are some things I don't like about .Net (no checked exceptions is one) there is one thing I find very good and useful.</p>
<p>In .Net an object that implements the IDisposable interface can be initiated with the Using command like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>TextWriter w <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">CreateText</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;log.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span> w.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;This is line one&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>After the block has executed, using will take care of closing the writer and freeing the resources.<br />
This gives me solid readable code without memoryleaks.</p>
<p>Now looking at Java, closing resources usually involves putting a close() in a finally block, with another try-catch construction to catch exceptions that the close() can throw.<br />
this is ugly, errorprone and can obfuscate the real exception that happened.</p>
<p>Java 7 comes to the rescue, one of the approved proposals for java 7 is "automatic resource management"<br />
the following code block will demonstrate this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> copy<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> src, <span style="color: #003399;">String</span> dest<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
         <span style="color: #003399;">OutputStream</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>dest<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buf, <span style="color: #cc66cc;">0</span>, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//optional catch block</span>
    <span style="color: #666666; font-style: italic;">//optional finally block</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In Java 6 or lower the above code would contain 2 try-finally blocks to handle closing the input and ouput streams, another 2 try-catch blocks to handle exceptions from the close() commands, and more catches for exceptions you would want to handle specifically</p>
<p>Off all the features in java 7 this is the one I'm looking forward the most, the forkjoin and invokeDynamic changes following close though.<br />
here is a list of all the features in java 7<br />
<a href="http://openjdk.java.net/projects/jdk7/features/">http://openjdk.java.net/projects/jdk7/features/</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
