<?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</title>
	<atom:link href="http://info.michael-simons.eu/feed/" rel="self" type="application/rss+xml" />
	<link>http://info.michael-simons.eu</link>
	<description>Just another nerd blog</description>
	<lastBuildDate>Wed, 25 Jan 2012 07:53:01 +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>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>Mac OS X Lion loses network connection after sleep</title>
		<link>http://info.michael-simons.eu/2012/01/05/mac-os-x-lion-loses-network-connection-after-sleep/</link>
		<comments>http://info.michael-simons.eu/2012/01/05/mac-os-x-lion-loses-network-connection-after-sleep/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 15:53:03 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[English posts]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Network]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=621</guid>
		<description><![CDATA[Sleep related problems have never been a problem for me with OS X (at least the sleep of the machine) since Lion. Sleep works perfectly fine but after wake my machine has no internet connection anymore, at least it looks like this. It&#8217;s only domains that aren&#8217;t resolved anymore. It also affects wired and wifi [...]]]></description>
			<content:encoded><![CDATA[<p>Sleep related problems have never been a problem for me with OS X (at least the sleep of the machine) since Lion.</p>
<p>Sleep works perfectly fine but after wake my machine has no internet connection anymore, at least it looks like this. It&#8217;s only domains that aren&#8217;t resolved anymore. It also affects wired and wifi networks. There are a lot of threads in the Apple forums that blame USB drives, Optical drives and the like but this sounds a bit like voodoo.</p>
<p>I found the following working solution:</p>
<p>Edit &#8220;/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist&#8221; 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;?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: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Label<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.apple.mDNSResponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OnDemand<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;false</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UserName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>_mdnsresponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GroupName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>_mdnsresponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ProgramArguments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/sbin/mDNSResponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-launchd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-AlwaysAppendSearchDomains<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-DisableSleepProxyClient<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>		
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MachServices<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.apple.mDNSResponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sockets<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Listeners<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SockFamily<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Unix<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SockPathName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/var/run/mDNSResponder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SockPathMode<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;integer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>438<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/integer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>EnableTransactions<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and relaunch the mDNSResponder like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl unload <span style="color: #660033;">-w</span> <span style="color: #000000; font-weight: bold;">/</span>System<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>com.apple.mDNSResponder.plist 
<span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl load <span style="color: #660033;">-w</span> <span style="color: #000000; font-weight: bold;">/</span>System<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>com.apple.mDNSResponder.plist</pre></div></div>

<p>This fixes to things: The domain resolution described <a href="http://www.makingitscale.com/2011/fix-for-broken-search-domain-resolution-in-osx-lion.html">here</a> and the 2 hourly automatic wake from sleep described <a href="http://hints.macworld.com/article.php?story=20100401103451497">here</a>.</p>
<p><ins>Update:</ins></p>
<p>It seems that fixed the problem just for that one time. To fix it every wake i use &#8220;SleepWatcher&#8221; by <a href="http://www.bernhard-baehr.de/" title="Bernhard Bähr"></a>. Installed as said in the read me, save this file somewhere as restart_mDNSResponder</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #c20cb9; font-weight: bold;">killall</span> <span style="color: #660033;">-HUP</span> mDNSResponder</pre></div></div>

<p>and copy this</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;</span>?xml <span style="color: #007800;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #007800;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span>?<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;!</span>DOCTYPE plist PUBLIC <span style="color: #ff0000;">&quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>plist <span style="color: #007800;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>dict<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>Label<span style="color: #000000; font-weight: bold;">&lt;/</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>de.bernhard-baehr.sleepwatcher<span style="color: #000000; font-weight: bold;">&lt;/</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>ProgramArguments<span style="color: #000000; font-weight: bold;">&lt;/</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>array<span style="color: #000000; font-weight: bold;">&gt;</span>
                <span style="color: #000000; font-weight: bold;">&lt;</span>string<span style="color: #000000; font-weight: bold;">&gt;/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>sleepwatcher<span style="color: #000000; font-weight: bold;">&lt;/</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>
                <span style="color: #000000; font-weight: bold;">&lt;</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>-V<span style="color: #000000; font-weight: bold;">&lt;/</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>
                <span style="color: #000000; font-weight: bold;">&lt;</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>-w <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>restart_mDNSResponder<span style="color: #000000; font-weight: bold;">&lt;/</span>string<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;/</span>array<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>RunAtLoad<span style="color: #000000; font-weight: bold;">&lt;/</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>true<span style="color: #000000; font-weight: bold;">/&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>KeepAlive<span style="color: #000000; font-weight: bold;">&lt;/</span>key<span style="color: #000000; font-weight: bold;">&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;</span>true<span style="color: #000000; font-weight: bold;">/&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>dict<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>plist<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>to /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher-20compatibility.plist. </p>
<p>and execute</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> launchctl load <span style="color: #660033;">-w</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>LaunchDaemons<span style="color: #000000; font-weight: bold;">/</span>de.bernhard-baehr.sleepwatcher-20compatibility.plist</pre></div></div>

<p>This did the trick for me.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=621&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_621" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2012/01/05/mac-os-x-lion-loses-network-connection-after-sleep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git snippets</title>
		<link>http://info.michael-simons.eu/2011/12/22/git-snippets/</link>
		<comments>http://info.michael-simons.eu/2011/12/22/git-snippets/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 20:11:36 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Shortcuts]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Tipps]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=614</guid>
		<description><![CDATA[These are a view things that i had looked up to solve some problems and i plan to update this post regularly… To push a new branch to remote git push origin new_branch To delete a remote branch git push origin :new_branch To push new tags git push --tags origin To delete a remote tag [...]]]></description>
			<content:encoded><![CDATA[<p><em>These are a view things that i had looked up to solve some problems and i plan to update this post regularly…</em></p>
<p>To push a new branch to remote</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> push origin new_branch</pre></div></div>

<p>To delete a remote branch</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> push origin :new_branch</pre></div></div>

<p>To push new tags</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> push <span style="color: #660033;">--tags</span> origin</pre></div></div>

<p>To delete a remote tag</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> tag <span style="color: #660033;">-d</span> <span style="color: #000000;">12345</span>
<span style="color: #c20cb9; font-weight: bold;">git</span> push origin :refs<span style="color: #000000; font-weight: bold;">/</span>tags<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">12345</span></pre></div></div>

<p>To reset a local branch to exactly match a remote branch</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> fetch origin
<span style="color: #c20cb9; font-weight: bold;">git</span> reset <span style="color: #660033;">--hard</span> origin<span style="color: #000000; font-weight: bold;">/</span>master</pre></div></div>

<p>To abort a rebase</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> rebase <span style="color: #660033;">--abort</span></pre></div></div>

<p>Changing the origin of your git repository (relocate the repository)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> config remote.origin.url <span style="color: #7a0874; font-weight: bold;">&#91;</span>new origin url<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p><em>Last update: 2012/01/04</em></p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=614&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_614" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/12/22/git-snippets/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>Scripted network mounts with Windows</title>
		<link>http://info.michael-simons.eu/2011/12/12/scripted-network-mounts-with-windows/</link>
		<comments>http://info.michael-simons.eu/2011/12/12/scripted-network-mounts-with-windows/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 09:05:23 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Shortcuts]]></category>
		<category><![CDATA[Mount]]></category>
		<category><![CDATA[Network Share]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=569</guid>
		<description><![CDATA[I have some services and scheduled tasks that call a Batch file under windows. The scheduled tasks cannot access network drives that the assigned user has defined, but luckily there is a &#8220;mount&#8221; pendant within Windows as well. To mount a network share within the Batch script use: net use t: \\server\share /persistent:no The authentication [...]]]></description>
			<content:encoded><![CDATA[<p>I have some services and scheduled tasks that call a Batch file under windows. The scheduled tasks cannot access network drives that the assigned user has defined, but luckily there is a &#8220;mount&#8221; pendant within Windows as well.</p>
<p>To mount a network share within the Batch script use:</p>
<pre>
net use t: \\server\share /persistent:no
</pre>
<p>The authentication is taken from the user that is assigned to the task.</p>
<p>If this not enough use</p>
<pre>
net use t: \\server\share /persistent:no /user:user@domain password
</pre>
<p>To unmount the share use</p>
<pre>
net use t: /delete
</pre>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=569&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_569" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/12/12/scripted-network-mounts-with-windows/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>Old and tired?</title>
		<link>http://info.michael-simons.eu/2011/11/09/old-and-tired/</link>
		<comments>http://info.michael-simons.eu/2011/11/09/old-and-tired/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 07:19:45 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[English posts]]></category>
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://info.michael-simons.eu/?p=553</guid>
		<description><![CDATA[Right now i&#8217;m at the #jaxcon in Munich. While sitting in a talk, this tweet by @cuchulin caught my attention: #wjax// #jaxcon People seems like java itself: They are getting older and look somehow tired. &#8212; erhard karger (@cuchulin) November 8, 2011 I really don&#8217;t think that&#8217;s Java who&#8217;s old and tired, neither as a [...]]]></description>
			<content:encoded><![CDATA[<p>Right now i&#8217;m at the #jaxcon in Munich. While sitting in a talk, this tweet by @cuchulin caught my attention:</p>
<blockquote class="twitter-tweet" width="500" lang="de"><p><a href="https://twitter.com/search/%2523wjax">#wjax</a>// <a href="https://twitter.com/search/%2523jaxcon">#jaxcon</a> People seems like java itself: They are getting older and look somehow tired.</p>
<p>&mdash; erhard karger (@cuchulin) <a href="https://twitter.com/cuchulin/status/134002280646184960" data-datetime="2011-11-08T20:20:08+00:00">November 8, 2011</a></p></blockquote>
<p><script src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<p>I really don&#8217;t think that&#8217;s Java who&#8217;s old and tired, neither as a language nor an ecosystem. It&#8217;s the people, who not only are looking tired but who are. Well, at least, I am.</p>
<p>I&#8217;m a curious person and always eager to learn new stuff but the last 3, 4 years the learning process and the stream of new tools, techniques, frameworks and other stuff accelerated even more than it did in the transition from client/server development to multi-tier-architectures 10 years ago. </p>
<p>At each conference there are new tools. New tools to &#8220;get things done&#8221;, new paradigms to organize your work and new tools to create stuff. It&#8217;s getting real hard to choose and if you want to get your head around the full stack, you&#8217;ve plenty of stuff to try, read and experiment on.</p>
<p>The last 2 years saw the coming of so many new languages. Some will vanish, some will stay, but it&#8217;s for sure: Another field of learning.</p>
<p>Given that situation plus the stream of information we get the whole day now (tweets, g+, facebook and more) it&#8217;s a tiring situation. Most of the people in the talks sit in front of their laptops or have an iPad and are constantly checking twitter and the like. Who wouldn&#8217;t be tired?</p>
<p>I really appreciated Adam Beans Talk &#8220;Just developed&#8221; yesterday. We really need to focus more on the things we want to create and to some extend less on the tools and methodologies.</p>
<p>On a personal note: I am tired. Not because of my &#8220;hard&#8221; family life with a 2-year-old, absolutely no. I just can&#8217;t really turn of my head anymore. The boundaries between work, home and spare time are diminishing. Most of the time it&#8217;s a good thing because i can spend more time at home with my family but on the other hand i have all the stuff to think about, to read and to try out often for the late evening and then they are to stay in my head.</p>
<p class="akst_link"><a href="http://info.michael-simons.eu/?p=553&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_553" class="akst_share_link " rel="nofollow">Share This</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://info.michael-simons.eu/2011/11/09/old-and-tired/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

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

