<?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>Wet Feet - Online Marketing and Technology Blog</title>
	<atom:link href="http://www.wetfeetblog.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wetfeetblog.com</link>
	<description></description>
	<lastBuildDate>Tue, 21 Feb 2012 18:36:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to build any Flex project with Ant the right way</title>
		<link>http://www.wetfeetblog.com/flex-project-build-ant-rsl/443</link>
		<comments>http://www.wetfeetblog.com/flex-project-build-ant-rsl/443#comments</comments>
		<pubDate>Tue, 21 Feb 2012 18:36:10 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=443</guid>
		<description><![CDATA[Objective To build flex application with ant without depending on FlexBuilder&#8482; and to have statically linked and dynamically loaded libraries from separate flex projects. Adobe calls them &#8220;Component Libraries&#8221; and &#8220;Runtim Shared Libraries&#8221;. The Problem Adobe Flex ecosystem is in dark ages of automation and enterprise tools. Their flagship product in automation department is an [...]<p><a href="http://www.wetfeetblog.com/flex-project-build-ant-rsl/443">How to build any Flex project with Ant the right way</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2012/02/half_built.jpg" alt="" title="half_built" class="float-right" width="200" /></p>
<h3>Objective</h3>
<p>To build flex application with ant without depending on FlexBuilder&#8482; and to have statically linked and dynamically loaded libraries from separate flex projects. Adobe calls them &#8220;Component Libraries&#8221; and &#8220;Runtim Shared Libraries&#8221;.</p>
<h3>The Problem</h3>
<p>Adobe Flex ecosystem is in dark ages of automation and enterprise tools. Their flagship product in automation department is an ant task written by amateur intern. They don&#8217;t even bother to escape file paths when the call to mxml compiler are made from this ant task wrapper &#8230;(Ask me how I know). So the ant tasks do not work and the documentation for the mxmlc and compc are confusing and cover 50% of the features. But enough about the bad, lets get on to the solution.<br />
<span id="more-443"></span><br />
I needed to create a pretty sophisticated app: We have a web application that talks JSON to a bunch of REST services. This app depends on some already prebuilt components that come in form of a library. This app also has to function as a stand alone demo application with no internet connection. For this we have split out the rest calls into 2 separate projects &#8211; the API (interface definitions) and demo implementation. if a flag is passed in via query parameter to the application it loads demo data in form of exampleDemData RSL.</p>
<h3>The Solution</h3>
<p>To build all this on maven I needed to create ant build script to produce the main app, html wrapper and demo data<br />
 RSL (runtime shared library). Here is how we do it. The project structure is set up like this:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;">\flexBuilds &nbsp; &nbsp; &nbsp;... ( where this build file resides )
\common-flex45 &nbsp; ... ( common components - component library )
\exampleApi &nbsp; &nbsp; &nbsp;... ( backend Access API - component library )
\exampleDemoData ... ( demo implementation of the API &nbsp;- RSL )
\example &nbsp; &nbsp; &nbsp; &nbsp; ... ( main Application )</div>
</div>
</pre>
<p>For building we will be calling mxmlc and compc compilers ourselves instead of going through the adobe provided ant task wrappers, since they do not expose all the features and do not work if you path has a single space in it.<br />
And now the build script:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;project</span> <span class="re0">name</span>=<span class="st0">&quot;Example Flex Build&quot;</span> <span class="re0">default</span>=<span class="st0">&quot;buildAll&quot;</span> <span class="re0">basedir</span>=<span class="st0">&quot;../&quot;</span><span class="re2">&gt;</span></span>

&nbsp; <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; Example Flex Build script.
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span>

&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;artifacts.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${basedir}/flexBuilds/atrifact&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;flex.sdk.home&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;/...../sdks/4.5.1&quot;</span> <span class="re2">/&gt;</span></span>


&nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;init&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mkdir</span> <span class="re0">dir</span>=<span class="st0">&quot;${artifacts.dir}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;delete</span> <span class="re0">includeemptydirs</span>=<span class="st0">&quot;true&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">dir</span>=<span class="st0">&quot;${artifacts.dir}&quot;</span> <span class="re0">includes</span>=<span class="st0">&quot;**/*&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/delete<span class="re2">&gt;</span></span></span> &nbsp;
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span>

&nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;buildAll&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;init&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;compile-library&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;output.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${artifacts.dir}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;project.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;common-flex45&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;extra.stuff&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/antcall<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;compile-library&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;output.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${artifacts.dir}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;project.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;exampleApi&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;extra.stuff&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/antcall<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;compile-library&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;output.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${artifacts.dir}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;project.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;exampleDemoData&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;extra.stuff&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;-directory=true&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/antcall<span class="re2">&gt;</span></span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;compile-webApp&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;app.name&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;ExampleApplication&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;app.file&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">value</span>=<span class="st0">&quot;example/src/com/wetfeetblog/example/ExampleApplication.mxml&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;app.title&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;Example Application&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;src.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;example&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;bin.dir&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;app&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param</span> <span class="re0">name</span>=<span class="st0">&quot;extra.stuff&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">value</span>=<span class="st0">&quot;-runtime-shared-library-path=${artifacts.dir}/exampleDemoData.swc,exampleDemoData.swf -verify-digests=false&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/antcall<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;copy</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/exampleDemoData.swc/library.swf&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">toFile</span>=<span class="st0">&quot;${artifacts.dir}/app/exampleDemoData.swf&quot;</span><span class="re2">/&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;All Flex Apps Compiled!&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span>

&nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;compile-library&quot;</span> <span class="re2">&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">id</span>=<span class="st0">&quot;sources&quot;</span> <span class="re0">dir</span>=<span class="st0">&quot;${basedir}/${project.dir}/src&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/*.mxml&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/*.as&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fileset<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;pathconvert</span> <span class="re0">property</span>=<span class="st0">&quot;classes&quot;</span> <span class="re0">pathsep</span>=<span class="st0">&quot; &quot;</span> <span class="re0">refid</span>=<span class="st0">&quot;sources&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;compositemapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;globmapper</span> <span class="re0">from</span>=<span class="st0">&quot;${basedir}/${project.dir}/src/*&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">to</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">handledirsep</span>=<span class="st0">&quot;true&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;mapper</span> <span class="re0">type</span>=<span class="st0">&quot;package&quot;</span> <span class="re0">from</span>=<span class="st0">&quot;*.as&quot;</span> <span class="re0">to</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;globmapper</span> <span class="re0">from</span>=<span class="st0">&quot;${basedir}/${project.dir}/src/*&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">to</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">handledirsep</span>=<span class="st0">&quot;true&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;mapper</span> <span class="re0">type</span>=<span class="st0">&quot;package&quot;</span> <span class="re0">from</span>=<span class="st0">&quot;*.mxml&quot;</span> <span class="re0">to</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/compositemapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/pathconvert<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;exec</span> <span class="re0">executable</span>=<span class="st0">&quot;${flex.sdk.home}/bin/compc&quot;</span> <span class="re0">outputproperty</span>=<span class="st0">&quot;compile.output&quot;</span> <span class="re0">dir</span>=<span class="st0">&quot;${basedir}&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;arg</span> <span class="re0">line</span>=<span class="st0">&quot;-debug=false -output '${output.dir}/${project.dir}.swc' ${extra.stuff} -library-path+=${project.dir}/libs -library-path+=${artifacts.dir} -source-path ${project.dir}/src/ -include-classes ${classes} &quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/exec<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;${compile.output}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span>

&nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;compile-webApp&quot;</span> <span class="re2">&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;Compiling ${app.name}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;exec</span> <span class="re0">executable</span>=<span class="st0">&quot;${flex.sdk.home}/bin/mxmlc&quot;</span> <span class="re0">outputproperty</span>=<span class="st0">&quot;compile.output&quot;</span> <span class="re0">dir</span>=<span class="st0">&quot;${basedir}&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;arg</span> <span class="re0">line</span>=<span class="st0">&quot;-debug=false -show-actionscript-warnings=false -output '${artifacts.dir}/${bin.dir}/${app.name}.swf' -library-path+='${artifacts.dir}' -library-path+=${src.dir}/libs ${extra.stuff} -source-path ${src.dir}/src/ -- &nbsp;${app.file}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/exec<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;${compile.output}&quot;</span> <span class="re2">/&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;copy</span> <span class="re0">file</span>=<span class="st0">&quot;${basedir}/${src.dir}/html-template/swfobject.js&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">todir</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;copy</span> <span class="re0">file</span>=<span class="st0">&quot;${basedir}/${src.dir}/html-template/playerProductInstall.swf&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">todir</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;copy</span> <span class="re0">file</span>=<span class="st0">&quot;${basedir}/${src.dir}/html-template/index.template.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">tofile</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;${app.title}&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${title}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;#ffffff&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${bgcolor}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;${app.name}&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${application}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;${app.name}&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${swf}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;100%&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${width}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;100%&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${height}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;10&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${version_major}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;0&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${version_minor}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;replace</span> <span class="re0">file</span>=<span class="st0">&quot;${artifacts.dir}/${bin.dir}/index.html&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">value</span>=<span class="st0">&quot;0&quot;</span> <span class="re0">token</span>=<span class="st0">&quot;${version_revision}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span>


&nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;deploy-content&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;Deploying ${deploy.local.location} to ${deploy.host} &quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;sshexec</span> <span class="re0">host</span>=<span class="st0">&quot;${deploy.host}&quot;</span> <span class="re0">username</span>=<span class="st0">&quot;${deploy.user}&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">password</span>=<span class="st0">&quot;${deploy.password}&quot;</span> <span class="re0">command</span>=<span class="st0">&quot;rm ${deploy.remote.location}/*&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">trust</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">failonerror</span>=<span class="st0">&quot;false&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;scp</span> <span class="re0">todir</span>=<span class="st0">&quot;${deploy.user}:${deploy.password}@${deploy.host}:${deploy.remote.location}/&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">verbose</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">trust</span>=<span class="st0">&quot;true&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">dir</span>=<span class="st0">&quot;${deploy.local.location}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/scp<span class="re2">&gt;</span></span></span> &nbsp; &nbsp; &nbsp;
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span>
&nbsp; 
<span class="sc3"><span class="re1">&lt;/project<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>Lets examine all the little tricks one by one. We will start with &#8220;compile-library&#8221;  target. It has multiple gotchas. For start compc is dumb enough not to recognize what needs to be compiled in, it needs a full list of classes fed into it to produce a library. But we do not want to list all the classes by hand, so when we add one more we will have to modify our build script. Lets get on with some ant magic.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;compile-library&quot;</span> <span class="re2">&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">id</span>=<span class="st0">&quot;sources&quot;</span> <span class="re0">dir</span>=<span class="st0">&quot;${basedir}/${project.dir}/src&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/*.mxml&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/*.as&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fileset<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;pathconvert</span> <span class="re0">property</span>=<span class="st0">&quot;classes&quot;</span> <span class="re0">pathsep</span>=<span class="st0">&quot; &quot;</span> <span class="re0">refid</span>=<span class="st0">&quot;sources&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;compositemapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;globmapper</span> <span class="re0">from</span>=<span class="st0">&quot;${basedir}/${project.dir}/src/*&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">to</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">handledirsep</span>=<span class="st0">&quot;true&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;mapper</span> <span class="re0">type</span>=<span class="st0">&quot;package&quot;</span> <span class="re0">from</span>=<span class="st0">&quot;*.as&quot;</span> <span class="re0">to</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;globmapper</span> <span class="re0">from</span>=<span class="st0">&quot;${basedir}/${project.dir}/src/*&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">to</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">handledirsep</span>=<span class="st0">&quot;true&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;mapper</span> <span class="re0">type</span>=<span class="st0">&quot;package&quot;</span> <span class="re0">from</span>=<span class="st0">&quot;*.mxml&quot;</span> <span class="re0">to</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/chainedmapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/compositemapper<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/pathconvert<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;exec</span> <span class="re0">executable</span>=<span class="st0">&quot;${flex.sdk.home}/bin/compc&quot;</span> <span class="re0">outputproperty</span>=<span class="st0">&quot;compile.output&quot;</span> <span class="re0">dir</span>=<span class="st0">&quot;${basedir}&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;arg</span> <span class="re0">line</span>=<span class="st0">&quot;-debug=false -output '${output.dir}/${project.dir}.swc' ${extra.stuff} -library-path+=${project.dir}/libs -library-path+=${artifacts.dir} -source-path ${project.dir}/src/ -include-classes ${classes} &quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/exec<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;echo</span> <span class="re0">message</span>=<span class="st0">&quot;${compile.output}&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>For compiling the main app the mxmlc compiler a little bit more sophisticated, so we can show it the main app mxml file and it figures out what needs to be included in the application. But the html wrapper we need to handle by hand, since mxmlc has no capabilities to do so. Luckily there is no code, just some placeholders needs to be replaced with real values. Ant can handle that with no problem.</p>
<p>No the tricky bits come into play when we want our exampleDemoData project to become the RSL. for that we need to specify that we wan the exploded output format, so we add &#8220;-directory=true&#8221; flag when compiling the library. In the main application we need to add a flag to use this swc as RSL. The flag to do so is &#8220;-runtime-shared-library-path=${artifacts.dir}/exampleDemoData.swc,exampleDemoData.swf&#8221;. If you want to rebuild the exampleDemoDAta project wihtout rebuilding the main application we need to add &#8220;-verify-digests=false&#8221; to the main app also, so the rsl version digest is not checked.</p>
<p>And that is it in a nutshel. You may find one more handy target for deploying your freshly built application on a unix based host via scp in this build script.</p>
<p><a href="http://www.wetfeetblog.com/flex-project-build-ant-rsl/443">How to build any Flex project with Ant the right way</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/flex-project-build-ant-rsl/443/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Servlet Filer to Log Request and Response Payload</title>
		<link>http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431</link>
		<comments>http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431#comments</comments>
		<pubDate>Tue, 04 Oct 2011 17:19:23 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[servlet filter]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=431</guid>
		<description><![CDATA[Objective To have one self containing class that can be plugged into any servlet container. Class will log everything about the request and response, including the payload data coming in and going out. The Problem I need to dump out the payload for posts, gets and responses to troubleshoot some issues where data got mismatched [...]<p><a href="http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431">Servlet Filer to Log Request and Response Payload</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2011/10/filter.jpg" alt="filter" title="filter" class="float-right" /></p>
<h3>Objective</h3>
<p>To have one self containing class that can be plugged into any servlet container. Class will log everything about the request and response, including the payload data coming in and going out.</p>
<h3>The Problem</h3>
<p>I need to dump out the payload for posts, gets and responses to troubleshoot some issues where data got mismatched between the request and response. Ideal solution would have all this data on one single line. Quick look around gave no off the shelf results, so I put together a quick Filter implementation to capture request and response data.</p>
<p><span id="more-431"></span><br />
The biggest challenge is to get the copies of request and response payload for logging and still eave it in tact for servlet processing. I had the write wrappers for input and output streams, as you will see in the inner classes. Here is the code:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.your.package.name</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.BufferedReader</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.ByteArrayInputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.ByteArrayOutputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.IOException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.InputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.InputStreamReader</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.OutputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.io.PrintWriter</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.Enumeration</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.HashMap</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.Locale</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.Map</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.Filter</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.FilterChain</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.FilterConfig</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletInputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletOutputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletResponse</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.Cookie</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequestWrapper</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletResponse</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.apache.commons.io.output.TeeOutputStream</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.apache.log4j.Logger</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> ReqRespDumpFilter <span class="kw1">implements</span> Filter <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span>ReqRespDumpFilter.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> init<span class="br0">&#40;</span>FilterConfig filterConfig<span class="br0">&#41;</span> <span class="kw1">throws</span> ServletException <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> doFilter<span class="br0">&#40;</span>ServletRequest request, ServletResponse response,FilterChain chain<span class="br0">&#41;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a>, ServletException <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest httpServletRequest <span class="sy0">=</span> <span class="br0">&#40;</span>HttpServletRequest<span class="br0">&#41;</span>request<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse httpServletResponse <span class="sy0">=</span> <span class="br0">&#40;</span>HttpServletResponse<span class="br0">&#41;</span>response<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span> requestMap <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">getTypesafeRequestMap</span><span class="br0">&#40;</span>httpServletRequest<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedRequestWrapper bufferedReqest <span class="sy0">=</span> <span class="kw1">new</span> BufferedRequestWrapper<span class="br0">&#40;</span>httpServletRequest<span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedResponseWrapper bufferedResponse <span class="sy0">=</span> <span class="kw1">new</span> BufferedResponseWrapper<span class="br0">&#40;</span>httpServletResponse<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">final</span> StringBuilder logMessage <span class="sy0">=</span> <span class="kw1">new</span> StringBuilder<span class="br0">&#40;</span><span class="st0">&quot;REST Request - &quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;[HTTP METHOD:&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span>httpServletRequest.<span class="me1">getMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;] [PATH INFO:&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span>httpServletRequest.<span class="me1">getPathInfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;] [REQUEST PARAMETERS:&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span>requestMap<span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;] [REQUEST BODY:&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span>bufferedReqest.<span class="me1">getRequestBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;] [REMOTE ADDRESS:&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span>httpServletRequest.<span class="me1">getRemoteAddr</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;]&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chain.<span class="me1">doFilter</span> <span class="br0">&#40;</span>bufferedReqest, bufferedResponse<span class="br0">&#41;</span><span class="sy0">;</span> &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logMessage.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot; [RESPONSE:&quot;</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span> bufferedResponse.<span class="me1">getContent</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;]&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>logMessage<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span><span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Throwable</span></a> a <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> Map<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span> getTypesafeRequestMap<span class="br0">&#40;</span>HttpServletRequest request<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span> typesafeRequestMap <span class="sy0">=</span> <span class="kw1">new</span> HashMap<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enumeration<span class="sy0">&lt;?&gt;</span> requestParamNames <span class="sy0">=</span> request.<span class="me1">getParameterNames</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>requestParamNames.<span class="me1">hasMoreElements</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> requestParamName <span class="sy0">=</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#41;</span>requestParamNames.<span class="me1">nextElement</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> requestParamValue <span class="sy0">=</span> request.<span class="me1">getParameter</span><span class="br0">&#40;</span>requestParamName<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; typesafeRequestMap.<span class="me1">put</span><span class="br0">&#40;</span>requestParamName, requestParamValue<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> typesafeRequestMap<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> destroy<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw1">class</span> BufferedRequestWrapper <span class="kw1">extends</span> HttpServletRequestWrapper <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayInputStream</span></a> bais <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayOutputStream</span></a> baos <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> BufferedServletInputStream bsis <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> buffer <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> BufferedRequestWrapper<span class="br0">&#40;</span>HttpServletRequest req<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">super</span><span class="br0">&#40;</span>req<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Read InputStream and store its content in a buffer.</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">InputStream</span></a> is <span class="sy0">=</span> req.<span class="me1">getInputStream</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">baos</span> <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayOutputStream</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">byte</span> buf<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw1">new</span> <span class="kw4">byte</span><span class="br0">&#91;</span>1024<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> letti<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>letti <span class="sy0">=</span> is.<span class="me1">read</span><span class="br0">&#40;</span>buf<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">baos</span>.<span class="me1">write</span><span class="br0">&#40;</span>buf, 0, letti<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">buffer</span> <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">baos</span>.<span class="me1">toByteArray</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> ServletInputStream getInputStream<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">bais</span> <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayInputStream</span></a><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">buffer</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">bsis</span> <span class="sy0">=</span> <span class="kw1">new</span> BufferedServletInputStream<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">bais</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">bsis</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 

&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getRequestBody<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> &nbsp;<span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">BufferedReader</span></a> reader <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">BufferedReader</span></a><span class="br0">&#40;</span><span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstreamreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">InputStreamReader</span></a><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">getInputStream</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder inputBuffer <span class="sy0">=</span> <span class="kw1">new</span> StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">do</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line <span class="sy0">=</span> reader.<span class="me1">readLine</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">null</span> <span class="sy0">!=</span> line<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputBuffer.<span class="me1">append</span><span class="br0">&#40;</span>line.<span class="me1">trim</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span>line <span class="sy0">!=</span> <span class="kw2">null</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> inputBuffer.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">trim</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; <span class="br0">&#125;</span>


&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw1">class</span> BufferedServletInputStream <span class="kw1">extends</span> ServletInputStream <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayInputStream</span></a> bais<span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> BufferedServletInputStream<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayInputStream</span></a> bais<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">bais</span> <span class="sy0">=</span> bais<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">int</span> available<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">bais</span>.<span class="me1">available</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">int</span> read<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">bais</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">int</span> read<span class="br0">&#40;</span><span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> buf, <span class="kw4">int</span> off, <span class="kw4">int</span> len<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">bais</span>.<span class="me1">read</span><span class="br0">&#40;</span>buf, off, len<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp;

&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">class</span> TeeServletOutputStream <span class="kw1">extends</span> ServletOutputStream <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">final</span> TeeOutputStream targetStream<span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> TeeServletOutputStream<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">OutputStream</span></a> one, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">OutputStream</span></a> two <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetStream <span class="sy0">=</span> <span class="kw1">new</span> TeeOutputStream<span class="br0">&#40;</span> one, two<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> write<span class="br0">&#40;</span><span class="kw4">int</span> arg0<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">targetStream</span>.<span class="me1">write</span><span class="br0">&#40;</span>arg0<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> flush<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">super</span>.<span class="me1">flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">targetStream</span>.<span class="me1">flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> close<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">super</span>.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">targetStream</span>.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; 
&nbsp; &nbsp; 
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">class</span> BufferedResponseWrapper <span class="kw1">implements</span> HttpServletResponse <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse original<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; TeeServletOutputStream tee<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayOutputStream</span></a> bos<span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> BufferedResponseWrapper<span class="br0">&#40;</span>HttpServletResponse response<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original <span class="sy0">=</span> response<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getContent<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> bos.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aprintwriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PrintWriter</span></a> getWriter<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">getWriter</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> ServletOutputStream getOutputStream<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> tee <span class="sy0">==</span> <span class="kw2">null</span> <span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bos <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ByteArrayOutputStream</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tee <span class="sy0">=</span> <span class="kw1">new</span> TeeServletOutputStream<span class="br0">&#40;</span> original.<span class="me1">getOutputStream</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, bos <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> tee<span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getCharacterEncoding<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">getCharacterEncoding</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getContentType<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">getContentType</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setCharacterEncoding<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> charset<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setCharacterEncoding</span><span class="br0">&#40;</span>charset<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setContentLength<span class="br0">&#40;</span><span class="kw4">int</span> len<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setContentLength</span><span class="br0">&#40;</span>len<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setContentType<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setContentType</span><span class="br0">&#40;</span>type<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setBufferSize<span class="br0">&#40;</span><span class="kw4">int</span> size<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setBufferSize</span><span class="br0">&#40;</span>size<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">int</span> getBufferSize<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">getBufferSize</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> flushBuffer<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tee.<span class="me1">flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> resetBuffer<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">resetBuffer</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">boolean</span> isCommitted<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">isCommitted</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> reset<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">reset</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setLocale<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Locale</span></a> loc<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setLocale</span><span class="br0">&#40;</span>loc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Locale</span></a> getLocale<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">getLocale</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> addCookie<span class="br0">&#40;</span>Cookie cookie<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">addCookie</span><span class="br0">&#40;</span>cookie<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">boolean</span> containsHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">containsHeader</span><span class="br0">&#40;</span>name<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> encodeURL<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">encodeURL</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> encodeRedirectURL<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">encodeRedirectURL</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;deprecation&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> encodeUrl<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">encodeUrl</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;deprecation&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> encodeRedirectUrl<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> original.<span class="me1">encodeRedirectUrl</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> sendError<span class="br0">&#40;</span><span class="kw4">int</span> sc, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> msg<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">sendError</span><span class="br0">&#40;</span>sc, msg<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> sendError<span class="br0">&#40;</span><span class="kw4">int</span> sc<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">sendError</span><span class="br0">&#40;</span>sc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> sendRedirect<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> location<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">sendRedirect</span><span class="br0">&#40;</span>location<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setDateHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <span class="kw4">long</span> date<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setDateHeader</span><span class="br0">&#40;</span>name, date<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> addDateHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <span class="kw4">long</span> date<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">addDateHeader</span><span class="br0">&#40;</span>name, date<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> value<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setHeader</span><span class="br0">&#40;</span>name, value<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> addHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> value<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">addHeader</span><span class="br0">&#40;</span>name, value<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setIntHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <span class="kw4">int</span> value<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setIntHeader</span><span class="br0">&#40;</span>name, value<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> addIntHeader<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name, <span class="kw4">int</span> value<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">addIntHeader</span><span class="br0">&#40;</span>name, value<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setStatus<span class="br0">&#40;</span><span class="kw4">int</span> sc<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setStatus</span><span class="br0">&#40;</span>sc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;deprecation&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setStatus<span class="br0">&#40;</span><span class="kw4">int</span> sc, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> sm<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; original.<span class="me1">setStatus</span><span class="br0">&#40;</span>sc, sm<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<h3> web.xml configuration </h3>
<p>You need to add filter declaration to your web.xml and configure filter mapping for specific url(s).</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;filter<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;filter-name<span class="re2">&gt;</span></span></span>RRDumpFilter<span class="sc3"><span class="re1">&lt;/filter-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;filter-class<span class="re2">&gt;</span></span></span>com.your.package.name.ReqRespDumpFilter<span class="sc3"><span class="re1">&lt;/filter-class<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/filter<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;filter-mapping<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;filter-name<span class="re2">&gt;</span></span></span>RRDumpFilter<span class="sc3"><span class="re1">&lt;/filter-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;url-pattern<span class="re2">&gt;</span></span></span>/*<span class="sc3"><span class="re1">&lt;/url-pattern<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/filter-mapping<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<h3>Conclusion</h3>
<p>Now you can have your pie and eat it too. I mean see your request and response payload without adding logging of it in every servlet/controller/resource/endpoint/etc. Tested with java 1.6 in Tomcat 6. Dependency on commons-io to provide TeeOutputStream  implementation and log4j, but you can substitute.</p>
<p><a href="http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431">Servlet Filer to Log Request and Response Payload</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>2011 March 6,  SCCA Atlanta Autocross @ Turner Field</title>
		<link>http://www.wetfeetblog.com/2011-march-6-scca-atlanta-autocross/397</link>
		<comments>http://www.wetfeetblog.com/2011-march-6-scca-atlanta-autocross/397#comments</comments>
		<pubDate>Tue, 08 Mar 2011 14:10:35 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Car Stuff]]></category>
		<category><![CDATA[cars]]></category>
		<category><![CDATA[racing]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=397</guid>
		<description><![CDATA[After spending 3 years off the course, I pulled out my 87 325is out of a pile of leaves. Battery was dead, but after charging it up the beast came back to life. Old tires where shot, so I went on Buford highway and got some used tires. With runing car now I was able [...]<p><a href="http://www.wetfeetblog.com/2011-march-6-scca-atlanta-autocross/397">2011 March 6,  SCCA Atlanta Autocross @ Turner Field</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>After spending 3 years off the course, I pulled out my 87 325is out of a pile of leaves. Battery was dead, but after charging it up the beast came back to life. Old tires where shot, so I went on Buford highway and got some used tires. With runing car now I was able to register for the autox event. Since my schedule limited me to morning session only, I did not spent time trying to figure out what class my BMW supposed to be in and registered tor Time Only morning session.</p>
<p><span id="more-397"></span><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_00.jpg" alt="" title="03_scca_00" width="600" height="452" class="alignnone size-full wp-image-398" /></p>
<p>Upon arrival parking lot look very very wet! And it was still raining, 40 degrees and 15mph wind gusts. Ouch! </p>
<p> I was working the course first run and driving second. (Autocross events are volunteer based, so everybody has to contribute) After two hours in the middles of a very windy wet and cold parking lot, was my turn to drive.</p>
<p> My first run was a spin-out 75.447+2. I came in too fast into very tight corner and the rear end overtook the front. Now that I think about it, my car was too tail happy, I think I had my tire pressures too high for such a cold day. Enjoy the video.</p>
<p><object width="640" height="390"><param name="movie" value="http://www.youtube.com/v/TGcZnmM6yWM?hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/TGcZnmM6yWM?hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="390"></embed></object></p>
<p> On the second run I took on a passenger, he completely blocked the camera view, so there is no video. I took it easier and finished the run in 59.276</p>
<p> Third run Turned out to be my best of the day &#8211; 57.852 Enjoy!</p>
<p><object width="640" height="390"><param name="movie" value="http://www.youtube.com/v/LhXxVbccfQU?hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/LhXxVbccfQU?hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="390"></embed></object></p>
<p>On the second and third runs something in the front suspension was banging very loudly, so on the lasts run I did not go as hard as on the Third one. Result 58.207. </p>
<p><object width="640" height="390"><param name="movie" value="http://www.youtube.com/v/HzjyUkbOkHI?hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/HzjyUkbOkHI?hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="390"></embed></object></p>
<p>Here are some pictures from the event </p>
<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_01.jpg" alt="" title="03_scca_01" width="600" height="399" class="alignnone size-full wp-image-406" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_02.jpg" alt="" title="03_scca_02" width="600" height="399" class="alignnone size-full wp-image-406" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_03.jpg" alt="" title="03_scca_03" width="600" height="399" class="alignnone size-full wp-image-408" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_04.jpg" alt="" title="03_scca_04" width="600" height="399" class="alignnone size-full wp-image-409" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_05.jpg" alt="" title="03_scca_05" width="600" height="399" class="alignnone size-full wp-image-410" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_06.jpg" alt="" title="03_scca_06" width="600" height="399" class="alignnone size-full wp-image-411" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_07.jpg" alt="" title="03_scca_07" width="600" height="399" class="alignnone size-full wp-image-412" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_08.jpg" alt="" title="03_scca_08" width="600" height="399" class="alignnone size-full wp-image-413" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_09.jpg" alt="" title="03_scca_09" width="600" height="399" class="alignnone size-full wp-image-414" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_10.jpg" alt="" title="03_scca_10" width="600" height="399" class="alignnone size-full wp-image-415" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_11.jpg" alt="" title="03_scca_11" width="600" height="399" class="alignnone size-full wp-image-416" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_12.jpg" alt="" title="03_scca_12" width="600" height="399" class="alignnone size-full wp-image-417" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_13.jpg" alt="" title="03_scca_13" width="600" height="399" class="alignnone size-full wp-image-418" /><br/><br />
<img src="http://www.wetfeetblog.com/wp-content/uploads/2011/03/03_scca_14.jpg" alt="" title="03_scca_14" width="600" height="399" class="alignnone size-full wp-image-419" /><br/><br />
<img src="http://lh3.googleusercontent.com/_EnfBz3pV1SM/TXR5KhNQSaI/AAAAAAACJqk/DzacyDarhdA/DSC03751.JPG" /><br/><br />
<img src="http://lh6.googleusercontent.com/_EnfBz3pV1SM/TXR5LgfnWdI/AAAAAAACJqo/8AZU1dUKr7Y/DSC03752.JPG" /><br/><br />
<img src="http://lh4.googleusercontent.com/_EnfBz3pV1SM/TXR5Mny5YhI/AAAAAAACJqs/Gpg2mwFOKIo/DSC03753.JPG" /><br/><br />
<img src="http://lh5.googleusercontent.com/_EnfBz3pV1SM/TXR5N2EJcvI/AAAAAAACJq0/-HCqVsHOvtA/DSC03754.JPG" /><br/><br />
<img src="http://lh5.googleusercontent.com/_EnfBz3pV1SM/TXR5O_pJ-vI/AAAAAAACJq4/Gypk6q6ue-Q/DSC03755.JPG" /><br/></p>
<p><a href="http://www.wetfeetblog.com/2011-march-6-scca-atlanta-autocross/397">2011 March 6,  SCCA Atlanta Autocross @ Turner Field</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/2011-march-6-scca-atlanta-autocross/397/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google App Engine &#8211; Full Text Search with JDO &#8211; Revisited</title>
		<link>http://www.wetfeetblog.com/google-app-engine-full-text-search-with-jdo-revisited/368</link>
		<comments>http://www.wetfeetblog.com/google-app-engine-full-text-search-with-jdo-revisited/368#comments</comments>
		<pubDate>Wed, 19 May 2010 15:08:45 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[JDO]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=368</guid>
		<description><![CDATA[Objective This article will show you how to implement a full text search in Google App Engine using JDO. I tried my hand at this couple month ago, but after watching this presentation I decided to do it properly. The Problem In my first attempt I managed to get the search working, but after watching [...]<p><a href="http://www.wetfeetblog.com/google-app-engine-full-text-search-with-jdo-revisited/368">Google App Engine &#8211; Full Text Search with JDO &#8211; Revisited</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2010/05/files_and_archives_3.jpg" alt="" title="files_and_archives_3" class="float-right" /></p>
<h3>Objective</h3>
<p>This article will show you how to implement a full text search in Google App Engine using JDO. I tried my hand at this <a href="http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287" >couple month ago</a>, but after watching <a href="http://code.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html">this presentation</a> I decided to do it properly.</p>
<h3>The Problem</h3>
<p>In my first attempt I managed to get the search working, but after watching Brett Slatkin&#8217;s presentaion I realized where the problem is. In short deserializing a list of strings (which is our search index) is a very costly operation, but he presented with a solution. Bellow you will find my solution to this problem.<br />
<span id="more-368"></span></p>
<h3>Data Model</h3>
<p>For this example we will use such data model: we have Customer (name, contact, notes) which has a list of Addresses and Phones. We need ability to find customer by name, address or phone. </p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Customer <span class="br0">&#123;</span>
&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> contactName<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> comments<span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span> addresses <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Phone<span class="sy0">&gt;</span> phones <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Phone<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>dependent<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> CustomerIndex index<span class="sy0">;</span>

&nbsp; &nbsp;<span class="co1">// getters and setter go here....</span>
<span class="br0">&#125;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Address <span class="br0">&#123;</span>
&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line1<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line2<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> city<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> state<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> zip<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp;<span class="co1">// getters and setter go here....</span>
<span class="br0">&#125;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Phone <span class="br0">&#123;</span>
&nbsp; &nbsp;@PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> phone<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp;<span class="co1">// getters and setter go here....</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>If you paid attention you notice that we have an interesting child in the Customer class called CustomerIndex. Here it is:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> CustomerIndex <span class="br0">&#123;</span>
&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Set<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> index<span class="sy0">;</span>

&nbsp; &nbsp;<span class="co1">// getters and setter go here....</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<h3>Search Approach</h3>
<p>Here is the theory of what we gonna do: Since deserializing of List properties is a very very costly operation (and we do not care abut the data it holds anyway) we move customer search index property into a Child object. We will perform a search on this Child and we will get only the keys of the child objects. This way we do not have to incur the penalty of deserializing our search index (the search happens on the index). Once we have our child object keys we will load Parent objects with those keys. We can do this because a child key is a composite key and always includes parent key.<br />
To make our search more usable we will use <a href="http://lucene.apache.org/">Lucenen</a> and SnowballAnalyzer for word stemming.<br />
Here is the method that gives us the Set of words. We use it to generate the index of searchable words as well as search phrases.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">protected</span> Set<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> getIndex<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> input, <span class="kw4">int</span> maxTokens <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; Set<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> returnSet <span class="sy0">=</span> <span class="kw1">new</span> HashSet<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; Analyzer analyzer <span class="sy0">=</span> &nbsp;<span class="kw1">new</span> SnowballAnalyzer<span class="br0">&#40;</span> org.<span class="me1">apache</span>.<span class="me1">lucene</span>.<span class="me1">util</span>.<span class="me1">Version</span>.<span class="me1">LUCENE_30</span>,<span class="st0">&quot;English&quot;</span>, stopWords<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; TokenStream tokenStream <span class="sy0">=</span> analyzer.<span class="me1">tokenStream</span><span class="br0">&#40;</span> <span class="st0">&quot;content&quot;</span>, <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">StringReader</span></a><span class="br0">&#40;</span>input<span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span> tokenStream.<span class="me1">incrementToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <span class="br0">&#40;</span>returnSet.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&lt;</span> maxTokens<span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> tokenStream.<span class="me1">hasAttribute</span><span class="br0">&#40;</span> TermAttribute.<span class="kw1">class</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; TermAttribute attr <span class="sy0">=</span> tokenStream.<span class="me1">getAttribute</span><span class="br0">&#40;</span> TermAttribute.<span class="kw1">class</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> attr.<span class="me1">term</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; returnSet.<span class="me1">add</span><span class="br0">&#40;</span> attr.<span class="me1">term</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span><span class="kw1">catch</span><span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; logger.<span class="me1">equals</span><span class="br0">&#40;</span>exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="kw1">return</span> returnSet<span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Here is our search method:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span> searchCustomers<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> search1, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> entityId <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; PersistenceManager pm <span class="sy0">=</span> PMF.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; Set<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> search <span class="sy0">=</span> getIndex<span class="br0">&#40;</span>search1, 3<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; Query query <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span><span class="st0">&quot;SELECT id FROM &quot;</span> <span class="sy0">+</span> CustomerIndex.<span class="kw1">class</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; query.<span class="me1">setFilter</span><span class="br0">&#40;</span><span class="st0">&quot;index == param0&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; query.<span class="me1">declareParameters</span><span class="br0">&#40;</span><span class="st0">&quot;String param0&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; Query query2 <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span>Customer.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; query2.<span class="me1">setFilter</span><span class="br0">&#40;</span><span class="st0">&quot;id == keyParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; query2.<span class="me1">declareParameters</span><span class="br0">&#40;</span><span class="st0">&quot;com.google.appengine.api.datastore.Key keyParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span> custs <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; List<span class="sy0">&lt;</span>Key<span class="sy0">&gt;</span> keys<span class="sy0">;</span>
&nbsp; List<span class="sy0">&lt;</span>Key<span class="sy0">&gt;</span> parents <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Key<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; keys <span class="sy0">=</span> <span class="br0">&#40;</span>List<span class="sy0">&lt;</span>Key<span class="sy0">&gt;</span><span class="br0">&#41;</span> query.<span class="me1">execute</span><span class="br0">&#40;</span> search <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> k <span class="sy0">:</span> keys<span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; parents.<span class="me1">add</span><span class="br0">&#40;</span> k.<span class="me1">getParent</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; custs <span class="sy0">=</span> <span class="br0">&#40;</span>List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span><span class="br0">&#41;</span> query2.<span class="me1">execute</span><span class="br0">&#40;</span> parents <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> Customer cust <span class="sy0">:</span> custs <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> Address addr <span class="sy0">:</span> cust.<span class="me1">getAddresses</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> addr.<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> Phone ph <span class="sy0">:</span> cust.<span class="me1">getPhones</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> ph.<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span>exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; query.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; query2.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; pm.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="kw1">return</span> custs<span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>You will notice that we walk the address and Phone lists for each customer to load them form Storage. We do that so we can ship them over the wire. UI in this case is a Flex client, so we do JSON serialization of the results.</p>
<h3>Conclusion</h3>
<p>Text searching can be implemented in GAE and to boot it can be implemented efficiently. Just remember before you store this Customer record you need to build out the CustomerIndex object with the set of words that we will search on. I just concat all the properties to one string and Lucene build the set for me by calling my getIndex().</p>
<p><a href="http://www.wetfeetblog.com/google-app-engine-full-text-search-with-jdo-revisited/368">Google App Engine &#8211; Full Text Search with JDO &#8211; Revisited</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google-app-engine-full-text-search-with-jdo-revisited/368/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</title>
		<link>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328</link>
		<comments>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:00:12 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[JDO]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=328</guid>
		<description><![CDATA[Objective This post will show you how to use Spring JDBC Template to perform real world CRUD operations. I hope I can show you that you do not need overweight Hibernate to interact with database of your choice. The Problem Domain Almost all application I have coded needed to store and get data of some [...]<p><a href="http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328">Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2010/02/network_server_2.jpg" alt="" title="network_server_2" width="300" height="225" class="float-right" /></p>
<h3>Objective</h3>
<p>This post will show you how to use Spring JDBC Template to perform real world CRUD operations. I hope I can show you that you do not need overweight Hibernate to interact with database of your choice.</p>
<h3>The Problem Domain</h3>
<p>Almost all application I have coded needed to store and get data of some sort. In most cases it was relational database. With hibernate being all the rage these days I have used it in multiple projects with varying degree of problems and success. In <strong>my personal</strong> opinion hibernate tries to solve a problem that really is not there. If you can not wire SQL you should not be using Hibernate or be doing any software development.<br />
<span id="more-328"></span><br />
I advise the use of Spring JDBC templates. In essence Spring JDBC Template will offload all the mundane steps of database access from you shoulders only requiring you to write SQL and move data from result set into your model objects. Here is how Spring documentation describes responsibilities:</p>
<table summary="Spring JDBC - who does what?" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; border-left: 0.5pt solid ; border-right: 0.5pt solid ; " width="80%">
<colgroup>
<col>
<col>
<col></colgroup>
<thead>
<tr>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">Action</th>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">Spring</th>
<th style="border-bottom: 0.5pt solid ; " align="center">You</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Define connection parameters.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center"><span class="bold"><strong></strong></span></td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Open the connection.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Specify the SQL statement.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Declare parameters and provide parameter values</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Prepare and execute the statement.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Set up the loop to iterate through the results (if any).</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Do the work for each iteration.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Process any exception.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Handle transactions.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; " align="left">Close the connection, statement and resultset.</td>
<td style="border-right: 0.5pt solid ; " align="center">X</td>
<td style="" align="center">&nbsp;</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h3>CRUD Example with Spring JDBC Template</h3>
<p>For this example you will need spring-core, spring-tx and spring-aop components. We will use spring-tx to manage the transaction so we can get back the auto generated IDs of the persisted objects and aop to set up the transaction management. Here is meat for spring configuration file:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;propertyPlaceholder&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;location&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;/WEB-INF/config.properties&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;mysqlDataSource&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">destroy-method</span>=<span class="st0">&quot;close&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;driverClass&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;com.mysql.jdbc.Driver&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;jdbcUrl&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.url}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;user&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.user}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;password&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.password}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;initialPoolSize&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.connections.start}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;maxPoolSize&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.connections.max}&quot;</span><span class="re2">/&gt;</span></span> &nbsp;
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;idleConnectionTestPeriod&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;270&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;txManager&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;dataSource&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;mysqlDataSource&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;tx:advice</span> <span class="re0">id</span>=<span class="st0">&quot;txAdvice&quot;</span> <span class="re0">transaction-manager</span>=<span class="st0">&quot;txManager&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;tx:attributes<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc-1">&lt;!-- all methods starting with 'get' are read-only --&gt;</span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;get*&quot;</span> <span class="re0">read-only</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;list*&quot;</span> <span class="re0">read-only</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!-- other methods use the default transaction settings (see below) --&gt;</span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/tx:attributes<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/tx:advice<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;aop:config<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;aop:pointcut</span> <span class="re0">id</span>=<span class="st0">&quot;uOwnerDataSvcOperation&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">expression</span>=<span class="st0">&quot;execution(* com.es.rto.dao.UOwnerDataServiceImpl.*(..))&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;aop:advisor</span> <span class="re0">advice-ref</span>=<span class="st0">&quot;txAdvice&quot;</span> <span class="re0">pointcut-ref</span>=<span class="st0">&quot;uOwnerDataSvcOperation&quot;</span><span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/aop:config<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;namedTemplate&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;constructor-arg</span> <span class="re0">index</span>=<span class="st0">&quot;0&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;mysqlDataSource&quot;</span><span class="re2">/&gt;</span></span> &nbsp; 
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.es.rto.dao.QandADao&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;jdbcTemplate&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;namedTemplate&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;uOwnerDataSvc&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.es.rto.dao.UOwnerDataServiceImpl&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>First thing we do we load our config.properties file where we have database configuration defined. Second we define a pooled data source to connect to mysql database. Third bean is a transaction manager that is watching our data source from the step two. Next we set up our transaction manager. We instruct transaction manager that methods starting with &#8220;get&#8221; and &#8220;list&#8221; are read only. This means that they will not be subject to transaction management. Then we define named jdbc template and pass in the data source as first constructor argument and then pass in the template into our own DAO bean.</p>
<h3>Why do we need Transaction Management?</h3>
<p>When we create a new row in a mysql table with autoincrement id value we need a way to get that auto generated id. To do so we need to query the database using same connection. When we use jdbcTemplate.update() or jdbcTemplate.query() it will check out a connection form the pool execute the query, process the results and return this connection to the pool. So if we do update() and then get() to query &#8220;SELECT LAST_INSERT_ID()&#8221; we will get another connection, which is a problem since we need to do this on the same connection to get the right id. This is where the transaction comes in. Instead of releasing connection back to the pool spring will hold on to it until we are done with it.</p>
<h3>Java code side of the Spring JDBC template</h3>
<p> We will use 3 beans to illustrate the scenario: question, answer and user.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> Question <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> questionId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> categoryId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> userId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> areaId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> question<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Date</span></a> created<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> User user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Answer<span class="sy0">&gt;</span> answers<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> answerCount<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> Answer <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> answerId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> questionId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> userId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> answer<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Date</span></a> created<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> User user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> User <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> id<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> retypeE<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password2<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> buyer<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> seller<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> active<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> privacy<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">int</span> pageSize <span class="sy0">=</span> <span class="nu0">20</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> next<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> administrator<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Here are the 2 classes that do all the work:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> QandADao <span class="br0">&#123;</span>

&nbsp; <span class="kw1">private</span> <span class="kw1">final</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span>QandADao.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">protected</span> NamedParameterJdbcTemplate jdbcTemplate <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; 

&nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
&nbsp; <span class="kw1">public</span> List<span class="sy0">&lt;</span>Question<span class="sy0">&gt;</span> getQuestions<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> search, <span class="kw4">int</span> page, <span class="kw4">int</span> step <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; MapSqlParameterSource params <span class="sy0">=</span> <span class="kw1">new</span> MapSqlParameterSource<span class="br0">&#40;</span> <span class="st0">&quot;search&quot;</span>, <span class="st0">&quot;%&quot;</span><span class="sy0">+</span>search<span class="sy0">+</span><span class="st0">&quot;%&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> sql <span class="sy0">=</span> <span class="st0">&quot;SELECT q.*, count( a.answer_id ) as answers, u.name, u.seller &nbsp;FROM users u, questions q LEFT JOIN answers a &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;ON &nbsp;a.question_id = q.question_id WHERE a.answer LIKE :search AND q.user_id = u.id &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;GROUP BY q.question_id, q.category_id, q.user_id, q.question, q.created, q.area_id, u.name, u.seller &quot;</span><span class="sy0">+</span> &nbsp;
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;UNION &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;SELECT q.*, count( a.answer_id ) as answers, u.name, u.seller &nbsp;FROM users u, questions q LEFT JOIN answers a &quot;</span><span class="sy0">+</span> 
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;ON &nbsp;a.question_id = q.question_id WHERE q.question LIKE :search AND q.user_id = u.id &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;GROUP BY q.question_id, q.category_id, q.user_id, q.question, q.created, q.area_id, u.name, u.seller &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;LIMIT &quot;</span><span class="sy0">+</span><span class="br0">&#40;</span>step<span class="sy0">*</span><span class="br0">&#40;</span>page<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">&quot;, &quot;</span><span class="sy0">+</span>step<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> sql <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> jdbcTemplate.<span class="me1">query</span><span class="br0">&#40;</span>sql, params, <span class="kw1">new</span> RowMappers.<span class="me1">QuestionsMapper</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> DataAccessException exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span><span class="st0">&quot;FAILED to get Question List&quot;</span>, exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Question<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> Question saveQuestion<span class="br0">&#40;</span> Question question <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; BeanPropertySqlParameterSource namedParameters <span class="sy0">=</span> <span class="kw1">new</span> BeanPropertySqlParameterSource<span class="br0">&#40;</span>question<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> sql<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> question.<span class="me1">getQuestionId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">0</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; sql <span class="sy0">=</span> <span class="st0">&quot;INSERT INTO questions ( category_id, user_id, question, area_id, uname ) VALUES ( :categoryId, :userId, :question, :areaId, :name )&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">else</span>
&nbsp; &nbsp; &nbsp; &nbsp; sql <span class="sy0">=</span> <span class="st0">&quot;UPDATE questions SET question=:question, category_id=:categoryId, area_id=:areaId WHERE question_id=:questionId&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> sql <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; jdbcTemplate.<span class="me1">update</span><span class="br0">&#40;</span>sql, namedParameters<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> question.<span class="me1">getQuestionId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> 0 <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; question.<span class="me1">setQuestionId</span><span class="br0">&#40;</span> <span class="kw1">this</span>.<span class="me1">getInsertedID</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> DataAccessException exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span><span class="st0">&quot;SAVE answer FAILED!&quot;</span>, exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">throw</span> exc<span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; <span class="kw1">return</span> question<span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">protected</span> <span class="kw4">long</span> getInsertedID<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">return</span> jdbcTemplate.<span class="me1">queryForLong</span><span class="br0">&#40;</span> <span class="st0">&quot;SELECT LAST_INSERT_ID()&quot;</span> , <span class="kw1">new</span> MapSqlParameterSource<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> RowMappers <span class="br0">&#123;</span>

&nbsp; <span class="kw1">public</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw1">class</span> QuestionMapper <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arowmapper+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RowMapper</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> mapRow<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs, <span class="kw4">int</span> rowNum<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; Question q <span class="sy0">=</span> <span class="kw1">new</span> Question<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setQuestionId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;question_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setCategoryId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;category_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setUserId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;user_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setAreaId</span><span class="br0">&#40;</span> rs.<span class="me1">getLong</span><span class="br0">&#40;</span><span class="st0">&quot;area_id&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setQuestion</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;question&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setCreated</span><span class="br0">&#40;</span> rs.<span class="me1">getTimestamp</span><span class="br0">&#40;</span> <span class="st0">&quot;created&quot;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setName</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;uname&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; User u <span class="sy0">=</span> <span class="kw1">new</span> User<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setName</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;name&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setSeller</span><span class="br0">&#40;</span> rs.<span class="me1">getBoolean</span><span class="br0">&#40;</span><span class="st0">&quot;seller&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setId</span><span class="br0">&#40;</span>q.<span class="me1">getUserId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setUser</span><span class="br0">&#40;</span>u<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> q<span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span> 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>So what is going on here? Lets look at the <strong>QuestionMapper</strong>. It implements one method &#8211; mapRow. Its hob is to process a single row of the result set. As you see here we create a Question bean and a User bean which is included in the Questions bean. It can not be more strait forward then this.</p>
<p><strong>QandADao</strong> is the class where all the magic happens. First lets examine the getQuestions() method. First we define query parameter(s) using MapSqlParameterSource class. We simply put the values in the map and jdbc template will pull them out by the key. Next we write our sql query. In this case iti is pretty complex to pull out any questions where either question or the answer have the thing we are searching for. We use union to be able to pull out  the questions without the answers. Lastly we call query method on the jdbc template and we pass in our query, parameters and the row mapper instance. Spring takes care of all the plumbing. What we get out of this call is a list of Question objects.</p>
<p>saveQuestion() method is not much different. In this case we use BeanPropertySqlParameterSource which maps bean properties to sql parameters. If you define a sql parameter &#8220;name&#8221; your bean should have &#8220;getName()&#8221; method. Instead of calling query() we call update() on the jdbc template which only takes sql and the parameters.</p>
<h3>Conclusion</h3>
<p>You may have noticed that the effort to use the Spring approach to JDCB is very simple, flexible and powerful. You have all the control in all the right places! You write the sql with all the performance hints and tweaks, you write code how to transfer results to the domain objects the way you need it or like it. I only mentioned about one tenth of the options you have with spring. <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html">Read The Fine Manual (RTFM)</a> to explore the full potential.</p>
<p><a href="http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328">Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Flex Frameworks &#8211; PuremVC, Mate, Cairngorm and Robotlegs</title>
		<link>http://www.wetfeetblog.com/flex-frameworks-puremvc-mate-cairngorm-robotlegs/297</link>
		<comments>http://www.wetfeetblog.com/flex-frameworks-puremvc-mate-cairngorm-robotlegs/297#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:00:30 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[The Journey]]></category>
		<category><![CDATA[Robotlegs]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=297</guid>
		<description><![CDATA[Objective I just wanted to share my thoughts about the frameworks in the Flex environment. You may have noticed that I am in love with Spring when I write Java code, but how about Flex? For a simple example application I go with KISS (keep it stupid simple) approach, but for serious data driven app [...]<p><a href="http://www.wetfeetblog.com/flex-frameworks-puremvc-mate-cairngorm-robotlegs/297">Flex Frameworks &#8211; PuremVC, Mate, Cairngorm and Robotlegs</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2010/02/robotlegssketchsmall.gif" alt="" title="robotlegssketchsmall" width="221" height="260" class="float-right" /><br />
<h3>Objective</h3>
<p>I just wanted to share my thoughts about the frameworks in the Flex environment. You may have noticed that I am in love with Spring when I write Java code, but how about Flex? For a simple example application I go with KISS (keep it stupid simple) approach, but for serious data driven app I utilize a framework.</p>
<h3>How to choose a framework that works for You</h3>
<p>Firs of all you need to know what you are looking for, because if you do not know where you are going, any path will take you there. When I first started to p[lay with Flex about 3 years ago, I noticed that I need to add some structure to the application. Since I was already using Spring I  started looking for a framework. At that point in time there where only two alternatives worth looking at: Cairngorm (heavily pushed by Adobe) and PureMVC.<br />
<span id="more-297"></span><br />
To select a framework I used both framework to build a simple sample application. After spending a day on the exercise I chose PureMVC. PureMVC solution was cleaner, it suited my need better and I felt that it did a better job at structuring the application. I stuck with PureMVC fro quite a while, Until couple month ago I decided to take a look of something better came along.</p>
<p>All the hype was around this English word "Mate", meaning friend or buddy. Even some developers around me have deep religious belief that Mate will save the world. So I had to take a deeper look at this framework. I build a simple application to get a feel for the framework and found that I do not like quite a lot of things about Mate.</p>
<p>First of all everything is done in the sudo code called MXML. I like writing code, I like to have control and make things exactly like they should be and generated code not allays works exactly like I need it to. Then I started looking into some other aspects of the Mate framework and found a better solution. If you are interested in a deeper reading about what I do not like about mate here are couple posts from Shaun that I agree with:</p>
<p><a href="http://shaun.boyblack.co.za/blog/2009/03/13/as3-dependency-injection-and-autowire/">AS3 Dependency Injection and [Autowire]</a><br />
<a href="http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/">Another Architectural Framework, But Why?</a></p>
<p>After looking around I chose to use <a href="http://www.robotlegs.org/">Robotlegs Framework</a>. It gave me exactly what I needed: dependency injection, mediators and proxies.  What you need for your application may be completely different and you may choose another framework, but give robotlegs a good look over to see if it will fit your need too.</p>
<h3>Conclusion</h3>
<p>Select a framework with a particular need in mind, don&#8217;t just use one because Adobe pushed it or your mate recommended it. Understand framewrok&#8217;s pros and cons and choose the one that give you the most bang per line of code or xml if that is your coding flavor.</p>
<p>Let me know in the comments which other framework you have used and would recommend to others.</p>
<p><a href="http://www.wetfeetblog.com/flex-frameworks-puremvc-mate-cairngorm-robotlegs/297">Flex Frameworks &#8211; PuremVC, Mate, Cairngorm and Robotlegs</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/flex-frameworks-puremvc-mate-cairngorm-robotlegs/297/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google App Engine + JAVA + JDO = Simple Search</title>
		<link>http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287</link>
		<comments>http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287#comments</comments>
		<pubDate>Wed, 10 Feb 2010 15:02:24 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[JDO]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=287</guid>
		<description><![CDATA[Objective This article will show you how to implement a simple search in Google App Engine using JDO engine including searching in child objects. The Problem Domain In my application I need to search for data and you probably need to do the same in yours. In Google App Engine you can not query for [...]<p><a href="http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287">Google App Engine + JAVA + JDO = Simple Search</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img class="float-right" title="files_and_archives" src="http://www.wetfeetblog.com/wp-content/uploads/2010/02/files_and_archives.jpg" alt="" width="200" height="144" /></p>
<h3>Objective</h3>
<p>This article will show you how to implement a simple search in Google App Engine using JDO engine including searching in child objects.</p>
<h3>The Problem Domain</h3>
<p>In my application I need to search for data and you probably need to do the same in yours. In Google App Engine you can not query for properties of the child objects. In SQL world this means you can not use &#8220;where&#8221; clause. So how we can not construct a query which looks at child objects. So how can we implement search in such restrictive environment?<br />
<span id="more-287"></span></p>
<h3>The Solution</h3>
<p>I came up with a very simple solution &#8211; I created an &#8220;index&#8221; property where I store all the data that I need to search on. Let&#8217;s say we have a Customer bean that has multiple addresses and phones. We want to be able to search on address, phone and customer name. Here are our beans:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Customer <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> <span class="sy0">-</span>3665292067832675548L<span class="sy0">;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> contactName<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> comments<span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span> addresses <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Phone<span class="sy0">&gt;</span> phones <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Phone<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> index<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// Getters and Setter go here...</span>
<span class="br0">&#125;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Address <span class="br0">&#123;</span>
&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line1<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line2<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> city<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> state<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> zip<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// Getters and Setter go here...</span>
<span class="br0">&#125;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Phone <span class="br0">&#123;</span>
&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anumber+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Number</span></a> phone<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// Getters and Setter go here...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>In the Customer bean we defined an &#8220;index&#8221; property which is a String array. We will put all our searchable words int this index, so later we can construct a query. Before we save our customer we want to populate index property like this:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">void</span> rebuildIndex<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; index <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; Iterator<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span> it <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">addresses</span>.<span class="me1">iterator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span> it.<span class="me1">hasNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Address ad <span class="sy0">=</span> it.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> ad.<span class="me1">getLine1</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="kw2">null</span> <span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> idx <span class="sy0">=</span> ad.<span class="me1">getLine1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">int</span> i <span class="sy0">=</span> 0 <span class="sy0">;</span> i <span class="sy0">&lt;</span> idx.<span class="me1">length</span> <span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.<span class="me1">add</span><span class="br0">&#40;</span> idx<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> ad.<span class="me1">getLine2</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="kw2">null</span> <span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> idx <span class="sy0">=</span> ad.<span class="me1">getLine2</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">int</span> i <span class="sy0">=</span> 0 <span class="sy0">;</span> i <span class="sy0">&lt;</span> idx.<span class="me1">length</span> <span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.<span class="me1">add</span><span class="br0">&#40;</span> idx<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; Iterator<span class="sy0">&lt;</span>Phone<span class="sy0">&gt;</span> it2 <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">phones</span>.<span class="me1">iterator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span> it2.<span class="me1">hasNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Phone ph <span class="sy0">=</span> it2.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> ph.<span class="me1">getPhone</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="kw2">null</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.<span class="me1">add</span><span class="br0">&#40;</span> ph.<span class="me1">getPhone</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> idx <span class="sy0">=</span> name.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">int</span> i <span class="sy0">=</span> 0 <span class="sy0">;</span> i <span class="sy0">&lt;</span> idx.<span class="me1">length</span> <span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.<span class="me1">add</span><span class="br0">&#40;</span> idx<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; idx <span class="sy0">=</span> contactName.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">int</span> i <span class="sy0">=</span> 0 <span class="sy0">;</span> i <span class="sy0">&lt;</span> idx.<span class="me1">length</span> <span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index.<span class="me1">add</span><span class="br0">&#40;</span> idx<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Now we have all our searchable words from child beans and parent bean in one property we can search on. Here is how my search method looks like:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
@ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;custs&quot;</span><span class="br0">&#41;</span>
@RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/{search}&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">GET</span><span class="br0">&#41;</span>
<span class="kw1">public</span> List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span> searchCustomers<span class="br0">&#40;</span> @PathVariable <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> search, HttpSession session<span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> pmf.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; Query query <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span>Customer.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; query.<span class="me1">setFilter</span><span class="br0">&#40;</span><span class="st0">&quot;index == searchParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; query.<span class="me1">declareParameters</span><span class="br0">&#40;</span><span class="st0">&quot;String searchParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span> custs<span class="sy0">;</span>
&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; custs <span class="sy0">=</span> <span class="br0">&#40;</span>List<span class="sy0">&lt;</span>Customer<span class="sy0">&gt;</span><span class="br0">&#41;</span> query.<span class="me1">execute</span><span class="br0">&#40;</span> search <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; query.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; <span class="kw1">return</span> custs<span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<h3>Conclusion</h3>
<p>We found an easy way around the big table JDO implementation limitation in Google App Engine. It is a simple and strait forward workaround, but it has couple drawbacks: we can only search on full words and we duplicate data using more storage space. In my book storage is cheap so that is not a big concern for me. </p>
<p>As I progress more with my projects on Google App Engine, I will surely revisit the search topic and post about other solutions to this challenge. </p>
<p>If you have a better solution please post it in the comments, I would love to give it a try!</p>
<p><a href="http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287">Google App Engine + JAVA + JDO = Simple Search</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google_app_engine-java-jdo-simple_search/287/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</title>
		<link>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242</link>
		<comments>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242#comments</comments>
		<pubDate>Wed, 13 Jan 2010 15:30:12 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=242</guid>
		<description><![CDATA[Objective This article will show you how to persist real world data in Google App Engine. You will learn how to handle inheritance and one-to-many owned relationships in your model objects and store them correctly in the big table implementation of JDO. The Problem Domain In my application I need to segregate data by the [...]<p><a href="http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242">Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img class="float-right" title="tin_can" src="http://www.wetfeetblog.com/wp-content/uploads/2010/01/tin_can.jpg" alt="" width="152" height="180" /></p>
<h3>Objective</h3>
<p>This article will show you how to persist real world data in Google App Engine. You will learn how to handle inheritance and one-to-many owned relationships in your model objects and store them correctly in the big table implementation of JDO.</p>
<h3>The Problem Domain</h3>
<p>In my application I need to segregate data by the customer this data belongs to. We will use inheritance in model objects, so all of them will have customer id defined in the base class. We will make the JDO save base class properties in the sub class &#8220;table&#8221;. We will also learn how to instruct Google App Engine JDO to handle customers with multiple addresses and phone numbers for us. If we define the relationship between model objects correctly JDO will manage storing/retrieving/deleting of these lined objects without our involvement.</p>
<p><span id="more-242"></span></p>
<h3>Inheritance in Model Classes</h3>
<p>First let&#8217;s define a superclass for all our model classes:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.Serializable</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdGeneratorStrategy</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Inheritance</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.InheritanceStrategy</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PrimaryKey</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
@Inheritance<span class="br0">&#40;</span>strategy<span class="sy0">=</span>InheritanceStrategy.<span class="me1">SUBCLASS_TABLE</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> ModelBase <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a><span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> customerId<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>What we done here is defined  Model Base class that has two properties &#8211; ID and customerID and marked them as persistent. We also annotated that our class is persistence capable and defined Inheritance strategy. In this case we chose to use Subclass Table. What this means is that ID and customerID properties will be saved in the table of the class that inherits this one. You can read more about <a href="http://www.jpox.org/docs/1_2/jdo_orm/inheritance.html">inheritance strategies here</a>.</p>
<p>Now lets define our User class:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.Serializable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.ArrayList</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> User <span class="kw1">extends</span> ModelBase <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a> <span class="br0">&#123;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>When you persist an instance of this User class in the big table it will have id, customerId, email, name and password properties in the single user &#8220;table&#8221;.</p>
<h3>One-to-many relationships in Google App Engine JDO</h3>
<p>For this example lets define a customer class that has a list on phone and address objects:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.util.ArrayList</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Element</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Customer <span class="kw1">extends</span> ModelBase <span class="br0">&#123;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> contactName<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> comments<span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span> adresses <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> phones <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aarraylist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ArrayList</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</address>
</pre>
<p>As you can tell we have defined two lists and annotated them to be persistent. <strong>(mappedBy = &#8220;customer&#8221;)</strong> defines this relationship as &#8220;owned&#8221; &#8211; meaning the customer property of the Address and Phone object will hold the reference tho this customer object.<br />
And here is an example of the address object:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Address <span class="kw1">extends</span> EntityBase <span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line1<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line2<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> city<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> state<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> zip<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>You may also notice <strong>@Element(dependent = &#8220;true&#8221;)</strong> which means that when the customer object is deleted JDO will delete these child objects for you automatically.</p>
<h3>Conclusion</h3>
<p>JDO simplifies your life when you need to store and retrieve data if you know how to define all these relationships correctly. Compare the effort required to store and retrieve the same object from your favorite relational database, without using the monster called hibernate&#8230;&#8230;
<div class="codesnip-container" ></div>
<p><a href="http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242">Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</title>
		<link>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212</link>
		<comments>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212#comments</comments>
		<pubDate>Wed, 16 Dec 2009 13:55:25 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=212</guid>
		<description><![CDATA[Objective We are talking REST+JSON from browser to the server and back, so we need to report errors nicely. Exception thrown on the server have to be converted to error messages and transmitted to the client in JSON format. Luckily there is a very elegant solution for this in Spring. HandlerExceptionResolver interface Spring provides a [...]<p><a href="http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212">Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/12/error_screenshot.jpg" alt="error_screenshot" title="error_screenshot" class="float-right" /></p>
<h3>Objective</h3>
<p>We are talking REST+JSON from browser to the server and back, so we need to report errors nicely. Exception thrown on the server have to be converted to error messages and transmitted to the client in JSON format. Luckily there is a very elegant solution for this in Spring.</p>
<h3>HandlerExceptionResolver interface</h3>
<p>Spring provides a very handy <strong>HandlerExceptionResolver</strong> interface for mapping exceptions to views. If you check the documentation (and you should) you will notice that where are couple exception handlers already implemented, but we will create our own. Just because we can.</p>
<p><span id="more-212"></span></p>
<h3>Implementing JsonExceptionResolver</h3>
<p>Our implementation is very basic, but can be enhanced to return more data about where the exception occurred, etc. To implement HandlerExceptionResolver interface we need to provide one method:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletResponse</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.HandlerExceptionResolver</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.ModelAndView</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> JsonExceptionResolver <span class="kw1">implements</span> HandlerExceptionResolver <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> ModelAndView resolveException<span class="br0">&#40;</span>HttpServletRequest request, HttpServletResponse response,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> handler, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> exception <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ModelAndView mav <span class="sy0">=</span> <span class="kw1">new</span> ModelAndView<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mav.<span class="me1">setViewName</span><span class="br0">&#40;</span><span class="st0">&quot;MappingJacksonJsonView&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mav.<span class="me1">addObject</span><span class="br0">&#40;</span><span class="st0">&quot;error&quot;</span>, exception.<span class="me1">getMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mav<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

<span class="br0">&#125;</span></div>
</div>
</pre>
<p><strong>resolveException</strong> method returns ModelAndView, to which we add a model object &#8220;error&#8221; holding the message from the exception, and set the view name. The view in this case is MappingJacksonJsonView. Setting error to the exception message comes in handy when we need to return an error from lets say &#8220;saveUser&#8221; action &#8211; we simply throw exception from the controller method like this: <strong>throw new Exception(&#8220;USER_EMAIL_NOT_UNIQUE&#8221;);</strong></p>
<p>Next we need to hook in our exception resolver into Spring context like this:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;mediaTypes&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;entry</span> <span class="re0">key</span>=<span class="st0">&quot;json&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/map<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultContentType&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultViews&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">name</span>=<span class="st0">&quot;MappingJacksonJsonView&quot;</span></span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp;
<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;exceptionResolver&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.JsonExceptionResolver&quot;</span> <span class="re2">/&gt;</span></span></div>
</div>
</pre>
<p>We simply define the bean and name call it exceptionResolver. Spring detects implemented interface and automagicaly routes exceptions to this resolver. We needed to modify our view definition &#8211; we gave it name, so we can reference it in the code &#8211; &#8220;MappingJacksonJsonView&#8221;.</p>
<h3>Conclusion</h3>
<p>Trapping exceptions on the server and converting them to JSON error messages can be done with 4 lines of conde and 1 line of xml. You can use this approach to map all sorts of exceptions to custom error jsp pages, JSON errors or any other view type. Your application users do not have to see an ugly Tomcat 500 error page.</p>
<p><a href="http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212">Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Goolge app engine + java + spring + REST + JSON + Flex – Part 3</title>
		<link>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185</link>
		<comments>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185#comments</comments>
		<pubDate>Wed, 18 Nov 2009 21:25:56 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=185</guid>
		<description><![CDATA[Objective After reading this post you will be able to implement robust user authentication and authorization scheme without replicating code to check if user is logged in and if the are authorized to perform a certain action. The Problem Domain You, like me, face the same problem with each web application &#8211; user authentication and [...]<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">Goolge app engine + java + spring + REST + JSON + Flex – Part 3</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/the_lock.jpg" alt="the_lock" title="the_lock" class="float-right" /></p>
<h3>Objective</h3>
<p>After reading this post you will be able to implement robust user authentication and authorization scheme without replicating code to check if user is logged in and if the are authorized to perform a certain action. </p>
<h3>The Problem Domain</h3>
<p>You, like me, face the same problem with each web application &#8211; user authentication and authorization. Who is logged in? Can they delete this entity? Can they see this report? How to solve this problem without copy pasting same piece of code to every servlet/action/controller/whatever? If you are using spring MVC there is a very elegant and powerful solution &#8211; <b>Interceptors</b>.</p>
<p>You can have as many interceptors as you like, I usually have one for security and maybe one more for some other task. Interceptor fires before your controller/action gets invoked. If it returns <b>true</b> the flow can continue, if it returns <b>false</b> the execution stops right there &#8211; request has been handled by the interceptor.<br />
<span id="more-185"></span></p>
<h3>Step One &#8211; Adding an interceptor to our <a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87"><u>application form part 1</u></a></h3>
<p>First lets code our interceptor. I named it SessionInerceptor.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletResponse</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.handler.HandlerInterceptorAdapter</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> SessionInterceptor <span class="kw1">extends</span> HandlerInterceptorAdapter <span class="br0">&#123;</span>

&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">boolean</span> preHandle<span class="br0">&#40;</span> HttpServletRequest request, HttpServletResponse response, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> handler<span class="br0">&#41;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; User user <span class="sy0">=</span> <span class="br0">&#40;</span>User<span class="br0">&#41;</span>request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> uri <span class="sy0">=</span> request.<span class="me1">getRequestURI</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> base <span class="sy0">=</span> request.<span class="me1">getContextPath</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; uri <span class="sy0">=</span> uri.<span class="me1">substring</span><span class="br0">&#40;</span> base.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> user <span class="sy0">==</span> <span class="kw2">null</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span>uri.<span class="me1">startsWith</span><span class="br0">&#40;</span><span class="st0">&quot;/api/login&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">invalidate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span class="me1">setContentType</span><span class="br0">&#40;</span><span class="st0">&quot;application/json&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span class="me1">getWriter</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">write</span><span class="br0">&#40;</span><span class="st0">&quot;{<span class="es0">\&quot;</span>error<span class="es0">\&quot;</span>:<span class="es0">\&quot;</span>SESSION-EXPIRED<span class="es0">\&quot;</span>}&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>As you see we are extending HandlerInterceptorAdapter class by implementing preHandle() method. If you need you can use postHandle() to perfor some action after the main handler has finished work. My implementation for this example is pretty simple &#8211; check only if the user is present in the session. If no user is present and user is not trying to log in, send back an error. In pure web application you probably would want to send a redirect to the login page.</p>
<p>Now lets add this interceptor to the Spring MVC stack by adding these 5 lines to our rest-json-flex-servlet.xml file.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;interceptors&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.SessionInterceptor&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>One more thing &#8211; we need to enable session in google app engine. Add this line to your appengine-web.xml</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;sessions-enabled<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/sessions-enabled<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<h3>Step 2 &#8211; Implement Login Controller</h3>
<p>Now we have our SessionInterceptor stopping us from accessing the services, we need to ad the way to log in. My inplementation of LoginController:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.IOException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.PersistenceManager</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.Query</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.apache.log4j.Logger</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.beans.factory.annotation.Autowired</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.stereotype.Controller</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.ModelAttribute</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestBody</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMapping</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMethod</span><span class="sy0">;</span>

@Controller
@RequestMapping<span class="br0">&#40;</span><span class="st0">&quot;/login&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> LoginController <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span> LoginController.<span class="kw1">class</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @Autowired
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> PMF pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;user&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> User login<span class="br0">&#40;</span> @RequestBody User user, HttpServletRequest request <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Hardcoded for demo website</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> user.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">equalsIgnoreCase</span><span class="br0">&#40;</span><span class="st0">&quot;user1&quot;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> user.<span class="me1">getPassword</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">equals</span><span class="br0">&#40;</span><span class="st0">&quot;password1&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setId</span><span class="br0">&#40;</span>1001<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setEmail</span><span class="br0">&#40;</span><span class="st0">&quot;fake@email.com&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> , user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> pmf.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span><span class="st0">&quot;select from com.lureto.rjf.User &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;where password == passwordParam &amp;&amp; name == nameParam &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;parameters String passwordParam, String nameParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span> users<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; users <span class="sy0">=</span> <span class="br0">&#40;</span>List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span><span class="br0">&#41;</span> query.<span class="me1">execute</span><span class="br0">&#40;</span> user.<span class="me1">getPassword</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, user.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> users.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0 <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User luser <span class="sy0">=</span> users.<span class="me1">get</span><span class="br0">&#40;</span>0<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> , luser<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> luser<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> PMF getPmf<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setPmf<span class="br0">&#40;</span>PMF pmf<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">pmf</span> <span class="sy0">=</span> pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>What happens here is very strait forward: we construct a query to the big table to find a user matching supplied name and password. If we find one, we create a session and put our user object there. Our SessionInterceptor expects to find this user there once we try access /api/user/ endpoints.<br />
Since Spring is scanning this package and we are using annotation, we do not need to add this controller to the xml file.</p>
<h3>Step 4 &#8211; Enhance existing User bean to support permissions</h3>
<p>Here is my User.java</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> User <span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> permissions<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="kw1">public</span> User<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; permissions <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="co1">// getters and setters here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<h3>Step 5 &#8211; Add Login Screen to Flex application</h3>
<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/login_screen.png" alt="login_screen" title="login_screen" width="457" height="282" class="alignnone size-full wp-image-199" /></p>
<p>This is how the finished screen looks like, once you log in, you end up in this screen.</p>
<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/user-edit-screen.png" alt="user-edit-screen" title="user-edit-screen" width="631" height="535" class="alignnone size-full wp-image-200" /></p>
<p>Full source code for <a href="http://www.wetfeetblog.com/wp-content/uploads/2009/11/JsonRestClient.mxml"><u>JsonRestClient.mxml</u></a>.</p>
<h3>Step 6 &#8211; User Authorization Implementation</h3>
<p>As you see from all the work we have done to this point we are not checking for user permissions. I intentional left it out. The best solution will depend on your particular application. In this case, we need to come up with the permission convention first. I would probably use &#8220;action-uri&#8221; convention. So to be able to get the user list you need to have &#8220;GET-user&#8221; permission, to update it would be &#8220;POST-user&#8221;, etc. You may come up with something better for your situation.</p>
<h3>Conclusion</h3>
<p>Interceptors in Spring is the perfect way of moving user authentication and authorization code form controllers into centralized place. This makes your controllers cleaner. You already know that the right user with right permissions is performing this action.</p>
<p><a href="http://www.wetfeetblog.com/wp-content/uploads/2009/11/rest-json-flex-part3.zip"><u>Source Code for Java part can be found Here.</u></a></p>
<p><a href="http://rest-json-flex.appspot.com/"><u>Demo application can be found here</u></a>. (check the LoginController in Step 2 to figure out the user + password to log in )</p>
<p>Leave me a comment what permission checking convention you would use.</p>
<p>Best of Luck!<br />
Tomas</p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">Goolge app engine + java + spring + REST + JSON + Flex – Part 3</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

