<?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; Code snippets</title>
	<atom:link href="http://blog.gomilko.com/category/code-snippets/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>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>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>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>MySQL rownum imitation</title>
		<link>http://blog.gomilko.com/2007/04/28/mysql-rownum-imitation</link>
		<comments>http://blog.gomilko.com/2007/04/28/mysql-rownum-imitation#comments</comments>
		<pubDate>Sat, 28 Apr 2007 14:06:27 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[DB]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/2007/04/28/mysql-rownum-imitation/</guid>
		<description><![CDATA[I got tired looking for a good solution to imitate a  rownum in MySQL every time I need it. Here I post the most
universal solution that doesn&#8217;t need a script mode and can be executed in a single statement:

SELECT @rownum:=@rownum+1 rownum, t.*
FROM &#40;SELECT @rownum:=0&#41; r, mytable t;
&#160;
This solution and others you can find in [...]]]></description>
			<content:encoded><![CDATA[<p>I got tired looking for a good solution to imitate a  <code>rownum</code> in MySQL every time I need it. Here I post the most<br />
universal solution that doesn&#8217;t need a script mode and can be executed in a single statement:
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">SELECT</span> @rownum:=@rownum<span class="nu0">+1</span> rownum, t.*<br />
<span class="kw1">FROM</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> @rownum:=<span class="nu0">0</span><span class="br0">&#41;</span> r, mytable t;<br />
&nbsp;</div>
<p>This solution and others you can find in comments to the <a href="http://dev.mysql.com/doc/refman/5.0/en/user-variables.html">MySQL Reference Manual</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2007/04/28/mysql-rownum-imitation/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Concatenating within one sql select</title>
		<link>http://blog.gomilko.com/2006/02/13/draft-useful-orcale-sql-query</link>
		<comments>http://blog.gomilko.com/2006/02/13/draft-useful-orcale-sql-query#comments</comments>
		<pubDate>Mon, 13 Feb 2006 14:09:57 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Code snippets]]></category>

		<guid isPermaLink="false">http://blog.gomilko.com/archives/9</guid>
		<description><![CDATA[Let us assume that we have a result set (select or subselect) with a column which contains values we want concatenate using some sort of separators.
For example we have the following data:


txt





aa


bb


cc


&#8230;





How can we concatenate these values within single SQL select using Oracle dialect to get string aa,bb,cc,&#8230; ?  Of course this problem could [...]]]></description>
			<content:encoded><![CDATA[<p>Let us assume that we have a result set (select or subselect) with a column which contains values we want concatenate using some sort of separators.<br />
For example we have the following data:</p>
<table width="100" cellspacing="2" cellpadding="2" border="1">
<tr>
<td>txt</td>
</tr>
<tr>
<td>
<table width="100%">
<tr>
<td>aa</td>
</tr>
<tr>
<td>bb</td>
</tr>
<tr>
<td>cc</td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
</table>
</td>
</tr>
</table>
<p>How can we concatenate these values within single SQL select using Oracle dialect to get string <em>aa,bb,cc,&#8230; </em>?  Of course this problem could be solved with help of  PL/SQL within stored procedure, but sometimes it is unavailable option.  Actually, I had never thought about it before I was asked for advice in ICQ this morning. Right away I realized that  Oracle analytical/hierarchical or another specific technique could help here. As I&#8217;m not familiar with whole range of  additional non-ANSI SQL constructions I forwarded this question to my good friend and ex co-worker Sergey Kolomiets  who gave me an interesting solution quite easily.  For the reason of making note about this solution I decided to put it here:<br />
[code lang="sql"]<br />
with t as  (<br />
select 'aa' txt from dual<br />
union all<br />
select 'bb' from dual<br />
union all<br />
select 'cc' from dual<br />
union all<br />
select 'dd' from dual<br />
union all<br />
select 'zz' from dual<br />
union all<br />
select 'yy' from dual ),<br />
t_sub as (select rownum rn, t.* from t) select t_sub.*,<br />
SYS_CONNECT_BY_PATH(txt,',')  from t_sub<br />
where rn=(select count(*) from t)<br />
start with rn=1 connect by prior rn + 1  = rn<br />
[/code] </p>
<p>Of course it might not be the best high performance select, but main idea of using SYS_CONNECT_BY_PATH function is clear. In case you want to get rid of heading comma function  [code lang="sql"]ltrim(...,',')[/code] can be used.<br />
Before posting I&#8217;ve managed some google investigation and found nice<br />
article dedicated to this issue:  <a href="http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php#row_number"> String Aggregation Techniques </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gomilko.com/2006/02/13/draft-useful-orcale-sql-query/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
