<?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>info.michael-simons.eu &#187; Java</title>
	<atom:link href="http://info.michael-simons.eu/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://info.michael-simons.eu</link>
	<description>Just another nerd blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 10:26:00 +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>Get the uptime of your Java VM</title>
		<link>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/</link>
		<comments>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 10:26:00 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=657</guid>
		<description><![CDATA[You don&#8217;t need JConsole or similar for just displaying the approximate uptime of your application respectively your Java Virtual Machine: import java.lang.management.ManagementFactory; &#160; public class Demo &#123; public static void main&#40;String... args&#41; &#123; final long uptime = ManagementFactory.getRuntimeMXBean&#40;&#41;.getUptime&#40;&#41;; System.out.println&#40;String.format&#40;&#34;Up for %dms&#34;, uptime&#41;&#41;; &#125; &#125; If you use Joda-Time (and you should if you have anything [...]]]></description>
			<content:encoded><![CDATA[<p>You don&#8217;t need JConsole or similar for just displaying the approximate uptime of your application respectively your Java Virtual Machine:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.management.ManagementFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> uptime <span style="color: #339933;">=</span> ManagementFactory.<span style="color: #006633;">getRuntimeMXBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getUptime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Up for %dms&quot;</span>, uptime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you use <a href="http://joda-time.sourceforge.net/">Joda-Time</a> (and you should if you have anything to do with date/datetime processing), you can format it nicely like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.management.ManagementFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.MessageFormat</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.Period</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.PeriodType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.format.PeriodFormatter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.joda.time.format.PeriodFormatterBuilder</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> Period vmUptime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Period<span style="color: #009900;">&#40;</span>ManagementFactory.<span style="color: #006633;">getRuntimeMXBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getUptime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">normalizedStandard</span><span style="color: #009900;">&#40;</span>PeriodType.<span style="color: #006633;">yearDayTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">final</span> PeriodFormatter pf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PeriodFormatterBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">printZeroAlways</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">appendDays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendLiteral</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">MessageFormat</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{0,choice,0# days, |1# day, |2# days, }&quot;</span>, vmUptime.<span style="color: #006633;">getDays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">minimumPrintedDigits</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">appendHours</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendLiteral</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">appendMinutes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">toFormatter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Up for %s&quot;</span>, pf.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>vmUptime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You also have a nice example of the often unknown <a href="http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html">MessageFormat</a>.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=657&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_657" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/02/08/get-the-uptime-of-your-java-vm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The dangers of Javas ImageIO</title>
		<link>http://info.michael-simons.eu/2012/01/25/the-dangers-of-javas-imageio/</link>
		<comments>http://info.michael-simons.eu/2012/01/25/the-dangers-of-javas-imageio/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 07:52:11 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[ImageIO]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=642</guid>
		<description><![CDATA[Javas ImageIO works&#8230; well, most of the time. It contains some unfixed, jpeg related bugs, but it works. It may contain some dangers when used in a webbased application for generation large images on the fly. Most problems are related to ImageIOs filed based caching and not flushing buffers when an IOException in an underlying [...]]]></description>
			<content:encoded><![CDATA[<p>Javas ImageIO works&#8230; well, most of the time. It contains some unfixed, jpeg related bugs, but it works.</p>
<p>It may contain some dangers when used in a webbased application for generation large images on the fly.</p>
<p>Most problems are related to ImageIOs filed based caching and not flushing buffers when an IOException in an underlying stream occurs.</p>
<p>First, the <code>javax.imageio.ImageReader</code>. It caches some data in files. It is essential to dispose the reader <strong>and</strong> the underlying ImageInputStream after use if it&#8217;s not needed anymore like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>imageReader.<span style="color: #006633;">getInput</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> imageReader.<span style="color: #006633;">getInput</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> ImageInputStream<span style="color: #009900;">&#41;</span>			
  <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ImageInputStream<span style="color: #009900;">&#41;</span>imageReader.<span style="color: #006633;">getInput</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imageReader.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If it isn&#8217;t closed and disposed, the temporary cache files are either deleted not at all or maybe at the next garbage collection. I was able to bring down a VM by &#8220;java.io.FileNotFoundException: (Too many open files)&#8221; several times because i didn&#8217;t close a reader in a loop. Even the classloader wasn&#8217;t able to load any new classes after the ImageReader going mad on the file handle. </p>
<p>The other side is the <code>javax.imageio.ImageWriter</code>. There is an issue mentioned in the <a href="http://wiki.apache.org/tomcat/FAQ/KnownIssues#ImageIOIssues">Tomcat Wiki</a>.</p>
<p>I used the ImageWriter to dynamically create large images. I directly passed the <code>javax.servlet.ServletOutputStream</code> to the ImageWriter. If the generation of images takes long enough, there&#8217;s a good chance that the client aborts the request and the ServletOutputStream gets flushed right when the ImageWriter is writing to it. I didn&#8217;t have the exceptions mentioned in the wiki but my VM crashed. Great. I guess it had something to do with the native <code>org.apache.coyote.ajp.AjpAprProtocol</code> Ajp Apr connector i use, but that&#8217;s just guessing. </p>
<p>I solved this problem by using a temporary file and its related outputstream which i then stream like described <a href="/2011/06/28/apache-httpd-tomcat-und-sendfile/">here</a>. This solution is not only faster but i also can catch any exception related to an aborting client.</p>
<p>Also take care to dispose the write as well like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	 imageWriter.<span style="color: #006633;">setOutput</span><span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 imageWriter.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> IIOImage<span style="color: #009900;">&#40;</span>image, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>, iwp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                        
	 imageWriter.<span style="color: #006633;">abort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                    
	 <span style="color: #000000; font-weight: bold;">throw</span> e<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
	 <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>                           
		 out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                            
	 <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> inner<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                              
	 <span style="color: #009900;">&#125;</span>
	 imageWriter.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This took me several hours to figure out… I hope someone finds this post useful.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=642&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_642" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/01/25/the-dangers-of-javas-imageio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimizing web resources with wro4j, Spring and ehcache</title>
		<link>http://info.michael-simons.eu/2012/01/18/optimizing-web-resources-with-wro4j-spring-and-ehcache/</link>
		<comments>http://info.michael-simons.eu/2012/01/18/optimizing-web-resources-with-wro4j-spring-and-ehcache/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 10:18:11 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Minimization]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[wro4j]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=647</guid>
		<description><![CDATA[I think that almost no website today can do without JavaScript. There are some incredible good JavaScript libraries like jQuery for which an enormous mass of plugins and extensions exits. The downside of this is, that for example the JavaScript code of my daily picture project Daily Fratze is bigger than the whole startpage of [...]]]></description>
			<content:encoded><![CDATA[<p>I think that almost no website today can do without JavaScript. There are some incredible good JavaScript libraries like jQuery for which an enormous mass of plugins and extensions exits.</p>
<p>The downside of this is, that for example the JavaScript code of my daily picture project <a href="http://dailyfratze.de">Daily Fratze</a> is bigger than the whole startpage of my first &#8220;homepage&#8221; was.</p>
<p>With every problem there is a solution, namely JavaScript compressors and minifier. Those tools can compress the code by removing superfluous whitespaces, renaming variables and functions or even by optimizing the code.</p>
<p>So far i have used the <a href="http://alchim.sourceforge.net/yuicompressor-maven-plugin/compress-mojo.html">YUI compressor maven mojo</a> in my Spring based projects. This is a build time solution that compresses JavaScript and CSS files when creating a war file.</p>
<p>For me it had several disadvantages: I don&#8217;t see the effect of compressing when i develop my application and it could not concatenate multiple script files.</p>
<p>The later is important because every additional request a browser makes slows down the loading of a webpage. And manual hacking all JavaScript into one file? No way…</p>
<p><a href="http://code.google.com/p/wro4j/">wro4j</a> to the rescue:</p>
<blockquote>
<p>Free and Open Source Java project which brings together almost all the modern web tools: JsHint, CssLint, JsMin, Google Closure compressor, YUI Compressor, UglifyJs, Dojo Shrinksafe, Css Variables Support, JSON Compression, Less, Sass, CoffeeScript and much more. In the same time, the aim is to keep it as simple as possible and as extensible as possible in order to be easily adapted to application specific needs.</p>
</blockquote>
<p>My goal was to integrate wro4j with Spring and ehcache with a minimum number of additional configuration files.</p>
<p>If you&#8217;re interested in some of my ideas, read on:</p>
<p><span id="more-647"></span></p>
<p><em>Please note: This is not the complete solution. The wro.xml is missing as well as some of my infrastructure ideas. But you should get the idea on how to use wro4j with Spring, extend it with your own caching strategy and configure the processors.</em></p>
<p>First step: Creating a WroFilter implementation that doesn&#8217;t depend on wro.properties:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.http.ConfigurableWroFilter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.manager.factory.WroManagerFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Wro4jFilter <span style="color: #000000; font-weight: bold;">extends</span> ConfigurableWroFilter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> WroManagerFactory factory<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Wro4jFilter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> properties, <span style="color: #000000; font-weight: bold;">final</span> WroManagerFactory wroManagerFactory, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">boolean</span> debug<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">factory</span> <span style="color: #339933;">=</span> wroManagerFactory<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Properties</span> p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		p.<span style="color: #006633;">putAll</span><span style="color: #009900;">&#40;</span>properties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		p.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;debug&quot;</span>, <span style="color: #003399;">Boolean</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>debug<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setProperties</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> WroManagerFactory newWroManagerFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">factory</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you see this filter accepts a map that is converted to a properties instance from which the superclass is configured. I also inject the WroManagerFactory which will be my interface to ehcache. Finally i can overwrite the debug flag.</p>
<p>This bean is instantiated through an @Configuration class like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Configuration
@Profile<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WebConfigProd <span style="color: #000000; font-weight: bold;">extends</span> WebConfig <span style="color: #009900;">&#123;</span>
	@Bean<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wroFilter&quot;</span><span style="color: #009900;">&#41;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Filter getWroFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Wro4jFilter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">defaultWroProperties</span>, <span style="color: #000000; font-weight: bold;">new</span> Wro4jManagerFactory<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">cacheManager</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The defaultWroProperties is a <code>Map<String,String></Code> created from the Spring Environment so that i can keep my wro4j options in my application properties file. cacheManager is an instance of <code>net.sf.ehcache.CacheManager</code>. The CacheManager is needed for my own WroManagerFactory which you'll see later.</p>
<p>To use this filter you need the Spring DelegatinFilterProxy:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>webResourceOptimizer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.filter.DelegatingFilterProxy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>targetBeanName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>wroFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>targetFilterLifecycle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Having set the targetFilterLifecycle to true is actually essential because the original <code>ro.isdc.wro.http.WroFilter</code> relies on the init method.</p>
<p>So far i only have the filter. What's missing is the wro4j model. This is where groups of JavaScript and CSS files are defined and it can be - among others - an xml file. I want mine to reside with my other configuration files in an dedicated package. To accomplish that i've created a special WroManagerFactory:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.ehcache.CacheManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.cache.CacheEntry</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.cache.CacheStrategy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.cache.ContentHashEntry</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.extensions.processor.css.YUICssCompressorProcessor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.extensions.processor.js.GoogleClosureCompressorProcessor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.manager.factory.BaseWroManagerFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.model.factory.WroModelFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.model.factory.XmlModelFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.model.resource.processor.factory.ProcessorsFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.model.resource.processor.factory.SimpleProcessorsFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.model.resource.processor.impl.css.CssUrlRewritingProcessor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.javascript.jscomp.CompilationLevel</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Wro4jManagerFactory <span style="color: #000000; font-weight: bold;">extends</span> BaseWroManagerFactory <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> CACHE_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;some_cache_name&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> CacheManager cacheManager<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Wro4jManagerFactory<span style="color: #009900;">&#40;</span>CacheManager cacheManager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cacheManager</span> <span style="color: #339933;">=</span> cacheManager<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> ProcessorsFactory newProcessorsFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> SimpleProcessorsFactory rv <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleProcessorsFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// URLs in CSS needs to be rewritten as it is served from a different location than the original files. I'm not using @import statements, otherwise the appropriate processor should be added for rewriting them as well</span>
		rv.<span style="color: #006633;">addPreProcessor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CssUrlRewritingProcessor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
                <span style="color: #666666; font-style: italic;">// JavaScript compression by the Google Closure compressor	</span>
		rv.<span style="color: #006633;">addPreProcessor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GoogleClosureCompressorProcessor<span style="color: #009900;">&#40;</span>CompilationLevel.<span style="color: #006633;">SIMPLE_OPTIMIZATIONS</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// And css by YUI Css Compressor</span>
		rv.<span style="color: #006633;">addPreProcessor</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> YUICssCompressorProcessor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rv<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>			
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> WroModelFactory newModelFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> XmlModelFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">InputStream</span> getModelResourceAsStream<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/foo/bar/config/wro.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> CacheStrategy<span style="color: #339933;">&lt;</span>CacheEntry, ContentHashEntry<span style="color: #339933;">&gt;</span> newCacheStrategy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Wro4jCacheStrategy<span style="color: #339933;">&lt;</span>CacheEntry, ContentHashEntry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cacheManager</span>.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span>CACHE_NAME<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The location of my wro model is handled in <code>newModelFactory</code>. Nothing special. What's more interesting is <code>newCacheStrategy</code>. It get's an ehcache <code>net.sf.ehcache.Cache</code> from the CacheManager and passes it to my caching strategy:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.ehcache.Cache</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.ehcache.Element</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ro.isdc.wro.cache.CacheStrategy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Wro4jCacheStrategy<span style="color: #339933;">&lt;</span>K, V<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">implements</span> CacheStrategy<span style="color: #339933;">&lt;</span>K, V<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>	
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Cache cache<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Wro4jCacheStrategy<span style="color: #009900;">&#40;</span>Cache cache<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cache</span> <span style="color: #339933;">=</span> cache<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> put<span style="color: #009900;">&#40;</span>K key, V value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cache</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Element</span><span style="color: #009900;">&#40;</span>key, value, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> V get<span style="color: #009900;">&#40;</span>K key<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cache</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>V<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>element <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> element.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cache</span>.<span style="color: #006633;">removeAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So far i have:</p>
<ul>
<li>No need for wro.properties</li>
<li>The wro.xml model along with my other configuration files</li>
<li>The WroFilter initialized by Spring</li>
<li>Having ehcache cache my compressed and concatenated resources</li>
</ul>
<p>As you might have guessed there is also a WebConfigDev. The development configurations sets wro4j to development mode which means i can turn of minimisation at runtime through an url parameter like so "?minimize=false". </p>
<p>I didn't want to change my jsps all the time so i defined a bean that holds a property <code>minimizeResources</code> that is changable via JMX. I include my JavaScript like so:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;script&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;src&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:url</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/wro/project.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;minimize&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${globalOptions.minimizeResources}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/c:param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/c:url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:body</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>CSS is analog. I use the jsp:element syntax because all my jsps are actually jspx files.</p>
<p>With this solution i can easily turn off minimization in development mode. </p>
<p>In production mode i now have one compressed JavaScript file instead of 15 files and one CSS file instead of four. The first request takes a second or so but the compressed (and gzipped) content is cached. The Google Closure is actually pretty awesome and reduces my accumulated JavaScript code by over 50%. </p>
<p>Reducing the number of requests necessary had - for me - the biggest impact on my smartphone.</p>
<p>I really can recommend wro4j. It's very extensible and actually pretty easy to use. I cannot only be used to compress files but also can allow you to use CoffeeScript or SASS in Spring or JEE application.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=647&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_647" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/01/18/optimizing-web-resources-with-wro4j-spring-and-ehcache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a CSRF protection with Spring 3.1</title>
		<link>http://info.michael-simons.eu/2012/01/11/creating-a-csrf-protection-with-spring-3-1/</link>
		<comments>http://info.michael-simons.eu/2012/01/11/creating-a-csrf-protection-with-spring-3-1/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 09:37:58 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[CSRF]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Websecurity]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=628</guid>
		<description><![CDATA[CSRF Attacks still seems to be a problem, a pity that there is no standard solution in the Spring 3.1 framework. Although not probably, i wanted to protect my projects by malicious crafted links. I didn&#8217;t want to use an extra library but something which is already available in the Spring framework. Here is what [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery" title="CSRF Attacks">CSRF Attacks</a> still seems to be a problem, a pity that there is no standard solution in the Spring 3.1 framework. Although not probably, i wanted to protect my projects by malicious crafted links. </p>
<p>I didn&#8217;t want to use an extra library but something which is already available in the Spring framework. Here is what i come up with:</p>
<p><span id="more-628"></span></p>
<p>I choose the token protection mechanism for my implementation.</p>
<p>The core of my solution is the CSRFToken Service:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> CSRFTokenService <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> TOKEN_PARAMETER_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;_tk&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> TOKEN_ATTRIBUTE_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CSRFToken&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> METHODS_TO_CHECK <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">unmodifiableList</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span>, <span style="color: #0000ff;">&quot;PUT&quot;</span>, <span style="color: #0000ff;">&quot;DELETE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** Generates a new CSRF Protection token */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> generateToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** Obtains the token from the session. If there is no token, a new one will be generated */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTokenFromSession<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** This method tests, if a token is acceptable when a user is logged in */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> acceptsTokenIn<span style="color: #009900;">&#40;</span>HttpServletRequest request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.SecureRandom</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpSession</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.codec.binary.Base64</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Service</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.dailyfratze.services.CSRFTokenService</span><span style="color: #339933;">;</span>
&nbsp;
@Service<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;csrfTokenService&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CSRFTokenServiceImpl <span style="color: #000000; font-weight: bold;">implements</span> CSRFTokenService <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">SecureRandom</span> random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SecureRandom</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> generateToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		random.<span style="color: #006633;">nextBytes</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Base64.<span style="color: #006633;">encodeBase64URLSafeString</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTokenFromSession<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> request.<span style="color: #006633;">getUserPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getTokenFromSessionImpl</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getTokenFromSessionImpl<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> HttpSession session<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> token <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>session <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			token <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> session.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>TOKEN_ATTRIBUTE_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isBlank</span><span style="color: #009900;">&#40;</span>token<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>TOKEN_ATTRIBUTE_NAME, <span style="color: #009900;">&#40;</span>token <span style="color: #339933;">=</span> generateToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> token<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> acceptsTokenIn<span style="color: #009900;">&#40;</span>HttpServletRequest request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">boolean</span> rv <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Token is only verified if principal is not null</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getUserPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
			rv <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">final</span> HttpSession session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			rv <span style="color: #339933;">=</span> session <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getTokenFromSessionImpl</span><span style="color: #009900;">&#40;</span>session<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span>TOKEN_PARAMETER_NAME<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rv<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8220;getTokenFromSession&#8221; is called right after a user logs in, so that the token gets stored into his session.</p>
<p>As you can see in the implementation of &#8220;acceptsTokenIn&#8221;, the token is only needed and verified when the principal is not null, meaning when a user is authenticated.</p>
<p>The interface contains some constants: The name of the token in forms and requests and the name of the attribute under which the token is stored in the session. The token itself is just a base64 of some random bytes.</p>
<p>I only want the token to be checked in writing methods: METHODS_TO_CHECK, meaning only in put, delete and posts requests. My applications don&#8217;t change state based on get requests.</p>
<p>So where to check for the token? I use a pretty simple Spring &#8220;HandlerInterceptor&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.dailyfratze.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.HandlerInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.ModelAndView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.dailyfratze.services.CSRFTokenService</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CSRFInterceptor <span style="color: #000000; font-weight: bold;">implements</span> HandlerInterceptor <span style="color: #009900;">&#123;</span> 
	@Autowired
	<span style="color: #000000; font-weight: bold;">private</span> CSRFTokenService csrfTokenService<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> preHandle<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response, <span style="color: #003399;">Object</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">boolean</span> rv <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>CSRFTokenService.<span style="color: #006633;">METHODS_TO_CHECK</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">defaultIfBlank</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>csrfTokenService.<span style="color: #006633;">acceptsTokenIn</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			response.<span style="color: #006633;">addHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;X-DailyFratze-InvalidCSRFToken&quot;</span>, <span style="color: #003399;">Boolean</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			response.<span style="color: #006633;">sendError</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_FORBIDDEN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			rv <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> 		
		<span style="color: #000000; font-weight: bold;">return</span> rv<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> postHandle<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response, <span style="color: #003399;">Object</span> handler, ModelAndView modelAndView<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterCompletion<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response, <span style="color: #003399;">Object</span> handler, <span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>		
	<span style="color: #009900;">&#125;</span>		
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This interceptor stops the chain if the request method should be checked and the token is not acceptable by sending a HTTP forbidden error. The additional response header is used by Ajax calls to present a dialog that the session is invalidated. </p>
<p>How to get the token into forms? I wanted to be able to change the token name in only one place so i came up with the following custom tag:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.JspException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.tagext.TagSupport</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.dailyfratze.services.CSRFTokenService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.dailyfratze.utils.HelperRegistry</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Creates a hidden input field with the CSRF Token
 * @author michael.simons, 2011-09-20
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CSRFTokenTag <span style="color: #000000; font-weight: bold;">extends</span> TagSupport <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 745177955805541350L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> plainToken <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> doStartTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JspException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> CSRFTokenService csrfTokenService <span style="color: #339933;">=</span> HelperRegistry.<span style="color: #006633;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">pageContext</span>.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">pageContext</span>.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, CSRFTokenService.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;csrfTokenService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> token <span style="color: #339933;">=</span> csrfTokenService.<span style="color: #006633;">getTokenFromSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">pageContext</span>.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>StringUtils.<span style="color: #006633;">isBlank</span><span style="color: #009900;">&#40;</span>token<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>plainToken<span style="color: #009900;">&#41;</span>
					pageContext.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>token<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">else</span>
					pageContext.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>%1$s<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>%1$s<span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>%2$s<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span>, CSRFTokenService.<span style="color: #006633;">TOKEN_PARAMETER_NAME</span>, token<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> SKIP_BODY<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> doEndTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JspException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> EVAL_PAGE<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isPlainToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> plainToken<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPlainToken<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> plainToken<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">plainToken</span> <span style="color: #339933;">=</span> plainToken<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getTokenParameterName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> CSRFTokenService.<span style="color: #006633;">TOKEN_PARAMETER_NAME</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>with the corresponding mapping:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taglib</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tlib-version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tlib-version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;short-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>df<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/short-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uri<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://michael-simons.eu/taglibs/df<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/uri<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>csrfToken<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tag-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>de.dailyfratze.tags.CSRFTokenTag<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body-content<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>empty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body-content<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>plainToken<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>false<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/required<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;function<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>csrfTokenParameter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;function-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>de.dailyfratze.tags.CSRFTokenTag<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/function-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;function-signature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java.lang.String getTokenParameterName()<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/function-signature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/function<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/taglib<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I can use this tag in forms like so:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;foobar&quot;&gt;
   &lt;df:csrfToken /&gt;
&lt;/form&gt;</pre></div></div>

<p>Or for generating url parameters for example for ajax calls like so:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">&lt;c:url value=&quot;/foobar&quot;&gt;
  &lt;c:param name=&quot;${df:csrfTokenParameter()}&quot;&gt;
    &lt;df:csrfToken plainToken=&quot;true&quot;/&gt;
  &lt;/c:param&gt;
&lt;/c:url&gt;</pre></div></div>

<p>So if a token is invalid, the user is either redirect to an error page if it is a normal post, ajax calls through jQuery can be handled like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> isInvalidCSRFToken <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> rv <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xhr.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">403</span> <span style="color: #339933;">&amp;&amp;</span> xhr.<span style="color: #660066;">getResponseHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'X-DailyFratze-InvalidCSRFToken'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'true'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Session is invalid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		rv <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> rv<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'post'</span><span style="color: #339933;">,</span>
	url<span style="color: #339933;">:</span> theUrl<span style="color: #339933;">,</span>	
	dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text'</span><span style="color: #339933;">,</span>		
	complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>isInvalidCSRFToken<span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>	    				
			<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>	    				
		<span style="color: #006600; font-style: italic;">// handle the result</span>
	<span style="color: #009900;">&#125;</span> 	        
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code snippets are all taken from a running project. If you want to use them, use them. The package names are missing and must be added. Also the JavaScript code isn&#8217;t complete.</p>
<p>Feel free to comment, if you have suggestions, remarks or anything else. Also, if you can use this, i&#8217;d be happy to hear from you.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=628&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_628" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/01/11/creating-a-csrf-protection-with-spring-3-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>oEmbedding twitter updates with Java and WordPress</title>
		<link>http://info.michael-simons.eu/2011/12/20/oembedding-twitter-updates-with-java-and-wordpress/</link>
		<comments>http://info.michael-simons.eu/2011/12/20/oembedding-twitter-updates-with-java-and-wordpress/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 11:48:34 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[oEmbed]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=579</guid>
		<description><![CDATA[I&#8217;m really a big fan of oEmbed. My project Daily Fratze acts as oEmbed provider and consumer for example. Now I&#8217;m really happy that twitter announced that it now acts as an oembed provider: Introducing the statuses/oembed endpoint: dev.twitter.com/docs/api/1/get… ^TS &#8212; Twitter API (@twitterapi) Dezember 8, 2011 (I&#8217;d even be happier if twitter would autodiscover [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really a big fan of <a href="http://www.oembed.com/">oEmbed</a>. My project <a href="http://dailyfratze.de">Daily Fratze</a> acts as oEmbed provider and consumer for example. </p>
<p>Now I&#8217;m really happy that <a href="http://twitter.com">twitter</a> announced that it now acts as an oembed provider:</p>
<blockquote class="twitter-tweet" width="500" lang="de"><p>Introducing the statuses/oembed endpoint: <a href="http://t.co/vQGXtst9" title="https://dev.twitter.com/docs/api/1/get/statuses/oembed">dev.twitter.com/docs/api/1/get…</a> ^TS</p>
<p>&mdash; Twitter API (@twitterapi) <a href="https://twitter.com/twitterapi/status/144840776101273600" data-datetime="2011-12-08T18:08:26+00:00">Dezember 8, 2011</a></p></blockquote>
<p><script src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<p>(I&#8217;d even be happier if twitter would autodiscover providers <img src='http://info.michael-simons.eu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
<p>To use this in a <strong>Java</strong> based application you can use my <a href="https://github.com/michael-simons/java-oembed">java-oembed</a> lib with the following configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Oembed oembed <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OembedBuilder<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">httpClient</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">withCacheManager</span><span style="color: #009900;">&#40;</span>cacheManager<span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">withBaseUri</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://yourproject&quot;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">withConsumer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yourproject&quot;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">withProviders</span><span style="color: #009900;">&#40;</span>					
			<span style="color: #000000; font-weight: bold;">new</span> OembedProviderBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">withName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twitter&quot;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">withFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;json&quot;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">withMaxWidth</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">480</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">withEndpoint</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://api.twitter.com/1/statuses/oembed.%{format}&quot;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">withUrlSchemes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https?://twitter.com/#!/[a-z0-9_]{1,20}/status/<span style="color: #000099; font-weight: bold;">\\</span>d+&quot;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	 <span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">withHandlers</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CommonHandler<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twitter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The handler looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringEscapeUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jsoup.nodes.Document</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jsoup.nodes.Element</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ac.simons.oembed.OembedResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ac.simons.oembed.OembedResponseHandler</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CommonHandler <span style="color: #000000; font-weight: bold;">implements</span> OembedResponseHandler <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> handlerFor<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> CommonHandler<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> handlerFor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">handlerFor</span> <span style="color: #339933;">=</span> handlerFor<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> handlerFor<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handle<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> document, <span style="color: #003399;">Element</span> a, OembedResponse response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> StringBuilder hlp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> StringEscapeUtils.<span style="color: #006633;">escapeHtml</span><span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;video&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> response.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rich&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;span style=<span style="color: #000099; font-weight: bold;">\&quot;</span>display:block; text-align:center;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getHtml</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;photo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;span style=<span style="color: #000099; font-weight: bold;">\&quot;</span>display:block; text-align:center;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span> alt=<span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span> title=<span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span> style=<span style="color: #000099; font-weight: bold;">\&quot;</span>width: %d; height: %d;<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span>, response.<span style="color: #006633;">getUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, title, title, response.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, response.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
			hlp.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		a.<span style="color: #006633;">before</span><span style="color: #009900;">&#40;</span>hlp.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		a.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Be careful to get the latest release, twitter has some real large values for cache ages and i needed to update a member from int to long.</p>
<p>To have <strong>WordPress</strong> automatically embed statusupdates, add the following line to the &#8220;functions.php&#8221; of your current theme. Create the file if it isn&#8217;t available in the root folder of your theme:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">wp_oembed_add_provider<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#https?://twitter.com/\#!/[a-z0-9_]{1,20}/status/\d+#i'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'https://api.twitter.com/1/statuses/oembed.json'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Whenever you add the plain link (whithout an anchor tag) to a statusupdate on a single line in a post, it will be embedded like the example above.</p>
<p><strong>Update</strong></p>
<p>If you are more into plugins, just download my <a href="/wp-content/uploads/2011/12/wp_enable_twitter_oembed.zip">Enable Twitter oEmbed WordPress plugin</a>, install it and you&#8217;re good to go.</p>
<p>An alternative to oEmbed for Twitter was the <a href="http://wordpress.org/extend/plugins/twitter-blackbird-pie/">Twitter Blackbird Pie</a> Plugin for WordPress, but why adding more stuff if everything else is already there? My plugin is much more lightweight.</p>
<p>Embedding <a href="https://dailyfratze.de/michael">me</a> looks like this, by the way:</p>
<p><a href="https://dailyfratze.de/michael"><img src="https://dailyfratze.de/michael.jpg?size=m" alt="michael | DailyFratze.de ...täglich frisch!" width="480" height="360" /></a></p>
<p>the picture will always show my latest update on daily fratze.</p>
<p>As this is probably the last post here for this year, i wish every visitor some nice christmas holidays!</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=579&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_579" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/12/20/oembedding-twitter-updates-with-java-and-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java stuff</title>
		<link>http://info.michael-simons.eu/2011/11/28/java-stuff/</link>
		<comments>http://info.michael-simons.eu/2011/11/28/java-stuff/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 09:32:00 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Akismet]]></category>
		<category><![CDATA[Autolinker]]></category>
		<category><![CDATA[oEmbed]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=561</guid>
		<description><![CDATA[Here is some Java stuff I&#8217;ve written over the last year for Daily Fratze. All of the stuff is in use on Daily Fratze since June 2011. java-akismet java-akismet is a simple client for Akismet based on the latest version of Apache HttpComponents. java-oembed I really like the idea of oEmbed: A mechanism for auto [...]]]></description>
			<content:encoded><![CDATA[<p>Here is some Java stuff I&#8217;ve written over the last year for <a href="http://dailyfratze.de">Daily Fratze</a>. All of the stuff is in use on <em>Daily Fratze</em> since June 2011.</p>
<dl>
<dt><a href="https://github.com/michael-simons/java-akismet">java-akismet</a></dt>
<dd>java-akismet is a simple client for <a href="http://akismet.com/">Akismet</a> based on the latest version of <a href="http://hc.apache.org/">Apache HttpComponents</a>.</dd>
<dt><a href="https://github.com/michael-simons/java-oembed">java-oembed</a></dt>
<dd>I really like the idea of <a href="http://oembed.com/">oEmbed</a>: A mechanism for auto embedding stuff from other sites so that users don&#8217;t have to paste some html code into a textbox but just plain links. This is my version of a configurable Java client that can autodetect oEmbed endpoints as well as statically configured endpoints.</dd>
<dt><a href="https://github.com/michael-simons/java-autolinker">java-autolinker</a></dt>
<dd>This is my idea of an autolinker based on <a href="http://jsoup.org/">jsoup</a>. If you want to get autolinking right, you have to parse the text. Just scanning for regex that matches urls or email addresses is not enough. This autolinker first parses the text into a DOM tree and passes all text nodes to the configured linkers. At the moment it supports URLs, email addresses and twitter handles.</dd>
</dl>
<p>I&#8217;d be happy if someone can actually use this stuff too or even contribute to it <img src='http://info.michael-simons.eu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=561&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_561" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/11/28/java-stuff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing hibernate &#8220;Cannot release connection&#8221; exception using DBCP and MySQL.</title>
		<link>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/</link>
		<comments>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 08:04:34 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[DBCP]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=550</guid>
		<description><![CDATA[Every 8 hours i got a Hibernate exception &#8220;Cannot release connection&#8221; within a Java application using Hibernate, Apache DBCP on Tomcat: org.hibernate.exception.GenericJDBCException: Cannot release connection at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) .. .. Caused by: java.sql.SQLException: Already closed. Not only that the messages polluted my inbox, the exception was visible to the enduser, [...]]]></description>
			<content:encoded><![CDATA[<p>Every 8 hours i got a Hibernate exception &#8220;Cannot release connection&#8221; within a Java application using Hibernate, Apache DBCP on Tomcat:</p>
<pre>
org.hibernate.exception.GenericJDBCException: Cannot release connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    ..
    ..
Caused by: java.sql.SQLException: Already closed.
</pre>
<p>Not only that the messages polluted my inbox, the exception was visible to the enduser, resulting in a HTTP 500 error. An older <a href="http://mrather.blogspot.com/2008/09/hibernate-and-connection-pools.html">blog post</a> i found suggested dismissing DBCP and using c3p0, a solution that i&#8217;m not quite found of. At least, the post helped to reproduce the problem within my development setup. The underlying problem was indeed the MySQL wait_timeout.</p>
<p>There&#8217;s quite a long documentation on the <a href="http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html">Tomcat JDBC Connection Pool</a>. Although the Tomcat team recommends their own solution since Tomcat 7, i still wanted to go with DBCP.</p>
<p>The relevant keywords are &#8220;testOnBorrow&#8221;, &#8220;testOnReturn&#8221;, &#8220;testWhileIdle&#8221;, &#8220;validationQuery&#8221; and &#8220;timeBetweenEvictionRunsMillis&#8221;. The first 3 are boolean values. If set to true, the query given as validationQuery is executed on borrowing a connection from the pool, on returning or when idling. The first option is not an option on production use as the query is executed before <strong>each</strong> call. Although &#8220;Select 1&#8243; is probably very fast, i just don&#8217;t want to have. Also: The problem is an invalidated, idle connection so i set testWhileIdle to true. And what happened? Nothing! The problem stayed. So there is the last option timeBetweenEvictionRunsMillis which should, according to the docs, default to 5 seconds but it doesn&#8217;t. The documentation is <strong>wrong</strong>. It&#8217;s under zero, so the eviction thread that tests idle connections never run. I&#8217;ve tweeted the tomcat team, but there was no reaction. </p>
<p>So the correct configuration for a DBCP pool database source is:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Resource</span></span>
<span style="color: #009900;">	<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;javax.sql.DataSource&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">driverClassName</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxActive</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxIdle</span>=<span style="color: #ff0000;">&quot;30&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">maxWait</span>=<span style="color: #ff0000;">&quot;10000&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testOnBorrow</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testOnReturn</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">testWhileIdle</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">validationQuery</span>=<span style="color: #ff0000;">&quot;Select 1&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">timeBetweenEvictionRunsMillis</span>=<span style="color: #ff0000;">&quot;1800000&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This way the eviction thread runs every 30 minutes, testing idle connections with the query &#8220;Select 1&#8243; and removing them from the pool. The timeBetweenEvictionRunsMillis should not be to low. It should be adapted to the configured MySQL wait_timeout.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=550&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_550" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/11/21/fixing-hibernate-cannot-release-connection-exception-using-dbcp-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java and invalid SSL certificates (java-trustprovideragent)</title>
		<link>http://info.michael-simons.eu/2011/07/25/java-and-invalid-ssl-certificates-java-trustprovideragent/</link>
		<comments>http://info.michael-simons.eu/2011/07/25/java-and-invalid-ssl-certificates-java-trustprovideragent/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 09:28:13 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Certificates]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=534</guid>
		<description><![CDATA[It&#8217;s truly easy to generate a SSL certificate for example to use with tomcat (see here). This certificate is invalid as it is self-signed by you and it often doesn&#8217;t match the hostname. This is no problem when your access the project with a browser, with more or less jumps through hoops you accept the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s truly easy to generate a SSL certificate for example to use with tomcat (see <a href="http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html">here</a>). This certificate is invalid as it is self-signed by you and it often doesn&#8217;t match the hostname. This is no problem when your access the project with a browser, with more or less jumps through hoops you accept the development certificate and you&#8217;re done.</p>
<p>If you access the site through java itself you&#8217;ll have problem with all tools that basically use an URLConnection. You&#8217;ll end up with an exception like this:</p>
<pre>
Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
</pre>
<p>This is will hit you for example using HtmlUnit or my <a href="https://github.com/michael-simons/java-oembed">oembed client</a>. </p>
<p>It isn&#8217;t enough to import the certificate in question using <a href="http://download.oracle.com/javase/1.3/docs/tooldocs/win32/keytool.html">keytool</a> (at least, it didn&#8217;t work for me). </p>
<p>I search and i found this <a href="http://devcentral.f5.com/weblogs/joe/archive/2005/07/06/1345.aspx">post</a> titled &#8220;SSL Trust Provider for Java&#8221;. Interesting stuff. </p>
<p>This works by providing a &#8220;java.security.Provider&#8221; through the Security API accepting all certificates. Nice tip, thanks!</p>
<p>I didn&#8217;t want to change my sources though so i wrote a very little <a href="http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html">java agent</a> to instrument my development setup. I also added a &#8220;javax.net.ssl.HostnameVerifier&#8221; that accepts all host names, in case the certificates cn doesn&#8217;t match the development machines hostname. If i want my vm to trust all and everything, i just add “-javaagent:full/path/to/java-trustprovideragent-0.0.1-SNAPSHOT.jar”.</p>
<p>The code is on github <a href="https://github.com/michael-simons/java-trustprovideragent">java-trustprovideragent</a>, please feel free to use it.</p>
<p>Thanks to the original authors on <a href="http://devcentral.f5.com/">devcentral.f5.com</a>.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=534&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_534" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/07/25/java-and-invalid-ssl-certificates-java-trustprovideragent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySql compatible AES encryption / decryption in Java</title>
		<link>http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes-encryption-decryption-in-java/</link>
		<comments>http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes-encryption-decryption-in-java/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 12:51:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=527</guid>
		<description><![CDATA[MySQL has an aes_encrypt/aes_decrypt pair of functions (Encryption and Compression Functions) that enable encryption and decryption of data using the official AES algorithm. The functions are easy to use (select AES_ENCRYPT(&#8216;text&#8217;,'password&#8217;)) and the result is easy to store (insert into secrets values HEX(AES_ENCRYPT(&#8216;text&#8217;,'password&#8217;))) as hex values. I used this technique for a while but i [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL has an aes_encrypt/aes_decrypt pair of functions (<a href="http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_aes-encrypt">Encryption and Compression Functions</a>) that enable encryption and decryption of data using the official AES algorithm.</p>
<p>The functions are easy to use (select AES_ENCRYPT(&#8216;text&#8217;,'password&#8217;)) and the result is easy to store (insert into secrets values HEX(AES_ENCRYPT(&#8216;text&#8217;,'password&#8217;))) as hex values.</p>
<p>I used this technique for a while but i wanted to have a more database agnostic version of this encryption and tried to build the same methods with java.</p>
<p>Although it was relatively easy to find the exact cipher mode (which is AES/ECB/PKCS5Padding), i had a real hard time figuring out how the key is calculated from the given password (the key must be 16bytes long, per default MySql uses AES-128). It turns out that the MySQL algorithm just or&#8217;s the bytes of a given passphrase against the previous bytes if the password is longer than 16 chars and just leaves them 0 when the password is shorter than 16 chars. So you can generate a secret key spec in Java for an aes_encrypt/decrypt compatible cipher like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.UnsupportedEncodingException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.crypto.Cipher</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.crypto.spec.SecretKeySpec</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.codec.binary.Hex</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SecretKeySpec generateMySQLAESKey<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> key, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> encoding<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> finalKey <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> b <span style="color: #339933;">:</span> key.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span>encoding<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				finalKey<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++%</span>16<span style="color: #009900;">&#93;</span> <span style="color: #339933;">^=</span> b<span style="color: #339933;">;</span>			
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SecretKeySpec<span style="color: #009900;">&#40;</span>finalKey, <span style="color: #0000ff;">&quot;AES&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">UnsupportedEncodingException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Decrypt</span>
		<span style="color: #000000; font-weight: bold;">final</span> Cipher decryptCipher <span style="color: #339933;">=</span> Cipher.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	        				
		decryptCipher.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span>Cipher.<span style="color: #006633;">DECRYPT_MODE</span>, generateMySQLAESKey<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your super secret passphrase&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>decryptCipher.<span style="color: #006633;">doFinal</span><span style="color: #009900;">&#40;</span>Hex.<span style="color: #006633;">decodeHex</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;56A34D7AB6225616799F6559AA388F07C2C9E53983111BDD5F49F36461AAD789&quot;</span>.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Encrypt</span>
		<span style="color: #000000; font-weight: bold;">final</span> Cipher encryptCipher <span style="color: #339933;">=</span> Cipher.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	        				
		encryptCipher.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span>Cipher.<span style="color: #006633;">ENCRYPT_MODE</span>, generateMySQLAESKey<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your super secret passphrase&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Select aes_decrypt(unhex('%s'), 'your super secret passphrase');&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>Hex.<span style="color: #006633;">encodeHex</span><span style="color: #009900;">&#40;</span>encryptCipher.<span style="color: #006633;">doFinal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hallo nach Aachen&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You need <a href="http://commons.apache.org/codec/">Commons Codec</a> to run these. </p>
<p>This isn&#8217;t probably the most secure solution from a cryptographic point of view but it just replicates the built-in MySql function for other databases or just for interoperability. I hope to save someone else time with this as i spent about days about those view lines.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=527&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_527" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes-encryption-decryption-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache httpd, Tomcat und sendfile</title>
		<link>http://info.michael-simons.eu/2011/06/28/apache-httpd-tomcat-und-sendfile/</link>
		<comments>http://info.michael-simons.eu/2011/06/28/apache-httpd-tomcat-und-sendfile/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 08:47:04 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[NIO]]></category>
		<category><![CDATA[sendfile]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=518</guid>
		<description><![CDATA[I used to use mod_xsendfile by Nils Maier, who&#8217;s Homepage doesn&#8217;t seem to exist anymore, to send files from Ruby proxied by Apache respectively powered by modrails. Those files shouldn&#8217;t be in any public www directory as authorization needs to be checked, but are accessed very often so that streaming them is not an option. [...]]]></description>
			<content:encoded><![CDATA[<p>I used to use <em>mod_xsendfile</em> by Nils Maier, who&#8217;s Homepage doesn&#8217;t seem to exist anymore, to send files from Ruby proxied by Apache respectively powered by modrails. Those files shouldn&#8217;t be in any public www directory as authorization needs to be checked, but are accessed very often so that streaming them is not an option. </p>
<p>To use this technique you need <a href="http://info.michael-simons.eu/wp-content/uploads/2011/06/mod_xsendfile.c">mod_xsendfile</a>, which is attached to this post.</p>
<p>I just have rewritten my application from Ruby on Rails to Java and it&#8217;s easy to add the necessary headers in a HttpServletResponse:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">response.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;X-Sendfile&quot;</span>, file.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
response.<span style="color: #006633;">flushBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may add other headers like Content-Type and the like but you must not modify the body, hence the flushBuffer.</p>
<p>This works quite well&#8230; As long as had my Apache httpd running with mpm-prefork.</p>
<p>Switching to Apache mpm-worker caused some problems. I cannot say with a final conclusion that mod_xsendfile was causing troubles but i started to see the wrong files (images in this case) delivered or not delivered at all.</p>
<p>My alternate solution was streaming the files using Channels from java.nio but CPU usage went nuts. </p>
<p>The solution now employed is the using Apache Tomcats asynchronous writes that are available since Tomcat 6. Their documentation is rather <a href="http://tomcat.apache.org/tomcat-6.0-doc/aio.html">short</a> but so is implementing them:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
HttpServletRequest request = ...
HttpServletResponse response = ...
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>request <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #003399;">Boolean</span>.<span style="color: #000066; font-weight: bold;">TRUE</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.apache.tomcat.sendfile.support&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
  <span style="color: #000066; font-weight: bold;">long</span> l <span style="color: #339933;">=</span> file.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.apache.tomcat.sendfile.filename&quot;</span>, absolutePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.apache.tomcat.sendfile.start&quot;</span>, 0l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.apache.tomcat.sendfile.end&quot;</span>, l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  response.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length&quot;</span>, <span style="color: #003399;">Long</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  response.<span style="color: #006633;">flushBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>use_xsendfile<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// see above</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// stream files</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What i got wrong at first was setting those attributes in the response. That didn&#8217;t work. The must be set in the request and you must take care setting all of those and with the correct type (String respectively long). And that&#8217;s it.</p>
<p>The request attribute <strong>org.apache.tomcat.sendfile.support</strong> will be true when the connector is configured to either use the <strong>APR connector</strong> (org.apache.coyote.http11.Http11AprProtocol) or the <strong>non blocking Java connector</strong> (org.apache.coyote.http11.Http11NioProtocol) (the later one being easier  to deploy as it has no external dependencies).</p>
<p>CPU usage for sending those files is now nearly 0.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=518&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_518" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/06/28/apache-httpd-tomcat-und-sendfile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.610 seconds -->

