<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IN MY OPINION &#187; Java</title>
	<atom:link href="http://blog.gomilko.com/category/java/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.gomilko.com</link>
	<description>Andrew Gomilko's Blog</description>
	<lastBuildDate>Mon, 14 Jan 2008 08:09:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Acegi Conditional Roles</title>
		<link>http://blog.gomilko.com/2008/01/12/acegi-conditional-roles</link>
		<comments>http://blog.gomilko.com/2008/01/12/acegi-conditional-roles#comments</comments>
		<pubDate>Sat, 12 Jan 2008 16:54:50 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2008/01/12/acegi-conditional-roles/</guid>
		<description><![CDATA[Acegi &#8211; the Java security framework
I suppose that you know what is Spring Security (former Acegi project) is, or at least you are aware of its wide securing abilities for a Java web application. Besides lots of features which Acegi offers you out-of-the box, there are many configurable options. This time, I had a task [...]]]></description>
			<content:encoded><![CDATA[<h2>Acegi &#8211; the Java security framework</h2>
<p>I suppose that you know what is <a href="http://www.acegisecurity.org/">Spring Security</a> (former Acegi project) is, or at least you are aware of its wide securing abilities for a Java web application. Besides lots of features which Acegi offers you <a href="http://blog.springsource.com/main/2007/12/06/whats-new-in-spring-security-2/">out-of-the box</a>, there are many configurable options. This time, I had a task to implement a functionality which allows restricting access to method invocations based not only on the granted roles to the logged in user, but taking in account passed parameters as well. Let&#8217;s see the result.</p>
<h2>@Secured annotations</h2>
<p>Assume that you are developing pretty common web application with standard security model with users, granted roles, etc. Of course, the main goal of the security infrastructure is accepting/denying user access to different parts of your application based on the granted access rights. Acegi offers you two main ways of controlling access: <a href="http://www-128.ibm.com/developerworks/java/library/j-acegi1/">URL based</a> which analyzes the clients http requests to your application and based on the <a href="http://www.ibm.com/developerworks/java/library/j-acegi3/index.html">AOP principles</a> which controls method invocations. </p>
<p>With the URL based method you can achieve rough securing coverage very simply. For instance, if you have /admin part, you just deny the access to all users who don&#8217;t have ROLE_ADMIN granted role to the URLs which starts from /admin/** string. The part /store you can make available only to users with role ROLE_CUSTOMER, etc. You can cover all you URLs with such rules, but unfortunately, if you want more precise control, you have to write lots of business logic code in your Controllers/Managers which performs additional checks in the methods body.</p>
<p>Let&#8217;s consider an idiomatic example (no real DAOs, no Hibernate etc) consisting of: </p>
<ul>
<li>a controller ShoppingCartAddItemController which is mapped to /shop/card/addItem.jsp url,
<li>
<li>a service class IShoppingCartManager which provides with business logic,
<li>
<li>ROLE_USER which is granted to Scott user:</li>
</ul>
<pre class="prettyprint">
public class ShoppingCartAddItemController extends AbstractController {

    private IShoppingCartManager shoppingCartManager;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Integer customerId = ServletRequestUtils.getIntParameter(request, "customerId");
        Integer itemId = ServletRequestUtils.getIntParameter(request, "itemId");
        Integer amount = ServletRequestUtils.getIntParameter(request, "amount");

        shoppingCartManager.addItem(customerId, itemId, amount);

        return new ModelAndView("redirect:/shop/item/congratulations.jsp");
    }

    public void setShoppingCartManager(IShoppingCartManager shoppingCartManager) {
        this.shoppingCartManager = shoppingCartManager;
    }
}

public interface IShoppingCartManager {

    @Secured({"ROLE_USER" })
    void addItem(Integer customerId, Integer itemId, Integer amount);

    @Secured({"ROLE_USER" })
    void deleteItem(Integer customerId, Integer itemId);

}

public class ShoppingCartManager implements IShoppingCartManager {

    public void addItem(Integer customerId, Integer itemId, Integer amount) {
        // use DAO
    }

    public void deleteItem(Integer customerId, Integer itemId) {
        // use DAO
    }

}
</pre>
<p>The ROLE_USER guarantees that the secured method can be invoked from the controller if the logged in user is granted by this role. But in this case, being logged in, I can change the URL parameter <i>customerId</i> to another value and Acegi won&#8217;t check it. To prevent it, we need to paste something like this in every Manager method (or Controller or Filter for every URL which matches /shop/**/*?customerId=&#8230; pattern):</p>
<pre class="prettyprint">
    public void addItem(Integer customerId, Integer itemId, Integer amount) {
         Integer loggedCustomerId = securityManager.getLoggedCustomerId();
         if (loggedCustomerId != customerId) {
              throw new AccessDeniedException("access denied");
         }
         // business logic
    }
</pre>
<p>The main question is: how can we minimize coding, if we know that lots of methods requires the same type of access check? The answer is pretty obvious &#8211; we need a AOP aspect which would do this routine instead of us. From another point of view, it would be great if we could employ already used @Secured annotation facilities. </p>
<h2>Conditional Roles</h2>
<p>After brief <a href="http://www.google.com/search?q=Acegi+Conditional+Roles">googling</a>, I caught the first clue. It was a <a href="http://forum.springframework.org/showthread.php?t=22321">similar question</a> on Spring framework forum, and eventually I came to the <a href="http://forum.springframework.org/showthread.php?t=39385">topic</a> which lead to Conditional Roles patch <a href="http://jira.springframework.org/browse/SEC-273">SEC-273</a>. It was an implementation by Usama Rashwan of the idea of <a href="http://karldmoore.blogspot.com/">Karl Moore</a>.</p>
<p>The idea is elegant and straightforward. All tricks with method invocations in Acegi could be done by implementing <a href="http://www.acegisecurity.org/acegi-security/apidocs/org/acegisecurity/vote/RoleVoter.html">RoleVoter</a> interface where you can implement your own access decision logic. But writing your own RoleVoter for every scenario is violating DRY principle. So, it was suggested to write one RoleVoter and to use a Scripting Language like OGNL, <a href="http://mvel.codehaus.org/">MVEL</a> to write conditions right in the role names after special delimiter &#8220;::&#8221;.</p>
<p>In our case, the new @Secured annotation changes to:</p>
<pre class="prettyprint">
    @Secured({"ROLE_USER::authentication.principal.customerId == arg0" })
    void addItem(Integer customerId, Integer itemId, Integer amount);
</pre>
<p>where <i>authentication</i> is SecurityContextHolder.getContext().getAuthentication() object which usually holds all logged in user details. This object is put to MVEL context by AbstractInvocationChecker. Besides this object, you can operate by arguments passed to the secured method, here arg0 is the first parameter passed to the method. The MVEL scripting language is very flexible, it can cope with JavaBeans, operations on collections, etc. Of course you can&#8217;t do there rocket science computations, but for our needs it is enough.</p>
<p>I suggest the next way of usage:</p>
<ul>
<li>Extend <a href="http://www.acegisecurity.org/acegi-security/apidocs/org/acegisecurity/userdetails/UserDetails.html">UserDetails</a> class with information required in decision point when the secured method is invoked. It could be ids of objects which is allowed to be manipulated by logged in user.</li>
<li>Populate UserDetails with all needed information while a user logging in by UserDetailsService. This object is available as &#8220;authentication.principal&#8221; object in MVEL context.</li>
<li>Write conditional roles based on this objects mixing with passed arguments.</li>
<li>Update the SecurityContextHolder.getContext().getAuthentication() if necessary during the user activity.</li>
</ul>
<h2>Buzz</h2>
<p>If you are interested in this topic &#8211; just download the patch, it has a comprehensive example of usage and clear source code. The   JIRA issue stated that this patch goes to 2.0.0 M2 which is going to be released soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2008/01/12/acegi-conditional-roles/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>YUI Compression tool as Ant Task</title>
		<link>http://blog.gomilko.com/2007/11/29/yui-compression-tool-as-ant-task</link>
		<comments>http://blog.gomilko.com/2007/11/29/yui-compression-tool-as-ant-task#comments</comments>
		<pubDate>Thu, 29 Nov 2007 18:45:52 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/11/29/yui-compression-tool-as-ant-task/</guid>
		<description><![CDATA[Minification over obfuscation and other uglifier techniques
Every mature web application enters phase when you need to optimize overall performance. Unfortunately, choosing right DB, tuning your SQL queries and using sophisticated load-balancers are not enough if a user&#8217;s browser have to wait for a 5 seconds to load your overloaded AJAX-based grid. And if it happens, [...]]]></description>
			<content:encoded><![CDATA[<h2>Minification over obfuscation and other uglifier techniques</h2>
<p>Every mature web application enters phase when you need to optimize overall performance. Unfortunately, choosing right DB, tuning your SQL queries and using sophisticated load-balancers are not enough if a user&#8217;s browser have to wait for a 5 seconds to load your overloaded AJAX-based grid. And if it happens, you have to think a lot and try many client-side optimization solutions. Now, developers have a great analyzing <a href="http://developer.yahoo.com/yslow/">YSlow</a> tool which can give you dozens of improvement tips. Actually, you can do all this work just analyzing response headers and content with another excellent tool &#8211; <a href="http://www.getfirebug.com/">Firebug</a>.</p>
<p>So, when our <a href="http://sonopia.com">application</a> had been optimized against almost all <a href="http://developer.yahoo.com/performance/rules.html">possible bottlenecks</a>, it was decided to use a sort of compression (apart from gzip, which is already employed) of resource files (js, css) based on its structure.</p>
<p>After a little comparison and talks with DHTML gurus, I chose using <a href="http://yuiblog.com/blog/2006/03/06/minification-v-obfuscation/">minification instead of obfuscation</a>, because the later one could be really source of evil bugs. Nowadays, the best supported minifier I found is <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>. </p>
<h2>YUI Compressor</h2>
<p>I chose YUI Compressor because it is Java-based free open-sourced tool which means that I always can investigate why something goes wrong, and probable contribute something valuable. This tool can be used as a standalone command line application or used from your code instantiating necessary classes. It could parse javascript files as well as css. Also there is wide range of parameters (e.g. shorten local variables or not, preserve semicolons or not).</p>
<h2>Ant script</h2>
<p>Good overview of the tool usage from Ant script is <a href="http://www.julienlecomte.net/blog/2007/09/16/">here</a>. However, it is just calls to the compressor as to command-line program, which is verbose and sometimes not appropriate. Usually for such tools, there are custom Ant tasks. And it was obvious, that since sources of the tool is available, the Ant task should exist for the YUI Compressor too. After some googling, I found what I needed &#8211; semi-abandoned page <a href="http://www.ubik-ingenierie.com/ubikwiki/index.php?title=Minifying_JS/CSS">Minifying JS/CSS</a>. Many thanks to the author &#8211; Philippe Mouawad, well done! I would like to see this patch in the release and reference on the official site.</p>
<p>Simple example of usage:</p>
<div class="dean_ch" style="white-space: wrap;">
<p>&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;path</span> <span class="re0">id</span>=<span class="st0">&quot;yuicompressor.classpath&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">dir</span>=<span class="st0">&quot;${yuicompressor.dir}&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/yuicompressor-2.2.5.jar&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/YUIAnt.jar&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211; include name=&quot;**/rhino*.jar&quot;/ &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fileset<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/path<span class="re2">&gt;</span></span></span>&nbsp;</p>
<p>&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;minify-js-css&quot;</span> <span class="re0">description</span>=<span class="st0">&quot;Minifiy a set of files&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;taskdef</span> <span class="re0">name</span>=<span class="st0">&quot;yuicompress&quot;</span> <span class="re0">classname</span>=<span class="st0">&quot;com.yahoo.platform.yui.compressor.YUICompressTask&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;classpath<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;path</span> <span class="re0">refid</span>=<span class="st0">&quot;yuicompressor.classpath&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/classpath<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/taskdef<span class="re2">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;delete</span> <span class="re0">dir</span>=<span class="st0">&quot;${js-min.dir}&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mkdir</span> <span class="re0">dir</span>=<span class="st0">&quot;${js-min.dir}&quot;</span> <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;yuicompress</span> <span class="re0">linebreak</span>=<span class="st0">&quot;300&quot;</span> <span class="re0">warn</span>=<span class="st0">&quot;false&quot;</span> <span class="re0">munge</span>=<span class="st0">&quot;yes&quot;</span> <span class="re0">preserveallsemicolons</span>=<span class="st0">&quot;true&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">outputfolder</span>=<span class="st0">&quot;${js-min.dir}&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fileset</span> <span class="re0">dir</span>=<span class="st0">&quot;${js.dir}&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;include</span> <span class="re0">name</span>=<span class="st0">&quot;**/*.js&quot;</span> <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fileset<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/yuicompress<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<h2>Encountered problems</h2>
<p>Everything works fine except one malicious trouble, I was struggling with for a day. If Rhino (which is stated as dependency library)<br />
was included in task classpath, all returning characters &#8220;\n&#8221; was pasted as a new line, and an JS error occurred.</p>
<div class="dean_ch" style="white-space: wrap;">
var a = &quot;aaa\nbbb&quot;;<br />
// turns after minification into:<br />
var a = &quot;aaa<br />
bbb&quot;;<br />
&nbsp;</div>
<p>This bug was eliminated by removing Rhino jar from classpath.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/11/29/yui-compression-tool-as-ant-task/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sysdeo Tomcat plugin configuration file</title>
		<link>http://blog.gomilko.com/2007/09/12/sysdeo-tomcat-plugin-configuration-file</link>
		<comments>http://blog.gomilko.com/2007/09/12/sysdeo-tomcat-plugin-configuration-file#comments</comments>
		<pubDate>Wed, 12 Sep 2007 19:42:30 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/09/12/sysdeo-tomcat-plugin-configuration-file/</guid>
		<description><![CDATA[I use in all my web based Java application Tomcat as an IDE and Sysdeo Tomcat launcher for web container starting and stopping. Every project has it&#8217;s own configuration of Tomcat launch process. And if you have many workspaces (e.g. couple of ongoing branches &#8211; release on production, development version, etc.) you must configure this [...]]]></description>
			<content:encoded><![CDATA[<p>I use in all my web based Java application Tomcat as an IDE and Sysdeo Tomcat launcher for web container starting and stopping. Every project has it&#8217;s own configuration of Tomcat launch process. And if you have many workspaces (e.g. couple of ongoing branches &#8211; release on production, development version, etc.) you must configure this plugin each time you create a new workspace. If you&#8217;re using standard configuration, the only thing you have to do is specifying Tomcat home directory. Whilst, if it is needed to allocate more memory or enable JMX ports, you encounter the problem that you have to enter manually every single parameter in the JVM Settings dialog. There are no Copy All &#038; Paste All functionality, and you can&#8217;t even copy configuration to send it to co-worker or to project wiki. Only one, IMHO, meaningless action you can do is dumping current configuration to the Eclipse log. However, output is messy and hard to read. </p>
<p><img src="http://gallery.gomilko.com/d/2591-1/sysdeo-tomacat-preferences.png" width="600" height="247" alt="preferences" title="preferences" /></p>
<p>Today I decided to find out where this information is stored to know where to paste it next time. The search led me here:<br />
<code>workspace\.metadata\.plugins\org.eclipse.debug.core\.launches\Tomcat 5.x.launch</code>. This is the configuration file which stores everything you can edit through that dialog. Next time, I definitely will edit properties directly there. </p>
<p><img src="http://gallery.gomilko.com/d/2589-1/configuration-file.png" width="600" height="229" alt="configuration file" title="configuration file" /></p>
<p>Also I finally defeated the problem connected to Hot Deploy feature being occurred after I <a href="/2007/07/10/moving-to-eclipse-europa-for-j2ee-programmer/">moved to Eclipse Europa</a>. Since that time, any code modifications caused Tomcat crash, even if signature of classes/methods weren&#8217;t changed. I had to restart it every time and it became tedious. The problem was eliminated after I removed <code>reloadable="true"</code> parameter in the web application <a href="http://tomcat.apache.org/tomcat-5.0-doc/config/context.html">context</a> file.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/09/12/sysdeo-tomcat-plugin-configuration-file/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Playing with UrlRewrite Filter</title>
		<link>http://blog.gomilko.com/2007/07/16/playing-with-urlrewrite-filter</link>
		<comments>http://blog.gomilko.com/2007/07/16/playing-with-urlrewrite-filter#comments</comments>
		<pubDate>Mon, 16 Jul 2007 15:06:18 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/07/16/playing-with-urlrewrite-filter/</guid>
		<description><![CDATA[Introduction
Nowadays, every modern web development environment offers a way to manipulate URL on the fly using a rule-based configuration rather than hard coded program logic. Probably every approach has its origins in famous Apache mod_rewrite module. In the Java world, de facto standard is a wonderful Url Rewrite Filter. It can do everything what you [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Nowadays, every modern web development environment offers a way to manipulate URL on the fly using a rule-based configuration rather than hard coded program logic. Probably every approach has its origins in famous Apache <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">mod_rewrite</a> module. In the Java world, de facto standard is a wonderful <a href="http://tuckey.org/urlrewrite/">Url Rewrite Filter</a>. It can do everything what you expect from such tool and has some delicious topping as a benefit:</p>
<ul>
<li>analyze URL against a pattern (regexp or wildcard)</li>
<li>analyze all possible HTTP data like cookies, request parameters, host, remote info, etc</li>
<li>change data like cookie, session, request attributes</li>
<li>redirect or forward to static or dynamically(based on analysis data) formed URL</li>
<li>run your own rolled Java code (e.g. logging, statistics)</li>
</ul>
<p>During the last few days I got more familiar with this powerful tool and want to show the value it can bring into any Java based Web application. I won&#8217;t describe the syntax of the configuration file because there is a comprehensive <a href="http://tuckey.org/urlrewrite/manual/3.0/">manual</a> which outlines all options. Also I&#8217;m not going to describe simplest use cases, let&#8217;s start from something interesting like the first step in integration with affiliate partner.<br />
For instance, in case of integration with <a href="http://www.cj.com/">Commission Junction</a> affiliate service provider, your application have to set a cookie which identifies a user that come from an affiliate partner, and then redirect the user to the page stated as a request parameter.<br />
So, there are some steps I want to implement with UrlRewrite library:<br />
<code><br />
1. User came to your site by clicking on a banner with link http://example.com/?CJURL=http%3a%2f%2fexample.com%2fregister%2fnew.html (parameter is encoded string http://example.com/register/new.html) Tip: to url encode test data you can use a simple <a href="http://www.opinionatedgeek.com/DotNet/Tools/UrlEncode/Encode.aspx">online encoding tool</a>.<br />
2. Your application recognizes a CJURL request parameter and set a cookie (expire time >= 24h) to know that the user came from CJ affiliate program<br />
3. Redirect the user to the landing page equal to CJURL parameter<br />
</code></p>
<h2>Setting cookies</h3>
<p>This snippet sets cookie if a user comes from our affiliate partner. Despite of the fact that in UrlRewrite manual stated that expire time have to be set in minutes, it actually is in seconds. Probably it is just a typo in the manual, but be careful and alway test real expire date with your browser. It is possible to set all parameters (value can be in the format “[value][:domain[:lifetime[:path]]]”), although we use only two parameters.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;condition</span> <span class="re0">type</span>=<span class="st0">&quot;query-string&quot;</span><span class="re2">&gt;</span></span>^CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/condition<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>^(.*)$<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211; Affiliate service provider &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211; expire time in seconds&#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;set</span> <span class="re0">type</span>=<span class="st0">&quot;cookie&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;asp&quot;</span><span class="re2">&gt;</span></span>cj::86400<span class="sc3"><span class="re1">&lt;/set<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/rule<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<h2>Redirecting to a request parameter value</h2>
<p>It seems to be a simple task to get a value from request query and redirect to url created from that parameter. But you can&#8217;t do it explicitly in UrlRewrite. <a href="http://groups.google.co.nz/group/urlrewrite/browse_thread/thread/3fd3deedca98c0d0">Setting request parameters in <to> </a> is not allowed. There is a technique to parse a query and use the regexp back reference as a value, but you&#8217;ll have an encoded value as a result. We need a real request parameter, because in this case the value will be decoded.<br />
To make it clear I&#8217;ll give some illustration:<br />
1. The ideal solution if UrlRewrite supported it would be:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;condition</span> <span class="re0">type</span>=<span class="st0">&quot;query-string&quot;</span><span class="re2">&gt;</span></span>^CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/condition<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>^(.*$)<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;to</span> <span class="re0">type</span>=<span class="st0">&quot;redirect&quot;</span><span class="re2">&gt;</span></span>%{parameter:CJURL}<span class="sc3"><span class="re1">&lt;/to<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<p>But as I stated earlier, unfortunately we can&#8217;t use request parameter value in &lt;to&gt; clause.</p>
<p>2. If we had a simple parameter like [a-zA-z0-9] without special symbols used in regular URL we can parse it from query string. This approach can be used in beautifying urls according to REST style:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;rule</span> <span class="re0">match-type</span>=<span class="st0">&quot;wildcard&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>/*/*/*<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;to</span> <span class="re0">type</span>=<span class="st0">&quot;forward&quot;</span><span class="re2">&gt;</span></span>/$1.do?$2=$3<span class="sc3"><span class="re1">&lt;/to<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/rule<span class="re2">&gt;</span></span></span> <br />
&nbsp;</div>
<p>But we need to decode this strings, because we can&#8217;t redirect to <strong>http%3a%2f%2fexample.com</strong> before we convert it to <strong>http://example.com</strong>. This conversion is done by container when you ask for a request parameter value. Therefore this solution WON&#8217;T work either:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;condition</span> <span class="re0">type</span>=<span class="st0">&quot;query-string&quot;</span><span class="re2">&gt;</span></span>^CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/condition<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>^(.*)?CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;to</span> <span class="re0">type</span>=<span class="st0">&quot;redirect&quot;</span><span class="re2">&gt;</span></span>$2<span class="sc3"><span class="re1">&lt;/to<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/rule<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<p>3. Luckily we can use in &lt;to&gt; clause a request attribute. The only problem we have to solve is to write the request parameter value to request attribute somehow, and then read this attribute in &lt;to&gt; clause. I didn&#8217;t investigate heavily is it possible using UrlRewrite declarative facilities and employed another great option: calling self rolled Java code. The final version is something like:<br />
urlrewrite.xml:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="coMULTI">&lt;!&#8211; Affiliates tracking &#8211;&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;note<span class="re2">&gt;</span></span></span>Commission Junction affiliate program<span class="sc3"><span class="re1">&lt;/note<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;condition</span> <span class="re0">type</span>=<span class="st0">&quot;query-string&quot;</span><span class="re2">&gt;</span></span>^CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/condition<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>^(.*)?CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;run</span> <span class="re0">class</span>=<span class="st0">&quot;com.example.web.util.RequestToAttributeSetter&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;init-param<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param-name<span class="re2">&gt;</span></span></span>parameterName<span class="sc3"><span class="re1">&lt;/param-name<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param-value<span class="re2">&gt;</span></span></span>CJURL<span class="sc3"><span class="re1">&lt;/param-value<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/init-param<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/run<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/rule<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;rule<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;condition</span> <span class="re0">type</span>=<span class="st0">&quot;query-string&quot;</span><span class="re2">&gt;</span></span>^CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/condition<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;from<span class="re2">&gt;</span></span></span>^(.*)?CJURL=(.*)$<span class="sc3"><span class="re1">&lt;/from<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;to</span> <span class="re0">type</span>=<span class="st0">&quot;redirect&quot;</span><span class="re2">&gt;</span></span>%{attribute:CJURL}<span class="sc3"><span class="re1">&lt;/to<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211; Affiliate service provider &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211; expire time in minutes &#8211;&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;set</span> <span class="re0">type</span>=<span class="st0">&quot;cookie&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;asp&quot;</span><span class="re2">&gt;</span></span>cj::86400<span class="sc3"><span class="re1">&lt;/set<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/rule<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<p>Java code:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">package</span> com.<span class="me1">example</span>.<span class="me1">web</span>.<span class="me1">util</span>;</p>
<p><span class="co2">import javax.servlet.ServletConfig;</span><br />
<span class="co2">import javax.servlet.ServletRequest;</span><br />
<span class="co2">import javax.servlet.ServletResponse;</span><br />
<span class="co2">import javax.servlet.http.HttpServletRequest;</span></p>
<p><span class="coMULTI">/**<br />
&nbsp;* Workaround of UrlRewrite filter restriction which doesn&#8217;t allow setting request parameter as<br />
&nbsp;* an redirect target in &lt;to&gt; tag. &lt;br/&gt;<br />
&nbsp;* Similar solution: http://sujitpal.blogspot.com/2006/09/search-and-replace-with.html<br />
&nbsp;* @author Andrey Gomilko<br />
&nbsp;*/</span><br />
<span class="kw2">public</span> <span class="kw2">class</span> RequestToAttributeSetter <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> parameterName;</p>
<p>&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> run<span class="br0">&#40;</span>ServletRequest request, ServletResponse response<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>parameterName != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest req = <span class="br0">&#40;</span>HttpServletRequest<span class="br0">&#41;</span> request;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> value = req.<span class="me1">getParameter</span><span class="br0">&#40;</span>parameterName<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.<span class="me1">setAttribute</span><span class="br0">&#40;</span>parameterName, value<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> init<span class="br0">&#40;</span>ServletConfig config<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">parameterName</span> = config.<span class="me1">getInitParameter</span><span class="br0">&#40;</span><span class="st0">&quot;parameterName&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> destroy<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span></p>
<p>&nbsp;</p></div>
<h2>Unit testing of UrlRewrite configuration</h2>
<p>Let&#8217;s assume you have already written your own <strong>urlrewrite.xml</strong> file with filter configuration and want to test it. If you decide testing rules on the working server (e.g. Tomcat), you&#8217;ll have to restart it every time you have changed something. More convenient way is to use JUnit tests for this purpose. Fortunately URLRewrite has all necessary internal classes and request/response mock classes  to dry run this filter without container and analyze the result. The very good example which covers basic use cases is <a href="http://sujitpal.blogspot.com/search/label/junit">A JUnit test for UrlRewriteFilter</a>. To test rules corresponding to CJ example we need to test cookies in addition to standard tests. Since our UrlRewrite rule is based on the <strong>host</strong>  HTTP header value, you have to set header values to the MockRequest because it doesn&#8217;t parse propagated url itself. To test CJ example, I got as a base the test snippet from <a href="http://sujitpal.blogspot.com/search/label/junit">A JUnit test for UrlRewriteFilter</a> and added some specific methods:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw4">void</span> assertRedirect<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> fromUrl, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> toUrl, Cookie cookie<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UrlRewriter rewriter = <span class="kw2">new</span> UrlRewriter<span class="br0">&#40;</span>conf<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; MockRequest request = <span class="kw2">new</span> MockRequest<span class="br0">&#40;</span>fromUrl<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURL+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">URL</span></a> url = <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURL+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">URL</span></a><span class="br0">&#40;</span>fromUrl<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">addHeader</span><span class="br0">&#40;</span><span class="st0">&quot;host&quot;</span>, url.<span class="me1">getHost</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>url.<span class="me1">getQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span> != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">setQueryString</span><span class="br0">&#40;</span>url.<span class="me1">getQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AStringTokenizer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">StringTokenizer</span></a> st = <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AStringTokenizer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">StringTokenizer</span></a><span class="br0">&#40;</span>url.<span class="me1">getQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="st0">&quot;&amp;&quot;</span><span class="br0">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>st.<span class="me1">hasMoreElements</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> token = st.<span class="me1">nextToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> parts = token.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot;=&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name = parts<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> value = parts<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">addParameter</span><span class="br0">&#40;</span>name, java.<span class="me1">net</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURLDecoder+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">URLDecoder</span></a>.<span class="me1">decode</span><span class="br0">&#40;</span>value, <span class="st0">&quot;UTF-8&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span><span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMalformedURLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">MalformedURLException</span></a> me<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// do nothing </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; MockResponse response = <span class="kw2">new</span> MockResponse<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; RewrittenUrl rewrittenUrl = rewriter.<span class="me1">processRequest</span><span class="br0">&#40;</span>request, response<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertNotNull<span class="br0">&#40;</span><span class="st0">&quot;Could not redirect URL from:&quot;</span> + fromUrl + <span class="st0">&quot; to:&quot;</span> + toUrl, rewrittenUrl<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;Redirect from:&quot;</span> + fromUrl + <span class="st0">&quot; to:&quot;</span> + toUrl + <span class="st0">&quot; did not succeed&quot;</span>, toUrl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , rewrittenUrl.<span class="me1">getTarget</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span class="br0">&#40;</span>rewrittenUrl.<span class="me1">isRedirect</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; </p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AList+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> cookies = response.<span class="me1">getCookies</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="nu0">1</span>, cookies.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Cookie setCookie = <span class="br0">&#40;</span>Cookie<span class="br0">&#41;</span>cookies.<span class="me1">get</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span>cookie.<span class="me1">getMaxAge</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, setCookie.<span class="me1">getMaxAge</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span>cookie.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, setCookie.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span>cookie.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, setCookie.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testCJRewriteDecoded<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> fromUrl = <span class="st0">&quot;http://example.com/?CJURL=http%3a%2f%2fexample.com%2fregister%2fnew.html&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> toUrl = <span class="st0">&quot;http://example.com/reqister/new.html&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Cookie cookie = <span class="kw2">new</span> Cookie<span class="br0">&#40;</span><span class="st0">&quot;asp&quot;</span>, <span class="st0">&quot;cj&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cookie.<span class="me1">setMaxAge</span><span class="br0">&#40;</span><span class="nu0">86400</span><span class="br0">&#41;</span>; <span class="co1">// 24h</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertRedirect<span class="br0">&#40;</span>fromUrl, toUrl, cookie<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp;</div>
<h2>Conclusion</h2>
<p>The main idea of such tools is to reduce coding and transfer all possible application logic to the well-proven tools which can be easily tuned through configuration files. This approach eliminates number of possible bugs and keep tied logic in one place. So, before writing your own filter, think once more about UrlRewrite, probably it&#8217;ll fit your needs!</p>
<p>And stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/07/16/playing-with-urlrewrite-filter/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Moving to Eclipse Europa for J2EE programmer</title>
		<link>http://blog.gomilko.com/2007/07/10/moving-to-eclipse-europa-for-j2ee-programmer</link>
		<comments>http://blog.gomilko.com/2007/07/10/moving-to-eclipse-europa-for-j2ee-programmer#comments</comments>
		<pubDate>Tue, 10 Jul 2007 21:22:26 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/07/10/moving-to-eclipse-europa-for-j2ee-programmer/</guid>
		<description><![CDATA[Europa release
With a short delay after Europa release announcement, I&#8217;ve moved to it. Thanks to the fact that Eclipse doesn&#8217;t require an installation process and can be just unzipped and run, the last few times I got the tuned Eclipse from my co-workers. Those distributions were well-tuned for J2EE development, with all necessary plugins, project [...]]]></description>
			<content:encoded><![CDATA[<h3>Europa release</h3>
<p>With a short delay after Europa release announcement, I&#8217;ve moved to it. Thanks to the fact that Eclipse doesn&#8217;t require an installation process and can be just unzipped and run, the last few times I got the tuned Eclipse from my co-workers. Those distributions were well-tuned for J2EE development, with all necessary plugins, project <a href="http://checkstyle.sourceforge.net/">checkstyles</a>, attached sources and so on. Seeing as this time nobody around me offered me such a favor, I did everything on my own.<br />
Actually, to develop a J2EE project, you need to install a dozen of plugins (depends on which frameworks are employed) in addition to the bare distribution, so it&#8217;s better to have a cheat sheet every time you start this Eclipse tuning campaign. Since I didn&#8217;t have such a plan, I used my current Eclipse 3.1 working set as an example.<br />
This time, I wrote some short notes during the installation process, and I&#8217;d like to share it here, to have something in the future to stick with.</p>
<h3>Download</h3>
<p>Firstly, you need to download an appropriate Eclipse distribution. However there is already a bundle for J2EE developer, I was interested in installing everything I want from the scratch. Therefore I went to <a href="http://www.eclipse.org/downloads/">http://www.eclipse.org/downloads/</a> and downloaded <strong>Eclipse Classic</strong> distribution for Windows (140 MB). It was extracted to C:\Java\eclipse3.3 directory where I store all Java stuff like IDEs, JDKs, etc. Then, as usually I wanted to create a custom shortcut with extended memory allocation for Eclipse, but suddenly noticed <strong>eclipse.ini</strong> file. To allow Eclipse allocation of more memory, just edit the <strong>eclipse.ini</strong> file (increase Xms and Xmx values):</p>
<div class="dean_ch" style="white-space: wrap;">
-showsplash<br />
org.eclipse.platform<br />
&#8211;launcher.XXMaxPermSize<br />
256m<br />
-vmargs<br />
-Xms256m<br />
-Xmx512m<br />
&nbsp;</div>
<h3>Web tools</h3>
<p><em>Some general notes.</em> All plugins can be installed in two common ways: through cute <strong>Help->Software Updates->Find and Install&#8230;</strong> and copying all unzipped stuff to <strong>ECLIPSE_HOME/plugins</strong> directory. Of course I prefer the first option, and every plugin I&#8217;ll be installing through this wizard, except of Sysdeo.<br />
By &#8220;default plugins&#8221; I mean plugins which can be installed through &#8220;Europa Discovery Site&#8221; (run the mentioned wizard and find it to be accustomed, if not yet).</p>
<p>From the past experience I knew that I was using a <a href="http://www.eclipse.org/webtools">WTP</a> (stands for Web Tools Platform) plugin for web development. The problem was that there were no mentioned WTP plugin on the &#8220;Europa Discovery Site&#8221;. I carried brief investigation and dig out that the main part of that plugin is a <a href="http://www.eclipse.org/webtools/wst/main.php">WST</a> subproject (the web standard tools subproject).<br />
Steps to reproduce after <strong>Help->Software Updates->Find and Install&#8230;</strong>:<br />
<img src="http://gallery.gomilko.com/d/2452-1/WST.png" alt="WST plugin" /><br />
When I checked WST check box, the wizard apparently gave a tip to select all dependent (required by WST) things too. After I installed a WST, the link to WTP update site suddenly appeared (I guess that it was due to WST), and I decided to install it to have a full stack.<br />
<img src="http://gallery.gomilko.com/d/2459-1/WTP.png" alt="WTP plugin" /></p>
<h3>Subversion integration</h3>
<p>When I installed all useful tools from the Europa repository, I moved to things which update sites have to be added manually. The process is almost the same as when we were updating standard components, the only difference is the update site where the plugin is stored. We need to add http://subclipse.tigris.org/update_1.2.x as a <strong>New Remote Site&#8230;</strong><br />
<img src="http://gallery.gomilko.com/d/2457-1/updateManager.png" alt="Update manager" /></p>
<h3>Tomcat launcher</h3>
<p>The simplest and the most valuable plugin for Eclipse invoking I ever used is a <a href="http://www.eclipsetotale.com/tomcatPlugin.html">Sysdeo</a> Tomcat launcher. Download the archive and install according to Installation section steps. Here, the unzipped archive should be placed manually to the <strong>ECLIPSE_HOME/plugins</strong> directory.<br />
<img src="http://gallery.gomilko.com/d/2455-1/sysdeoButtons.png" alt="Sysdeo butons" /></p>
<h3>Useful tools</h3>
<p>As you know how to install plugins, I&#8217;ll mention only links to update site:</p>
<ol>
<li><a href="http://eclipse-cs.sourceforge.net/update">http://eclipse-cs.sourceforge.net/update</a> &#8211; checkstyle plugin, helps to maintain your coding style due to different available code conventions (Sun, Eclipse, your own)
</li>
<li>
<a href="http://springide.org/updatesite/SpringIDE">http://springide.org/updatesite/SpringIDE</a> &#8211; Spring related things like bean definitions, xml configuration auto completion
</li>
<li>
<a href="http://e-p-i-c.sf.net/updates">http://e-p-i-c.sf.net/updates</a> &#8211; Perl IDE, just for fun to play with RegExps or to write some admin tools
</li>
<li>
<a href="http://www.fabioz.com/pydev/updates">http://www.fabioz.com/pydev/updates</a> &#8211; PyDev (Python IDE) not to be restricted only by Java world and be more broadminded
</li>
<li>
<a href="http://eclipse-tools.sourceforge.net/implementors/">http://eclipse-tools.sourceforge.net/implementors/</a> &#8211; nice Alt+F3 short key usage, allows quick jumps to implementation from interfaces.
</li>
</ol>
<p>Stay tuned!<br />
P.S. I know that saying this phrase is almost as popular as mentioning iPhone in the blog post <img src='http://blog.gomilko.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/07/10/moving-to-eclipse-europa-for-j2ee-programmer/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Perl for Java Programmer: Generating a country list</title>
		<link>http://blog.gomilko.com/2007/06/20/country-list-sql-generating</link>
		<comments>http://blog.gomilko.com/2007/06/20/country-list-sql-generating#comments</comments>
		<pubDate>Wed, 20 Jun 2007 09:08:35 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/06/20/country-list-sql-generating/</guid>
		<description><![CDATA[Introduction
Almost every business application requires a country list as a dictionary data. Even simple registration form might contain country input field. And if you&#8217;re going to store billing or shipping information beyond one country you definitely have to have this important dictionary in your system. Firstly, you should decide which countries will be in your [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Almost every business application requires a country list as a dictionary data. Even simple registration form might contain country input field. And if you&#8217;re going to store billing or shipping information beyond one country you definitely have to have this important dictionary in your system. Firstly, you should decide which countries will be in your list. Depends on your goals it might be short list of the largest countries in the world or full list with all Islands, Territories and even <a href="http://en.wikipedia.org/wiki/Antarctica">Antarctica</a>.  Let&#8217;s review common sources for filling your country list dictionary.</p>
<h2>Existing country lists</h2>
<p>If you have billing or shipping integration with 3rd parties like <a href="http://www.paypal.com">PayPal</a>, you can get this list from the register page html source code. Open a page with the registration information on the any trusted web portal, then find in the source code country list data (e.g. &#8220;View -> Page Source&#8221; in FF):</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="sc3"><span class="re1">&lt;option</span> <span class="re0">value</span>=<span class="st0">&quot;AL&quot;</span><span class="re2">&gt;</span></span>Albania<span class="sc3"><span class="re1">&lt;/option<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;option</span> <span class="re0">value</span>=<span class="st0">&quot;DZ&quot;</span><span class="re2">&gt;</span></span>Algeria<span class="sc3"><span class="re1">&lt;/option<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;option</span> <span class="re0">value</span>=<span class="st0">&quot;AD&quot;</span><span class="re2">&gt;</span></span>Andorra<span class="sc3"><span class="re1">&lt;/option<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;option</span> <span class="re0">value</span>=<span class="st0">&quot;AO&quot;</span><span class="re2">&gt;</span></span>Angola<span class="sc3"><span class="re1">&lt;/option<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;option</span> <span class="re0">value</span>=<span class="st0">&quot;AI&quot;</span><span class="re2">&gt;</span></span>Anguilla<span class="sc3"><span class="re1">&lt;/option<span class="re2">&gt;</span></span></span><br />
&#8230;&#8230;&#8230;<br />
&nbsp;</div>
</ul>
<p>Now you can copy&#038;paste this information and then parse/use it in any convenient way. As you can see, the value in this list is a two character identifier which is called &#8216;<a href="http://en.wikipedia.org/wiki/ISO_3166-1">ISO 3166-1 2 Letter Code</a>&#8216; . It is very useful as a unique identifier and can work as a primary key in the countries DB table.<br />
However there are lot&#8217;s of sites which already have well-proven lists, I&#8217;d suggest take this list directly from the original source &#8211; United Nations published<br />
official list, which is republished in the many places like <a href="http://schmidt.devlib.org/data/countries.html">List of countries and territories</a> or <a href="http://www.addressdoctor.com/en/products/ressources/isocodes.asp">ISO country codes</a>. </p>
<h3>Generating SQL for country table</h3>
<p>Let&#8217;s take one of that list and paste everything to the Excel sheet, then save it in the CSV format. The result should be similar to <a href='http://blog.gomilko.com/wp-content/uploads/2007/05/ISOCodes.csv' title='ISOCodes.csv'>ISOCodes.csv</a> and contains data separated by commas:</p>
<div class="dean_ch" style="white-space: wrap;">
ABW,AW,Aruba<br />
AFG,AF,Afghanistan<br />
AGO,AO,Angola<br />
AIA,AI,Anguilla<br />
ALA,AX,Aland Islands<br />
&#8230;&#8230;&#8230;<br />
&nbsp;</div>
<p>Once we have data stored in Perl readable format, we can parse it and generate SQL code:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co1">#!/usr/bin/perl</span></p>
<p><span class="co1"># PERL MODULE</span><br />
<span class="kw2">use</span> Text::<span class="me2">CSV</span>::<span class="me2">Simple</span>;<br />
&nbsp; &nbsp; <br />
<span class="co1"># This script generetes sql code for country table</span></p>
<p><a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> &lt;&lt;SQL_END;<br />
DROP TABLE IF EXISTS country;<br />
CREATE TABLE country <span class="br0">&#40;</span><br />
&nbsp; short_code varchar<span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span> NOT NULL COMMENT <span class="st0">&#8216;ISO 3166-1 2 Letter Code&#8217;</span>,<br />
&nbsp; name varchar<span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span> NOT NULL,<br />
&nbsp; PRIMARY KEY &nbsp;<span class="br0">&#40;</span>short_code<span class="br0">&#41;</span>,<br />
&nbsp; UNIQUE KEY UNQ_COUNTRY_NAME <span class="br0">&#40;</span>name<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span> ENGINE=InnoDB DEFAULT CHARSET=utf8;<br />
SQL_END</p>
<p><span class="kw1">my</span> <span class="re0">$tplSQL</span> = <span class="st0">&quot;INSERT INTO country (short_code, name) VALUES (<span class="es0">\&quot;</span>%s<span class="es0">\&quot;</span>,<span class="es0">\&quot;</span>%s<span class="es0">\&quot;</span>);<span class="es0">\n</span>&quot;</span>;<br />
<span class="kw1">my</span> <span class="re0">$tplXML</span> = <span class="st0">&quot;&lt;country short_code=<span class="es0">\&quot;</span>%s<span class="es0">\&quot;</span> name=<span class="es0">\&quot;</span>%s<span class="es0">\&quot;</span> /&gt;<span class="es0">\n</span>&quot;</span>;<br />
<span class="kw1">my</span> <span class="re0">$csvFile</span>=<span class="st0">&quot;H:/workspace/update/data/ISOCodes.csv&quot;</span>;</p>
<p><span class="kw1">my</span> <span class="re0">$parser</span> = Text::<span class="me2">CSV</span>::<span class="me2">Simple</span>-&gt;<span class="me1">new</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">my</span> <span class="re0">@data</span> = <span class="re0">$parser</span>-&gt;<span class="me1">read_file</span><span class="br0">&#40;</span><span class="re0">$csvFile</span><span class="br0">&#41;</span>;</p>
<p><a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> <span class="st0">&quot;&#8211; SQL DUMP<span class="es0">\n</span>&quot;</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">@data</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/printf.html"><span class="kw3">printf</span></a> <span class="re0">$tplSQL</span>, <span class="re0">@$_</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="re0">@$_</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span>;<br />
<a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> <span class="st0">&quot;&#8211; Data for DBUnit tests<span class="es0">\n</span>&quot;</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">@data</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/printf.html"><span class="kw3">printf</span></a> <span class="re0">$tplXML</span>, <span class="re0">@$_</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="re0">@$_</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>I&#8217;d prefer not to populate data directly to DB, but generate a script which can be edited and invoked many times. This script doesn&#8217;t work with command line parameters and use hard coded path because I&#8217;m not a Perl programmer who develops a multi-purpose tool:) I&#8217;m just a Java developer who needed a valid country list. After executing this script you&#8217;ll get <a href="http://blog.gomilko.com/wp-content/uploads/2007/05/country.sql">SQL code</a> and DBUnit xml snippets. </p>
<h3>For those who are curious</h3>
<p>Unfortunately, there are no one common countries list, however there are some official lists supported by different organizations like UN, bank communities, post offices, phone companies. When you&#8217;re choosing a proper list, take in account countries which are disappeared like USSR or was divided like Serbia and Montenegro. Choose a primary key from large variety of existent (ISO 2,3 characters, number, phone code,&#8230;)  which satisfies your needs. If you have a unique key, you can get another useful information about chosen country from other sources afterwards. To get answers on those questions, I&#8217;d recommend to skim through <a href="http://www.madore.org/~david/misc/countries.html">How many countries are there in the world?</a> and then google on interested keywords.</p>
<p>This is a part of Perl for Java Programmer series. Previous post was <a href="http://blog.gomilko.com/2007/06/18/perl-for-java-installation/">Perl for Java programmer: Installation</a>. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/06/20/country-list-sql-generating/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl for Java programmer: Installation</title>
		<link>http://blog.gomilko.com/2007/06/18/perl-for-java-installation</link>
		<comments>http://blog.gomilko.com/2007/06/18/perl-for-java-installation#comments</comments>
		<pubDate>Mon, 18 Jun 2007 19:42:47 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/06/18/perl-for-java-installation/</guid>
		<description><![CDATA[Introduction
Let&#8217;s assume that you are a Java server side programmer. Your current web project involves working with server side specific data, such as lots of media files which are stored in file storages, DB data, XML files, configuration files etc. All files are stored under proper sub-directories, your objects are beautifully mapped to DB using [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Let&#8217;s assume that you are a Java server side programmer. Your current web project involves working with server side specific data, such as lots of media files which are stored in file storages, DB data, XML files, configuration files etc. All files are stored under proper sub-directories, your objects are beautifully mapped to DB using your preferable ORM like <a href="http://www.hibernate.org/">Hibernate</a>. Another bunch of data is serialized to XML using something like <a href="http://xstream.codehaus.org/">xstream</a>. Everything works like a charm, until the data storage format or convention is changed. For example, you decide to move all your pictures to another sub-folder. If you have references to these files in your DB you have to modify it too. Or, let&#8217;s say, in the next release of your product the textual data stored in the XMLs have to be updated to conform new POJOs which has new properties. What I&#8217;m trying to say here, is that if you develop an application which works with lots of non homogeneous data, you have to have an approach to migrate it from one version to another.<br />
I think, that the best solution is using a script language which supports string parsing, SQL executing, file I/O. I chose Perl because of it well-known excellent work with regexps and good reputation between system administrators. </p>
<h2>Installing/Configuring Perl</h2>
<p>If you are a lucky user of the *nix OS, I believe, you don&#8217;t need any additional installation of the Perl, because it is already included in your distributive. For folks who are using Windows, I would recommend installing <a href="http://www.activestate.com/products/activeperl/">ActivePerl</a> &#8211; Perl binary distribution for Win32 platform. Basically, I don&#8217;t know any other, and this one seems to be the best. Installing is a well-known &#8220;Next-Next-Next&#8221; joy.<br />
My current IDE for Java is <a href="http://www.eclipse.org/">Eclipse</a>. And one of great advantages of this famous IDE is it plugin system. There are lots <a href="http://www.eclipseplugincentral.com/">plugins</a> developed by different people almost for every purpose. The first plugin I found was <a href="http://e-p-i-c.sourceforge.net/">EPIC</a> &#8211; open source Perl IDE. Using this plugin you obtain simple but powerful facilities for Perl programming. It is a code highlighting and intellisense while typing, running a program by clicking right mouse-click on a perl source code file and choosing &#8220;Run as perl&#8221;. Output of the program is directed to Eclipse console. And of course, you can debug the program if output is helpless.<br />
Finally, I&#8217;d like to emphasize the very useful tool which is supplied with ActivePerl  &#8211; <em>Perl Package Manager</em>. Once you need any additional Perl package, you can easily install it in your system from the global repository. <a href="http://blog.gomilko.com/wp-content/uploads/2007/04/perlpackagemanager.jpg" title="Perl Package Manager"><img src="http://blog.gomilko.com/wp-content/uploads/2007/04/perlpackagemanager.jpg" alt="Perl Package Manager" /></a><br />
For instance, you write a simple script which use B connection to a MySQL instance. But there is an error:</p>
<pre>
install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC
Perhaps the DBD::mysql perl module hasn't been fully installed,
or perhaps the capitalisation of 'mysql' isn't right.
</pre>
<p>The only thing you have to do, is to open <em>Perl Package Manager</em>, find missed in standard configuration package <em>DBD::mysql</em>, and install it.</p>
<h2>Roadmap of this guide</h2>
<p>Today I decided that writing less but more frequently is easier and using his approach I don&#8217;t need keeping this important post still in drafts. I&#8217;d rather ship every day part by part than wait for another month to complete it.<br />
Therefore, I&#8217;m planning to cover in the near weeks these topics:</p>
<ul>
<li>String manipulation</li>
<li>Files manipulation</li>
<li>DB work</li>
<li>XML work</li>
<li>SQL generating</li>
</ul>
<p>Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/06/18/perl-for-java-installation/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Natural order for strings with numbers in Java</title>
		<link>http://blog.gomilko.com/2007/05/31/natural-order-for-strings-with-numbers-in-java</link>
		<comments>http://blog.gomilko.com/2007/05/31/natural-order-for-strings-with-numbers-in-java#comments</comments>
		<pubDate>Thu, 31 May 2007 13:29:12 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/05/31/natural-order-for-strings-with-numbers-in-java/</guid>
		<description><![CDATA[File names default sort order
If you are developing an application which works with files, one day you&#8217;ll try to get list of the names of all files in a particular directory. It could be like this simplified snippet:

&#160; &#160; public List&#60;String&#62; getAllImageNames&#40;&#41; &#123;
&#160; &#160; &#160; &#160; List&#60;String&#62; names = new ArrayList&#60;String&#62;&#40;&#41;;
&#160; &#160; &#160; &#160; File [...]]]></description>
			<content:encoded><![CDATA[<h2>File names default sort order</h2>
<p>If you are developing an application which works with files, one day you&#8217;ll try to get list of the names of all files in a particular directory. It could be like this simplified snippet:</p>
<div class="dean_ch" style="white-space: wrap;">
<p>&nbsp; &nbsp; <span class="kw2">public</span> List&lt;String&gt; getAllImageNames<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List&lt;String&gt; names = <span class="kw2">new</span> ArrayList&lt;String&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">File</span></a> imagesDir = <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">File</span></a><span class="br0">&#40;</span>IMAGE_BASE_DIRECTORY<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!imagesDir.<span class="me1">exists</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> names;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">File</span></a> image : imagesDir.<span class="me1">listFiles</span><span class="br0">&#40;</span>COMMON_IMG_FILTER<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names.<span class="me1">add</span><span class="br0">&#40;</span>image.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> names;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">static</span> <span class="kw2">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileFilter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">FileFilter</span></a> COMMON_IMG_FILTER = <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileFilter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">FileFilter</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">boolean</span> accept<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">File</span></a> pathname<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> pathname.<span class="me1">isFile</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &amp;&amp; <span class="br0">&#40;</span>!pathname.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">startsWith</span><span class="br0">&#40;</span><span class="st0">&quot;.&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>Let&#8217;s assume that you have some image files with the same name suffix and an index number as a trailing part:</p>
<table>
<tr>
<th>System order</th>
<th>Natural order</th>
<tr>
<tr>
<td>IMG_1.jpg</td>
<td>IMG_1.jpg</td>
<tr>
<tr>
<td>IMG_2.jpg</td>
<td>IMG_2.jpg</td>
<tr>
<tr>
<td>IMG_21.jpg</td>
<td>IMG_3.jpg</td>
<tr>
<tr>
<td>IMG_22.jpg</td>
<td>IMG_21.jpg</td>
<tr>
<tr>
<td>IMG_3.jpg</td>
<td>IMG_22.jpg</td>
<tr>
</table>
<p>Everything seems to be all right, but returned list is sorted in a way Java usually sorts strings. While this results could completely satisfy your program API, it is not very useful to work with such lists for a human. Moreover, such lists are very common in our life, for example, many digital photo cameras store pictures with such names.<br />
Let&#8217;s make the problem more general. We have a list of strings, which could contain digits, and we want to sort this array to get a natural ordered list. You&#8217;d say that it is piece of cake, because Java has good facility to perform array/list sorting : <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)">Collections.sort()</a> and <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html">Comparator</a>. Therefore, we need an appropriate <code>Comparator</code> implementation.</p>
<h2>Known solutions for natural order sorting</h2>
<p>Unfortunately, there are no any well-known library like <a href="http://jakarta.apache.org/commons/">Jakarta Commons</a> to perform this sort. And if you try to google it, you will find only some posts like this one with home-grown solutions and grumbling about lack of existing proven solution. The most important thing is to know the right keywords to google against it. Most of articles on this topic are about <a href="http://www.google.com/search?hl=uk&#038;q=Natural+order&#038;btnG=%D0%9F%D0%BE%D1%88%D1%83%D0%BA+Google&#038;meta=">Natural order</a>. </p>
<p>I found some valuable resources:</p>
<ul>
<li><a href="http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html">Natural String Order</a> at Stephen Friedrich&#8217;s Blog &#8211; an article of Java professional with working Java <a href="http://www.eekboom.com/java/compareNatural/src/com/eekboom/utils/Strings.java">source</a> and even JUnit <a href="http://www.eekboom.com/java/compareNatural/src_test/com/eekboom/utils/TestStrings.java">tests</a>. The implemented algorithm is very comprehensive and can work with different set of sorting rules (Ascii, case insensitive and locale specific).</li>
<li><a href="http://jroller.com/page/tfenne/?anchor=humanestringcomparator_sorting_strings_for_people">HumaneStringComparator: Sorting Strings for People</a> &#8211; another one <em>Comparator</em> implementation by Tim Fennell.</li>
<li><a href="http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java">Implementation</a> by Pierre-Luc Paour.</li>
<li><a href="http://sourcefrog.net/projects/natsort/">Natural Order String Comparison</a> &#8211; C version of implementation and links to another languages.</li>
<li><a href="http://www.naturalordersort.org/">Natural Order Numerical Sorting</a> &#8211; overview of the problem, links to another articles and solutions, but almost all of them are broken or not Java. It seemed that the author was keen about that idea, registered a special domain and gathered essential information, and then left this site floating without any maintain.</li>
</ul>
<h2>Stress test</h2>
<p>One of the main software development rule states: don&#8217;t invent your own wheel unless the problem is not your core business. As string sorting was a utility purpose matter, before implementing my own solution I decided to test already found. The test was easy and straightforward &#8211; every <em>Comparator</em> had to sort the same number of randomly generated and shuffled strings. The strings were generated by fixed pattern: <code>[a-z][0..1000].jpg</code>. Input data was the large array (100000) of such strings:<br />
<code>..............<br />
jqazrrveqy113.jpg<br />
wzedsgzmvo912.jpg<br />
aayexqpfdu311.jpg<br />
zvpzxjwkml354.jpg<br />
nelacribtl964.jpg<br />
rehsgmzugb244.jpg<br />
eoptzxybtz459.jpg<br />
ukbeogpmhe157.jpg<br />
zgvnrzohwc176.jpg<br />
.............. .<br />
</code><br />
I was interested in only one algorithm efficiency parameter &#8211; time elapsing during full sort. As usual, the elapsing time itself doesn&#8217;t tell much about the efficiency. To have a minimum achievable value to compare with I chose a result given by the comparator based on a standard Java <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)">String.compareTo()</a> method which should be the fastest because it doesn&#8217;t deal with string parsing.</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; &nbsp; &nbsp; Comparator&lt;String&gt; baseComparator = <span class="kw2">new</span> Comparator&lt;String&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">int</span> compare<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> strA, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> strB<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Assuming that strA always != null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> strA.<span class="me1">compareTo</span><span class="br0">&#40;</span>strB<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp;</div>
<p>The test results are shown in a table:</p>
<table>
<tr>
<th>Author
<th>
<th>Version</th>
<th>Sort Time</th>
<th>Pros</th>
<th>Cons</th>
</tr>
<tr>
<td>Pierre-Luc Paour
<td>
<td><a href="http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java">NaturalOrderComparator</a></td>
<td>453ms</td>
<td>Quite fast</td>
<td>hard to read C-style algorithm</td>
</tr>
<tr>
<td>Stephen Friedrich
<td>
<td><a href="http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html">NaturalComparator</a></td>
<td>4828ms</td>
<td>Locale specific</td>
<td>Too slow</td>
</tr>
<tr>
<td>Stephen Friedrich
<td>
<td><a href="http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html">NaturalComparatorAscii</a></td>
<td>360ms</td>
<td>The fastest one</td>
<td>Only ASCII</td>
</tr>
<tr>
<td>Stephen Friedrich
<td>
<td><a href="http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html">NaturalComparatorIgnoreCaseAscii</a></td>
<td>500ms</td>
<td>Fast enough, case insensitive</td>
<td>Only ASCII</td>
</tr>
<tr>
<td>Tim Fennell
<td>
<td><a href="http://jroller.com/page/tfenne/?anchor=humanestringcomparator_sorting_strings_for_people">HumaneStringComparator </a></td>
<td>4797ms</td>
<td>Very good example of OOP style, brief understandable algorithm, use Java facilities extensively</td>
<td>Too slow and inefficient</td>
</tr>
<tr>
<td>Java native
<td>
<td>String.compareTo()</td>
<td>235ms</td>
<td>standard</td>
<td>standard</td>
</tr>
</table>
<h2>Conclusion</h2>
<p>As you can see from the result sheet, the most powerful and fast is Stephen Friedrich&#8217;s package. It is written with good Java style, well commented and supplied with JUnit tests. It provides different kind of sorting depends on what are going to sort &#8211; simple ASCII strings like file names or country specific strings containing special characters.<br />
Of course, one day I&#8217;d like to see any well-proven solution in the <a href="http://jakarta.apache.org/commons/">Jakarta Commons</a> project and simply add it as a jar.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/05/31/natural-order-for-strings-with-numbers-in-java/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Eclipse chewing gum</title>
		<link>http://blog.gomilko.com/2007/04/23/eclipse-chewing-gum</link>
		<comments>http://blog.gomilko.com/2007/04/23/eclipse-chewing-gum#comments</comments>
		<pubDate>Mon, 23 Apr 2007 14:04:28 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/04/23/eclipse-chewing-gum/</guid>
		<description><![CDATA[http://www.wrigley.com/wrigley/products/products_eclipse.asp
Must chew for every Java programmer! Moreover, the last one on the page is branded in the same colors as Eclipse IDE. I&#8217;m eager to taste it.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.wrigley.com/wrigley/products/products_eclipse.asp">http://www.wrigley.com/wrigley/products/products_eclipse.asp</a><br />
Must chew for every Java programmer! Moreover, the last one on the page is branded in the same colors as <a href="http://www.eclipse.org/">Eclipse IDE</a>. I&#8217;m eager to taste it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/04/23/eclipse-chewing-gum/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Difference between a bug and a feature</title>
		<link>http://blog.gomilko.com/2007/01/24/difference-between-a-bug-and-a-feature</link>
		<comments>http://blog.gomilko.com/2007/01/24/difference-between-a-bug-and-a-feature#comments</comments>
		<pubDate>Wed, 24 Jan 2007 15:39:21 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/archives/40</guid>
		<description><![CDATA[You can figure out the main difference between two main concepts of modern software development here. The only one thing there omitted is a bugafeature paradigm.
]]></description>
			<content:encoded><![CDATA[<p>You can figure out the main difference between two main concepts of modern software development <a href="http://home.kreme.com/bug3.jpg">here</a>. The only one thing there omitted is a <i>bugafeature</i> paradigm.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/01/24/difference-between-a-bug-and-a-feature/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
