<?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>No One Is Perfect &#187; My Code</title>
	<atom:link href="http://watchitlater.com/blog/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://watchitlater.com/blog</link>
	<description>A reluctant foray into the world of blogging.</description>
	<lastBuildDate>Tue, 08 Nov 2011 12:32:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Default HTML-escape using Freemarker</title>
		<link>http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/</link>
		<comments>http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 16:03:14 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[My Code]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[freemarker]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[springframework]]></category>

		<guid isPermaLink="false">http://watchitlater.com/blog/?p=355</guid>
		<description><![CDATA[Most java developers have at least heard of Freemarker. FreeMarker is a &#8220;template engine&#8221;; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It&#8217;s a Java package, a class library for Java programmers. It&#8217;s not an application for end-users in itself, but something that programmers can embed [...]]]></description>
			<content:encoded><![CDATA[<p>Most java developers have at least heard of <a href="http://freemarker.sourceforge.net/">Freemarker</a>.</p>
<blockquote><p>
FreeMarker is a &#8220;template engine&#8221;; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It&#8217;s a Java package, a class library for Java programmers. It&#8217;s not an application for end-users in itself, but something that programmers can embed into their products.
</p></blockquote>
<p>It is the &#8220;generic&#8221; nature of Freemarker that trips up java web developers. Freemarker by default does not provide any facilities to allow default HTML-escaping of content &#8211; a necessity if you want to attempt to prevent <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-Site Scripting attacks</a> on your web applications. Yes I know that it has the <code>?html</code> built-in, and that you can wrap blocks of text in <code>&lt;#escape x as x?html&gt;</code> directives, but you have to remember to do that on each page.</p>
<p>What if there was another way?</p>
<p>The class below is a Freemarker TemplateLoader that automatically wraps each loaded template with the HTML-escape directive. Now there is no need to remember to do that in your templates. You can find it being used in my <a href="https://github.com/tomcz/example-webapp">example project</a> on GitHub.</p>
<pre name="code" class="java:nogutter">
import freemarker.cache.TemplateLoader;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class HtmlTemplateLoader implements TemplateLoader {

    public static final String ESCAPE_PREFIX = &quot;&lt;#ftl strip_whitespace=true&gt;&lt;#escape x as x?html&gt;&quot;;
    public static final String ESCAPE_SUFFIX = &quot;&lt;/#escape&gt;&quot;;

    private final TemplateLoader delegate;

    public HtmlTemplateLoader(TemplateLoader delegate) {
        this.delegate = delegate;
    }

    @Override
    public Object findTemplateSource(String name) throws IOException {
        return delegate.findTemplateSource(name);
    }

    @Override
    public long getLastModified(Object templateSource) {
        return delegate.getLastModified(templateSource);
    }

    @Override
    public Reader getReader(Object templateSource, String encoding) throws IOException {
        Reader reader = delegate.getReader(templateSource, encoding);
        try {
            String templateText = IOUtils.toString(reader);
            return new StringReader(ESCAPE_PREFIX + templateText + ESCAPE_SUFFIX);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }

    @Override
    public void closeTemplateSource(Object templateSource) throws IOException {
        delegate.closeTemplateSource(templateSource);
    }
}
</pre>
<p>To wire this up using SpringFramework&#8217;s Freemarker support you do have to take another step and extend its FreeMarkerConfigurer to register the HtmlTemplateLoader as the one to use for view resolution and rendering. If on the other hand you don&#8217;t use Spring then you have one less bit of code to maintain.</p>
<pre name="code" class="java:nogutter">
import freemarker.cache.TemplateLoader;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.List;

public class HtmlFreeMarkerConfigurer extends FreeMarkerConfigurer {

    @Override
    protected TemplateLoader getAggregateTemplateLoader(List&lt;TemplateLoader&gt; templateLoaders) {
        logger.info(&quot;Using HtmlTemplateLoader to enforce HTML-safe content&quot;);
        return new HtmlTemplateLoader(super.getAggregateTemplateLoader(templateLoaders));
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Provision EC2 instance using boto</title>
		<link>http://watchitlater.com/blog/2011/09/provision-ec2-instance-using-boto/</link>
		<comments>http://watchitlater.com/blog/2011/09/provision-ec2-instance-using-boto/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 12:09:23 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[My Code]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[boto]]></category>
		<category><![CDATA[devops]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[fabric]]></category>
		<category><![CDATA[puppet]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://watchitlater.com/blog/?p=343</guid>
		<description><![CDATA[Sam Newman recently published a very interesting blog entry on using fabric to apply puppet scripts on remote machines. He left the provision_using_boto() method as an exercise to the reader. That just sounded tempting enough to be a challenge since I hadn&#8217;t gotten around to looking at boto. You can find the result of my [...]]]></description>
			<content:encoded><![CDATA[<p>Sam Newman recently published a very interesting <a href="http://www.magpiebrain.com/2011/08/21/using-fabric-to-apply-puppet-scripts/">blog entry</a> on using <a href="http://docs.fabfile.org/en/1.2.2/index.html">fabric</a> to apply <a href="https://github.com/puppetlabs/puppet">puppet</a> scripts on remote machines. He left the <code>provision_using_boto()</code> method as an exercise to the reader. That just sounded tempting enough to be a challenge since I hadn&#8217;t gotten around to looking at <a href="http://boto.cloudhackers.com/">boto</a>. You can find the result of my attempt on <a href="https://github.com/tomcz/aws_py/tree/master/ec2">GitHub</a>. To be precise <a href="https://github.com/tomcz/aws_py/blob/master/ec2/aws.py">aws.py</a> implements the provisioning using boto and <a href="https://github.com/tomcz/aws_py/blob/master/ec2/fabfile.py">fabfile.py</a> drives fabric and puppet. Hope you find it as useful as I have.</p>
]]></content:encoded>
			<wfw:commentRss>http://watchitlater.com/blog/2011/09/provision-ec2-instance-using-boto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kramdown and Webby</title>
		<link>http://watchitlater.com/blog/2011/08/kramdown-and-webby/</link>
		<comments>http://watchitlater.com/blog/2011/08/kramdown-and-webby/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 12:23:19 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[My Code]]></category>
		<category><![CDATA[kramdown]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[webby]]></category>

		<guid isPermaLink="false">http://watchitlater.com/blog/?p=335</guid>
		<description><![CDATA[A number of the sites that I manage for fun are simply static web pages. The dynamic nature is handled by javascript classes and plugins. This means that I really don&#8217;t need or want an application server to serve these sites, but I do want to still use some of the practices that I apply [...]]]></description>
			<content:encoded><![CDATA[<p>A number of the sites that I manage for fun are simply static web pages. The dynamic nature is handled by javascript classes and plugins. This means that I really don&#8217;t need or want an application server to serve these sites, but I do want to still use some of the practices that I apply to web application development. Enter <a href="http://webby.rubyforge.org/">webby</a> &#8211; it works by combining the contents of a page with a layout to produce HTML. Awesomely simple and powerful. It can use the rdiscount gem to process files in markdown format into html pages, but I prefer to use the <a href="http://kramdown.rubyforge.org/">kramdown</a> library. No problem, here is how you can add kramdown support to your webby project.</p>
<pre name="code" class="ruby:nogutter">
if try_require('kramdown', 'kramdown')
  Webby::Filters.register :kramdown do |input|
    Kramdown::Document.new(input, :parse_block_html => true).to_html
  end
else
  Webby::Filters.register :kramdown do |input|
    raise Webby::Error, "'kramdown' must be installed to use the kramdown filter"
  end
end
</pre>
<p>Simply paste the above into a file called <code>kramdown.rb</code> in your project&#8217;s lib directory and then you can specify <code>filter: kramdown</code> in your webby templates.</p>
]]></content:encoded>
			<wfw:commentRss>http://watchitlater.com/blog/2011/08/kramdown-and-webby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

