<?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>Gridshore &#187; Spring Framework</title>
	<atom:link href="http://www.gridshore.nl/tag/spring-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gridshore.nl</link>
	<description>A weblog about software engineering, Architecture, Technology an other things we like.</description>
	<lastBuildDate>Tue, 13 Dec 2011 15:36:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using the NOS open data API with the springframework and jackson</title>
		<link>http://www.gridshore.nl/2011/01/20/using-the-nos-open-data-api-with-the-springframework-and-jackson/</link>
		<comments>http://www.gridshore.nl/2011/01/20/using-the-nos-open-data-api-with-the-springframework-and-jackson/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 23:36:34 +0000</pubDate>
		<dc:creator>jettro</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/?p=1134</guid>
		<description><![CDATA[<p>Some time ago the NOS started providing their data as open data through an API. Using this API you can get the latest news items. You can limit the results to sport news. There is also a search available and a few more categories to find news for. The API is a REST like [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago the NOS started providing their data as open data through an API. Using this API you can get the latest news items. You can limit the results to sport news. There is also a search available and a few more categories to find news for. The API is a REST like api and can return xml as well as JSON. In this blog post I am creating an API with the Spring framework RESTTemplate. </p>
<p><span id="more-1134"></span>
<p>First the basics. If you want to try these examples yourself you need to request a key. More information can be found here:</p>
<p><a href="http://open.nos.nl/">http://open.nos.nl/</a></p>
<p>Because groovy is a very nice language to do this kind of experimentation I&#8217;ll show you de groovy code first. Just a few lines to print a JSON string:</p>
<pre class="brush: groovy; title: ; notranslate">
def key = &quot;Your Key&quot;
nos = new RESTClient(&quot;http://open.nos.nl/v1/&quot;)
JSON newsItems = nos.get(path : &quot;latest/article/key/$key/output/json/category/sport/&quot;).data
print newsItems
</pre>
<p>The result is a long string. If you want to convert a string to nice formatted json check the resources for a link. The following image gives you an idea of the structure of the returned JSON:</p>
<p><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2011-01-19-at-22.27.31.png" alt="Screen shot 2011-01-19 at 22.27.31.png" border="0" width="600" height="395"/></p>
<p>Now let us do the same thing in java. Usually this takes much more code. To be honest, it is not at all bad if you use the RESTTemplate of the Spring framework. Have a look at the following code:</p>
<pre class="brush: java; title: ; notranslate">
public class SearchApiUsingSpring {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(&quot;http://open.nos.nl/v1/latest/article/key/{key}/output/json/category/sport/&quot;,
                String.class, &quot;YOUR KEY&quot;);
        System.out.println(result);
    }
}
</pre>
<p>Of course the result is the same, not bad is it? Just three lines of real code. Nice, but not enough for my example. Of course I want to use the result as an object. Therefore I am going to use an object mapper called Jackson. Check resources for a link. Jackson is a framework that enables us to map JSON objects to actual java objects. Based on the structure of the returned JSON I created a java object and used jackson annotations to map it to the JSON structure. The spring RESTTemplate facilitates using mappers. A Jackson mapper is available. The following code shows the improved sample.</p>
<pre class="brush: java; title: ; notranslate">
public class SearchApiUsingSpring {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        LatestArticle result = restTemplate.getForObject(&quot;http://open.nos.nl/v1/latest/article/key/{key}/output/json/category/sport/&quot;,
                LatestArticle.class, &quot;Your Key&quot;);

        for (Article article : result.getItems().get(0)) {
            System.out.printf(&quot;Title : %s\n&quot;, article.getTitle());
            System.out.printf(&quot;Description : %s\n&quot;, article.getDescription());
            System.out.printf(&quot;Link : %s\n&quot;, article.getLink());
            System.out.println(&quot;_________________________________&quot;);
        }
    }
}
</pre>
<p>Just a very small difference, besides the printing stuff. The change is the type returned by the getForObject method. Now it is not a string anymore but a LatestArticle. To be able to use Jackson you do need to create the classes with a few annotations.</p>
<pre class="brush: java; title: ; notranslate">
@JsonIgnoreProperties(ignoreUnknown = true)
public class LatestArticle {
    @JsonProperty(&quot;latest_article&quot;)
    private ArrayList&lt;ArrayList&lt;Article&gt;&gt; items = new ArrayList&lt;ArrayList&lt;Article&gt;&gt;();

	// getters and setters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Article {
    private String id;
    private String type;
    private String title;
    private String description;
    private String link;

	// getters and setters
}
</pre>
<p>I use the annotation @JsonIgnoreProperties, I do not want to break my code if a property that is available in json is not available in my java class. I&#8217;ll remove it later on when I have all the properties mapped. Why not immediately? Well I want to explain what I did with the date fields first. Mapping a date is a bit different. We do need to map the date using the specific format. I include Joda time to the path and create a synchronizer for a Joda date time object. Jackson has support for joda time out of the box. But I used a different format and therefore I had to create my own. The class Article is slightly enhanced with a DateTime property called published. I only show the setter this time. This is where we have to assign our own deserializer. The code for my deserializer is also shown.</p>
<pre class="brush: java; title: ; notranslate">
// from Article
    @JsonDeserialize(using = JsonDateDeserializer.class)
    public void setPublished(DateTime published) {
        this.published = published;
    }

public class JsonDateDeserializer extends JsonDeserializer&lt;DateTime&gt; {
    private final DateTimeFormatter formatter = DateTimeFormat.forPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;);

    @Override
    public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return formatter.parseDateTime(jp.getText());
    }
}
</pre>
<p>Now we can also print the published date. Be sure not to put annotations on the field and on the setter. I had the problem with the last_update, I need JsonProperty annotation. First I put it on the field, but than the JsonDeserialize was not read anymore.</p>
<p>That is it for now, I will write more when I am ready. Than I will also publish the code. Some of the resources that were useful to me are written down below.</p>
<h3>Resources</h3>
<p><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-resttemplate">http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-resttemplate</a><br/><br />
<a href="http://jackson.codehaus.org/">http://jackson.codehaus.org/</a><br/><br />
<a href="http://jsonviewer.stack.hu/">http://jsonviewer.stack.hu/</a><br/><br />
<a href="http://java.dzone.com/articles/how-serialize-javautildate">http://java.dzone.com/articles/how-serialize-javautildate</a><br/></p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2011%2F01%2F20%2Fusing-the-nos-open-data-api-with-the-springframework-and-jackson%2F&amp;title=Using%20the%20NOS%20open%20data%20API%20with%20the%20springframework%20and%20jackson&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2011/01/20/using-the-nos-open-data-api-with-the-springframework-and-jackson/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing with the spring mobile project</title>
		<link>http://www.gridshore.nl/2010/11/28/playing-with-the-spring-mobile-project/</link>
		<comments>http://www.gridshore.nl/2010/11/28/playing-with-the-spring-mobile-project/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 20:02:13 +0000</pubDate>
		<dc:creator>jettro</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring-mobile]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/?p=1105</guid>
		<description><![CDATA[<p>Spring seems to be all over the place today. Interesting projects just seem to keep coming out of their development machines. Hard to keep up. In my previous blog I had a look at the spring-social project. In this blog post I&#8217;ll have a go at the spring-mobile project. I&#8217;ll go through the things [...]]]></description>
			<content:encoded><![CDATA[<p>Spring seems to be all over the place today. Interesting projects just seem to keep coming out of their development machines. Hard to keep up. In <a href="http://www.gridshore.nl/2010/11/04/connecting-to-linkedin-using-spring-social/">my previous blog</a> I had a look at the spring-social project. In this blog post I&#8217;ll have a go at the spring-mobile project. I&#8217;ll go through the things you have to do for configuration and I&#8217;ll have a go at creating a very basic sample application that you can pickup and play around with. I&#8217;ll even extend the framework just a little bit to implement a usecase I have that is not covered by the project itself.</p>
<p><span id="more-1105"></span><br />
<h2>Introduction</h2>
<p>Spring mobile has it&#8217;s own home, you can <a href="http://www.springsource.org/spring-mobile">find the homepage here</a>. The project has a <a href="http://git.springsource.org/spring-mobile/samples">sample project</a> as well. So why did I write this blog? Just to understand the project better and see how it fits in my personal projects. Maybe others will get a better understanding as well by reading this blog. Let me know if you find something interesting or have some improvements.</p>
<p>The sourcecode is available at Github : <a href="http://github.com/jettro/GridshoreSamples.git">http://github.com/jettro/GridshoreSamples.git</a></p>
<h2>The sample</h2>
<p>I wanted to keep the sample as simple as possible to show how spring-mobile works. The sample must be complete enough to be able to explain the features that are important. Therefore I do not really create a functional thing. Just one page that shows whether you are using a mobile browser and some information about the browser you are using.</p>
<h3>Simulation mobile platforms</h3>
<p>Within Safari it is easy to pretend you are using another client than the safari browser that is installed. Use the developer menu bar item. Look at the following screendump.</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-16.27.40.png" alt="Screen shot 2010-11-28 at 16.27.40.png" border="0" width="600" height="386" /></p>
<h3>How does it work?</h3>
<p>To understand how the resolvers can do what they do, resolve the client and learn about it capabilities, you need to know a bit about an html request. I take it that all the readers of my blog have the basic understanding of a html request header. Each request has a body and a header. The header contains some meta data about the client. To get the basic understanding, look at the code of the LiteDeviceResolver. This resolver uses a few items from the header like User-Agent, OperaMini and wap support to determine if a client is a mobile client. The class has a number of keywords that it checks for presence in the User-Agent string. In our case using the iPhone browser, there would be a match for the keyword phone.</p>
<p><code>Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7</code></p>
<h3>Building the application</h3>
<p>I use gradle to build the sample. Using the intellij and jetty plugin, gradle works like a charm. I do not want to go into to much details about gradle. You can find the file in the provided git repository.</p>
<p>Most important thing for the build configuration are the dependencies for spring mobile and an important library I use, wurfl. More on that later.</p>
<p><code><br />
compile "org.springframework.mobile:spring-mobile-device:1.0.0.M1"<br />
runtime "net.sourceforge.wurfl:wurfl:1.2"<br />
</code></p>
<h2>Spring mobile configuration</h2>
<p>Spring mobile is about resolving the client or device used to send a request. Therefore configuring spring-mobile is about configuring the device resolver. There are two options. The default makes use of the LiteDeviceResolver. The second option makes use of the WurflDeviceResolver. More on these resolvers follows. For both configurations you just need one bean. The following code blocks show both cases.</p>
<pre class="brush: xml; title: ; notranslate">
    &lt;mvc:interceptors&gt;
        &lt;bean class=&quot;org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor&quot;/&gt;
    &lt;/mvc:interceptors&gt;
</pre>
<pre class="brush: xml; title: ; notranslate">
    &lt;mvc:interceptors&gt;
        &lt;bean class=&quot;org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor&quot;&gt;
            &lt;constructor-arg&gt;
                &lt;device:wurfl-device-resolver root-location=&quot;/WEB-INF/wurfl/wurfl-2.0.25.zip&quot;/&gt;
            &lt;/constructor-arg&gt;
        &lt;/bean&gt;
    &lt;/mvc:interceptors&gt;
</pre>
<p>That is most of it already. Now you can resolve the different devices. There is one more thing if you want to actually do something with a the obtained device information. You might want to inject the Device into the Controller. From the reference documentation I copied the following class that registers WebArgumentResolver with the controller invocation. I do not really understand why this is not available as a provided bean. I think this will come, or maybe I misunderstood something. For completeness I&#8217;ll show you the code as well.</p>
<h3>CustomWebArgumentResolverInstaller</h3>
<pre class="brush: java; title: ; notranslate">
@Component
public class CustomWebArgumentResolverInstaller {
    @Autowired
    public CustomWebArgumentResolverInstaller(AnnotationMethodHandlerAdapter controllerInvoker) {
        WebArgumentResolver[] resolvers = new WebArgumentResolver[1];
        resolvers[0] = new DeviceWebArgumentResolver();
        controllerInvoker.setCustomArgumentResolvers(resolvers);
    }
}
</pre>
<h2>Device resolvers</h2>
<p>Device resolvers use headers from the request to determine the Browser. The LiteDeviceResolver and with it the LiteDevice are created using the device resolving technique as copied from wordpress. It does not expose any capabilities of a mobile device, just that you are dealing with a mobile device.</p>
<p>The Device interface only exposes the isMobile method. You can also use the toString method. The following jsp shows some very basic lines of text.</p>
<p>&lt;p&gt;Is Mobile : &lt;c:out value=&quot;${device.mobile}&quot;/&gt;&lt;/p&gt;<br />
&lt;p&gt;Device:&lt;/p&gt;<br />
&lt;c:out value=&quot;${device}&quot;/&gt;</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-15.42.53.png" alt="Screen shot 2010-11-28 at 15.42.53.png" border="0" width="189" height="99"/></p>
<p>Spring mobile comes out of the box with support for wurfl, to be honest, I had never heard of it before. But it is a pretty extensive library that knows about a lot of phones and other mobile devices. It knows a lot more about the capabilities of your devise than the default LiteDevice. Spring mobile also provides namespace support for other device resolving strategies. The wurfl device resolver has the option to supply the data in a zip file and a patch in an xml file. The following image shows you part of the toString output of the same jsp. I did not use the patch functionality.</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-16.06.24.png" alt="Screen shot 2010-11-28 at 16.06.24.png" border="0" width="600" height="531" /></p>
<p>The WurflDevice implementation exposes a lot more properties. You can ask for all capabilities and the markup. The following jsp code is used to show additional information.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core_rt&quot; %&gt;
&lt;p&gt;Is Mobile : &lt;c:out value=&quot;${device.mobile}&quot;/&gt;&lt;/p&gt;
&lt;h2&gt;Basic Device info&lt;/h2&gt;
&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;id&lt;/td&gt;
        &lt;td&gt;&lt;c:out value=&quot;${device.id}&quot;/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;UserAgent&lt;/td&gt;
        &lt;td&gt;&lt;c:out value=&quot;${device.userAgent}&quot;/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;MarkUp&lt;/td&gt;
        &lt;td&gt;&lt;c:out value=&quot;${device.markUp}&quot;/&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;h2&gt;Capabilities&lt;/h2&gt;
&lt;table&gt;
    &lt;tr&gt;
        &lt;th&gt;key&lt;/th&gt;
        &lt;th&gt;value&lt;/th&gt;
    &lt;/tr&gt;
    &lt;c:forEach var=&quot;item&quot; items=&quot;${device.capabilities}&quot;&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;c:out value=&quot;${item.key}&quot;/&gt;&lt;/td&gt;
            &lt;td&gt;&lt;c:out value=&quot;${item.value}&quot;/&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/c:forEach&gt;
&lt;/table&gt;
</pre>
<p>The screen than looks like this</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-16.36.48.png" alt="Screen shot 2010-11-28 at 16.36.48.png" border="0" width="600" height="521" /></p>
<h2>iPad and other mobile devices with big screens</h2>
<p>Some time a go I wanted to browse to the <a href="http://schradinova.nl/">website of Schradinova</a> using my iPad. I was redirected to the mobile website, but the mobile website was not available yet. There was no way to go to the actual site. By now the mobile site works, but still I cannot go to the original website using the iPad. Therefore I want to present the option to the user to explicitly go to the mobile site or to the normal site.</p>
<p>One way to accomplish this is by providing a mechanism to read a request parameter before doing the client resolving. Therefore I go back to my most simple example of showing the toString of the device. I have created a subclass of the spring provided DeviceResolvingHandlerInterceptor. My interceptor looks for the request param <em>mobile</em>. If found a dummy device is created with the isMobile method implemented. Might not be the nicest solution, but it does show the possibilities. The next code block shows the new HandlerInterceptor and the dummy device which is a direct copy of the LiteDevice.</p>
<pre class="brush: java; title: ; notranslate">
public class WithOverruleDeviceResolvingHandlerInterceptor extends DeviceResolvingHandlerInterceptor {
    public static final String mobile = &quot;mobile&quot;;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (null != request.getParameter(mobile)) {
            boolean mobileOverruled = Boolean.parseBoolean(request.getParameter(mobile));
            Device overruledDevice;
            if (mobileOverruled) {
                overruledDevice = DummyDevice.MOBILE_INSTANCE;
            } else {
                overruledDevice = DummyDevice.NOT_MOBILE_INSTANCE;
            }
            request.setAttribute(CURRENT_DEVICE_ATTRIBUTE, overruledDevice);
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }
}

public class DummyDevice implements Device {

    public static final DummyDevice MOBILE_INSTANCE = new DummyDevice(true);

    public static final DummyDevice NOT_MOBILE_INSTANCE = new DummyDevice(false);

    public boolean isMobile() {
        return mobile;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(&quot;[DummyDevice &quot;);
        builder.append(&quot;mobile&quot;).append(&quot;=&quot;).append(isMobile());
        builder.append(&quot;]&quot;);
        return builder.toString();
    }

    private final boolean mobile;

    /**
     * Creates a GenericDevice.
     */
    private DummyDevice(boolean mobile) {
        this.mobile = mobile;
    }
}
</pre>
<p>I also add a link to the page to go to the mobile or the normal equivalent of the page. The following screendumps give you the different options using a mobile browser, a normal browser and using the explicit request for a mobile site or the normal site.</p>
<p>We start with the normal browser doing a normal request. Than we push the mobile link and after that the normal link. Look at the url bar, the is mobile and the first part of the description. Without an explicit preferred site it will show LiteDevice, in the other case it shows DummyDevice. Now three screendumps.</p>
<p><img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-20.52.31.png" alt="Screen shot 2010-11-28 at 20.52.31.png" border="0" width="600" height="241" /><br/><br />
<img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-20.57.22.png" alt="Screen shot 2010-11-28 at 20.57.22.png" border="0" width="600" height="241" /><br/><br />
<img style="display:block; margin-left:auto; margin-right:auto;" src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-11-28-at-20.57.54.png" alt="Screen shot 2010-11-28 at 20.57.54.png" border="0" width="600" height="241" /><br/></p>
<p>If you really want to see it work for a mobile browser I&#8217;d urge you to checkout the sample and try it yourself <img src='http://www.gridshore.nl/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2>Last remarks</h2>
<p>Well, that is it. You should be capable now of creating your own mobile site. Finally I want to mention that spring mobile also has an option to redirect a user with a mobile browser to a completely different site. Check the reference manual to see how that works.</p>
<p>If you got this far, thanks for reading. See you next time.</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2010%2F11%2F28%2Fplaying-with-the-spring-mobile-project%2F&amp;title=Playing%20with%20the%20spring%20mobile%20project&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2010/11/28/playing-with-the-spring-mobile-project/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating a w3c validated rss feed using Rome and spring 3</title>
		<link>http://www.gridshore.nl/2010/02/16/creating-a-w3c-validated-rss-feed-using-rome-and-spring-3/</link>
		<comments>http://www.gridshore.nl/2010/02/16/creating-a-w3c-validated-rss-feed-using-rome-and-spring-3/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 11:16:00 +0000</pubDate>
		<dc:creator>jettro</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[rome]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[w3c]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/?p=999</guid>
		<description><![CDATA[<p> <p>For my current customer I had to create an rss feed. In the java domain you immediately grab Rome to do the job. There was a catch. My customer wants (with good reason) to have feeds validated by the w3c feed validator. This turned out to be a slightly more complicated job. Luckily [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.gridshore.nl/wp-content/uploads/romelogo-small.jpg" alt="romelogo-small.jpg" border="0" width="200" height="144" align="left" />
<p>For my current customer I had to create an rss feed. In the java domain you immediately grab <a href="https://rome.dev.java.net/">Rome</a> to do the job. There was a catch. My customer wants (with good reason) to have feeds validated by the <a href="http://validator.w3.org/feed/">w3c feed validator</a>. This turned out to be a slightly more complicated job. Luckily Rome has good support for extensions, so at least it was possible.</p>
<p>In this blog post I describe the challenges I had creating the validated feed. If you want more in depth information please check my post on my employers blog : <a href="http://blog.jteam.nl/2009/12/17/serving-a-heavy-load-rss-feed-with-spring-3-and-ehcache/">serving a heavy load rss feed with spring 3 and ehcache</a>.</p>
<p><span id="more-999"></span><br />
<h2>Rome extension</h2>
<p>At the moment I am missing the atom namespace in the resulting xml.</p>
<pre>
&lt;rss version="2.0" <strong>xmlns:atom="http://www.w3.org/2005/Atom</strong>"&gt;
</pre>
<p>I also miss the actual atom link:</p>
<pre>
&lt;atom:link href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" /&gt;
</pre>
<p>Now we must tell Rome how to generate this content. Rome supports a plugin structure. Detailed information can be found on this page:</p>
<p><a href="http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialSampleModule">http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialSampleModule</a></p>
<p>An extension consists of a module containing the data to be used by the extension, a generator if the extension needs to add items to the generated feed and a parser if the extension is about reading feeds. In my case I only need the module component and the generator. Finally you need to tell Rome about the extension, this is done in a configuration file that needs to be available on the classpath.</p>
<h3>Module</h3>
<p>The only data I need is the link for the atom element, the href. The module itself consists of an interface and an implementation. The interface defines the getters and setters. The implementation has a few additional methods. The following code block shows them both.</p>
<pre class="brush: java; title: ; notranslate">
public interface AtomNSModule extends Module {
    public static final String URI = &quot;http://www.w3.org/2005/Atom&quot;;
    String getLink();
    void setLink(String href);
}

public class AtomNSModuleImpl extends ModuleImpl implements AtomNSModule {
    private String link;

    public AtomNSModuleImpl() {
        super(AtomNSModule.class, URI);
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Class getInterface() {
        return AtomNSModule.class;
    }

    public void copyFrom(Object obj) {
        AtomNSModule module = (AtomNSModule) obj;
        module.setLink(this.link);
    }
}
</pre>
<p>The code is not to hard to understand. Concentrate on the <strong>copyForm</strong> method. Here we put the data from our model into the object that is used by the framework to extract data. This is why you need the interface as well.</p>
<h3>Generator</h3>
<p>Here we add the elements to the generated xml. JDom is used to generate xml from an object tree of Elements. The following code block shows the complete generator.</p>
<pre class="brush: java; title: ; notranslate">
public class AtomNSModuleGenerator implements ModuleGenerator {
    private static final Namespace ATOM_NS = Namespace.getNamespace(&quot;atom&quot;, AtomNSModule.URI);

    private static final Set NAMESPACES;

    static {
        Set nss = new HashSet();
        nss.add(ATOM_NS);
        NAMESPACES = Collections.unmodifiableSet(nss);
    }

    public String getNamespaceUri() {
        return AtomNSModule.URI;
    }

    public Set getNamespaces() {
        return NAMESPACES;
    }

    public void generate(Module module, Element element) {
        AtomNSModule atomNSModule = (AtomNSModule) module;
        Element root = element;
        while (root.getParent() != null &amp;&amp; root.getParent() instanceof Element) {
            root = (Element) element.getParent();
        }
        root.addNamespaceDeclaration(ATOM_NS);

        Element atomLink = new Element(&quot;link&quot;, ATOM_NS);
        atomLink.setAttribute(&quot;href&quot;, atomNSModule.getLink());
        atomLink.setAttribute(&quot;rel&quot;, &quot;self&quot;);
        atomLink.setAttribute(&quot;type&quot;, &quot;application/rss+xml&quot;);

        element.addContent(0, atomLink);
    }
}
</pre>
<p>Focus on the <strong>generate</strong> method. In this method we add the namespace declaration to the root element and we create a new element that is added to the provided element. The provided element is the channel element. The result of the feed now looks like this.</p>
<pre>
&lt;rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"&gt;
  &lt;channel&gt;
    &lt;atom:link href="http://localhost:8080/rss/nieuws.rss" rel="self" type="application/rss+xml" /&gt;
	...
  &lt;/channel&gt;
&lt;/rss&gt;
</pre>
<h3>Configuration</h3>
<p>The last part is telling Rome to use the new component. This is done by putting a rome.properties file on the classpath with the following contents.</p>
<pre>
rss_2.0.feed.ModuleGenerator.classes=nl.gridshore.rss.romeextension.AtomNSModuleGenerator
</pre>
<p>It is important to specify the rss_2.0 because multiple generators are available and this is the one that is used.</p>
<h2>Encoding &#8211; response type</h2>
<p>Another problem I had had to do with encoding. You can set the type of encoding in the generated xml, but the response of the spring controller/view combination does not follow this. Therefore you have to set the encoding of the response object explicitly. In the view component I added the following lines of code:</p>
<pre class="brush: java; title: ; notranslate">
    @Override
    protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
        super.prepareResponse(request, response);
        response.setCharacterEncoding(CharacterEncodingConstants.UTF8);
    }
</pre>
<p>I hope this can help others to create better feeds as well, suggestions for improvements are of course welcome.</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2010%2F02%2F16%2Fcreating-a-w3c-validated-rss-feed-using-rome-and-spring-3%2F&amp;title=Creating%20a%20w3c%20validated%20rss%20feed%20using%20Rome%20and%20spring%203&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2010/02/16/creating-a-w3c-validated-rss-feed-using-rome-and-spring-3/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>When good guys start looking like bullies&#8230;.</title>
		<link>http://www.gridshore.nl/2008/09/23/when-good-guys-start-looking-like-bullies/</link>
		<comments>http://www.gridshore.nl/2008/09/23/when-good-guys-start-looking-like-bullies/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 21:42:40 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[FOSS]]></category>
		<category><![CDATA[licensing]]></category>
		<category><![CDATA[OSS]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[SpringSource]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/2008/09/23/when-good-guys-start-looking-like-bullies/</guid>
		<description><![CDATA[An opinion on SpringSource&#8217;s new maintenance policy  Introduction <p>On September 17, 2008 SpringSource announced a new Maintenance Policy regarding its software offerings, particularly its primary product (the Spring Framework) both in its open source and Enterprise editions. The new policy reads as follows:</p> <p>Customers who are using SpringSource Enterprise, available under a subscription, will [...]]]></description>
			<content:encoded><![CDATA[<h2>An opinion on SpringSource&#8217;s new maintenance policy </h2>
<h3>Introduction</h3>
<p>On September 17, 2008 <a href="http://www.springsource.com">SpringSource</a> announced a new <a href="http://www.springsource.com/products/enterprise/maintenancepolicy">Maintenance Policy</a> regarding its software offerings, particularly its primary product (the <a href="http://www.springframework.org">Spring Framework</a>) both in its open source and Enterprise editions. The new policy reads as follows:</p>
<blockquote><p>Customers who are using SpringSource Enterprise, available under a subscription, will receive maintenance releases for three years from the general availability of a major new version. These customers receive ongoing, rapid patches as well as regular maintenance releases to address bugs, security vulnerabilities and usability issues, making SpringSource Enterprise the best option for production systems.</p>
<p><span id="more-242"></span></p>
<p>After a new major version of Spring is released, community maintenance updates will be issued for three months to address initial stability issues. Subsequent maintenance releases will be available to SpringSource Enterprise customers. Bug fixes will be folded into the open source development trunk and will be made available in the next major community release of the software.</p></blockquote>
<h3>What does the new policy mean?</h3>
<p dir="ltr">So what does this new policy mean for developers and projects that rely on the Spring Framework? Well, it depends a lot on who you are and what your circumstances are.</p>
<p dir="ltr">For starters, it is important to note that this new policy does <em>not</em> mean that Spring Framework has suddenly ceased being an open source project. It is still open source software, published under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License (version 2.0)</a> and available for download by anybody (from the <a href="http://springframework.cvs.sourceforge.net/springframework/">CVS repository</a>). It will also remain possible to submit patches, build your own release, et cetera.</p>
<p dir="ltr">The changes brought about by the new policy, in practice, will only affect how prepackaged spring jars will be released by SpringSource. SpringSource will continue to release prepackaged jars of new major and minor versions (spring-project-x.0.0 and spring-project-x.y.0). In addition, SpringSource will provide prepackaged jars with bugfixes on those released versions for three months after the version is released. After that the new bugfix jars will only be available to people with an Enterprise license. Other people will have to check out bugfixes from the trunk and rebuild themselves, patch their existing libraries with patches from the trunk as needed or wait for the next major/minor release.</p>
<p dir="ltr">For most people the new situation will not make that much difference &#8212; most open source developers can simply rebuild or wait. The biggest pinch will probably be felt by commercial projects with restrictive corporate policies on upgrading libraries. Those projects will have to push their business customers to purchase Enterprise licenses if they need access to frequent bugfixes releases.</p>
<h3>Is this fair?</h3>
<p dir="ltr">The next major question is: is this new policy in any way fair? Is it not a gross violation of open source principles? Should open source software not be free?</p>
<p dir="ltr">Well, the first thing to remember here is the old adage about free software as formulated in the <a href="http://www.gnu.org/">GNU&#8217;s</a> <a href="http://www.gnu.org/philosophy/free-sw.html">Free Software Definition</a>: the &#8220;free&#8221; in free software means free as in <em>free speech</em>, not free as in free beer. There is nothing in the open source philosophy, nor in practically any open source license that forbids charging money for software &#8212; as long as you make the source code freely available. Open source or free software is not a contradiction of any business model except the &#8220;trade secret&#8221; model. Making money off of software, related services (like prepackaging) or licensing models is absolutely allowed. If it wasn&#8217;t, open source would not have caught on. Can you imagine a Linux distribution like <a href="http://www.opensuse.org/">OpenSuSE</a> evolving in a situation where you cannot charge for the software at all?</p>
<p dir="ltr">Another point that SpringSource raises (and it is a very valid one) is that they put a lot of effort into those bugfixes. The first three months of fixes always address stability issues (and those will remain available to the general public) but after that the problems noticed pretty much always move into the area of non-critical but still annoying issues. That&#8217;s not exclusive to Spring by the way, pretty much every project has a similar pattern with a different timeframe. Most projects (open source and commercial) address these issues by incorporating fixes into the new development version, with a patch of some sort for issues that are particularly bad. SpringSource has always done that as well, but has also &#8220;backported&#8221; bugfixes into intermediate bugfix releases while having a relatively wide spacing between major releases. That policy is taking more and more effort as more people use the framework (so SpringSource feels) and is also taking up a lot of resources that SpringSource wants to commit to other parts of their portfolio. So they have decided to stop the &#8220;backporting&#8221; for free and become a more &#8220;normal&#8221; OSS project with respect to bugfixes for the OSS community.</p>
<p dir="ltr">That aside, all open source licensing (like any other licensing) is a matter of intellectual property rights (specifically copyright). So even if the license on the Spring Framework had made it mandatorily free, SpringSource could at any time have chosen a different license and we&#8217;d be in the same position.</p>
<p dir="ltr">So, all in all, no, it is <strong>not</strong> unfair.</p>
<h3>So why does it <em>feel</em> wrong?</h3>
<p dir="ltr">And that&#8217;s just the problem here, isn&#8217;t it? Even though SpringSource is <em>technically</em> in the right at every turn, the new policy just <em>feels</em> wrong. Why? Probably because it feels like SpringSource is taking something away from the community. It feels like we had something for free and now we have to pay for it (even if we didn&#8217;t make frequent use of the free thingy when it was free and even though we can still get the free thingy with a little more effort). In other words, SpringSource has created itself a little public relations problem.</p>
<p dir="ltr">So is that bad? Possibly. Not in the sense that it will kill the Framework (Spring has a lot of traction and I have yet to see a framework that combines the three staples of Spring as well). But it will make people start looking at SpringSource in a different light and possibly look a little harder at alternatives to the SpringSource offerings. After all, SpringSource is no longer the Unlimited Fount of All That Is Good &#8212; SpringSource now has a nasty, commercial side that wants to make money and is not afraid to shortchange the community to do it (or at least, so it will now seem to quite a few people).</p>
<p dir="ltr">Where I think SpringSource went wrong with their new policy is in the area of presenting themselves as a trustworthy group of people. Let me explain: the open source community has a large problem with confidence when it comes to acceptance by traditionally commercial software developers. Commercial softwarehouses worked for decades in the classical model of secrecy, escrow, copyright and &#8220;what belongs to me is mine and keep your filthy paws off&#8221;. The idea that you gain more through openness and contribution, despite centuries worth of experience in science, is relatively new to software development outside the universities. The Internet, primary vehicle of OSS development, only started becoming popular in 1995, thirteen years ago. And OSS as a term is even younger. Most commercial entities are still getting to grips with the notion. A large part of such a change is trust and trust like that takes time to build. And when big players like SpringSource start doing things like this new licensing policy, it doesn&#8217;t help the trust along at all.</p>
<p dir="ltr">Let me give you another example of where a well-intentioned project went off the track due to mishandling the way people viewed it: the <a href="http://www.gnu.org/copyleft/gpl.html">GNU General Public License</a>. The GPL was a mainstay of the open source community for years in version 1.0 and 2.0. Pretty much every OSS and FOSS project adopted the GPL because it embodied the essence of OSS. Then came GPLv3 (around the time that OSS started getting general attention) and that&#8230; Well, &#8220;flopped&#8221; is probably too strong. But it was not an enormous success. So why is this? Is it because of digital rights management or because GPLv3 makes it mandatory to give away all your software for free and kill Bill Gates? I personally don&#8217;t think so. I personally think that GPLv3&#8242;s biggest problem is one <a href="http://en.wikipedia.org/wiki/Richard_Stallman">Richard M. Stallman</a>, who has a nasty habit of going around and claiming that everything that has ever remotely been near any open source software must now be open source (even if it plainly isn&#8217;t) and screaming everything that commercial software developers really don&#8217;t want to hear. Public presentation and public relations, in other words.</p>
<h3>What to make of this&#8230; except a bonfire?</h3>
<p dir="ltr">So, to recap: SpringSource is technically in the right, but I think they may have lightly shot themselves in the foot with this one. What kind of damage are they looking at? For starters, I think they may have made the battle for corporate acceptance a little more uphill than it was. Worse, I think they&#8217;ve lost themselves a lot of community respect &#8212; not by actually being bad, but by being ham-handed in their approach. At worst they may have opened the door for some community members to start looking elsewhere.</p>
<p dir="ltr">Ironically, of course, that presents an opportunity of sorts for SpringSource as well. Over the past year and a half Spring has become sort of a de facto standard for enterprise Java development. And being a standard can be dangerous, since it can lull you to sleep. If SpringSource has now accidentally created a chance for its rivals, that means SpringSource may soon have to compete on pure quality again. And what better way to earn (back) your OSS credentials than that?</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2008%2F09%2F23%2Fwhen-good-guys-start-looking-like-bullies%2F&amp;title=When%20good%20guys%20start%20looking%20like%20bullies....&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2008/09/23/when-good-guys-start-looking-like-bullies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

