tag:blogger.com,1999:blog-16408320324551409742024-03-07T23:24:03.855-08:00CRed IndustriesI have been working on a project in my spare time to learn new tools and technologies. The project is a rest server stack built with java using Jersey with a flex client as an example of how to use the server. I released all the code I wrote as open source and I am writing this blog to describe how and why I created each section.
You can download the code for the server stack and the Flex client at http://code.google.com/p/java-rest-platform/Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-1640832032455140974.post-9825565279961471672012-10-03T22:19:00.000-07:002012-10-03T22:20:00.369-07:00Maven and eclipse part 2<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<h4>
Installing the maven eclipse plugin:</h4>
In my previous <a href="http://credindustries.blogspot.ca/2011/08/maven-and-eclipse.html">post </a>i described how to use Maven to build an eclipse project to import. Now i will describe how to use the Maven Integration for Eclipse plugin to import a Maven project.<br />
<br />
From the help menu select the Eclipse marketplace<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-xNOMqllyzXQ/UG0XDMJM0QI/AAAAAAAAAdA/r8PzzCv1bKg/s1600/eclipseMarket.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-xNOMqllyzXQ/UG0XDMJM0QI/AAAAAAAAAdA/r8PzzCv1bKg/s320/eclipseMarket.png" width="320" /></a></div>
<br />
Then search for the maven plugin<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-dMVCzY_H7eA/UG0XqaZXknI/AAAAAAAAAdQ/yMNNKruNZU0/s1600/installmavenEclipse.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://3.bp.blogspot.com/-dMVCzY_H7eA/UG0XqaZXknI/AAAAAAAAAdQ/yMNNKruNZU0/s400/installmavenEclipse.png" width="280" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-dkDGKy252to/UG0Xl42DLsI/AAAAAAAAAdI/_PBZljiObgw/s1600/eclipseMarket.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
Then click the install button on the Maven Integration for Eclipse.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-lY4Hg9zKOrk/UG0YFkYyNJI/AAAAAAAAAdY/mBvDkHYT8D8/s1600/installmavenEclipse2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="222" src="http://1.bp.blogspot.com/-lY4Hg9zKOrk/UG0YFkYyNJI/AAAAAAAAAdY/mBvDkHYT8D8/s400/installmavenEclipse2.png" width="400" /></a></div>
Then click next and finish the installation.<br />
<br />
Once you have installed the maven Integration for Eclipse, we can import our maven projects.<br />
Open the File menu then select the import option.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-whf3lXNE0zo/UG0V3SN0NAI/AAAAAAAAAc4/_Vje4_FQVyg/s1600/importMvn0.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://1.bp.blogspot.com/-whf3lXNE0zo/UG0V3SN0NAI/AAAAAAAAAc4/_Vje4_FQVyg/s400/importMvn0.png" width="247" /></a></div>
<br />
From the import menu, we want to import an Existing Maven project. This is located under the Maven folder.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-SRo4ct9hg3A/UG0VFZSB3hI/AAAAAAAAAco/dVaRTPpwJsM/s1600/importMvn1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://4.bp.blogspot.com/-SRo4ct9hg3A/UG0VFZSB3hI/AAAAAAAAAco/dVaRTPpwJsM/s400/importMvn1.png" width="387" /></a></div>
<br />
Now browse to the root directory of your project. This is the folder with your parent maven pom file. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-8iImRYwGwx0/UG0VFn1HJRI/AAAAAAAAAcw/K3WdKMXXUJg/s1600/importMvn2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://4.bp.blogspot.com/-8iImRYwGwx0/UG0VFn1HJRI/AAAAAAAAAcw/K3WdKMXXUJg/s400/importMvn2.png" width="392" /></a></div>
Now you can select finish. This will import your project as a Maven project using the Maven integration. <br />
<br />
You can now build the project automatically using Maven from Eclipse.<br />
<br />
One issue i ran into with Maven in combination with eclipse and wtp is that it wasn't deploying my other pom dependencies correctly so i had to disable "Resolve dependencies from Workspace projects" from the properties menu<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-4qxhwTeL3_o/UG0bCL4LxhI/AAAAAAAAAdo/B7nx7HY3WUE/s1600/mavenDep.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="381" src="http://2.bp.blogspot.com/-4qxhwTeL3_o/UG0bCL4LxhI/AAAAAAAAAdo/B7nx7HY3WUE/s400/mavenDep.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-97hKRNAn4RA/UG0aKTsZi5I/AAAAAAAAAdg/APbdFWoAqlY/s1600/mavenDep.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<br />
<br />
<br />
<br />
<br />Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-36823782063189989582012-08-29T21:52:00.002-07:002012-10-03T21:46:34.913-07:00JUnit Tests<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><br />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves>false</w:TrackMoves>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-CA</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin-top:0cm;
mso-para-margin-right:0cm;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0cm;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
table.MsoTableGrid
{mso-style-name:"Table Grid";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-fareast-language:EN-US;}
</style>
<![endif]-->
<br />
<h3 class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-weight: bold;">Unit Tests:</span></h3>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-weight: bold;">Unit tests give
developers the ability to isolate and test individual sections of code to
validate it against expected results. They are useful to verify code while
development and when updated to ensure correctness. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-weight: bold;">In this blog we will be
looking at JUit 4.X with maven. To include the JUnit test dependency in your
POM<span style="mso-spacerun: yes;"> </span>you need to include:</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">dependency</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">groupId</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><u><span style="color: black; font-family: Consolas; font-size: 10.0pt;">junit</span></u><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">groupId</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">artifactId</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><u><span style="color: black; font-family: Consolas; font-size: 10.0pt;">junit</span></u><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">artifactId</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">version</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="color: black; font-family: Consolas; font-size: 10.0pt;">4.10</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">version</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">scope</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="color: black; font-family: Consolas; font-size: 10.0pt;">test</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">scope</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-spacerun: yes;">
</span></span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10.0pt;">dependency</span><span style="color: teal; font-family: Consolas; font-size: 10.0pt;">></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">This will allow you to include JUint with your
project. To use JUnit tests you will need to import at least these basics:</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">import</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> org.junit.Assert;</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">import</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> org.junit.Test;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">JUint uses annotations to define tests
and methods used to setup and cleanup after tests. So you can annotate multiple
methods with each of these to have many tests and multiple setup or clean up
functions.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">@Test</span></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Defines the method
to run for a test. </span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">@Ignore</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Ignores a test case</span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 2;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">@Before</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Executes the method
before each test to setup any data or resources</span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 3;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">@After</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Executes the method
after each tests to cleanup and data or resources</span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 4;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">@BeforeClass<span style="color: black;"></span></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Executes the method
once before all tests cases in this class. Can be used to setup systems that
can be used for all tests to save time. This method must be static.</span></div>
</td>
</tr>
<tr style="mso-yfti-irow: 5; mso-yfti-lastrow: yes;">
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.45pt;" valign="top" width="121"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">@AfterClass<span style="color: black;"></span></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 388.35pt;" valign="top" width="518"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">Executes the method
once after all tests cases in this class, to clean up any resources once the
testing of this class is complete. This method must be static.</span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">The @test annotation can have two
optional parameters. the first being the type of exception that is expected. If
the exception is not thrown, the test case will fail. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-CA;"><span style="mso-tab-count: 1;"> </span>@Test(<span style="mso-bidi-font-weight: bold;">expected=</span></span><span style="color: black; font-family: "Courier New"; font-size: 10.0pt;">NullPointerException</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-weight: bold; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-CA;">.class</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-CA;">)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-CA;">the second option parameter is a timeout, the test
will fail if it takes longer then the timeout.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<br /></div>
<pre><span style="mso-tab-count: 1;"> </span>@Test(<span style="mso-bidi-font-weight: bold;">timeout=100</span>)</pre>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">First we will declare a simple class we want to
test. This just has one function that returns the value 10.</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> ClassToTest {</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> functionTotest() {</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> 10;</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">}</span><span style="font-family: Consolas; font-size: 10.0pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-weight: bold;">Now we want to declare
our test cases. First we need to create our class to hold our test case, There
isn't anything specific needed to declare a class as holding test cases. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>package</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> com.cred.industries.platform.test;</span><span style="font-family: Consolas; font-size: 10.0pt; mso-bidi-font-weight: bold;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>public</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> MyTestCase {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">We need to annotate our <span style="color: black;">methods
to define our unit tests. First we need to setup our tests cases, we can annotate a
method with </span><span style="color: #646464;">@Before</span><span style="color: black;"> to use it to setup our classes. This will execute this
method once before all tests cases</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">private</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> ClassToTest </span><span style="color: #0000c0; font-family: Consolas; font-size: 10.0pt;">clsToTest</span><span style="color: black; font-family: Consolas; font-size: 10.0pt;">;</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #646464; font-family: Consolas; font-size: 10.0pt;">@Before</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> beforeTest() {</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span><span style="color: #0000c0; font-family: Consolas; font-size: 10.0pt;">clsToTest</span><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> ClassToTest();</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">To declare a test case we annotate a method with <span style="color: #646464;">@Test</span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #646464; font-family: Consolas; font-size: 10.0pt;">@Test</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10.0pt;">void</span></b><span style="color: black; font-family: Consolas; font-size: 10.0pt;"> testCase1() {</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;">We can use Assert.* to validate our
data. If an assertion is failed it will fail the test case</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>Assert.<i>assertNotNull</i>(</span><span style="color: #0000c0; font-family: Consolas; font-size: 10.0pt;">clsToTest</span><span style="color: black; font-family: Consolas; font-size: 10.0pt;">);</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 2;"> </span>Assert.<i>assertTrue</i>(</span><span style="color: #0000c0; font-family: Consolas; font-size: 10.0pt;">clsToTest</span><span style="color: black; font-family: Consolas; font-size: 10.0pt;">.functionToTest() ==
10);</span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 10.0pt;"><span style="mso-tab-count: 1;"> </span>}</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">To run our test cases we open the class with the
test cases. Then in eclipse we select from the menu, Run -> Debug as ->
JUint Test</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;"><span id="goog_819370384"></span><span id="goog_819370385"></span><br /></span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-YYK9Z_4oZ5M/UD7v-6xGacI/AAAAAAAAAaE/-h7DGGya-o4/s1600/debugAs.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-YYK9Z_4oZ5M/UD7v-6xGacI/AAAAAAAAAaE/-h7DGGya-o4/s1600/debugAs.png" /></a></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-fareast-language: EN-CA; mso-no-proof: yes;"></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">This will also open the results tab that will give
you a break down on the test case results. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-EhflOYPZO5s/UD7wEiUY9FI/AAAAAAAAAaM/axTqt2fHPc8/s1600/Results.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="58" src="http://4.bp.blogspot.com/-EhflOYPZO5s/UD7wEiUY9FI/AAAAAAAAAaM/axTqt2fHPc8/s640/Results.png" width="640" /></a></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt; mso-fareast-language: EN-CA; mso-no-proof: yes;"><br /></span><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;"> That is enough to setup most test cases, but for more documentation see <a href="http://www.junit.org/">http://www.junit.org/</a></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<h3 class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 12.0pt;">Maven:</span></h3>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">Maven will automatically run test cases as long as
they are located in </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<tt><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;"><span style="mso-tab-count: 1;"> </span>src/test/java</span></tt></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<tt><span style="font-size: 10.0pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;"><span style="mso-tab-count: 1;"> </span>src/test/resources</span></tt><span style="font-family: Consolas; font-size: 10.0pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">to run tests with maven you just execute</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<pre><span style="font-family: Consolas;"><span style="mso-tab-count: 1;"> </span></span>mvn test</pre>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 10.0pt;">Your test cases will run by default with maven
builds. However, You may not always want to run test case because some can take
a long time. You can disabled or skip tests using </span></div>
<pre><span class="pln">mvn install </span><span class="pun">-</span><span class="typ">Dmaven</span><span class="pun">.</span><span class="pln">test</span><span class="pun">.</span><span class="pln">skip</span><span class="pun">=</span><span class="kwd">true</span></pre>
Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-51180061764411713452012-01-08T21:15:00.000-08:002012-08-22T19:49:48.284-07:00Java 7's automatically closing files and resources with try catch<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black;">As part of Java 7's project coin Java introduces automatically closing files and resources. This allows us to greatly simplify the error prone boiler plate code in Java 6. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black;">I won't go into the Java 6 way of handling resources since it has been blogged about many times, but I will show you an example of what it looks like below. As you can see you declare your resources, then in a try block use them. The try block has a finally that you need to close your resources in. But when closing the resources you need surround each in try-catch to make sure if you get an exception in one close it doesn't prevent the other ones from closing. As you can see there are a lot of steps to take, and a lot of places to go wrong. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">Connection connect = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">PreparedStatement preparedStatement = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">ResultSet resultSet = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">String metricValuename = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"test"</span><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> metricId = 0;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">//if we don't find the value lets create one</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> String sqlQuery = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"INSERT INTO metricsvaluename VALUES(?,?)"</span><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect = DBConnectionManager.<i>getInstance</i>().getConnection();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.setAutoCommit(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">false</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement = connect.prepareStatement(sqlQuery,</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> Statement.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">RETURN_GENERATED_KEYS</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement.setInt(1, 0);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement.setString(2, metricValuename);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> updated = preparedStatement.executeUpdate();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(updated == 0) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> ApplicationException(</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">.toString(),</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"no rows changed when creating new MetricValueId "</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;"> </span><span style="color: black; font-family: Consolas; font-size: 9pt;"> + metricValuename);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> resultSet = preparedStatement.getGeneratedKeys();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(resultSet.next()) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> metricId = resultSet.getInt(1);</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> DoSomethingWithId(metricId);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.commit();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> } </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">else</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.rollback();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> ApplicationException(</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">.toString(),</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"no metricTypeId generated "</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> + metricValuename);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { connect.rollback(); } </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (IllegalArgumentException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { connect.rollback(); } </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">finally</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(resultSet != </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">) resultSet.close(); }</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> } </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(preparedStatement != </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">) preparedStatement.close(); } </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> } </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(connect != </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">) connect.close(); } </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">}</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Using Java 7 we are able to remove a lot of the boiler plate code, and thus remove a lot of potential errors. By declaring your resources inside of the try ( ... ) statement we allow the system to manage the lifetime of the resources and automatically close them once we leave the try block. </div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">String metricValuename = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"test"</span><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> metricId = 0;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">String sqlQuery = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"INSERT INTO metricsvaluename VALUES(?,?)"</span><span style="color: black; font-family: Consolas; font-size: 9pt;">;</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Connection connect = DBConnectionManager.<i>getInstance</i>().getConnection();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> PreparedStatement preparedStatement = connect.prepareStatement(sqlQuery,</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> Statement.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">RETURN_GENERATED_KEYS</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">);){ </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.setAutoCommit(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">false</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement.setInt(1, 0);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement.setString(2, metricValuename);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;"> try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> updated = preparedStatement.executeUpdate();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(updated == 0) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> ApplicationException(</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">.toString(),</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"no rows changed creating new MetricValueId "</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> +</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> metricValuename);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (ResultSet resultSet = </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> preparedStatement.getGeneratedKeys()) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(resultSet.next()) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> metricId = resultSet.getInt(1);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.commit();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> } </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">else</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> connect.rollback();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> ApplicationException(</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 9pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 9pt;">.toString(),</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 9pt;">"no metricTypeId generated "</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> + metricValuename);</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> } </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (IllegalArgumentException | SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;"> try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { connect.rollback(); } </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">}</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
Since the automatic resource closing only deals with the closing of the resources we still need to worry about when to commit and rollback transactions for JDBC. Before we would roll back the transaction as part of the catch </div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ... </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">//open and use some resources</span><span style="font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { connect.rollback(); } </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">}</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;"> finally</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ... </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">//close the resources</span><span style="font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<span style="font-size: 9pt;">}</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
However with Java 7 this does not work because the resources is closed before we reach the catch statement. The code below gives me a compile error that connect cannot be resolved.</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Connection connect = DBConnectionManager.<i>getInstance</i>().getConnection()) {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ... </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">// use some resources</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> { connect.rollback(); } </span><b style="mso-bidi-font-weight: normal;"><span style="color: red; font-family: Consolas; font-size: 9pt;">//compiler error</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 9pt;"> }</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">}</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
To overcome this, inside of the try we create the resources with, we use another try to catch any exceptions so we can rollback our changes. Then using Java 7's enhanced exception handling you can manage the exception how you like. </div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Connection connect = DBConnectionManager.<i>getInstance</i>().getConnection()) {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ... </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">// do some work you dont need to rollback</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;"> try</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> ... </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;">// do any work you need to rollback</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> connect.commit();</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;"> </span><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> (IllegalArgumentException | SQLException e) {</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;"> try</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> { connect.rollback(); } </span><span style="font-family: Consolas; font-size: 8pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: #3f7f5f; font-family: Consolas; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> </span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;"> e.printStackTrace();</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: black; font-family: Consolas; font-size: 9pt;">}</span><span style="font-family: Consolas; font-size: 9pt;"></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
Also above you can see that we can catch many exceptions in the same catch using the syntax</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;"> catch</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: black; font-family: Consolas; font-size: 9pt;">(IllegalArgumentException | SQLException e) </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<span style="color: black;">this allows us to use the same code for multiple exceptions without cheating and just catching </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;"> </span></b><b><span style="color: #7f0055; font-family: Consolas; font-size: 9pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 9pt;"> (Exception e)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;">
</div>
Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-18500172868391526952011-11-05T22:50:00.000-07:002011-11-05T22:50:00.446-07:00Authentication and Authorization<div class="MsoNormal"></div><div class="MsoNormal">When securing your web service there are two main things you need to worry about. Authentication and Authorization. </div><div class="MsoNormal"><br />
</div><div class="MsoNormal">I am sure everyone is familiar with authentication, since every time we log into Facebook or our banks website we need to enter our user name and passwords. This is then verified against the user name and password set when you created your account for authentication. </div><div class="MsoNormal"><br />
</div><div class="MsoNormal">Once you are authenticated you are restricted to only viewing and editing content that you are authorized to view. In Facebook, you are not authorized to edit strangers profiles and you are not authorized to access the administrators endpoints. </div><div class="MsoNormal"><br />
</div><div class="MsoNormal">For each endpoint you visit on the web service you need to be both authorized and authenticated. Typically with web pages you would use http authentication schemes such as basic access authentication or digest access authentication. Then depending on how secure your service needs to be, you can use TSL to encrypt all communication. Once a user is authenticated they stay logged in for the session and don't need to authenticate again. However, one of the principles of REST is that it is stateless, so we shouldn't be saving session data between calls to the server. For each call to an endpoint we should be sending enough data to the sever to authenticate the user. </div><div class="MsoNormal"><br />
</div><div class="MsoNormal">For this project I decided to use 2 legged oAuth, since the client will be written in actionscript and not an http web browser. Well truth be told, there is no actual 2 legged oAuth 1.0. So I am using a cut down version, where we use the consumer key and consumer secret to authenticate the client and use the oAuth signature methods to validate them. Since REST is stateless we need to send enough information to authenticate the user with each call to the endpoint. We could send an oAuth signature with each call but instead I:</div><div class="MsoNormal" style="margin-left: 36.0pt;">- Have the user log in which uses oAuth to authenticate the users.</div><div class="MsoNormal" style="margin-left: 36.0pt;">- We then create a session key to save into our DB and return it to the user.</div><div class="MsoNormal" style="margin-left: 36.0pt;">- Each subsequent call I include the session key with the call and use that to identify the user. </div><div class="MsoNormal" style="margin-left: 36pt;"><br />
</div><div class="MsoNormal">Unless you secure the connection with TLS there are some obvious security issues, such as someone packet sniffing can getting your session key. Or someone could guess your session key, so we make sure that they expire within a short time of not hearing from the users. The more I think about it the less I like creating a session key and the more I like the idea of using oAuth to authenticate each call. It would be more secure since we don't have a session key to hijack.</div><div class="MsoNormal"><br />
</div><div class="MsoNormal">Where authentication is checked once up front, authorization is something that has to be integrated all over your code. First off you can restrict the access to endpoints by using the jersey annotation @RolesAllowed, such as:</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @PUT</span><span style="font-family: Consolas; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Consumes</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@RolesAllowed</span><span style="color: black; font-family: Consolas; font-size: 10pt;">({Role.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">ADMIN_STRING</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">, Role.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">CS_STRING</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">, Role.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">USER_STRING</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">})</span><span style="font-family: Consolas; font-size: 10pt;"></span></div><div class="MsoNormal"><span style="color: black; font-family: Consolas; font-size: 10pt; line-height: 115%;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt; line-height: 115%;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt; line-height: 115%;"> DetailedCustomerTrans updateCustomer(DetailedCustomerTrans custTrans) </span></div><div class="MsoNormal"><br />
</div><div class="MsoNormal"><span style="color: black;">But that doesn't help when you have an endpoint that allows a user to update their details. All customers have access to that endpoint, but you don't want to authorize Customer A to edit Customer B's data. So you need to specifically code those business rules into your program. </span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-58958128696591618752011-11-05T22:49:00.002-07:002012-03-15T11:18:59.738-07:00Authentication and Authorization code examples<div class="MsoNormal" style="background-color: white;"><span style="color: black;">Now that we have the basics covered let's get into the code.</span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;">All the code below can be found in fullwith my open source project:</span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;"><a href="http://code.google.com/p/java-rest-platform/">http://code.google.com/p/java-rest-platform/</a></span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;"> </span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;"></span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;">In the flex client we use a library found at </span>http://code.google.com/p/oauth-as3/ to build the oAuth signature. </div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 8pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> oAuthConsumer:<span style="background: none repeat scroll 0% 0% rgb(255, 255, 255); color: black;">OAuthConsumer</span> = </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">AuthConsumer</span>(customer.personaName, customer.password);</span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 8pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> signMethod:IOAuthSignatureMethod = </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> OAuthSignatureMethod_HMAC_SHA1();</span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 8pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> oAuthRequest:OAuthRequest = </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt;">new </span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;">OAuthRequest(OAuthRequest.HTTP_MEHTOD_POST, </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> ServerDAODefines.serverURL + mSessionPath, </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt;">null</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;">, oAuthConsumer);</span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> </span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 8pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> urlString:String = oAuthRequest.buildRequest(signMethod);</span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 8pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> uri:URI = </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt;"> URI(urlString);</span><span style="font-family: "Courier New"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; margin-left: 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 8pt; line-height: 115%;">client.post(uri, </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 8pt; line-height: 115%;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 8pt; line-height: 115%;"> ByteArray());</span></div><div class="MsoNormal" style="background-color: white;"><br />
</div><div class="MsoNormal" style="background-color: white;"><span style="color: black;">We use this to call the endpoint to create a session. However, since we are not authenticated at this point we need to make sure the endpoint is open to all. We can do that using the Jersey annotation @PermitAll. This will allow all users access to this endpoint. </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: Consolas; font-size: 8pt;">@POST</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: Consolas; font-size: 8pt;">@Produces</span><span style="color: black; font-family: Consolas; font-size: 8pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">)</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: Consolas; font-size: 8pt;">@PermitAll</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; margin-left: 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt; line-height: 115%;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt; line-height: 115%;"> SessionResultTrans createSession(</span><span style="color: #646464; font-family: Consolas; font-size: 8pt; line-height: 115%;">@Context</span><span style="color: black; font-family: Consolas; font-size: 8pt; line-height: 115%;"> HttpContext hc) {</span></div><div class="MsoNormal" style="background-color: white;"><span style="color: black;">Now we use the Jersey oAuth libraries to read in the oAuth signature to verify the users.</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">// wrap incoming request for OAuth signature verification</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthServerRequest request = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthServerRequest(hc.getRequest());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">// get incoming OAuth parameters</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthParameters params = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthParameters();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> params.readRequest(request);</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> String consumerKey = params.getConsumerKey();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthSecrets secrets = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> OAuthSecrets();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//get the customer associated with the session.</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerFacade custFacade = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerFacade();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerBO cust = custFacade.getCustomerByPersona(consumerKey);</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(cust == </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ApplicationException(Status.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNAUTHORIZED</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">,</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.toString(), </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"unable to find Customer "</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;"> </span><span style="color: black; font-family: Consolas; font-size: 8pt;">+ consumerKey);</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//... set secrets based on consumer key and/or token in parameters ...</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> secrets.setConsumerSecret(cust.getPassword());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//Authenticate the user using OAuth </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(!OAuthSignature.<i>verify</i>(request, params, secrets)) { </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ApplicationException(Status.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNAUTHORIZED</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">,</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.toString(), </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"failed to</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;"> verify OAuthSignature "</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> + cust.getCustomerId());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><br />
</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;">The session key is a randomly generated string that we save into the Db and return to the client. Below is the code we use to create the session key.</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">final</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> SessionKey {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">static</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">final</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> String </span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">symbols</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;"> = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">static</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">final</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">numSymbols</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;"> = </span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">symbols</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.length();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">static</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Random </span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">rnd</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;"> = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Random();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * The constructor is private since this is a utility class </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * that does not need to be instantiated </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> */</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">private</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> SessionKey() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * generates random XX character long session keys based on the </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * symbols String</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: Consolas; font-size: 8pt;">@param</span></b><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> len number of characters in the generated session key</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: Consolas; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> session key</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> */</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">static</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> String createSessionKey(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> len) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> StringBuilder key = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> StringBuilder();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">for</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> i = 0; i < len; i++) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> key.append(</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">symbols</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.charAt( </span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">rnd</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.nextInt(</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">numSymbols</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">) ));</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> key.toString();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black;">The session in the DB saves the unique session Id, the customer associated with the session and some timestamps to validate the session against. </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">PreparedStatement <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255); color: black;">preparedStatement</span><span style="background-color: white;"> </span>= </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> String sqlQuery = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"INSERT INTO session VALUES(?, ?, "</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;"> + "UTC_TIMESTAMP(), UTC_TIMESTAMP())"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span><span style="background-color: white;"> </span>= connect.prepareStatement(sqlQuery);</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span>.setString(1, sessionBO.getSessionId());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span>.setInt(2, sessionBO.getCustomerId());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> rowUpdated = <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span>.executeUpdate();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(rowUpdated == 0) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ApplicationException(ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.toString(),</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"failed to create session with "</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> + sessionBO.getCustomerId() +</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">" no rows updated"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">);</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> (SQLException e) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ApplicationException(e, ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.toString(),</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"failed to create session with "</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> + sessionBO.getCustomerId());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">} </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">finally</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">try</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> { </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(<span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span><span style="background-color: white;"> </span>!= </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">) <span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">preparedStatement</span>.close(); } </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">catch</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> (Throwable ignore) { </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">/* Propagate the original exception */</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> } </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black;">For the rest of the interactions between the client and server, we send the session key with each request in the header. </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 10pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> uri:URI = </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 10pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> URI(ServerDAODefines.serverURL + </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> mCustomerPath + </span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"/"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> + mFindByPersonaName +</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> </span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"/"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> + persona);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #6699cc; font-family: "Courier New"; font-size: 10pt;">var</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> request:HttpRequest = </span><b><span style="color: #0033ff; font-family: "Courier New"; font-size: 10pt;">new</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> Get();</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;">request.contentType = </span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"text/plain"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;">;</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;">request.addHeader(</span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"sessionid"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;">, sessionKey);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;">request.addHeader(</span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"Accept"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;">, </span><b><span style="color: #990000; font-family: "Courier New"; font-size: 10pt;">"application/xml"</span></b><span style="color: black; font-family: "Courier New"; font-size: 10pt;">);</span><span style="font-family: "Courier New"; font-size: 10pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;">client.request(uri, request);</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0.0001pt;"></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black;">On the servers side we don't want </span><span style="color: black;">authenticate the users in each endpoint, so we setup a filter to authenticate the users. To setup this up we need to add some initialization parameters to jersey to tell it about our filter. The first filter is our filter we use to authenticate the user and the second filter enforces the authorization for the roles allowed to access each endpoint</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">com.sun.jersey.spi.container.ContainerRequestFilters</span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">com.cred.industries.platform.filter.AuthenticationFilter</span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">com.sun.jersey.spi.container.ResourceFilters</span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-family: Consolas; font-size: 8pt;">com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 8pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 8pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 8pt;">></span><span style="color: black; font-size: 8pt;"> </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;">In the filter we get the session Id out of the header and use it to get the session from the DB. With the session data we can figure out what customer is associated with the session.</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: Consolas; font-size: 8pt;">@Override</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ContainerRequest filter(ContainerRequest cr) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> String sessionId = cr.getHeaderValue(</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">SESSION_PARAMETER</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">);</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> SessionFacade session = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> SessionFacade();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerBO cust = session.authenticateUserbySessionId(sessionId);</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;">We also save the customer into the thread local storage, so anywhere in this thread we can find out who is the requesting customer. This is used later on to see if the user is authorized.<br />
With the customer we setup the security context so jersey can filter the requests authorization at each endpoint. Jersey needs a Security context Object to authorize the roles against. </div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> cr.<span style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">setSecurityContext</span>(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Authorizer(cust));</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> cr;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">* </span><span style="color: #7f7f9f; font-family: Consolas; font-size: 8pt;"><p></span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">SecurityContext used to perform authorization checks.</span><span style="color: #7f7f9f; font-family: Consolas; font-size: 8pt;"></p></span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">*/</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Authorizer </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">implements</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> SecurityContext {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">private</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerBO </span><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">mCustomer</span><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Authorizer(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">final</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> CustomerBO customer) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">mCustomer</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> = customer;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Principal getUserPrincipal() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Principal() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> String getName() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">mCustomer</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.getRoles().getHighestRole().toString();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> };</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> isUserInRole(String role) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">mCustomer</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.getRoles().hasRole(Role.<i>convert</i>(role));</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> isSecure() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"https"</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.equals(</span><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">mUriInfo</span><span style="color: black; font-family: Consolas; font-size: 8pt;">.getRequestUri().getScheme());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> String getAuthenticationScheme() {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> SecurityContext.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">BASIC_AUTH</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">;</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> }</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span><span style="font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;">That covers authentication and the authorization of endpoints, but for authorization of business objects I created an interface that each of my business objects inherits. With this we can easily query the business objects to see if we are authorized to access or modify. </div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">interface</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> Authorization {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * used to control who can update this BO.</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: Consolas; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> true if the authenticated can modify the BusinessObject</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> */</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> authorizedToModify();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * used to control who can read this BO. </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: Consolas; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> true if the authenticated can read the BusinessObject</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> */</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> authorizedToAccess();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;">/**</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * used to control access to parts of the BO. For example, in the </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * Customer BO we only want a </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * super user to be able to change the <u>persona</u> name. </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: Consolas; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> true if the authenticated user can modify/read any </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> * part of the BusinessObject as the SuperUser</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: Consolas; font-size: 8pt;"> */</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> authorizedAsSuperuser();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 8pt;">Here is an example of how my customer business object implements this interface</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">/**</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * returns if the currently authenticated customer is authorized to modify</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * this customer BO. </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * a CS, <u>admin</u> or yourself can make changes to this BO</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> true if this is a CS, <u>admin</u> or yourself</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> */</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: "Candara","sans-serif"; font-size: 8pt;">@Override</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">public</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> authorizedToModify() {</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;">//a CS, <u>admin</u> or yourself can make changes to this BO</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;"> //thread local storage we setup with customer at start to access </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;"> //anywhere with this request. Only lives as long as the request.</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> CustomerBO authenticatedUser = SessionData.<i>getSessionCustomer</i>(); </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> CustomerRolesBO roles = authenticatedUser.getRoles();</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">return</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> authenticatedUser != </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">null</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> && (roles.hasRole(Role.</span><i><span style="color: #0000c0; font-family: "Candara","sans-serif"; font-size: 8pt;">ADMIN</span></i><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">) </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> || roles.hasRole(Role.</span><i><span style="color: #0000c0; font-family: "Candara","sans-serif"; font-size: 8pt;">CS</span></i><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">) </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> || </span><span style="color: #0000c0; font-family: "Candara","sans-serif"; font-size: 8pt;">mCustomerId</span><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> == authenticatedUser.getCustomerId());</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">}</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">/**</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * anyone can access this BO</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> true</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> */</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: "Candara","sans-serif"; font-size: 8pt;">@Override</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">public</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> authorizedToAccess() {</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;">//any one can read this BO</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">return</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">true</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">;</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">}</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">/**</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * returns if the currently authenticated customer is a super user</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * <u>Ie</u> a user with permissions to change anything related to this BO</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * In this case a CS or <u>admin</u> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> * </span><b><span style="color: #7f9fbf; font-family: "Candara","sans-serif"; font-size: 8pt;">@return</span></b><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> true if this is a CS, or <u>admin</u>.</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f5fbf; font-family: "Candara","sans-serif"; font-size: 8pt;"> */</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #646464; font-family: "Candara","sans-serif"; font-size: 8pt;">@Override</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">public</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> authorizedAsSuperuser() {</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><span style="color: #3f7f5f; font-family: "Candara","sans-serif"; font-size: 8pt;">//a CS, <u>admin</u> or yourself can make changes to this BO</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> CustomerBO authenticatedUser = SessionData.<i>getSessionCustomer</i>();</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> CustomerRolesBO roles = authenticatedUser.getRoles();</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">return</span></b><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> authenticatedUser != </span><b><span style="color: #7f0055; font-family: "Candara","sans-serif"; font-size: 8pt;">null</span></b><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> && (roles.hasRole(Role.</span><i><span style="color: #0000c0; font-family: "Candara","sans-serif"; font-size: 8pt;">ADMIN</span></i><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">)</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;"> || roles.hasRole(Role.</span><i><span style="color: #0000c0; font-family: "Candara","sans-serif"; font-size: 8pt;">CS</span></i><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">));</span><span style="font-family: "Candara","sans-serif"; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: "Candara","sans-serif"; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><span style="color: black;">Then in code when using the customer business object we can quickly access the authorizations to prevent or allow access </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> isAuthorizedToModify = custToModify.authorizedToModify();</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//you are only allowed to modify your account unless you </span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//are an <u>admin</u> or CS</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(!isAuthorizedToModify) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">throw</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;"> ApplicationException(Status.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNAUTHORIZED</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">,</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> ReturnCodes.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 8pt;">UNKNOWN_ERROR</span></i><span style="color: black; font-family: Consolas; font-size: 8pt;">.toString(), </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">"not authorized to modify "</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> +</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> custToUpdate.getPersonaName() + </span><span style="color: #2a00ff; font-family: Consolas; font-size: 8pt;">" by "</span><span style="color: black; font-family: Consolas; font-size: 8pt;"> + authenticatedUser.getPersonaName());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> </span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: #3f7f5f; font-family: Consolas; font-size: 8pt;">//only a super user for the BO can change the <u>persona</u> name</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 8pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 8pt;">(isSuperUser) {</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;"> custToModify.setPersonaName(custToUpdate.getPersonaName());</span><span style="font-family: Consolas; font-size: 8pt;"></span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin: 0cm 0cm 0cm 36pt;"><span style="color: black; font-family: Consolas; font-size: 8pt;">}</span></div><div class="MsoNormal" style="background-color: white; line-height: normal; margin-bottom: 0cm;"><br />
</div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-26565739212107116702011-09-15T22:25:00.000-07:002011-09-15T22:50:35.534-07:00Java Rest with Jersey<div class="WordSection1"><div class="MsoNormal"><span style="font-size: large;"><b><span style="line-height: 115%;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Rest with Jersey</span></span></b></span><b style="mso-bidi-font-weight: normal;"><span style="font-size: 14pt; line-height: 115%;"><o:p></o:p></span></b></div><br />
<div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">In my last blog post <a href="http://credindustries.blogspot.com/2011/09/what-is-rest.html">http://credindustries.blogspot.com/2011/09/what-is-rest.html</a></span><br />
<span style="font-size: small;">I talked about the principals behind REST. Now we will discuss the implantation</span><br />
<span style="font-size: small;">details using the jersey platform. For more code examples visit <a href="http://code.google.com/p/java-rest-platform/">http://code.google.com/p/java-rest-platform/</a></span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">I spent a lot of time piecing together different parts of Jersey and below I am just covering the very basics. But if you are interested in using jersey I very much recommend this book. </span></div><br />
<iframe frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="http://rcm-ca.amazon.ca/e/cm?t=creind-20&o=15&p=8&l=as1&asins=0596158041&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="height: 240px; width: 120px;"></iframe><br />
<br />
<div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><o:p>Y</o:p>our Jersey resources can be either a singletons that will handle all requests or a class that will be instantiated per request and disposed of after the request is done. Since the object is created for each request it is stateless, where as a singleton can hold state between requests. In my project I chose to use classes that are instantiated with each request. </span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">We will need to tell your server container where your jersey resources are located. This is done through the web.xml, which provides configuration and deployment details for the web services and resources.</span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: teal; font-family: Consolas; font-size: 10pt;"><?</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">xml</span><span style="font-family: Consolas; font-size: 10pt;"> <span style="color: #7f007f;">version</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"1.0" </span></i><span style="color: #7f007f;">encoding</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"UTF-8"</span></i><span style="color: teal;">?></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">web-app</span><span style="font-family: Consolas; font-size: 10pt;"> <span style="color: #7f007f;">xmlns:xsi</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"http://www.w3.org/2001/XMLSchema-instance"</span></i> <span style="color: #7f007f;">xmlns</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"http://java.sun.com/xml/ns/javaee"</span></i> <span style="color: #7f007f;">xsi:schemaLocation</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</span></i> <span style="color: #7f007f;">id</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"WebApp_ID"</span></i> <span style="color: #7f007f;">version</span><span style="color: black;">=</span><i><span style="color: #2a00ff;">"3.0"</span></i><span style="color: teal;">></span><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">display-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">rest</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">display-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">Jersey REST Service</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><o:p></o:p> </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: teal; font-family: Consolas; font-size: 10pt;"> <</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-class</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">com.sun.jersey.spi.container.servlet.ServletContainer</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-class</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">com.sun.jersey.config.property.packages</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">param-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">com.cred.industries.platform.resources</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">param-value</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">init-param</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">load-on-startup</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">1</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">load-on-startup</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"></span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"></span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-mapping</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">Jersey REST Service</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-name</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"><</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">url-pattern</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="color: black; font-family: Consolas; font-size: 10pt;">/resource/*</span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">url-pattern</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: teal; font-family: Consolas; font-size: 10pt;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt;">servlet-mapping</span><span style="color: teal; font-family: Consolas; font-size: 10pt;">></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal"><span style="color: teal; font-family: Consolas; font-size: 10pt; line-height: 115%;"></</span><span style="color: #3f7f7f; font-family: Consolas; font-size: 10pt; line-height: 115%;">web-app</span><span style="color: teal; font-family: Consolas; font-size: 10pt; line-height: 115%;">></span><br />
<span style="color: teal; font-family: Consolas; font-size: 10pt; line-height: 115%;"></span><br />
<span style="color: teal; font-family: Consolas; font-size: 10pt; line-height: 115%;"><o:p></o:p></span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><br />
<div style="text-align: justify;"><span style="font-size: small;">Here we are creating a servelet for jersey where the init-param of</span></div></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"> </span><span style="color: black; font-family: "Courier New",Courier,monospace; font-size: small; line-height: 115%;">com.sun.jersey.config.property.packages </span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="color: black; font-size: small;">tells us what package our jersey classes are going to be in. In our case our jersey resources will be in the </span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="color: black; font-size: small;"> </span><span style="color: black; font-family: "Courier New",Courier,monospace; font-size: small; line-height: 115%;">com.cred.industries.platform.resources </span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="color: black; font-size: small; line-height: 115%;">package</span><span style="color: black; font-size: small;">. All Jersey resources in that package will be accessible under the /resou</span><span style="color: black; font-size: small;">rces/ url. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="color: black; font-size: small;">In my last <a href="http://credindustries.blogspot.com/2011/09/what-is-rest.html">blog post</a> I talked about 5 principals of Rest, now we will look at how jersey handles those principals. If you haven't read that post I recommend reading it first as we build on those concepts here. </span><span style="font-size: small;"><b><span style="line-height: 115%;"> </span></b></span></div><div class="MsoNormal"><b><span style="font-size: 14pt; line-height: 115%;"> </span></b></div><div class="MsoNormal"><br />
<div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: large;"><b><span style="line-height: 115%;">Addressable resources </span></b></span></div></div><br />
<br />
<div style="text-align: justify;"><div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Each resources within the system can be addressed by a unique URI.</span></span><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Jersey uses the Java annotation @Path to make it very simple to create an address for a resources. We can use the @Path to expose classes and also member functions within classes to give them a sub path.</span></span></div></div><div class="MsoNormal"></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/customer"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerRes {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@POST</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/friend"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> void addFriendToCustomer()<br />
</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">We are exposing the CustomerRes class under the path "/customer" then the member function addFriendToCustomer under the path "/friend". So to address the resources addFriendToCustomer we need to post to /resource/customer/friend. Using Rest we can also access parameters that are part of the path and query parameters. </span></span><span style="font-family: "Cambria","serif";"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: black; font-family: Consolas; font-size: 10pt;">@GET</span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/{id: \\d+}"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTrans getCustomerById(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@PathParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> custId) </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">In this example we are exposing the getCustomerById member function under the path<span style="color: blue;"> "/{id: \\d+}.</span> the id: will map to the @PathParam("id") and the <span style="color: blue;">\\d+</span> tells jersey to match it to a regular expression \d+ which will match one or more digits. </span></span><span style="font-family: "Cambria","serif";"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p> </o:p></span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@GET</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/group/{id}"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #7f0055;"><b> </b></span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList getCustomersByName(</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@PathParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) String custName, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"0"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setStart, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"10"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setSize) <o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin-bottom: 0cm; text-align: justify;"><span style="font-size: small;"><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /></span><div style="text-align: justify;"><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">We can also have a mix of path and query parameters in a request. This request would look like:</span></span></div><div style="text-align: left;"><span style="font-size: small;"> <span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">http://localhost:8080/platform/rest/customer/group/name?start=100&size=20</span></span></span></div></div><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Where "name" is the customer we are looking for and start and size tells us where in the list we are looking and how many to get.</span></span><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><o:p></o:p></div><br />
<div class="MsoNormal"><b style="mso-bidi-font-weight: normal;"><span style="font-size: 14pt; line-height: 115%;"><span style="font-size: large;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">A standardized interface</span></span><o:p></o:p></span></b></div><div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">There is a limited set of methods and operations that we can use to access our resources. </span></span><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Jersey uses Java annotations @GET @POST @PUT and @DELETE to specify the verb or action we are to take on the resource. </span></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @POST</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/friend"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #7f0055;"><b> </b></span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> void addFriendToCustomer(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> custId)</span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"> </span></div><o:p></o:p><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> </span><br />
<span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @PUT</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/friend"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> void updateFriendOfCustomer(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> custId) </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><o:p></o:p></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @DELETE</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/friend"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> void removeFriendFromCustomer(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> custId) <o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @GET</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/friend"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> friendsTrans getCustomersFriends(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> custId) </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><o:p> </o:p></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">All of these would be accessed under the path "/resource/customer/friend" but depending on the http method they will do very different things. That is why we consider the friend resources as the noun and the http methods as the verbs. Using different verbs with a noun will do very different things. Using the "POST" verb and the "/friend" noun we will add a new friend, but if we change the verb to "DELETE" we are now deleting friends. </span></span><span style="font-family: "Cambria","serif";"><o:p> </o:p></span></div><br />
<div class="MsoListParagraphCxSpFirst" style="margin-left: 0cm; mso-add-space: auto;"><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: large;"><b><span style="line-height: 115%;">Self-descriptive messages</span></b></span></div><b><span style="font-family: "Cambria","serif"; font-size: 14pt; line-height: 115%;"><o:p></o:p></span></b></div><div class="MsoListParagraphCxSpMiddle" style="margin-left: 0cm; mso-add-space: auto;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Cambria","serif"; font-size: 14pt; line-height: 115%;"><o:p></o:p></span></b></div><div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Each request to the server specifies the media type that it produces and consumes such that a single resource can handle different formats.</span></span><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Again Jersey uses Java annotations to define both the format that the server accepts and the format that it produces. </span></span></div><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Cambria","serif"; font-size: 14pt; line-height: 115%;"><o:p></o:p></span></b><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @GET</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/group/{id}"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Consumes</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">TEXT_PLAIN</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Produces</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList getCustomersByName(</span><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @PathParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) String custName, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"0"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setStart, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"10"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setSize) <o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @GET</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/group/{id}"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Consumes</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">TEXT_PLAIN</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Produces</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_JSON</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList getCustomersByName(</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@PathParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) String custName, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"0"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setStart, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"10"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setSize) <o:p></o:p></span></div><br />
<div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">We have the same path producing different formats depending on what the client asks for. You have probably seen me returning an object CustomerTrans. These are what I call transfer objects and are built using JAXB. Jersey supports JAXB so you can automatically marshal and un-marshal objects without having to parse xml or json. By sending a matching XML to a resources jersey in the body of your HTTP message Jersey will automatically un-marshal it into a parameter.</span></span><span style="color: black; font-family: "Cambria","serif";"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@POST</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/create"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Consumes</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Produces</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTrans createCustomer(CustomerTrans custTrans)<o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin-bottom: 0cm;"> </div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin-bottom: 0cm;"><div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">This will take a XML object representing the CustomerTrans xml that was in the HTTP body and convert it to object that java can use. </span></span></div><span style="color: black; font-size: small;"></span><br />
<span style="color: black; font-size: small;"><o:p></o:p></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-size: small;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: small;"><br />
</span></div><span style="color: black; font-family: "Cambria","serif"; font-size: small;">To create JAXB objects we use Java annotations </span><span style="color: #646464; font-family: Consolas; font-size: small;">@XmlRootElement</span><span style="color: black; font-family: "Cambria","serif";"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">.</span></span><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: "Cambria","serif";"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@XmlRootElement</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(name = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"customerTrans"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">class</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTrans {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@XmlAttribute</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(name = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"customerId"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">mCustomerId</span><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@XmlElement</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(name = </span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"personaName"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> String </span><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">mPersonaName</span><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div> <br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #7f0055;"><b> </b></span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTrans() {</span><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p></span><span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><span style="color: #7f0055;"><b> </b></span></span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTrans(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> customerId, String personaName) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">super</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">this</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">.</span><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">mCustomerId</span><span style="color: black; font-family: Consolas; font-size: 10pt;"> = customerId;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">this</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">.</span><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">mPersonaName</span><span style="color: black; font-family: Consolas; font-size: 10pt;"> = personaName;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;">}</span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div> </div><div class="WordSection1" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">To create the JAXB object you need to annotate a class with <span style="color: #999999;">@XmlRootElement</span> and give it a name, in our case customerTrans. Then we can expose member variables or functions using <span style="color: #666666;">@XmlAttribute</span> and <span style="color: #666666;">@XmlElement</span> to define something as an xml attribute or and xml element respectively. <br />
</span></div><div class="WordSection1"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">This will marshal and un-marshal xml in this format. </span></span><span style="color: black; font-family: "Cambria","serif";"><o:p></o:p></span><br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;"><customerTrans customerId="3"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;"> <personaName>test</personaName><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: "Courier New"; font-size: 10pt;"></customerTrans></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: "Cambria","serif";"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b><span style="line-height: 115%;"><span style="font-size: large;">Communicate stateless </span></span></b></span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: small;"></span></div><br />
<div style="text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Each request must provide sufficient information, so the server can respond to it without needing client session state on the server. </span></span><br style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;" /><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">Each request to jersey by default does not maintain client state from one request to the next. So your client needs to pass all information need for jersey to find and process your request.</span></span></div><br />
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: .0001pt; margin: 0cm; mso-add-space: auto;"><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: large;"><b>Hypermedia as the engine of application state (HATEOAS)</b></span></div><b><span style="font-family: "Cambria","serif"; font-size: 14pt;"><o:p></o:p></span></b></div><div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: .0001pt; margin: 0cm; mso-add-space: auto;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Cambria","serif"; font-size: 14pt;"><o:p></o:p></span></b></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><span style="font-size: small;"></span><br />
<div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small; line-height: 115%;">Clients discover the resources and interfaces available to them from the server not from fixed hardcoded paths.<o:p></o:p></span></div></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small; line-height: 115%;">As an example of this we will look at the request</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"> <span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">h</span></span><span style="font-family: "Courier New",Courier,monospace; font-size: x-small;">ttp://localhost:8080/platform/rest/customer/group/pa?start=0&size=20</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><span style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;">As you can see the client sends the information that we are looking for all customers matching "pa" and we want the set starting from 0 including up to 20. But when sending the result we don't want to keep state on the server about what the next or previous set of customers so we send links to the next and previous set with the results from this set. </span></span><span style="font-size: small;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @GET</span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Path</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"/group/{id}"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@Consumes</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">TEXT_PLAIN</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;"> @Produces</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(MediaType.</span><i><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">APPLICATION_XML</span></i><span style="color: black; font-family: Consolas; font-size: 10pt;">)</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;"> public</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList getCustomersByName(</span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@PathParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"id"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) String custName, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"0"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setStart, </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@DefaultValue</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"10"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><span style="color: #646464; font-family: Consolas; font-size: 10pt;">@QueryParam</span><span style="color: black; font-family: Consolas; font-size: 10pt;">(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">) </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> setSize) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> List<CustomerTrans> custTrans = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> ArrayList<CustomerTrans>();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//use the customer facade to find the customer</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerFacade custFacade = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerFacade();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//we get the set size plus one so we can tell if there is a </span><br />
<span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;"> //next result set</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> custTrans = custFacade.getCustomersByName(custName, </span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"> setStart, setSize + 1);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//when returning the links to the next/previous sets we </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;"> //</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">need to figure </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">but where we are </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">since we tried to grab </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">one more than </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">the asked for size we can tell if there </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;"></span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">are more</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;"> results. </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">If there are more results we will <br />
</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//</span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">add a link to them and remove the extra </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">customer found</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">boolean</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> isNextSet = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">false</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">(custTrans.size() > setSize) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> isNextSet = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">true</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> custTrans.remove(setSize);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> URI next = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> URI prev = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><br />
<span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><o:p></o:p></div><span style="font-family: Consolas; font-size: 10pt;"></span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//the next link is the start plus the number of <u>cust</u> found</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> UriBuilder linkBuilder = </span><span style="color: #0000c0; font-family: Consolas; font-size: 10pt;">mUri</span><span style="color: black; font-family: Consolas; font-size: 10pt;">.getAbsolutePathBuilder();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">(isNextSet) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> linkBuilder.queryParam(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, setStart + custTrans.size());</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> linkBuilder.queryParam(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, setSize);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> next = linkBuilder.build();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//the previous link is the <u>min</u> of start minus the result set size or 0 <o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;"> //If 0 we <u>dont</u> send a link back</span><span style="color: black; font-family: Consolas; font-size: 10pt;"> <o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">int</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> previousStart = Math.<i>max</i>(setStart - setSize, 0);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">(setStart != 0) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> linkBuilder.queryParam(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"start"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, previousStart);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> linkBuilder.queryParam(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"size"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, setSize);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> prev = linkBuilder.build();</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="color: #3f7f5f; font-family: Consolas; font-size: 10pt;">//add the found customers</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList result = </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> CustomerTransList(custTrans);</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">(next != </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> result.setNext(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> LinkTrans(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"next"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, next.toString()));</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> } </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">else</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> result.setNext(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> LinkTrans(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"next"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, </span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">""</span><span style="color: black; font-family: Consolas; font-size: 10pt;">));</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"></span></div><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">if</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">(prev != </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">null</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;">) {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> result.setPrevious(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> LinkTrans(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"prev"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, prev.toString()));</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> } </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">else</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> {</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> result.setPrevious(</span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">new</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> LinkTrans(</span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">"prev"</span><span style="color: black; font-family: Consolas; font-size: 10pt;">, </span><span style="color: #2a00ff; font-family: Consolas; font-size: 10pt;">""</span><span style="color: black; font-family: Consolas; font-size: 10pt;">));</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> }</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><b><span style="color: #7f0055; font-family: Consolas; font-size: 10pt;">return</span></b><span style="color: black; font-family: Consolas; font-size: 10pt;"> result;</span><span style="font-family: Consolas; font-size: 10pt;"><o:p></o:p></span></div></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-44450900709152678472011-09-08T20:38:00.000-07:002011-09-08T20:41:08.029-07:00What is REST?<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
REST or REpresentational State transfer is a web service paradigm with the main principals: </div>
<div style="text-align: justify;">
<br />
<span style="font-size: large;"><b>Addressable resources </b></span><br />
<span style="font-size: small;"><b></b></span><br />
Each resources within the system can be addressed by a unique URI.In the case of REST, a resources is data on the server that you want to expose a way to retrieve or manipulate. The data could be anything from a customer to the current time in London.<br />
<ul>
</ul>
Although HTTP isn't required for REST I will focus on the use of HTTP as the transport layer for REST. To interact with a resources we need to access it though a URI, these are much like the URIs we use to access web pages . For example one resources URI could be <br />
<ul>
</ul>
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">http://localhost:8080/platform/rest/customers?start=100&size=20</span></blockquote>
In this case the resources lists customers, and we can pass parameters to the resource to specify more information. Here we are telling the resource that we want 20 customers starting from the 100th customer. <br />
These resources can be considered the nouns for REST, they define a person, place or thing. As such I try to use nouns as the URI path. <br />
<br />
<span style="font-size: large;"><b>A standardized interface </b></span><br />
There is a limited set of methods and operations that we can use to access our resources. <br />
<br />
Expanding on the idea of nouns and verbs where the addressable resources URI was the noun the interface is the verb. In the case of HTTP we use GET, POST, PUT and DELETE. So with REST we don't want a URI<br />
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">http://localhost:8080/platform/rest/createcustomer </span></blockquote>
Since "createcustomer" is a verb, when we want resources to be nouns. Instead we would have URI<br />
<blockquote>
<span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">http://localhost:8080/platform/rest/customer</span></span></blockquote>
We would POST or PUT to the URI to tell the server that we want to create a new resource. This would then create a new resources<br />
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">http://localhost:8080/platform/rest/customer/56743</span></blockquote>
If we call that URI with the GET operation we would get the details for the customer with the ID 56743, and if we want to update that customer we would call POST to the same URI. So the same URI can have different actions depending on the verb. <br />
<br />
<span style="font-size: large;"><b>Self-descriptive messages </b></span><br />
<br />
Each request to the server specifies the media type that it produces and consumes such that a single resource can handle different formats. Such as JSON for java script or XML for Flex applications <br />
<br />
<span style="font-size: large;"><b>Communicate Stateless </b></span><br />
<br />
Each request must provide sufficient information, so the server can respond to it without needing client session state on the server. <br />
<br />
There is no client state held in the server, only the state each resources that the client accesses. So each request to the server should contain everything the server needs to fulfill the request. For example, when we sent the request to the server <br />
<blockquote style="font-family: "Courier New",Courier,monospace;">
<blockquote>
<span style="font-size: x-small;">http://localhost:8080/platform/rest/customers?start=100&size=20</span></blockquote>
</blockquote>
Instead of the server remembering where we are in the list of customers. We tell the server what resources we need based on our clients state. This also implies that for secure resources we need to send authentication information with each request, since the server does not have a session. In later posts I will cover how I managed these constraints. <br />
<br />
<br />
<span style="font-size: large;"><b>Hypermedia as the engine of application state (HATEOAS) </b></span><br />
<br />
Clients discover the resources and interfaces available to them from the server not from fixed hardcoded paths. <br />
<br />
This is an area that my rest platform is weak on, but let's look at an example. Lets say we request some information on a customer from URI<br />
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">http://localhost:8080/platform/rest/customer/56743 </span></blockquote>
This returns an XML document: <br />
<br />
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><customerTrans customerId="56743> </span><br />
<span style="font-size: x-small;"> <personaName>Colin</personaName> </span><br />
<span style="font-size: x-small;"></customerTrans> </span></blockquote>
<br />
After receiving the response we may want to perform further operations on this customer such as adding them as a friend. Based on the response we got back we would have to know the hard coded URI to add the customer as a friend. But using HATEOAS, when we get the original response for the customer it tells us all the operations we can do on it. <br />
<br />
Such as: <br />
<div style="text-align: left;">
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><customerTrans customerId="56743"> </span><br />
<span style="font-size: x-small;"> <personaName>Colin</personaName></span><br />
<span style="font-size: x-small;"> <link rel="addFriend" method="post </span><span style="font-size: x-small;"> <br /> href="http://localhost:8080/platform/rest/friend/56743" /></span><br />
<span style="font-size: x-small;"></customerTrans> </span></blockquote>
</div>
<br />
If this user was already a friend maybe we would get back a different set of URI's that allow us to remove friends and send messages. <br />
<br />
<div style="text-align: left;">
<blockquote style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><customerTrans customerId="56743"> </span><br />
<span style="font-size: x-small;"> <personaName>Colin</personaName> </span><br />
<span style="font-size: x-small;"> <link rel="removeFriend" method="delete" </span>
<br />
<span style="font-size: x-small;"> href="http://localhost:8080/platform/rest/friend/56743" /> </span><br />
<span style="font-size: x-small;"> <link rel="message" method="post" </span> <br />
<span style="font-size: x-small;"> href="http://localhost:8080/platform/rest/mgs/56743" /> </span><br />
<span style="font-size: x-small;"></customerTrans> </span>
</blockquote>
</div>
As you can see in the above example, we get URI's back from the server that tell the client what actions are possible. </div>
<div style="text-align: justify;">
</div>
Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-757191430225128992011-08-13T18:05:00.000-07:002011-09-08T14:46:09.957-07:00Introduction and setup instructions<div style="text-align: justify;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">I have been working on a project in my spare time to learn new tools and technologies. It is a Java rest server stack using Jersey with a MySQL Database. </span> It has a Flex client as an example of how to communicate between a server and client. <span style="font-size: small;"> While working on this project I have learned so much from Blogs, Forums, books and technical documents, that I felt I needed to give back to the community. So I released all the code I wrote as open source and I am writing this blog to describe how and why I created each section. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">You can download the code for the server stack and the Flex client at </span></div><blockquote style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><div class="MsoNormal" style="text-align: justify;"><span style="font-size: small;"><a href="http://code.google.com/p/java-rest-platform/">http://code.google.com/p/java-rest-platform/</a> </span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">I have given a large effort into following best practices and design patterns, but as with everyone i am still learning. So if you see any areas we can improve this code please feel free to let me know. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"> </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"><br />
</span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;"> </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"><span style="font-size: small;">Now as a first step I will go though how to setup everything need to run the server stack. The instructions will be broken up into the next few blog posts. </span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-18126004428608922872011-08-13T17:52:00.000-07:002011-09-08T15:04:22.866-07:00Tools we need to install<div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Make sure when you download these you get all 64 bit or all 32 bit. Mixing and matching doesn't always work. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Below is the name and location of all the tools needed to run this server stack and the version that it was build and tested with </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: medium none; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; margin-left: 26.7pt;"><tbody>
<tr> <td style="border: 1pt solid black; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Tool</span></div></td> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: black black black -moz-use-text-color; border-style: solid solid solid none; border-width: 1pt 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Location</span></div></td> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: black black black -moz-use-text-color; border-style: solid solid solid none; border-width: 1pt 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Built against version</span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Eclipse</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://www.eclipse.org/downloads/"><span style="color: blue;">http://www.eclipse.org/downloads/</span></a></span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Indigo</span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Tomcat</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://tomcat.apache.org/"><span style="color: blue;">http://tomcat.apache.org/</span></a></span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">7.0.19</span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Maven</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://maven.apache.org/"><span style="color: blue;">http://maven.apache.org/</span></a></span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">3.0.3</span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Java EE SDK</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://www.oracle.com/technetwork/java/javaee/downloads/index.html"><span style="color: blue;">http://www.oracle.com/technetwork/java/javaee/downloads/</span></a> </span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">Java 6 Update 3</span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">MySQL</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://www.mysql.com/"><span style="color: blue;">http://www.mysql.com/</span></a></span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal;"><span style="font-size: small;">Community Server 5.5.15 And Workbench <span class="subtext">5.2.34</span></span></div></td> </tr>
<tr> <td style="-moz-border-bottom-colors: none; -moz-border-image: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; border-color: -moz-use-text-color black black; border-style: none solid solid; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 66pt;" valign="top" width="88"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">TortoiseSVN</span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 304.7pt;" valign="top" width="406"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://tortoisesvn.tigris.org/">http://tortoisesvn.tigris.org/</a></span></div></td> <td style="border-color: -moz-use-text-color black black -moz-use-text-color; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 81.4pt;" valign="top" width="109"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">1.6.16</span></div></td> </tr>
</tbody></table><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">There is lots of great information out there on how to install these tools, so I won't go into too many details. I will just cover the slightly more confusing parts.</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">- After installing Java make sure the environment variables are setup correctly. On Windows 7 you can type in "<b>system</b>" in the start menu, then select "system" under the control panel divider. Now select "<b>Advanced system settings</b>" on the right, then Environment Variables. </span><br />
<span style="font-size: small;">Under the system variables, we need to make sure there is a - JAVA_HOME - system variable. This is the directory where the Java 6 SDK is installed. (default C:\Program Files\Java\jdk1.6.0_24) If there is not one click the new and use the Variable name:</span><span style="font-size: small;"> JAVA_HOME and Variable value: your install directory for the jdk.</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">We also need to update the - PATH - system variable to include the bin directory for java. (default ;%JAVA_HOME%\bin) If it is not there just add it to the end of the Variable value: when hitting edit</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">After installing tomcat, it is advisable to turn off the tomcat service since you don't need it always running. To do this type "<b>services</b>" in the start menu, Find Apache Tomcat 7 and right click to get properties. Now under start-up type set it to manual. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">To install maven see the readme.txt that comes with maven. </span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-78984759065310928892011-08-13T17:10:00.000-07:002011-08-25T15:07:56.218-07:00Checking out the Google code project<div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">The Google code project uses Subversion for its version control system. Wikipedia defines version control as </span></div><blockquote style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">"any practice that tracks and provides control over changes to source code. Software developers sometimes use revision control software to maintain documentation and configuration files as well as source code."</span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">In our case we need to use TortoiseSVN as the client program to download the source code from the Google Subversion servers, this is called "checking out" the code.</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: left;"><span style="font-size: small;">To check out the code select the directory you want to download it to. Right click on the directory and select SVN Checkout in the context menu.</span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: left;"><blockquote><div class="separator" style="clear: both;"><span style="font-size: small;"><a href="http://4.bp.blogspot.com/-N4BBB0xzwJs/TkcdbCBp9dI/AAAAAAAAADI/fmFs0YeVwSw/s1600/TortoiseCheckout.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-N4BBB0xzwJs/TkcdbCBp9dI/AAAAAAAAADI/fmFs0YeVwSw/s1600/TortoiseCheckout.jpg" /></a></span></div></blockquote></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: left;"><span style="font-size: small;"> Now we need to point SVN to the Google servers by entering the URL </span></div><blockquote style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;"> <a href="http://java-rest-platform.googlecode.com/svn/trunk/">http://java-rest-platform.googlecode.com/svn/trunk/</a></span><span style="font-size: small;"> </span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;"> into the "URL of repository field". We can just use the defaults for the rest as seen below. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;"><br />
</span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><span style="font-size: small;"><a href="http://1.bp.blogspot.com/-0v4gbjyQMaA/TkcdtAHy5TI/AAAAAAAAADM/IRIWk5z3Z-c/s1600/TortoiseRepository.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-0v4gbjyQMaA/TkcdtAHy5TI/AAAAAAAAADM/IRIWk5z3Z-c/s1600/TortoiseRepository.jpg" /></a></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: center;"><span style="font-size: small;"></span><span style="font-size: small;"></span></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: justify;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">After clicking OK, it will download all the source code into the directory you selected. We now have all of the source code needed to compile and run the project. </span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-5458603507088310662011-08-13T17:00:00.000-07:002011-09-01T16:17:40.416-07:00Maven and eclipse<div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Maven is a powerful tool to make building and managing java projects easier. It is extremely quick and easy to get a basic project setup using maven, but for the more advanced user there is a lot of features and even more plugins to extend the functionality. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Maven uses a Project Object Model (POM.xml) file to describe and build your project. This is a maven build file that defines all the dependencies and rules needed to compile this project.</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">The pom.xml is located under</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;"> <repository>\server\rest\pom.xml</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Open a command window by running cmd.exe then navigate to the pom.xml file.</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">One feature of Maven is that we can have maven create the eclipse project for us. To create the eclipse project type </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;"> mvn eclipse:eclipse -Dwtpversion=2.0 </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">into the command window. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">This is going to create the eclipse project files such as .settings, .classpath, and .project. It is also going to download a lot of dependencies that we defined, so instead of having to hunt down dozens of jar libraries, we can tell maven what libraries we want, and it will download them for us. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Maven saves these library's to </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;"> C:\Users\<your login>\.m2\repository</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><br />
</div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Now open eclipse. When it asks for the workspace, enter in the directory you want to work in. This should not be the same directory you just build the project in, but the workspace directory should contain your project. So in my case my working directory is </span><br />
<blockquote><span style="font-size: 12pt;">D:\java-rest\server and my project is in D:\java-rest\server\rest.</span></blockquote></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Once eclipse has opened we need to import our new project that we created with maven. So Select file->import->general->existing project into workspace. Select the directory with the .settings, .classpath, and .project files. </span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><a href="http://1.bp.blogspot.com/-jlw1Xnr9zGo/Tkcl50lp0CI/AAAAAAAAADQ/c1eswQUerOQ/s1600/eclipseMavenImport.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://1.bp.blogspot.com/-jlw1Xnr9zGo/Tkcl50lp0CI/AAAAAAAAADQ/c1eswQUerOQ/s400/eclipseMavenImport.jpg" width="343" /></a></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: 12pt;"><br />
</span><span style="font-size: 12pt;"></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">You probably have several build errors such as:</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;"> Unbound classpath variable: 'M2_REPO/asm/asm/3.1/asm-3.1.jar' </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">This is because eclipse doesn't know where maven downloaded these jar files to. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">So we need to add maven integration with eclipse so we can automatically launch maven builds from eclipse. For this we will use a eclipse plug-in </span><a href="http://wiki.eclipse.org/Maven_Integration"><span style="font-size: 12pt;">M2E</span></a><span style="font-size: 12pt;"></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Luckily eclipse has a great plug-in architecture that makes it extremely easy download and install plugins directly from eclipse. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">To install plugins we need to go to help->install new software then paste in</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;"> </span><a href="http://download.eclipse.org/technology/m2e/releases"><span style="color: blue; font-size: 12pt;">http://download.eclipse.org/technology/m2e/releases</span></a><span style="font-size: 12pt;"> </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">into the "works with:" field.</span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><a href="http://1.bp.blogspot.com/-FrT9-0NkTjM/TkcmHALH2YI/AAAAAAAAADU/XH2fSmKi87A/s1600/eclipseInstall.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="192" src="http://1.bp.blogspot.com/-FrT9-0NkTjM/TkcmHALH2YI/AAAAAAAAADU/XH2fSmKi87A/s400/eclipseInstall.jpg" width="400" /></a></div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><br />
</div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">If you want to save this site you can click add then give it a name so you can remember it later. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Click ok and wait for it to download the packages. then click the check box and click next. Finish the install. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Now we need to install the maven wtp for eclipse plugin - </span></div><blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Click the window -> preferences -> maven -> discovery ->open catalog. </span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">Find and select the m2e-wtp plugin to check it off and click finish to install it. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">After these steps we have integrated maven into eclipse but we still need to tell eclipse that this specific project is a maven project. To convert the project to a maven project by right clicking on your project then configure-> convert to maven project</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><br />
</div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><a href="http://3.bp.blogspot.com/-8nK570X06HU/TkcnPFPxsKI/AAAAAAAAADY/lFSGWldGrHQ/s1600/ConfigureAsMavenProject.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="97" src="http://3.bp.blogspot.com/-8nK570X06HU/TkcnPFPxsKI/AAAAAAAAADY/lFSGWldGrHQ/s400/ConfigureAsMavenProject.jpg" width="400" /></a></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: 12pt;"><br />
</span><span style="font-size: 12pt;"></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: 12pt;">(above picture is skipping some menu items at the black bar to make the list shorter)</span><br />
<br />
<span style="font-size: 12pt;">*Update</span><br />
<span style="font-size: 12pt;"> - It looks like the </span><span style="font-size: 12pt;">m2e-wtp plugin is no longer available though the m2e catalog. But you can still install it using the install software method. </span><span style="font-size: 12pt;">With the same install instructions as we used to install m2e add the link to the </span><span style="font-size: 12pt;">help->install new software.</span><br />
<span style="font-size: 12pt;">Updates at: </span><br />
<span style="font-size: 12pt;"></span><br />
<blockquote><span style="font-size: small;"><a href="http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/">http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/</a></span><span style="font-size: small;"></span></blockquote><span style="font-size: small;"> or</span><span style="font-size: small;"> </span><br />
<blockquote><span style="font-size: small;"><a href="http://m2eclipse.sonatype.org/sites/m2e-extras">http://m2eclipse.sonatype.org/sites/m2e-extras</a></span></blockquote><blockquote></blockquote><span style="font-size: 12pt;"><br />
</span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-7281272590473323842011-08-13T16:50:00.000-07:002011-08-25T15:08:16.976-07:00Tomcat eclipse integration<div style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Now we need to add the tomcat server to eclipse:</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">A good blog post about this is here </span></div><blockquote style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><a href="http://www.coreservlets.com/Apache-Tomcat-Tutorial/eclipse.html"><span style="color: blue;">http://www.coreservlets.com/Apache-Tomcat-Tutorial/eclipse.html</span></a></span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">First we need to make sure that the server tab is showing in eclipse. Select </span></div><blockquote style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;"><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;">eclipse -> window-> show view-> servers or window-> show view-> other -> find the servers.</span></div></blockquote><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Now in the server view right click and select new server. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Select the Apache tomcat 7 server.</span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><span style="font-size: small;"><a href="http://2.bp.blogspot.com/-qT04lnp5rnM/Tkcn1E3PcGI/AAAAAAAAADc/_HVcX6i1TPE/s1600/NewServer.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://2.bp.blogspot.com/-qT04lnp5rnM/Tkcn1E3PcGI/AAAAAAAAADc/_HVcX6i1TPE/s400/NewServer.jpg" width="382" /></a></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Using the default settings above should be fine. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">In the next step you need to tell eclipse where Apache is installed to. Under Tomcat installation directory click "Browse.." and navigate to the directory you installed tomcat. For me it was</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"> </span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><span style="font-size: small;"><a href="http://3.bp.blogspot.com/-P7FLssprk7Q/TkcoAM24qLI/AAAAAAAAADg/G1MjBldmH9A/s1600/NewServerPart2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="321" src="http://3.bp.blogspot.com/-P7FLssprk7Q/TkcoAM24qLI/AAAAAAAAADg/G1MjBldmH9A/s400/NewServerPart2.jpg" width="400" /></a></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">Now click next. Now we can tell tomcat what resources to run. The resources is our rest application. So click the "rest" resource and select add, then click finish. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="separator" style="clear: both; font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; text-align: center;"><span style="font-size: small;"><a href="http://3.bp.blogspot.com/-4ayNd-AcdMs/TkcoKYSNNFI/AAAAAAAAADk/A8DBlX6SR_M/s1600/NewServerPart3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="378" src="http://3.bp.blogspot.com/-4ayNd-AcdMs/TkcoKYSNNFI/AAAAAAAAADk/A8DBlX6SR_M/s400/NewServerPart3.jpg" width="400" /></a></span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm;"><span style="font-size: small;"><br />
</span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">To test right click on the server and select start. </span></div><div class="MsoNormal" style="font-family: "Helvetica Neue",Arial,Helvetica,sans-serif; line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-size: small;">If you get an error saying the port 8080 is already in use you need to make sure that the tomcat service isn't running. To stop the service run administrative tools from the start menu then select services. Find the Apache Tomcat service and stop it. Also set it to manual start-up, since this is your dev machine you don't need it to always be running. Eclipse will launch it, when you start debugging your project. </span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0tag:blogger.com,1999:blog-1640832032455140974.post-67475679499791759572011-08-13T16:30:00.000-07:002011-08-25T15:09:15.791-07:00MySQL setup<div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Now we need to setup MySQL, first we are going to create a user for the java rest stack to connect to MySQL with. Select Server Administration, by double clicking your DB you setup. </span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 5.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 5.0pt; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-N0YW7MXEFAM/TkcpXrJxgYI/AAAAAAAAADo/CCUoMw3pLYk/s1600/MySQLServerAdministration.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="340" src="http://3.bp.blogspot.com/-N0YW7MXEFAM/TkcpXrJxgYI/AAAAAAAAADo/CCUoMw3pLYk/s400/MySQLServerAdministration.jpg" width="400" /></a></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 5.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 5.0pt; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;"></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 5.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 5.0pt; mso-layout-grid-align: none; text-autospace: none;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Under security click on "Users and Privileges" then to create an account add account on the bottom of the page. </span></div><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-R1uwVXqC-tg/TkcpmLm-UGI/AAAAAAAAADs/Z4FTwoj4Lpo/s1600/MySQLAddUser.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="312" src="http://4.bp.blogspot.com/-R1uwVXqC-tg/TkcpmLm-UGI/AAAAAAAAADs/Z4FTwoj4Lpo/s400/MySQLAddUser.jpg" width="400" /></a></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Give them a name and password and remember them, we will need them shortly. When done select apply. This will create a new user in the MySQL.user table, but it will not give the new user any privileges. To give privileges we select "Schema Privileges" tab and highlight the new user. Then click the "Add Entry..." button, use the default settings and select ok. </span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">I gave the user SELECT, INSERT, UPDATE, DELETE, EXECUTE, SHOW, CREATE TEMPORARY TABLES and LOCK TABLES privileges. </span></div><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-Y2vSarFz2m0/Tkcpy_TT1uI/AAAAAAAAADw/9rn-H-Pxt-s/s1600/MySQLUserRights.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="95" src="http://4.bp.blogspot.com/-Y2vSarFz2m0/Tkcpy_TT1uI/AAAAAAAAADw/9rn-H-Pxt-s/s400/MySQLUserRights.jpg" width="400" /></a></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: 5.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 5.0pt; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;"></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">This is so they can manipulate the date in the tables, but do not have the ability to change the tables structure (such as ALTER or DROP). That isn't to say that a user can't destroy a database with the UPDATE ability, it is just harder.</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">When ready save the changes.</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">*Note that these settings are intended for local use in a tutorial, for a production database you would restrict the user even more. </span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Now we need to build the DB, that the rest server will use. Go back to the main page and select your DB under SQL Development.</span></div><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-nAOihxLpaaw/Tkcp-Q_cQuI/AAAAAAAAAD0/A1nIKVCFPyU/s1600/MySQLOpenDB.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="220" src="http://4.bp.blogspot.com/-nAOihxLpaaw/Tkcp-Q_cQuI/AAAAAAAAAD0/A1nIKVCFPyU/s320/MySQLOpenDB.jpg" width="320" /></a></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Open the provided SQL files and execute them in the following order. They need to be in this order because some of the tables have dependencies on other tables. (ie one table has a foreign key constraint that references a primary key of a different table. )</span></div><div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">1.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">createdb.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">2.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">customer.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">3.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">role.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">4.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">customerRoles.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">5.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">session.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">6.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">ServerConfigTypes.sql</span></div><div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">7.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">serverConfig.sql</span></div><div class="MsoListParagraphCxSpLast" style="line-height: normal; margin: 5pt 0cm 5pt 41.25pt; text-align: justify; text-indent: -18pt;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">8.<span style="font: 7pt "Times New Roman";"> </span></span><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">createCustomers.sql</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><u><span style="font-family: "Times New Roman","serif"; font-size: 14pt;"></span></u></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><u><span style="font-family: "Times New Roman","serif"; font-size: 14pt;"></span></u></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><u><span style="font-family: "Times New Roman","serif"; font-size: 14pt;"><span style="text-decoration: none;"></span></span></u></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">Now we need to setup the the rest server stack so it can communicate with mysql db to save our customers in. </span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="font-family: "Times New Roman","serif"; font-size: 12pt;">The java <span style="background: none repeat scroll 0% 0% white;">file com.cred.industries.platform.database</span></span><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 10pt;">.</span><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;">DBConfig it stores the config information we need to connect to the SQL database. Usually it will load the config values from an xml file</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;"> scr/main/resources/dbconfig.xml</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;">But it is also hard-coded with some setup values. (I could not figure out how to get it to load the xml file when running unit tests.) Change the DBUserName and DBPassword in both the .java and .xml file, to match the names of the user you just created in MySQL. You should not use the default setup, as they are not secure user names and passwords. </span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><br />
</div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;">Congratulations, you should now have a working rest server stack. To test it either use the Flex client or run the unit tests under</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;"> </span><span style="background: none repeat scroll 0% 0% rgb(255, 255, 255); color: black; font-family: Consolas; font-size: 10pt;">co</span><span style="color: black; font-family: Consolas; font-size: 10pt;"><span style="background-color: white;">m.</span>cred.industries.platform.test</span><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;">.</span><span style="color: black; font-family: Consolas; font-size: 10pt;">CustomerTest</span></div><div class="MsoNormal" style="line-height: normal; margin: 5pt 0cm; text-align: justify;"><span style="color: black; font-family: "Times New Roman","serif"; font-size: 12pt;">To do this, click on the debug drop-down and select CustomerTest unit test in eclipse. </span></div><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-AR6wDzqSaag/TkcqtPmbHDI/AAAAAAAAAD4/9Wh32vGDzPw/s1600/EclipseUnitTest.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-AR6wDzqSaag/TkcqtPmbHDI/AAAAAAAAAD4/9Wh32vGDzPw/s1600/EclipseUnitTest.jpg" /></a></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 5.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 5.0pt; mso-layout-grid-align: none; text-autospace: none;"><span style="color: black; font-family: Consolas; font-size: 10pt;"><br />
</span><span style="color: black; font-family: Consolas; font-size: 10pt;"> </span><span style="background: none repeat scroll 0% 0% white; font-family: "Times New Roman","serif"; font-size: 12pt;"></span></div>Colin Redmondhttp://www.blogger.com/profile/14664134532870740587noreply@blogger.com0