<?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>Grails Rocks &#187; Groovy and Grails</title>
	<atom:link href="http://www.anyware.co.uk/2005/category/groovy-and-grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anyware.co.uk/2005</link>
	<description>Grails, Apple, usability and world stuff</description>
	<lastBuildDate>Fri, 27 Jan 2012 13:52:31 +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>Getting started with Arduino / Rainbowduino</title>
		<link>http://www.anyware.co.uk/2005/2012/01/17/getting-started-with-arduino-rainbowduino/</link>
		<comments>http://www.anyware.co.uk/2005/2012/01/17/getting-started-with-arduino-rainbowduino/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 10:30:00 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Programming links]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=1135</guid>
		<description><![CDATA[I recently bought some Arduino microcontroller/prototyping boards and components. The plan is to rediscover my childhood interest in electronics and familiarise myself with these tools so that maybe in future I can help teach some of this at schools. This is because I believe that its the practical stuff that can integrate with other school [...]]]></description>
			<content:encoded><![CDATA[<p>I recently bought some <a href="http://arduino.cc">Arduino</a> microcontroller/prototyping boards and components. The plan is to rediscover my childhood interest in electronics and familiarise myself with these tools so that maybe in future I can help teach some of this at schools. This is because I believe that its the practical stuff that can integrate with other school lessons that can give young people the best start at programming.</p>
<p>The great thing is that actually, with only a little knowledge, doing this stuff is really easy. The Arduino board designs and software are open source, which is a good thing. However as usual this means the docs can be problematic.</p>
<p><span id="more-1135"></span>There&#8217;s a confusing array of Arduino hardware out there. The boards have the ATmega micro controller chips on them that you program, and lots of digital and analog input/outputs so you can control and measure things. There are boards with Ethernet, Wi-fi, USB and even LED matrix drivers.</p>
<h2>Basic Arduino (UNO)</h2>
<p>Using this Arduino which has just a USB port and the I/O connectors, plus a bread board and 4-digit 7-segment LED display from an <a href="http://earthshineelectronics.com/">Earthshine</a> starter kit, I managed after digging out the spec sheet on the display, to create a counter from 0001 to 9999. This sounds trivial, but it has a few challenges:</p>
<ol>
<li>The LED display is basically 4 x 8 LEDs (7 segment + decimal point)</li>
<li>There are only 12 pins on it. Huh?</li>
<li>If you don&#8217;t send power to one of the LEDs, it goes off.</li>
</ol>
<p>The spec sheet showed that 4 pins control which of the 4 digits you are addressing &#8211; set them LOW (common cathode) to enable the corresponding digit.</p>
<p>Then you set some of the other 8 pins HIGH to represent the digit or symbol you want to show.</p>
<p>So to set all 4 digits without any shift register chips or other smarts to &#8220;remember&#8221; the state of segments for each digit, you have program the Arduino to continually loop through the 4 selector pins, set the segment pins HIGH, wait a few milliseconds, then set the selector pin for that digit HIGH (to turn it off), and then repeat the same process for the next digit&#8217;s selector pin. What happens is that each digit is illuminated for say 5ms and then it moves to the next one.</p>
<p>They are continually going on and off, but because you constantly loop at high speed over them, it appears to be a continuous display.</p>
<h2>Much more fun &#8211; the Rainbowduino</h2>
<p>The <a href="http://www.seeedstudio.com/wiki/Rainbowduino_v3.0">Rainbowduino</a> is a self contained Arduino board and the latest v3.0 one has built in mini-USB. So its just like an Arduino UNO but has a bunch of other controllers to drive an 8&#215;8 RGB LED matrix with constant current (which I think means when lots of LEDs are on, they stay the same brightness). CAUTION: the LEDs are also seemingly as bright as the sun and staring at them is likely to leave you with a disturbing 8&#215;8 grid of dots burnt into your retina.</p>
<p>So I bought the Rainbowduino plus an 8&#215;8 RGB LED Matrix. The Rainbowduino is built to have the matrix directly plugged into the top of it, which is very handy.</p>
<div id="attachment_1140" class="wp-caption aligncenter" style="width: 302px"><a href="http://www.anyware.co.uk/2005/wp-content/uploads/2012/01/rainbowduino-off.jpg?9d7bd4"><img class="size-medium wp-image-1140 " title="Rainbowduino board plus LED matrix mounted on top" src="http://www.anyware.co.uk/2005/wp-content/uploads/2012/01/rainbowduino-off-e1326794159242-292x300.jpg?9d7bd4" alt="" width="292" height="300" /></a><p class="wp-caption-text">Rainbowduino board plus LED matrix mounted on top</p></div>
<p>There are some tricks to getting this working however:</p>
<ol>
<li>Unlike the Arduino UNO, my Mac would not recognise the Rainbowduino when plugged in to USB. I had to install the <a href="http://www.ftdichip.com/Drivers/VCP.htm">&#8220;FTDI&#8221; drivers from here</a>. Thanks to @andydavies for the tip there</li>
<li>The docs on Rainbowduino are sketchy, and seem to be mainly targeted at an LED 3D cube rather than the matrix, and using the Processing language (which is not what the Arduino IDE uses).</li>
<li>It turns our there are several options for controlling this matrix. You can flash your own Arduino &#8220;sketches&#8221; (programs) directly onto the Rainbowduino, or you can use the I2C serial protocol to drive the Rainbowduino from another Arduino &#8211; I am guessing this means the Rainbowduino ATMega has a custom boot loader that supports serial comms to drive the display. I may be wrong.</li>
</ol>
<p>To solve (2) I had to find the driver code that would work with the Arduino IDE. The <a href="http://www.seeedstudio.com/wiki/Rainbowduino_v3.0#Resources">v3 Rainbowduino driver is here</a>. If you save that to ~/Documents/Arduino/Libraries/Rainbowv3 or similar (for Mac OS X at least that is where it is) and restart Arduino IDE, you will see you can now insert it into your Arduino sketch using the &#8220;Import Library&#8230;&#8221; menu option.</p>
<p>You will then find it does not compile, complaining it cannot find &#8220;WProgram.h&#8221;. This is I think because the library is built to run with &#8220;Wiring&#8221; programming language. To fix this you need to edit the Rainbowduino.h file from the driver that you saved, and change the #include &lt;WProgram.h&gt; to &lt;Arduino.h&gt;.</p>
<p>After that you can use the driver, which is incredibly simple:</p>
<ol>
<li>in setup() call Rb.init()</li>
<li>in loop() call some Rb methods such as: Rb.blankDisplay() and Rb.setPixelXY(x, y, rgbcolour)</li>
</ol>
<div id="attachment_1141" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.anyware.co.uk/2005/wp-content/uploads/2012/01/rainbowduino-spectrum.jpg?9d7bd4"><img class="size-medium wp-image-1141 " title="Rainbowduino with diagonal spectrum" src="http://www.anyware.co.uk/2005/wp-content/uploads/2012/01/rainbowduino-spectrum-e1326794503914-300x277.jpg?9d7bd4" alt="" width="300" height="277" /></a><p class="wp-caption-text">Rainbowduino with diagonal spectrum</p></div>
<p>Why so dark? LEDs are too bright to photograph without a filter. I put lots of tissue paper over them.</p>
<p>So here&#8217;s an example to apply the colour spectrum diagonally to the entire grid in action:</p>
<script src="https://gist.github.com/1626042.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1626042">Gist</a>.</p></noscript>
<p>Note that if you find your reds are not working you probably have a C typing problem, and need to explicitly cast your values to uint32_t to avoid the RR of your RRGGBB being lost to 16bit accuracy.</p>
<p>And seeing as most of my life is spent in <a href="http://grails.org">Grails</a>&#8230; this wouldn&#8217;t be complete without a video of the Grails cup shining in all its 8&#215;8 glory.</p>
<div style='text-align:center;'>
<object type="application/x-shockwave-flash" width="400" height="300" data="http://www.vimeo.com/moogaloop.swf?clip_id=35189087&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA">
	<param name="quality" value="best" />
	<param name="allowfullscreen" value="true" />
	<param name="scale" value="showAll" />
	<param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=35189087&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA" />
	<param name="wmode" value="opaque" />
</object>
</div>
<p>And here&#8217;s the code, if you want some Grails glory yourself:</p>
<script src="https://gist.github.com/1626085.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1626085">Gist</a>.</p></noscript>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2012/01/17/getting-started-with-arduino-rainbowduino/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Resources 1.1(.1) plugin released</title>
		<link>http://www.anyware.co.uk/2005/2011/10/21/resources-1-1-1-plugin-released/</link>
		<comments>http://www.anyware.co.uk/2005/2011/10/21/resources-1-1-1-plugin-released/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 00:31:07 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[plugin release]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=1094</guid>
		<description><![CDATA[After a frantic 12 hours or so of coding and debugging, I managed to release version 1.1.1 of the Resources plugin for Grails today. It was meant to be 1.1 but there was a bug that needed fixing immediately after releasing so 1.1.1 is where we&#8217;re at. This version is tested with Grails 1.3.7 and [...]]]></description>
			<content:encoded><![CDATA[<p>After a frantic 12 hours or so of coding and debugging, I managed to release version 1.1.1 of the <a href="http://grails.org/plugin/resources">Resources plugin</a> for <a href="http://grails.org">Grails</a> today.</p>
<p>It was meant to be 1.1 but there was a bug that needed fixing immediately after releasing so 1.1.1 is where we&#8217;re at. This version is tested with Grails 1.3.7 and Grails 2.0 RC1 which is set to drop this weekend.</p>
<p>What&#8217;s new in this release of Resources?</p>
<p>Well aside from some useful bug fixes there&#8217;s a few interesting changes:</p>
<ol>
<li>If you forget to include r:layoutResources tags, you will get an error in the console. The error may or may not display in the browser currently depending on whether you used site mesh layouts or not. We may be able to improve this in future. This means that people who wondered where their JS code was disappearing to after installing Resources into a 2.0 app will now get an error telling them that they need to add r:layoutResources.</li>
<li>There is now support for laying out arbitrary dispositions by passing a &#8220;disposition&#8221; to the r:layoutResources tag. This means you can position sets of resources wherever you like in the output, using multiple dispositions. Currently this is only relevant for JavaScript but in future we should be able to support images so that you can e.g. have a bunch of &#8220;preloaded&#8221; images that are hidden in the page somewhere.</li>
<li>The integration with Grails 2.0 has been &#8220;finalized&#8221; now. The ResourceService has changed to a &#8220;grailsResourceProcessor&#8221; bean, and all g:javascript invocations result in called to r:require or r:script as necessary. The default layouts in 2.0 now use layoutResources and a default &#8220;application&#8221; module exists in new projects.</li>
</ol>
<p>There are still some outstanding important bugs that I will endeavour to fix soon, as well as improving the error reporting on all fronts.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/10/21/resources-1-1-1-plugin-released/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Optimising your Application with Grails Resources Plugin</title>
		<link>http://www.anyware.co.uk/2005/2011/09/12/optimising-your-application-with-grails-resources-plugin/</link>
		<comments>http://www.anyware.co.uk/2005/2011/09/12/optimising-your-application-with-grails-resources-plugin/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 20:16:04 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=906</guid>
		<description><![CDATA[It&#8217;s been nearly a year since we first announced the Resources framework for Grails. It&#8217;s about time we looked at some of the more advanced features that it provides so that you can make the most of this very powerful plugin. The start of this article will go over some of the basics again, as [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been nearly a year since we first announced the <a href="http://grails.org/plugin/resources">Resources framework</a> for <a href="http://grails.org">Grails</a>. It&#8217;s about time we looked at some of the more advanced features that it provides so that you can make the most of this very powerful plugin.</p>
<p><span id="more-906"></span>The start of this article will go over some of the basics again, as they are a prerequisite for understanding the more advance topics covered later in the piece.</p>
<h2>Recap: What is Resources framework for?</h2>
<p style="text-align: center;"><a class="lightbox" href="http://www.anyware.co.uk/2005/wp-content/uploads/2011/09/Resources-article-diagram.png?9d7bd4"><img class="size-full wp-image-968 aligncenter" title="Resources article diagram" src="http://www.anyware.co.uk/2005/wp-content/uploads/2011/09/Resources-article-diagram.png?9d7bd4" alt="" width="464" height="216" /></a></p>
<p>It solves the growing problems around application and plugin static resources. Your CSS, JS and other files have dependencies on each other, and must load in the correct order. You also want to optimize these so that your application loads quickly and caches well for your users.</p>
<p>It provides a way to declare your resources, their dependencies, and a processing chain to modify the resources and response headers to perform all manner of tasks such as bundling multiple resource files into one file, zipping the response, applying long-term client-side caching, or compiling Less or Sass files to regular CSS on the fly.</p>
<h2>Where do I start?</h2>
<p>First you install the &#8220;<a href="http://grails.org/plugin/resources">resources</a>&#8221; plugin. To actually see some magic happening you probably want to also install the <a href="http://grails.org/plugin/cached-resources">cached-resources</a> and/or <a href="http://grails.org/plugin/zipped-resources">zipped-resources</a> plugins also, but this is not a requirement.</p>
<p>Then you declare some resources in your app, or pull some in from a plugin that supports Resources. Let&#8217;s start by declaring a resource that exists in your application, say main.css. To do this you create a file in <strong>grails-app/conf/</strong> with a name that ends in &#8220;<strong>Resources.groovy</strong>&#8220;. So we might create <strong>AppResources.groovy</strong> and use the DSL to declare the resource:</p>
<script src="https://gist.github.com/1211514.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211514">Gist</a>.</p></noscript>
<p>This is a module declaration. For full details see the <a href="http://grails-plugins.github.com/grails-resources">Resources documentation</a>, but in essence this declares a resource module called &#8220;common&#8221; and it has one CSS resource in it, the file supplied by your application in <strong>web-app/css/main.css</strong>, and on JS file.</p>
<p>So far, so good. Now you need to pull this resource into a page. To do this requires using some Resources tags.</p>
<h2>Using resources in your pages</h2>
<p>There are two main tags involved in using resources, with some others that provide extra features. The main two are <strong>r:require</strong> and <strong>r:layoutResources</strong>.</p>
<p>The <strong>r:require</strong> tag tells the framework which resource modules the current GSP requires. The framework will make sure that all the resources declared in those modules are pulled in in the correct order.</p>
<p>To achieve this, you need to include <strong>r:layoutResources</strong> tags in your &lt;head&gt; and &lt;body&gt; sections. Typically you do this in your SiteMesh layout. You use two calls because one will render the resources that you need to link to in the &lt;head&gt; section of your page (all your CSS, and some of your JavaScript, maybe some favico or iPhone icons). The other goes at the end of the &lt;body&gt; of your page, to render any &#8220;deferred&#8221; resources, which is usually just your JavaScript code.</p>
<p>So here&#8217;s an example of a Grails Sitemesh layout using these tags:</p>
<script src="https://gist.github.com/1211550.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211550">Gist</a>.</p></noscript>
<p>Here you see that you still use the normal Grails sitemesh tags to layout the head and body. These provide the route for you GSP page to actually affect the output, including the output of the Resources plugin&#8217;s <strong>r:layoutResources</strong> tags. That&#8217;s because your GSP will call <strong>r:require</strong> and <strong>r:script</strong> tags something like this:</p>
<script src="https://gist.github.com/1211555.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211555">Gist</a>.</p></noscript>
<p>This GSP is set to use a Sitemesh layout like the example one given earlier. It uses the <strong>r:require</strong> tag to say that this page needs the &#8220;app&#8221; resource module. Using this information, the <strong>r:layoutResources</strong> tags will render links to all the CSS, JS and other resources of that &#8220;common&#8221; module, in the correct places.</p>
<p>This means that some may end up in head, and some may end up in body. Remember that we invoked <strong>r:layoutResources</strong> twice, once in each location? Well, that&#8217;s why.</p>
<h2>What is this &#8220;disposition&#8221; stuff?</h2>
<p>The disposition of a resource tells the framework which part of the page it belongs &#8211; its a form of arbitrary grouping. The default dispositions supported are &#8220;head&#8221; and &#8220;defer&#8221; but in fact resources can define any disposition they like &#8211; and this can be useful for declaring resources that are not rendered by the normal layoutResources calls (more about this later).</p>
<p>The framework applies a default disposition based on the type of the resource &#8211; so CSS always defaults to &#8220;head&#8221; but JavaScript always defaults to &#8220;defer&#8221;, so that it runs only when the rest of the page has loaded. Images and icons are also supported to represent favicon and iPhone icons, which render in &#8220;head&#8221;.</p>
<p>Resource modules usually contain multiple resources and the main reason to specify a disposition when declaring a resource is to force JavaScript to load in the &lt;head&gt; section of the page. To achieve that you just update your resource declaration to include a &#8220;disposition&#8221; attribute:</p>
<script src="https://gist.github.com/1211561.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211561">Gist</a>.</p></noscript>
<p>Now your page will load with jQuery in the head section, with no code changes to your layout or GSP!</p>
<h2>Dependencies</h2>
<p>So far, so good but how do we control the order of resources when they are pulled into the page? How can I make sure that my application JS or CSS code always loads after the JS and CSS from a plugin that I&#8217;m using?</p>
<p>This is where Resources dependencies come in. There are two simple rules</p>
<ol>
<li>Resources from a given module are loaded in the order they are defined in the module</li>
<li>Resources from modules are loaded in module dependency order</li>
</ol>
<p>The first rule ensures that when you declare resources you can intuitively see which CSS or JS loads before another.</p>
<p>The second rule uses the &#8220;dependsOn&#8221; method in module declarations to list the names of other resource modules on which the module depends. Those will always be loaded before the module that depends on them. Here&#8217;s an example declaration that alters our specimen module to depend on the jQuery plugin&#8217;s query module instead, and jQuery UI:</p>
<script src="https://gist.github.com/1211581.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211581">Gist</a>.</p></noscript>
<p>Notice that we have specified both jQuery UI and jQuery in the dependsOn. We don&#8217;t need to. However notice that they are also declared in the &#8220;wrong&#8221; order. This is to illustrate that the order you use here is not important &#8211; the &#8216;jquery-ui&#8217; module declared by the Grails jQueryUI plugin depends on on &#8216;jquery&#8217; (provided by the Grails jQuery plugin) as well &#8211; so it will always make sure that is loaded first.</p>
<p>As your application does not define those two modules, you would need to install the Grails jQuery-UI plugin &#8211; or define them yourself. Including the jQuery-UI plugin automatically pulls in the jQuery plugin for you, because of Grails&#8217; transitive plugin dependencies. TheResources framework was designed to work hand in hand with the other Grails dependency mechanisms.</p>
<p>So now you have done this, you still have no changes to make to your GSP! It still just requires the &#8220;common&#8221; module. Notice how this has decoupled your GSPs and layouts from the details of the resources that they need. This is an important feature that gives you great scope to modularise your resources, and even have page-specific modules if you like that contain nothing other than dependencies themselves:</p>
<script src="https://gist.github.com/1211564.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211564">Gist</a>.</p></noscript>
<p>With this, an edit.gsp could just <strong>r:require</strong> the &#8220;editPage&#8221; module and never care about the explicit details of what is required.</p>
<h2>Dealing with your ugly inline Javascript</h2>
<p>You already know that inline Javascript is pretty horrendous, but some things are just plain shameful and yet we can&#8217;t help ourselves. Frequently you want to write a Grails taglib that simplifies using a JS library and to do that it must render some code inline when the tag is invoked.</p>
<p>With Resources you can trivially throw this JS code around to wherever you like. You can have the code render in &lt;head&gt; or at the end of the body, and it will automatically do so after loading the resources that the page depends on. To do this you just use the &lt;<strong>r:script</strong>&gt; tag instead of a normal &lt;script&gt; or &lt;g:javascript&gt; tag:</p>
<script src="https://gist.github.com/1211607.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211607">Gist</a>.</p></noscript>
<p>In this example the script code specified in &lt;head&gt; <em>does not run immediately</em>. The <strong>r:script</strong> tag also uses &#8220;disposition&#8221; and the default is &#8220;defer&#8221; so you will find that the alert only pops up once the rest of the page has loaded, and the HTML source will show the &lt;script&gt; tag at the end of the body, after all other deferred JS code has been pulled in.</p>
<p>All such fragments in the same disposition are concatenated together, so you don&#8217;t get a long list of &lt;script&gt; tags.</p>
<p>To force a fragment of inline JS to execute in &lt;head&gt;, just add <strong>disposition=&#8221;head&#8221;</strong> to the <strong>r:script</strong> tag invocation.</p>
<p>To write your own taglibs that generate code in this way, you just output the result of calling the <strong>r.script(attrs, Closure)</strong> tag from your tag.</p>
<h2>Linking to images</h2>
<p>Images often need to be optimised or set for long-term caching. However you don&#8217;t normally need to declare them in modules. To render images correctly you should use the <strong>&lt;r:img&gt;</strong> tag anywhere in your pages as you would and HTML &lt;img&gt; tag. All you do differently is supply a uri or dir/file attribute to tell it where to load from. Other attributes are passed through:</p>
<script src="https://gist.github.com/1211623.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211623">Gist</a>.</p></noscript>
<p>That&#8217;s all there is to it.</p>
<p>There are however some extra tricks you can pull. Any tag attributes specified in a module resource declaration are honoured when rendering the link. As such this means that you can specify your width &amp; height values if you declare your image &#8211; and never have to specify them when rendering links:</p>
<script src="https://gist.github.com/1211626.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211626">Gist</a>.</p></noscript>
<p>Now any <strong>r:img</strong> links to that logo will automatically include those attributes in the output. Notice that we had to include the disposition &#8220;image&#8221; which is not a predefined disposition &#8211; it is just anything other than &#8220;head&#8221; or &#8220;defer&#8221; to prevent the image being rendered by the <strong>r:layoutResources</strong> tags.</p>
<h2>Bundling: combining files to reduce the number of requests</h2>
<p>Page load time is a combination of many factors. Those that you can control from your application itself include how many requests are needed to load your page and whether you load resource before or after your HTML content.</p>
<p>To reduce the number of requests made, you need to combine one or more resources into a single file. This is called bundling and what you do is tell Resource which files can be bundled together. This is usually dependent on how your application uses the code, and inevitably requires some compromise.</p>
<p>For example you <em>could</em> bundle all your resources into one file across your entire app &#8211; resulting in just one CSS and one JS file for the entire site. With aggressive browser caching this could yield good results, but if you have a few hundred KB of code in these files it could mean it takes a long time for people to see your site the first time they visit or empty their cache.</p>
<p>To specify the bundle that a file belongs to, just add the &#8220;bundle&#8221; attribute to resource declarations. The Resources framework will not bundle together resources that are incompatible: different content types, or those with differing &#8220;attrs&#8221;. For example CSS with <strong>media=&#8217;print&#8217;</strong> will not be bundled with screen media.</p>
<p>It does however allow you to bundle resources <em>across module boundaries</em>. This is important as you will see shortly. It also automatically bundles using the module name by default, and maintains separate bundled files for head and defer dispositions automatically.</p>
<p>That is a lot to take in, but it basically means you don&#8217;t need to worry about anything.</p>
<p>Just set the bundle attribute:</p>
<script src="https://gist.github.com/1211629.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211629">Gist</a>.</p></noscript>
<p>Here the <strong>main.css</strong>, which previously might have auto-bundled into bundle &#8220;common&#8221; if there was more than one resource in the module, will now be bundled into bundle &#8220;core&#8221;.</p>
<p>The difference has no effect for now, but what you can do is override the module declarations that came from the jQuery plugins and make them go into the same bundle. To do this we use the &#8220;overrides&#8221; mechanism to change the properties of the resource modules supplied by the plugins. We can call the <strong>defaultBundle</strong> method to change the default used by them:</p>
<script src="https://gist.github.com/1211633.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211633">Gist</a>.</p></noscript>
<p>Now all the modules will throw their resources together, and you will find your page (again with zero GSP changes) is now pulling in just three files: bundle_core_head.js, bundle_core_head.css and bundle_core_defer.js</p>
<p><span class="Apple-style-span" style="font-size: 20px; font-weight: bold;">Changing how links to resources are rendered</span></p>
<p>Under the hood, the <strong>r:layoutResources</strong> tag uses the <strong>r:external</strong> tag to render links. The <strong>r:external</strong> tag is a smart tag that writes out the &#8220;right kind&#8221; of link for a resource. So for JS files it renders &lt;script src=&#8230;&gt; tags, for CSS it renders &lt;link href=&#8230;&gt;, and so on for favicons and a few other types.</p>
<p>The key thing is that you can pass any other attributes to <strong>r:external</strong> and they will pass through to the output. It so happens that <strong>r:layoutResources</strong> pass the &#8220;attrs&#8221; Map defined in your resource declaration when calling <strong>r:external</strong> for that resource. This means you can pass any extra attributes you like to the declaration and they make it to the output.</p>
<p>A typical usage for this is to specify media:&#8217;print&#8217; on a CSS resource, because the default for this is &#8220;screen, projection&#8221;:</p>
<script src="https://gist.github.com/1211639.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211639">Gist</a>.</p></noscript>
<p>Note that if you specify these extra properties, those files will not be bundled with others, as by definition this isn&#8217;t going to work out well for you.</p>
<h2>Wrapping the link in some other code. AKA MS IE workarounds</h2>
<p>The resource declaration can also supply some wrapper markup that is used to render the link, with the generated link being passed in as a string. You normally use this to wrap a resource such that it is commented out for all browsers except MS IE:</p>
<script src="https://gist.github.com/1211648.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211648">Gist</a>.</p></noscript>
<p>The wrapper Closure is passed the link text as an argument. You are then free to pre/post-fix it with anything you like.</p>
<h2>Linking to specific resources without using the dependency mechanisms</h2>
<p>On occasion you may need to link directly to a resource that is handled by the framework, without using <strong>r:layoutResources</strong> or <strong>r:require</strong>.</p>
<p>To do this you can use <strong>r:resource</strong> to get the URL of a resource, or <strong>r:external</strong> to create a link to it. You can use these to generate absolute links to those resources, for example in an HTML email.</p>
<p>These tags accept the regular uri, url or plugin/file/dir attributes. The <strong>r:external</strong> tag also supports a &#8220;type&#8221; attribute to give it a clue what kind of link to render if it is not obvious from the file extension.</p>
<h2>Overriding resources from plugins</h2>
<p>Plugins that expose modules normally do the right thing, but sometimes you might need to supply different content for a given file, or change some of the tag attributes or the bundle used. To do this you use the overrides mechanism shown earlier in the <strong>defaultBundle</strong> example.</p>
<p>The &#8220;overrides&#8221; closure lets you specify new values for some of the module defaults for a given module, but also lets you tweak individual resources if they can be addressed uniquely. Developers must add an &#8220;id&#8221; attribute to the resource when it is declared for this to work:</p>
<script src="https://gist.github.com/1211650.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211650">Gist</a>.</p></noscript>
<p>This changes the bundle on just the resources with id &#8220;js&#8221; in the original jquery module. There might be one or more resources with that id in the module. Resources in other modules are unaffected.</p>
<p>For full details of what you can override, please see the <a href="http://grails-plugins.github.com/grails-resources">Resources plugin documentation</a>.</p>
<h2>Using r:require in GSP fragments</h2>
<p>Using g:render to render GSP templates to build up your pages is a powerful technique. What you may not have realised is that you can completely modularise these templates now because the enclosing GSP no longer needs to know what libraries they need.</p>
<p>Yes, you can call <strong>r:require</strong> at any time in GSP rendering &#8211; including inside nested GSP templates &#8211; and the dependencies of your code will be stored along with your request and the resources all tallied up when the <strong>r:layoutResources</strong> called in your Sitemesh layout execute.</p>
<p>The only pre-requisite is that your <strong>r:require</strong> must be invoked before the corresponding <strong>r:layoutResources</strong> that you&#8217;d like to use to render it.</p>
<p>Picture how easy it is now to create portlet-like user interfaces where only the resources needed by UI elements displayed to the user will be loaded, and when they are they will be optimised. The portlets they have not installed or disabled will not load their resources if you do not render their GSP template.</p>
<h2>Dynamic requirements</h2>
<p>It is important to realise that the <strong>r:require</strong> tag can support normal GSP EL values in the modules list attribute. This brings great power because you can change the code pulled in by a page at runtime. For example you can make yourself a trivial per-user theme-switching system by storing the name of the theme they want to use in the session, and using a tag like this:</p>
<script src="https://gist.github.com/1211652.js"></script><noscript><p>View the code on <a href="https://gist.github.com/1211652">Gist</a>.</p></noscript>
<p>This will locate the theme module and all its dependencies with no further coding.</p>
<p>You could even switch the entire JS library and supporting code used by your plugin&#8217;s UI so that, for example, the user could choose between jQuery UI or YUI, or their own implementation.</p>
<h2>Including common resources in your layout</h2>
<p>Most likely you have some resources that are used in all pages using your Sitemesh layout. This is no problem because you can call <strong>r:require</strong> inside your Sitemesh layout, with the same proviso as &#8220;Using <strong>r:require</strong> in GSP fragments&#8221; &#8211; they must be invoked before the <strong>r:layoutResources</strong> call that would render them.</p>
<p>This means that in practice your individual GSP pages are likely to have only one or two module requirements as the rest is handled by the Sitemesh layout.</p>
<h2>Patterns for plugin authors</h2>
<p>Here are a few emerging patterns for plugin developers to heed when supporting the resources plugin.</p>
<ul>
<li>Specify id attributes on your resources when declaring them, to maximise the user&#8217;s opportunity to override specific details</li>
<li>Avoid monolithic modules, opting were possible to separate out functional areas such as the jQuery UI theme being separate from the jQuery UI code. This gives developers more flexibility. Obviously you must maintain correct dependencies between these</li>
<li>Put &#8220;development&#8221; un-minified versions of resources in &#8220;&lt;yourmodulename&gt;-dev&#8221; modules, so the user can pull in un-minified versions on demand, per-environment if they wish</li>
<li>Do not specify the bundling of your modules &#8211; at most use defaultBundle. No per-resource bundle attributes. Let the developer control this easily by overriding your defaultBundle settings in each module.</li>
<li>Use module names prefixed with the name of your plugin followed by a hyphen i.e. &#8220;jquery-main&#8221;, &#8220;myplugin-theme&#8221;, &#8220;acmeplugin-ui-styling&#8221;</li>
</ul>
<h2>Summing up</h2>
<p>All the things we&#8217;ve tackled here are core <a href="http://grails.org/plugin/resources">Resources</a> framework features &#8211; no extra plugins are required. The resource mapper that performs bundling is even built into the framework.</p>
<p>The real fun starts when you install some of the other mapper plugins &#8211; they may be renamed, re-encoded or even compiled from one form into another. It all happens at startup for declared resources, and at runtime for ad-hoc resources. Once it&#8217;s been done it is stored on disk on the server in that state and no more processing is done.</p>
<p>I hope you have fun with the Resources framework!</p>
<p>There are some nice refinements and maybe some new features coming in point releases at the time of writing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/09/12/optimising-your-application-with-grails-resources-plugin/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Results of the Grailsrocks Survey</title>
		<link>http://www.anyware.co.uk/2005/2011/06/17/results-of-the-grailsrocks-survey/</link>
		<comments>http://www.anyware.co.uk/2005/2011/06/17/results-of-the-grailsrocks-survey/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 09:04:43 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=827</guid>
		<description><![CDATA[A few weeks back I ran a little survey to get a rough idea of opinion on paid support for Grails plugins, as well as some potentially useful community stats. The most interesting figures for me are that almost 50% of respondents do think there is a need for commercial plugin support, and that a [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back I ran a little survey to get a rough idea of opinion on paid support for <a href="http://grails.org">Grails</a> plugins, as well as some potentially useful community stats.</p>
<p><span id="more-827"></span></p>
<p>The most interesting figures for me are that almost 50% of respondents do think there is a need for commercial plugin support, and that a lot of respondents&#8217; companies already pay for commercial tools and support.</p>
<p><a href="http://www.anyware.co.uk/2005/wp-content/uploads/2011/06/Screen-Shot-2011-09-05-at-15.36.03.png?9d7bd4"><img class="alignnone size-full wp-image-846" title="Grails Rocks Survey #1 Fragment" src="http://www.anyware.co.uk/2005/wp-content/uploads/2011/06/Screen-Shot-2011-09-05-at-15.36.03.png?9d7bd4" alt="" width="514" height="201" /></a></p>
<p><a href="http://www.anyware.co.uk/2005/wp-content/uploads/2011/06/Screen-Shot-2011-09-05-at-15.36.03.png?9d7bd4"></a>Also it was interesting to see the breakdown of usage of <a href="http://grailsrocks.com">my existing plugins</a>, as well as the spread of Grails versions in use. This survey happened just after the release of Grails 1.3.7.</p>
<p><a title="Grailsrocks plugin survey 2011" href="/grailsrocks/grailsrocks-plugin-market-2011-survey-results.html">View the survey results here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/06/17/results-of-the-grailsrocks-survey/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interviewed on DZone</title>
		<link>http://www.anyware.co.uk/2005/2011/05/10/interviewed-on-dzone/</link>
		<comments>http://www.anyware.co.uk/2005/2011/05/10/interviewed-on-dzone/#comments</comments>
		<pubDate>Tue, 10 May 2011 14:09:15 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Ideas]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=803</guid>
		<description><![CDATA[I was interviewed last week by Andres Almiray for DZone.com on matters Grails, Groovy and Weceem. The interview can be found here. &#160;]]></description>
			<content:encoded><![CDATA[<p>I was interviewed last week by Andres Almiray for DZone.com on matters <a href="http://grails.org">Grails</a>, <a href="http://groovy.codehaus.org">Groovy</a> and <a href="http://weceem.org">Weceem</a>.</p>
<p><a href="http://groovy.dzone.com/news/dzone-interviews-marc-palmer">The interview can be found here</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/05/10/interviewed-on-dzone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finally updated my resumé and open for clients&#8230;</title>
		<link>http://www.anyware.co.uk/2005/2011/03/09/finally-updated-my-resume-and-open-for-clients/</link>
		<comments>http://www.anyware.co.uk/2005/2011/03/09/finally-updated-my-resume-and-open-for-clients/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 14:59:36 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=788</guid>
		<description><![CDATA[I finally got round to updating my resumé &#8211; much overdue seeing as it has been languishing in a &#8220;pre-Grails work&#8221; state since 2006. I used to build it with a custom Java app that processed XML using WebMacro to produce multiple HTML formats. Yeah, XML. This time I decided to write it with less [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got round to <a href="http://www.anyware.co.uk/marc/">updating my resumé</a> &#8211; much overdue seeing as it has been languishing in a &#8220;pre-<a href="http://grails.org">Grails</a> work&#8221; state since 2006.</p>
<p>I used to build it with a custom Java app that processed XML using WebMacro to produce multiple HTML formats.</p>
<p>Yeah, XML.</p>
<p>This time I decided to write it with less emphasis on all the &#8220;7 years this&#8221;, &#8220;4 months that&#8221; so adored by recruitment agencies. Turns out I&#8217;ve actually really been a consultant for circa 75% of the last 14 years, and this level of skill detail is not needed by my prospective clients.</p>
<p>So now I have built it trivially using Markdown in TextMate. A little pain on the work history requires some inline HTML, but aside from that its trivial to build a new version. Using a butchered Blueprint CSS framework and some little CSS3 and Google Font sugar, its come out simple and clean, I hope.</p>
<p>Oh yes, if you want an extra hand on your Grails project, need some custom plugins, support or other Grails/Groovy related work let me know. I&#8217;m still paid to work on Weceem, but the time there is flexible &#8211; variety being the spice of life!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/03/09/finally-updated-my-resume-and-open-for-clients/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How we use Weceem CMS at NoticeLocal</title>
		<link>http://www.anyware.co.uk/2005/2011/02/15/how-we-use-weceem-cms-at-noticelocal/</link>
		<comments>http://www.anyware.co.uk/2005/2011/02/15/how-we-use-weceem-cms-at-noticelocal/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 21:24:01 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[noticelocal]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[startup]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=782</guid>
		<description><![CDATA[We have an online notice boards startup called NoticeLocal that we&#8217;ve written with Grails. In my experience over the years it became obvious that one of the major pain-points of using Java to deploy web apps is the hassle of updating a bit of content here or there, replacing an image &#8211; those kinds of [...]]]></description>
			<content:encoded><![CDATA[<p>We have an <a href="http://noticelocal.com">online notice boards startup called NoticeLocal</a> that we&#8217;ve written with <a href="http://grails.org">Grails</a>.</p>
<p>In my experience over the years it became obvious that one of the major pain-points of using Java to deploy web apps is the hassle of updating a bit of content here or there, replacing an image &#8211; those kinds of things that should be simple but aren&#8217;t. Also a lot of the time this stuff is not subject to the same strict versioning processes that we are using for our code.</p>
<p>As you may know I am paid to work on the <a href="http://weceem.org">Weceem CMS plugin</a> and application, that is also Grails based.</p>
<p>For NoticeLocal I decided that it would suit us well to use an instance of Weceem CMS running on one Tomcat, to supply the marketing/PR site for the service, as well as the multi-language content for UI elements in the main application.</p>
<p>It turned out this was really easy to do, even though Weceem currently has no API to speak of.</p>
<p>To do this we:</p>
<ol>
<li>Have a vanilla WeceemApp WAR running in one Tomcat instance</li>
<li>Have our NoticeLocal application WAR running in another Tomcat instance</li>
<li>Wrote a trivial taglib in the NoticeLocal application to retrieve content nodes from the CMS instance using vanilla HTTP requests to URLs with a predefined prefix, and stores the text them in a local ehcache.</li>
</ol>
<p>So, in Weceem we have a bunch of HTML content nodes that represent different elements of the main NoticeLocal app&#8217;s UI &#8211; we have several different Weceem templates that these different nodes use &#8211; e.g. one for side panels, one for full page content (but laid out inside the main NoticeLocal app sitemesh layout, and one for inline UI messages such as those shown on demand when the user performs some action.</p>
<p>So in our NoticeLocal application we use our taglib to render a &#8220;remote CMS message&#8221; in our pages using the not-so-well-known Grails/SiteMesh g:applyLayout tag that, just like Grails layouts, will parse out the body of the content that we&#8217;ve cached, and render it in situ.</p>
<p>Oh, and for bonus points we use a simple regex replace to give us GSP-like variable expansion when rendering the content from the local cache, so we can perform variable substitutions like ${userName} &#8211; so you can write that in a HTML node in Weceem, and by the time the NoticeLocal app renders it, it has your name in it.</p>
<p>At any point we can log into the CMS, update some UI message, and when the cache expires the users see the new updated stuff. No app redeploys.</p>
<p>Oh, and because its in a CMS we solve the hard problem &#8211; i18n translations of rich content with styling, images etc.</p>
<p>In a nutshell, it&#8217;s great!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2011/02/15/how-we-use-weceem-cms-at-noticelocal/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>OS X 10.6.5 update kills Safari cookies set on IP address</title>
		<link>http://www.anyware.co.uk/2005/2010/11/11/os-x-10-6-5-update-kills-safari-cookies-set-on-ip-address/</link>
		<comments>http://www.anyware.co.uk/2005/2010/11/11/os-x-10-6-5-update-kills-safari-cookies-set-on-ip-address/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 13:54:02 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[mac]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=753</guid>
		<description><![CDATA[Last night I was developing my Grails app locally, which uses cookies. We always had some trouble cross-browser with setting cookies on localhost or 127.0.0.1, so we updated our code many months ago to use the local interface&#8217;s default IP address. This worked fine. Until I installed OS X 10.6.5 update last night. Safari under [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was developing my Grails app locally, which uses cookies. We always had some trouble cross-browser with setting cookies on localhost or 127.0.0.1, so we updated our code many months ago to use the local interface&#8217;s default IP address. This worked fine.</p>
<p>Until I installed OS X 10.6.5 update last night.</p>
<p>Safari under 10.6.5 does not handle cookies stored against IP addresses in the same way anymore, presumably as a side effect of a cookie-related security &#8220;fix&#8221;.</p>
<p>So to state it simply, if you have a web app that sets a cookie with the Domain attribute set to an IP address, this cookie will be stored <strong>but never sent to your server</strong>. What appears to happen is that cookies without a domain set e.g. JSESSIONID set by Tomcat, are successfully stored against the IP.</p>
<p>However as soon as you include domain= in your Set-Cookie header, this is stored with a &#8220;.&#8221; at the start of the domain in the browser cookie list (even if you didn&#8217;t put a &#8220;.&#8221; at the start of your domain) and it is then never sent to the server on subsequent requests.</p>
<p>The workaround is to update your code to not set the domain to the IP address of your host, but to the local host name. This is not normally a problem except when you are using a development tool locally.</p>
<p>By changing your code to use the bonjour name of the local host e.g. &#8220;marcmacbook.local&#8221; the problem goes away again &#8211; provided you browse to marcmacbook.local to test your site.</p>
<p>Something tells me this is a regression in OS X, not a deliberate change relating to the CFNetwork security bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2010/11/11/os-x-10-6-5-update-kills-safari-cookies-set-on-ip-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weceem Tip: How to render items from an RSS feed</title>
		<link>http://www.anyware.co.uk/2005/2010/11/10/weceem-tip-how-to-render-items-from-an-rss-feed/</link>
		<comments>http://www.anyware.co.uk/2005/2010/11/10/weceem-tip-how-to-render-items-from-an-rss-feed/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 09:16:22 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[weceem]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=745</guid>
		<description><![CDATA[Last night I was working on a Weceem site and had cause to render some links to articles from an external RSS feed. I have a Weceem Widget node already for this &#8220;news&#8221; panel, but the RSS comes from another site. Turns out even without direct support for rendering feeds (Weceem does support producing feeds [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was working on a <a href="http://weceem.org">Weceem</a> site and had cause to render some links to articles from an external RSS feed. I have a Weceem Widget node already for this &#8220;news&#8221; panel, but the RSS comes from another site. Turns out even without direct support for rendering feeds (Weceem does support producing feeds out of the box), using <a href="http://groovy.codehaus.org">Groovy&#8217;s</a> magic methods on java.net.URL and the XmlSlurper you can easily do this.</p>
<p>Here&#8217;s a complete example:</p>
<p><script src="https://gist.github.com/1195788.js"></script></p>
<p>This will render five news articles from the BBC news feed. It is incredibly naïve:</p>
<ol>
<li>It doesn&#8217;t make any attempt at error handling</li>
<li>It will likely blow up if there are less than 5 items in the feed</li>
<li>It doesn&#8217;t do any caching &#8211; the feed is retrieved (by the server) every time the widget is rendered</li>
</ol>
<p>However with a few tweaks this can be turned into something more robust. I imagine we will add a specific tag for this that hides the error handling and caching issues into a future build of Weceem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2010/11/10/weceem-tip-how-to-render-items-from-an-rss-feed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smart ETag and Last-Modified handling with cache-headers plugin</title>
		<link>http://www.anyware.co.uk/2005/2010/10/26/smart-etag-and-last-modified-handling-with-cache-headers-plugin/</link>
		<comments>http://www.anyware.co.uk/2005/2010/10/26/smart-etag-and-last-modified-handling-with-cache-headers-plugin/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 15:30:05 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=740</guid>
		<description><![CDATA[I&#8217;ve been working on the next release of Weceem CMS these last few weeks and one of the issues to tackle was that Weceem was not setting any of the useful response headers used by browsers and intermediate proxies/caches. This obviously needed improvement, and this tied in well to work I had already done on [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on the next release of <a href="http://weceem.org">Weceem CMS</a> these last few weeks and one of the issues to tackle was that Weceem was not setting any of the useful response headers used by browsers and intermediate proxies/caches.</p>
<p><span id="more-740"></span>This obviously needed improvement, and this tied in well to work I had already done on the small but beautiful Grails <a href="http://grails.org/plugin/cache-headers">cache-headers</a> plugin.</p>
<p>The problem: with a CMS you generally can&#8217;t make the client cache any content, not without some config for the CMS author/administrator to specify how long to cache it for &#8211; this is a problem we will tackle later. However at the very least you want to avoid re-generating content when requested if the client already has the same content.</p>
<p>The solution: Implement ETag and Last-Modified handling correctly so that when you receive requests with If-None-Match or If-Modified-Since you can return &#8220;304 Not Modified&#8221; if the content is the same as that the client last downloaded. This can give you major improvements in client response time and also server load, even in applications with highly dynamic content.</p>
<p>The thing is, implementing this from scratch in every <a href="http://grails.org">Grails</a> controller action you have is rather dull.</p>
<p>So in steps the new <strong>withCacheHeaders</strong> method in the <a href="http://grails.org/plugin/cache-headers">Cache-Headers plugin</a>. This method works a little like Grails&#8217; excellent withFormat method, using a simple DSL to specify:</p>
<ul>
<li>How to generate the ETag for this request</li>
<li>How to calculate the Last-Modified date for this request</li>
<li>How to render the response, if we need to do this because the content has changed since the client last downloaded it.</li>
</ul>
<p>Using this information that you provide, the cache-headers plugin will automatically work out if it can return &#8220;304 Not Modified&#8221; or if it has to render your response for you.</p>
<p>Here&#8217;s a working example from Weceem&#8217;s content rendering controller:<br />
<script type="text/javascript" src="https://gist.github.com/1195792.js">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[</p>
<p>// ]]&gt;</script></p>
<p>As you can see there are three &#8220;method&#8221; calls inside the withCacheHeaders call: etag, lastModified and generate. You simply implement these and the plugin handles the rest.</p>
<p>In the &#8220;generate&#8221; closure we&#8217;re also calling the lastModified method added to controller actions by the cache-headers plugin. This lets us easily set the Last-Modified header of the response, and then we make a token effort at setting client/proxy caching headers with the &#8220;cache&#8221; method. This will be subject to later refinement.</p>
<p>For more details <a href="http://grails.org/plugin/cache-headers">see the docs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2010/10/26/smart-etag-and-last-modified-handling-with-cache-headers-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 3/19 queries in 0.259 seconds using disk: basic

Served from: www.anyware.co.uk @ 2012-02-10 03:31:22 -->
