<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1256788220020556950</id><updated>2025-11-06T02:22:29.779-08:00</updated><category term="java"/><category term="wicket"/><category term="algorithms"/><category term="basic auth"/><category term="security"/><category term="spring"/><category term="ajax"/><category term="apache commons"/><category term="autocomplete suggest jquery wicket"/><category term="bcrypt"/><category term="card"/><category term="class-path"/><category term="closeutils java resources"/><category term="commons"/><category term="connection"/><category term="conversion"/><category term="database"/><category term="debugging"/><category term="filter"/><category term="hook"/><category term="http client"/><category term="ifnull"/><category term="integer"/><category term="integration"/><category term="jar"/><category term="java image resize thumbnail imgscalr"/><category term="java pagination"/><category term="java url generate"/><category term="java wicket parameters"/><category term="java7"/><category term="jdbc"/><category term="jetty"/><category term="kerberos"/><category term="layout"/><category term="log4j"/><category term="logging"/><category term="main-class"/><category term="manifest"/><category term="maven"/><category term="md5"/><category term="mergesort"/><category term="mysql"/><category term="netbeans"/><category term="netbeans highlighting template"/><category term="nulls on top"/><category term="overflow"/><category term="pages count pagination sql"/><category term="parameters"/><category term="paramiko"/><category term="pattern"/><category term="pci"/><category term="php"/><category term="plugin"/><category term="postgresql"/><category term="proxy"/><category term="python"/><category term="quicksort"/><category term="release"/><category term="resources"/><category term="rest template"/><category term="ruby"/><category term="runtime"/><category term="sftp"/><category term="shutdown"/><category term="socks"/><category term="socksipy"/><category term="sort"/><category term="spring mvc"/><category term="spring security"/><category term="template"/><category term="timeout"/><category term="tinymce"/><category term="tutorial"/><category term="utils"/><category term="velocity"/><category term="wicket image url"/><category term="wicket panel components"/><category term="windows"/><category term="wysiwyg"/><category term="xdebug"/><title type='text'>Sergej Sizov - Software Engineering</title><subtitle type='html'>Writing code in Java, Ruby and JavaScript</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>33</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-1439456202449202222</id><published>2016-11-06T11:33:00.000-08:00</published><updated>2016-11-06T11:34:26.818-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bcrypt"/><category scheme="http://www.blogger.com/atom/ns#" term="md5"/><category scheme="http://www.blogger.com/atom/ns#" term="postgresql"/><category scheme="http://www.blogger.com/atom/ns#" term="security"/><title type='text'>Easy password hash migration from MD5 to BCrypt.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Many legacy production systems still use non-salted MD5 function for password hashing. These hashes are usually stored in the database and used for user authentication. The problem is that non-salted MD5 hash is no more secure today. There are so-called rainbow tables (precomputed MD5 hashes for word dictionaries) that can be used for easy reverse lookup.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Nowadays it is recommended to use BCrypt hashing function that incorporates a salt to protect against rainbow table attacks. Even if your database is compromised and leaked, it is not possible (or at least difficult for now) to decrypt users&#39; passwords.&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
&lt;i&gt;The obvious way to migrate to BCrypt is to update password hash on user sign in. Also, there should be some flag that indicates if password hash is already migrated to BCrypt or not. For non-migrated accounts we need to reset passwords and force users to change their password.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Is there any better and painless way, so that we will not bother users with password change? And the answer is - &lt;b&gt;double hashing&lt;/b&gt;. Let&#39;s hash our already hashed password (with MD5) with BCrypt once more.&lt;br /&gt;
&lt;br /&gt;
Our migrated database will contain values after the following transformation:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;plaintext -&amp;gt; MD5 -&amp;gt; BCrypt
&lt;/pre&gt;
&lt;br /&gt;
New passwords will be double hashed using the following function:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;BCrypt( MD5( &#39;PlainPassword&#39; ) )

&lt;/pre&gt;
&lt;br /&gt;
The following example is for PostgreSQL database. You need to install &lt;b&gt;pgcrypto&lt;/b&gt; extension to enable support for BCrypt:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;postgres=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION

postgres=# SELECT &lt;b&gt;crypt&lt;/b&gt;(&lt;b&gt;md5&lt;/b&gt;(&#39;YourPasswordGoesHere&#39;), &lt;b&gt;gen_salt&lt;/b&gt;(&#39;bf&#39;, 10));
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; crypt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
--------------------------------------------------------------
&amp;nbsp;$2a$10$y8jgt.GoqX8w0ScGlMYsrOrgWYyARuO9FjvSUAx7mnEjY3b1E6NLi
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
The following snippet shows password verification in Java with DigestUtils and jBCrypt:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;String hashed = Bcrypt.&lt;b&gt;hashpw&lt;/b&gt;(DigestUtils.&lt;b&gt;md5Hex&lt;/b&gt;(password), Bcrypt.&lt;b&gt;gensalt&lt;/b&gt;(10));

boolean password_valid = Bcrypt.&lt;b&gt;checkpw&lt;/b&gt;(user.getPasswordHash(), hashed));



&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/1439456202449202222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2016/11/easy-password-hash-migration-from-md5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/1439456202449202222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/1439456202449202222'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2016/11/easy-password-hash-migration-from-md5.html' title='Easy password hash migration from MD5 to BCrypt.'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-255185585566070406</id><published>2016-06-05T04:11:00.000-07:00</published><updated>2016-06-05T04:11:07.637-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ruby"/><title type='text'>Alexander Goldstein&#39;s Each! Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre&gt;&lt;b&gt;class&lt;/b&gt; Array
 &lt;b&gt;def&lt;/b&gt; each!
  &lt;b&gt;while&lt;/b&gt; count &amp;gt; 0
   &lt;b&gt;yield&lt;/b&gt;(shift)
  &lt;b&gt;end&lt;/b&gt;
 &lt;b&gt;end&lt;/b&gt;
&lt;b&gt;end&lt;/b&gt;
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/255185585566070406/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2016/06/alexander-goldsteins-each-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/255185585566070406'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/255185585566070406'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2016/06/alexander-goldsteins-each-pattern.html' title='Alexander Goldstein&#39;s Each! Pattern'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-4668428030044444888</id><published>2015-10-22T09:43:00.000-07:00</published><updated>2015-10-22T09:43:02.078-07:00</updated><title type='text'>SelectionSort in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre&gt; public static void selectionsort(int[] A) {
  for (int i = 0; i &amp;lt; A.length; i++) {
   int min = i;
   for (int j = i+1; j &amp;lt; A.length; j++) {
    if (A[j] &amp;lt; A[min]) {
     min = j;
    }
   }
   swap(A, i, min);
  }
 }

 public static void swap(int[] A, int i, int j) {
  int temp = A[j];
  A[j] = A[i];
  A[i] = temp;
 }

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/4668428030044444888/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2015/10/selectionsort-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4668428030044444888'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4668428030044444888'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2015/10/selectionsort-in-java.html' title='SelectionSort in Java'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-5571481558677981276</id><published>2015-10-22T09:42:00.001-07:00</published><updated>2015-10-22T09:42:23.740-07:00</updated><title type='text'>InsertionSort in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre&gt; public static void insertionsort(int[] A) {
  for (int i = 1; i &amp;lt; A.length; i++) {
   for (int j = i; j &amp;gt; 0 &amp;amp;&amp;amp; A[j] &amp;lt; A[j - 1]; j--) {
    swap(A, j, j - 1);
   }
  }
 }

 public static void swap(int[] A, int i, int j) {
  int temp = A[j];
  A[j] = A[i];
  A[i] = temp;
 }

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/5571481558677981276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2015/10/insertionsort-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5571481558677981276'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5571481558677981276'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2015/10/insertionsort-in-java.html' title='InsertionSort in Java'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-7116860696645174970</id><published>2015-10-22T09:41:00.001-07:00</published><updated>2015-10-22T09:41:36.080-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="mergesort"/><title type='text'>MergeSort in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre&gt; public static void mergesort(int[] A) {
  int[] aux = new int[A.length];
  mergesort(A, aux, 0, A.length-1);
 }
 private static void mergesort(int[] A, int[] aux, int lo, int hi) {
  if (lo &amp;gt;= hi) return;
  int m = (lo+hi) / 2;
  mergesort(A, aux, lo, m);
  mergesort(A, aux, m+1, hi);
  merge(A, aux, lo, m, hi);
 }
 private static void merge(int[] A, int[] aux, int lo, int m, int hi) {
  int i = lo;
  int j = m+1;
  for (int k=lo; k &amp;lt;= hi; k++) {
   if (i &amp;gt; m) {
    aux[k] = A[j];
    j++;
   } else if (j &amp;gt; hi) {
    aux[k] = A[i];
    i++;
   } else if (A[j] &amp;lt; A[i]) {
    aux[k] = A[j];
    j++;
   } else {
    aux[k] = A[i];
    i++;
   }
  }
  for (int k=lo; k &amp;lt;= hi; k++) {
   A[k] = aux[k];
  }
 }

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/7116860696645174970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2015/10/mergesort-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7116860696645174970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7116860696645174970'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2015/10/mergesort-in-java.html' title='MergeSort in Java'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-5320772938417413680</id><published>2015-10-22T06:37:00.001-07:00</published><updated>2015-10-22T06:37:22.587-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="quicksort"/><title type='text'>QuickSort in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;pre&gt; public static void quicksort(int[] A) {
  quicksort(A, 0, A.length-1);
 }
 
 private static void quicksort(int[] A, int lo, int hi) {
  if (lo &amp;gt;= hi) return;
  int p = partition(A, lo,hi);
  quicksort(A, lo, p-1);
  quicksort(A, p+1, hi);
 }
 
 private static int partition(int[] A, int lo, int hi) {
  int i = lo+1;
  int j = hi;
  while (i &amp;lt;= j) {
   while (A[i] &amp;lt; A[lo]) {
    i++;
    if (i &amp;gt; hi) break;
   }
   while (A[j] &amp;gt; A[lo]) {
    j--;
   }
   if (i &amp;lt;= j) {
    swap(A, i, j); 
    i++;
    j--;
   }
  }
  swap(A, lo, j); 
  return j;
 }

 public static void swap(int[] A, int i, int j) {
  int temp = A[j];
  A[j] = A[i];
  A[i] = temp;
 }

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/5320772938417413680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2015/10/quicksort-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5320772938417413680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5320772938417413680'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2015/10/quicksort-in-java.html' title='QuickSort in Java'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-6610384661785246303</id><published>2015-01-03T05:57:00.000-08:00</published><updated>2015-01-03T08:03:23.108-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="paramiko"/><category scheme="http://www.blogger.com/atom/ns#" term="proxy"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="sftp"/><category scheme="http://www.blogger.com/atom/ns#" term="socks"/><category scheme="http://www.blogger.com/atom/ns#" term="socksipy"/><title type='text'>Python socksipy: SFTP with Paramiko via SOCKS proxy</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I use Paramiko library to access SFTP servers in Python. Paramiko is easy to use, provides programmer with rich functionality, but it does not support proxy servers out of the box. There is a small library &lt;b&gt;socksipy&lt;/b&gt; that can be easily integrated with Paramiko.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Setting up a connection with Paramiko&lt;/h3&gt;
&lt;br /&gt;The following source code sets up connection to remote SFTP server:&lt;br /&gt;
&lt;pre&gt;import paramiko

try:
  ssh = paramiko.SSHClient()
  logger.debug(&#39;Attempting SFTP connection&#39;)
  ssh.connect(hostname=&#39;sftp.example.com&#39;)
  sftp = ssh.open_sftp()
  logger.debug(&#39;Connected to SFTP&#39;)
except Exception as e:
  logger.error(&#39;Connection failed: %s&#39;, str(e))
  sys.exit(&#39;Could not connect&#39;) 
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Configuring SOCKS proxy&lt;/h3&gt;
&lt;br /&gt;Socksipy can be downloaded from http://socksipy.sourceforge.net&lt;br /&gt;
&lt;br /&gt;
The following is the socksipy proxy object configuration:&lt;br /&gt;
&lt;pre&gt;import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, &#39;socks.example.com&#39;)
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Syntax&lt;/b&gt;: &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Setup Paramiko to communicate via SOCKS proxy&lt;/h3&gt;
&lt;br /&gt;We should replace paramiko socket with socksipy object:&lt;br /&gt;
&lt;pre&gt;import paramiko
import socks
            
logger.debug(&#39;Using SOCKS proxy&#39;)
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, &#39;socks.example.com&#39;)
&lt;b&gt;paramiko.client.socket.socket = socks.socksocket&lt;/b&gt;

try:
  ssh = paramiko.SSHClient()
  logger.debug(&#39;Attempting SFTP connection&#39;)
  ssh.connect(hostname=&#39;sftp.example.com&#39;)
  sftp = ssh.open_sftp()
  logger.debug(&#39;Connected to SFTP&#39;)
except Exception as e:
  logger.error(&#39;Connection failed: %s&#39;, str(e))
  sys.exit(&#39;Could not connect&#39;)
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;

More info about socksipy can be found here: http://socksipy.sourceforge.net/readme.txt
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/6610384661785246303/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2015/01/python-sftp-with-paramiko-via-socks.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/6610384661785246303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/6610384661785246303'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2015/01/python-sftp-with-paramiko-via-socks.html' title='Python socksipy: SFTP with Paramiko via SOCKS proxy'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-3598477105603843775</id><published>2014-07-16T04:18:00.001-07:00</published><updated>2014-07-16T04:19:53.643-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java7"/><category scheme="http://www.blogger.com/atom/ns#" term="kerberos"/><category scheme="http://www.blogger.com/atom/ns#" term="windows"/><title type='text'>Kerberos Windows configuration for Java 7</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
After migrating to Java 7, our Java client application was unable to authenticate via Kerberos on Windows. If you have the same problem, the following configuration may help you solve it. There is a key &lt;b&gt;allowtgtsessionkey&lt;/b&gt; in Windows registry that allows client application to decrypt session key of Kerberos &lt;b&gt;Ticket Granting Ticket&lt;/b&gt; (TGT).&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Windows Registry configuration&lt;/h3&gt;
Depending on the version of Windows you use, this registry key should be created in the following Registry path.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Windows XP&lt;/h4&gt;
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\&lt;br /&gt;
REG_DWORD name: allowtgtsessionkey&lt;br /&gt;
Value: 1&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Windows 2003 Server, Vista, 7, &amp;nbsp;8, etc.&lt;/h4&gt;
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters&lt;br /&gt;
REG_DWORD name: allowtgtsessionkey&lt;br /&gt;
Value: 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Default realm not found&lt;/h3&gt;
Another problem that occurred (not sure if it is connected to Java 7) was missing default realm.&lt;br /&gt;
&lt;br /&gt;
Find your&amp;nbsp;&lt;b&gt;krb5.ini&lt;/b&gt; file and add the following section if it is missing&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;[libdefaults]&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;default_realm = EUROPE.EXAMPLE.COM&lt;/b&gt;&lt;br /&gt;
[realms]&lt;br /&gt;
// KDC configuration for realms&lt;br /&gt;
EUROPE.EXAMPLE.COM = {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; kdc = eudc01.example.com&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/3598477105603843775/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2014/07/kerberos-windows-configuration-for-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/3598477105603843775'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/3598477105603843775'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2014/07/kerberos-windows-configuration-for-java.html' title='Kerberos Windows configuration for Java 7'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-8774152717010679932</id><published>2013-02-16T08:03:00.000-08:00</published><updated>2013-02-16T08:03:29.678-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="basic auth"/><category scheme="http://www.blogger.com/atom/ns#" term="spring security"/><title type='text'>Spring Security and Http Basic Authentication</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This article will show you how to use &lt;b&gt;Spring Security&lt;/b&gt; to protect your web application with &lt;b&gt;Http Basic&lt;/b&gt; Authentication.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Our application will require that users will authenticate themeselves with Http Basic Authentication. Unauthorized users will get &lt;b&gt;401 UNAUTHORIZED&lt;/b&gt; response and will have to authenticate themselves with login and password.&lt;br /&gt;
&lt;br /&gt;
I expect that your application is already using Spring Framework and Maven. So, to start using Spring Security we need to add these maven dependencies:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;dependency&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;/groupId&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;artifactId&amp;gt;spring-security-config&amp;lt;/artifactId&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;version&amp;gt;3.1.3.RELEASE&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;
&amp;lt;dependency&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;/groupId&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;artifactId&amp;gt;spring-security-web&amp;lt;/artifactId&amp;gt;
&amp;nbsp; &amp;nbsp;&amp;lt;version&amp;gt;3.1.3.RELEASE&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
We need to add &lt;b&gt;Spring Security Filter&lt;/b&gt; to web.xml configuration so that it will intercept all HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;filter&amp;gt;
   &amp;lt;filter-name&amp;gt;springSecurityFilterChain&amp;lt;/filter-name&amp;gt;
   &amp;lt;filter-class&amp;gt;
      org.springframework.web.filter.DelegatingFilterProxy
   &amp;lt;/filter-class&amp;gt;
&amp;lt;/filter&amp;gt;

&amp;lt;filter-mapping&amp;gt;
   &amp;lt;filter-name&amp;gt;springSecurityFilterChain&amp;lt;/filter-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
&amp;lt;/filter-mapping&amp;gt;

&lt;/pre&gt;
&lt;br /&gt;
Then we need to implement a &lt;b&gt;UserService &lt;/b&gt;that checks that the user exists (e.g. in a database).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;public class UserServiceImpl implements UserService {

   private Map&amp;lt;String, UserDetails&amp;gt; users = new HashMap&amp;lt;String, UserDetails&amp;gt;();

   @Override
   public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

      if (users.containsKey(username)) {
         return users.get(username);
      }

      throw new UsernameNotFoundException(&quot;User &quot; + username + &quot; not found&quot;);
   }
}&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
The next step is to implement a &lt;b&gt;BasicAuthenticationEntryPoint &lt;/b&gt;that would tell users that they have to authenticate in order to access this application.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;public class VozisBasicAuthenticationEntryPoint extends&amp;nbsp;
BasicAuthenticationEntryPoint {

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,&amp;nbsp;
AuthenticationException authException) throws IOException, ServletException {
      response.addHeader(&quot;WWW-Authenticate&quot;, &quot;Basic realm=\&quot;&quot; +&amp;nbsp;
         getRealmName() + &quot;\&quot;&quot;);
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
      PrintWriter writer = response.getWriter();
      writer.println(&quot;HTTP Status &quot; + HttpServletResponse.SC_UNAUTHORIZED +&amp;nbsp;
         &quot; - &quot; + authException.getMessage());
   }
}
&lt;/pre&gt;
&lt;br /&gt;
Now we have all components that we need, let&#39;s integrate them together. Below is the example of &lt;b&gt;security.xml&lt;/b&gt; configuration:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;beans:beans xmlns=&quot;http://www.springframework.org/schema/security&quot;
   xmlns:beans=&quot;http://www.springframework.org/schema/beans&quot;
   xmlns:util=&quot;http://www.springframework.org/schema/util&quot;
   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
   xsi:schemaLocation=&quot;
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.1.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.1.xsd &quot;&amp;gt;

   &amp;lt;global-method-security secured-annotations=&quot;enabled&quot; /&amp;gt;

   &amp;lt;http auto-config=&quot;false&quot; create-session=&quot;stateless&quot;&amp;gt;
      &amp;lt;http-basic entry-point-ref=&quot;authenticationEntryPoint&quot; /&amp;gt;
   &amp;lt;/http&amp;gt;

   &amp;lt;beans:bean id=&quot;authenticationEntryPoint&quot;
     class=&quot;com.blogspot.vozis.security.VozisBasicAuthenticationEntryPoint&quot;&amp;gt;
      &amp;lt;beans:property name=&quot;realmName&quot; value=&quot;Vozis&quot; /&amp;gt;
   &amp;lt;/beans:bean&amp;gt;

   &amp;lt;authentication-manager alias=&quot;authenticationManager&quot;&amp;gt;
      &amp;lt;authentication-provider user-service-ref=&quot;userService&quot; /&amp;gt;
   &amp;lt;/authentication-manager&amp;gt;

   &amp;lt;beans:bean id=&quot;userService&quot; class=&quot;com.blogspot.vozis.security.UserServiceImpl&quot; /&amp;gt;
&amp;lt;/beans:beans&amp;gt; &amp;nbsp; 
&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/8774152717010679932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2013/02/spring-security-and-http-basic.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/8774152717010679932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/8774152717010679932'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2013/02/spring-security-and-http-basic.html' title='Spring Security and Http Basic Authentication'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-7099074883822031434</id><published>2013-02-16T06:38:00.000-08:00</published><updated>2013-02-16T06:38:21.400-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="connection"/><category scheme="http://www.blogger.com/atom/ns#" term="database"/><category scheme="http://www.blogger.com/atom/ns#" term="jdbc"/><category scheme="http://www.blogger.com/atom/ns#" term="logging"/><title type='text'>Log4jdbc: Logging JDBC activity and SQL queries</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In an application that frequently queries a database it is important to know what is happening now, how often this or that query occurs and how long it takes to get result. &lt;b&gt;Log4jdbc&lt;/b&gt; allows to log database activity e.g. SQL queries, processing time, result sets, connection usage.&lt;br /&gt;
&lt;div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Log4jdbc&lt;/b&gt; creates a&amp;nbsp;&lt;b&gt;JDBC&lt;/b&gt;&amp;nbsp;&lt;b&gt;Proxy&lt;/b&gt;. Every SQL request is intercepted, logged with&amp;nbsp;&lt;b&gt;SLF4J&lt;/b&gt; and passed back to real JDBC driver. &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
So why and when do we need to log database activity? Many ORM frameworks e.g. Hibernate allow you to show SQL when running in debug mode.&lt;br /&gt;
&lt;br /&gt;
To see what Hibernate is doing with your database, just add these parameters to your Hibernate configuration:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;property name=&quot;show_sql&quot;&amp;gt;true&amp;lt;/property&amp;gt;
&amp;lt;property name=&quot;format_sql&quot;&amp;gt;true&amp;lt;/property&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
The following output will be generated by Hibernate logger:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;DEBUG org.hibernate.SQL &amp;nbsp;- select deliveryre0_.id as id1_, deliveryre0_.callback_reference_id as callback2_1_, deliveryre0_.client_correlator as client3_1_, deliveryre0_.client_id as client4_1_, deliveryre0_.created as created1_, deliveryre0_.last_status_change as last6_1_, deliveryre0_.status as status1_, deliveryre0_.sender_address as sender8_1_ from delivery_report_notification deliveryre0_ where deliveryre0_.client_id=? and deliveryre0_.client_correlator=?

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The disadvantage of these debug records is that they show PreparedStatements with placeholders instead of real values. It is certainly possible to debug parameter bindings in Hibernate, but the output is unnecessary verbose:&lt;/div&gt;
&lt;br /&gt;
&lt;pre&gt;Hibernate: INSERT INTO table (ID, DATE, PRICE) VALUES (?, ?, ?)

10:13:05,129 DEBUG IntegerType:133 - binding &#39;1&#39; to parameter: 1
10:13:05,133 DEBUG DateType:133 - binding &#39;14 October 2012&#39; to parameter: 2
10:13:05,134 DEBUG FloatType:133 - binding &#39;3.75&#39; to parameter: 3
&lt;/pre&gt;
&lt;br /&gt;
The output generated by Log4jdbc is more useful, it show a real query that is passed into database with real values:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;INFO &amp;nbsp;jdbc.sqltiming &amp;nbsp;- select deliveryre0_.id as id1_, deliveryre0_.callback_reference_id as callback2_1_, deliveryre0_.client_correlator as client3_1_, deliveryre0_.client_id as client4_1_, deliveryre0_.created as created1_, deliveryre0_.last_status_change as last6_1_, deliveryre0_.status as status1_, deliveryre0_.sender_address as sender8_1_ from delivery_report_notification deliveryre0_ where deliveryre0_.client_id=2 and deliveryre0_.client_correlator=&#39;ALPHABETA&#39; {executed in 1 msec}&lt;/pre&gt;
&lt;br /&gt;
This was an example of the jdbc.sqltiming logger. Log4jdbc has several loggers that can be used e.g. to track connection usage:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;jdbc.sqlonly&lt;/b&gt; - Logs only SQL. SQL executed within a prepared statement is automatically shown with it&#39;s bind arguments replaced with the data bound at that position, for greatly increased readability.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;jdbc.sqltiming&lt;/b&gt; -&amp;nbsp;Logs the SQL, post-execution, including timing statistics on how long the SQL took to execute.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;jdbc.audit&lt;/b&gt; - Logs ALL JDBC calls except for ResultSets. This is a very voluminous output, and is not normally needed unless tracking down a specific JDBC problem.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;jdbc.resultset&lt;/b&gt; - Even more voluminous, because all calls to ResultSet objects are logged.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;jdbc.connection&lt;/b&gt; - Logs connection open and close events as well as dumping all open connection numbers. This is very useful for hunting down connection leak problems.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Logback configuration&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;logger name=&quot;jdbc.sqlonly&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;
&amp;lt;logger name=&quot;jdbc.sqltiming&quot; level=&quot;INFO&quot; additivity=&quot;false&quot;&amp;gt;
&amp;nbsp; &amp;lt;appender-ref ref=&quot;DATABASE_LOG_FILE&quot; /&amp;gt;
&amp;lt;/logger&amp;gt;
&amp;lt;logger name=&quot;jdbc.audit&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;
&amp;lt;logger name=&quot;jdbc.resultset&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;
&amp;lt;logger name=&quot;jdbc.connection&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;
&amp;lt;logger name=&quot;jdbc.resultsettable&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;
&amp;lt;logger name=&quot;log4jdbc.debug&quot; level=&quot;OFF&quot; additivity=&quot;false&quot; /&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Log4jdbc is not available on Maven central repository, so you have to download it manually. Log4jdbc is available on Google Code at&amp;nbsp;&lt;a href=&quot;http://code.google.com/p/log4jdbc/&quot; target=&quot;_blank&quot;&gt;http://code.google.com/p/log4jdbc/&lt;/a&gt;. You can download its source codes or jars for JDBC 3 or 4.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Good news&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
There is an experimental fork called &lt;b&gt;log4jdbc-remix&lt;/b&gt;, that solves some of the Log4jdbc problems. It is available on Maven central repository:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;dependency&amp;gt;
&amp;nbsp; &amp;lt;groupId&amp;gt;org.lazyluke&amp;lt;/groupId&amp;gt;
&amp;nbsp; &amp;lt;artifactId&amp;gt;log4jdbc-remix&amp;lt;/artifactId&amp;gt;
&amp;nbsp; &amp;lt;version&amp;gt;0.2.7&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;

&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
Also you can use it in Spring as a datasource.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;bean id=&quot;dataSourceSpied&quot; class=&quot;...&quot;&amp;gt;
&amp;nbsp; &amp;lt;property name=&quot;driverClass&quot; value=&quot;${datasource.driverClassName}&quot;/&amp;gt;
&amp;nbsp; &amp;lt;property name=&quot;jdbcUrl&quot; value=&quot;${datasource.url}&quot;/&amp;gt;
&amp;nbsp; &amp;lt;property name=&quot;user&quot; value=&quot;${datasource.username}&quot;/&amp;gt;
&amp;nbsp; &amp;lt;property name=&quot;password&quot; value=&quot;${datasource.password}&quot;/&amp;gt;
&amp;nbsp; ...
&amp;lt;/bean&amp;gt;

&amp;lt;bean id=&quot;dataSource&quot; class=&quot;net.sf.log4jdbc.Log4jdbcProxyDataSource&quot;&amp;gt;
&amp;nbsp; &amp;lt;constructor-arg ref=&quot;dataSourceSpied&quot; /&amp;gt;
&amp;lt;/bean&amp;gt;

&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Log4jdbc-remix&lt;/b&gt; is available on Google Code at&amp;nbsp;&lt;a href=&quot;http://code.google.com/p/log4jdbc-remix/&quot;&gt;http://code.google.com/p/log4jdbc-remix/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/7099074883822031434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2013/02/log4jdbc-logging-jdbc-activity-and-sql.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7099074883822031434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7099074883822031434'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2013/02/log4jdbc-logging-jdbc-activity-and-sql.html' title='Log4jdbc: Logging JDBC activity and SQL queries'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-5551356041615343828</id><published>2013-02-15T05:42:00.000-08:00</published><updated>2013-02-15T05:47:31.933-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="basic auth"/><category scheme="http://www.blogger.com/atom/ns#" term="http client"/><category scheme="http://www.blogger.com/atom/ns#" term="rest template"/><category scheme="http://www.blogger.com/atom/ns#" term="spring"/><category scheme="http://www.blogger.com/atom/ns#" term="timeout"/><title type='text'>RestTemplate and HttpClient 4.1 configuration</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This example shows how to configure &lt;b&gt;Spring RestTemplate&lt;/b&gt; with Apache &lt;b&gt;HttpClient&lt;/b&gt; 4.1 as a transport.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;In this example we will configure the following parameters:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;connection timeout&lt;/li&gt;
&lt;li&gt;read timeout&lt;/li&gt;
&lt;li&gt;maximum connections (per host)&lt;/li&gt;
&lt;li&gt;maximum connections (total)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Also, we will set up &lt;b&gt;Http Basic Authentication&lt;/b&gt; using login and password.&lt;br /&gt;
&lt;br /&gt;
We are going to use JavaConfig &lt;b&gt;@Configuration&lt;/b&gt; class, that has all the parameters set from properties using @Value annotations.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;@Configuration
public class RestClientConfig {

@Value(&quot;${rest.client.login}&quot;)
private String restClientLogin;

@Value(&quot;${rest.client.password}&quot;)
private String restClientPassword;

@Value(&quot;${rest.client.connectionTimeoutMillis}&quot;)
private int restClientConnectionTimeoutMillis;

@Value(&quot;${rest.client.readTimeoutMillis}&quot;)
private int restClientReadTimeoutMillis;

@Value(&quot;${rest.client.maxConnectionsPerHost}&quot;)
private int restClientMaxConnectionsPerHost;

@Value(&quot;${rest.client.maxTotalConnections}&quot;)
private int restClientMaxTotalConnections;

// ...

}&lt;/pre&gt;
&lt;br /&gt;
Lets define HttpClient @Bean by creating new &lt;b&gt;PoolingClientConnectionManager&lt;/b&gt; and configuring maxConnectionsPerHost and maxTotalConnections.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;@Bean
public HttpClient getHttpClient() {

final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
&amp;nbsp;
connectionManager.setDefaultMaxPerRoute(restClientMaxConnectionsPerHost);&amp;nbsp;
connectionManager.setMaxTotal(restClientMaxTotalConnections);

final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
// ...
return httpClient;
}&lt;/pre&gt;
&lt;br /&gt;
Then we need to configure&amp;nbsp;&lt;b&gt;ClientHttpRequestFactory&lt;/b&gt; that allows to define read and connection timeouts.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;@Bean
public ClientHttpRequestFactory getClientHttpRequestFactory() {

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(getHttpClient());

factory.setConnectTimeout(restClientConnectionTimeoutMillis);
factory.setReadTimeout(restClientReadTimeoutMillis);

return factory;
}&lt;/pre&gt;
&lt;br /&gt;
Now we can create &lt;b&gt;RestTemplate&lt;/b&gt; @Bean&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;@Bean
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
return restTemplate;
}&lt;/pre&gt;
&lt;br /&gt;
Now we need to configure Http Basic Authentication. To do Basic Authentication we need to add &lt;b&gt;Authentication: Basic&lt;/b&gt; header to HTTP request. We can do it by registering a &lt;b&gt;HttpRequestInterceptor&lt;/b&gt; that would add the header to every request.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;public class HttpBasicAuthInterceptor implements HttpRequestInterceptor {

private UsernamePasswordCredentials creds;

public HttpBasicAuthInterceptor(UsernamePasswordCredentials creds) {
this.creds = creds;
}


@Override
public void process(HttpRequest request, HttpContext context)&amp;nbsp;
throws HttpException, IOException {
request.addHeader(new BasicScheme().authenticate(creds, request, context));
}

}&lt;/pre&gt;
&lt;br /&gt;
Register &lt;b&gt;HttpRequestInterceptor&lt;/b&gt; in HttpClient:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;HttpRequestInterceptor interceptor = new HttpBasicAuthInterceptor(new UsernamePasswordCredentials(restClientLogin, restClientPassword));

httpClient.addRequestInterceptor(interceptor);&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Full source code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;@Configuration
public class RestClientConfig {


 @Value(&quot;${rest.client.login}&quot;)
 private String restClientLogin;

 @Value(&quot;${rest.client.password}&quot;)
 private String restClientPassword;


 @Value(&quot;${rest.client.connectionTimeoutMillis}&quot;)
 private int restClientConnectionTimeoutMillis;

 @Value(&quot;${rest.client.readTimeoutMillis}&quot;)
 private int restClientReadTimeoutMillis;

 @Value(&quot;${rest.client.maxConnectionsPerHost}&quot;)
 private int restClientMaxConnectionsPerHost;

 @Value(&quot;${rest.client.maxTotalConnections}&quot;)
 private int restClientMaxTotalConnections;

  
 @Bean
 public HttpClient getHttpClient() {

 final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();

 connectionManager.setDefaultMaxPerRoute(restClientMaxConnectionsPerHost);
 connectionManager.setMaxTotal(restClientMaxTotalConnections);

    
 final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

    HttpRequestInterceptor interceptor = new HttpBasicAuthInterceptor(new UsernamePasswordCredentials(restClientLogin, restClientPassword));

    httpClient.addRequestInterceptor(interceptor);

 return httpClient;

 }


 @Bean
 public ClientHttpRequestFactory getClientHttpRequestFactory() {

 HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(getHttpClient());

 factory.setConnectTimeout(restClientConnectionTimeoutMillis);
 factory.setReadTimeout(restClientReadTimeoutMillis);

 return factory;

 } 


 @Bean
 public RestTemplate getRestTemplate() {
 RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
 return restTemplate;
 }

}  


public class HttpBasicAuthInterceptor implements HttpRequestInterceptor {

 private UsernamePasswordCredentials creds;

 public HttpBasicAuthInterceptor(UsernamePasswordCredentials creds) {
 this.creds = creds;
 }

 @Override
 public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {

 request.addHeader(new BasicScheme().authenticate(creds, request, context));
 }

}&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/5551356041615343828/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2013/02/resttemplate-and-httpclient-41.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5551356041615343828'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/5551356041615343828'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2013/02/resttemplate-and-httpclient-41.html' title='RestTemplate and HttpClient 4.1 configuration'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-2781835594332542349</id><published>2012-07-02T10:11:00.001-07:00</published><updated>2012-07-11T13:26:33.142-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="layout"/><category scheme="http://www.blogger.com/atom/ns#" term="spring mvc"/><category scheme="http://www.blogger.com/atom/ns#" term="template"/><category scheme="http://www.blogger.com/atom/ns#" term="tutorial"/><category scheme="http://www.blogger.com/atom/ns#" term="velocity"/><title type='text'>Spring MVC and Velocity WebApp</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This tutorial shows how to use &lt;b&gt;Velocity templates&lt;/b&gt; with &lt;b&gt;Spring MVC&lt;/b&gt;. Velocity is more flexible templating engine than JSP/JSTL standard templates. We will create a simple application that contains 2 pages: list and detail. These pages will have common layout. &lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Start with an empty Maven web application &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We need to add Spring and Velocity dependencies to our pom.xml file:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;javax&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;javaee-web-api&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;6.0&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;provided&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-context&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.1.0.RELEASE&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-context-support&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.1.0.RELEASE&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-web&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.1.0.RELEASE&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-webmvc&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.1.0.RELEASE&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.apache.velocity&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;velocity&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;1.7&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.apache.velocity&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;velocity-tools&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;2.0&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;

    &amp;lt;/dependencies&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web Application will be deployed to Jetty Web Container. Add the following plugin to plugins section of pom.xml:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;  &amp;lt;plugin&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;maven-jetty-plugin&amp;lt;/artifactId&amp;gt;
      &amp;lt;version&amp;gt;6.1.16&amp;lt;/version&amp;gt;
      &amp;lt;configuration&amp;gt;
           &amp;lt;contextPath&amp;gt;/&amp;lt;/contextPath&amp;gt;
           &amp;lt;scanTargets&amp;gt;
              &amp;lt;scanTarget&amp;gt;target/classes/&amp;lt;/scanTarget&amp;gt;
           &amp;lt;/scanTargets&amp;gt;
          &amp;lt;scanIntervalSeconds&amp;gt;5&amp;lt;/scanIntervalSeconds&amp;gt;
      &amp;lt;/configuration&amp;gt;
  &amp;lt;/plugin&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
To start the application call &lt;b&gt;mvn jetty:run&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The application will be available on http://127.0.0.1:8080/. When you open this address with a browser you will see just a Hello World message from index.jsp, indicating that empty application was successfully deployed. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; You may use any web container you like (e.g. Tomcat). I prefer Jetty because it is very fast and light-weight.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Dummy data model&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Our application will be showing a list of &lt;b&gt;Feeds&lt;/b&gt; with link to detail.&lt;br /&gt;
&lt;br /&gt;
Entity &lt;b&gt;Feed&lt;/b&gt; contains &lt;b&gt;Id &lt;/b&gt;and &lt;b&gt;Title&lt;/b&gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package com.blogspot.vozis.springvelocity.data;

/**
 * Feed
 * @author sergej.sizov
 */
public class Feed {

    private Integer id;
    private String title;

    public Feed(Integer id, String title) {
        this.id = id;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}&lt;/pre&gt;
&lt;br /&gt;
FeedService is a provider of Feeds. It has 2 methods: getFeedById() and getFeeds().&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package com.blogspot.vozis.springvelocity.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

/**
 * Feed Service
 * @author sergej.sizov
 */
@Service
public class FeedService {

    private Map&amp;lt;Integer, Feed&amp;gt; storage = new HashMap&amp;lt;Integer, Feed&amp;gt;();

    public FeedService() {
        storage.put(1, new Feed(1, &quot;Spring Tutorial&quot;));
        storage.put(2, new Feed(2, &quot;Velocity Tutorial&quot;));
        storage.put(3, new Feed(3, &quot;Java Tutorial&quot;));
    }

    public Feed getFeedById(Integer id) {
        return storage.get(id);
    }

    public List&amp;lt;Feed&amp;gt; getFeeds() {
        List&amp;lt;Feed&amp;gt; list = new ArrayList&amp;lt;Feed&amp;gt;();
        list.addAll(storage.values());
        return list;
    }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Spring Context Configuration&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now it is time to configure Spring framework. &lt;br /&gt;
&lt;br /&gt;
Create &lt;b&gt;spring-context.xml&lt;/b&gt; file in WEB-INF and put the following lines into it:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    &quot;&amp;gt;
    &amp;lt;context:component-scan base-package=&quot;com.blogspot.vozis.springvelocity.data&quot; /&amp;gt;

&amp;lt;/beans&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
You need to modify &lt;b&gt;base-package&lt;/b&gt; parameter according to your package name.&lt;br /&gt;
&lt;br /&gt;
Next, we need to register ContextListener that starts Spring Context on application start. Add the following lines to web.xml:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;    &amp;lt;context-param&amp;gt;
        &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;
        &amp;lt;param-value&amp;gt;/WEB-INF/spring-context.xml&amp;lt;/param-value&amp;gt;
    &amp;lt;/context-param&amp;gt;
    &amp;lt;listener&amp;gt;
        &amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;
    &amp;lt;/listener&amp;gt; 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Creating templates&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Create new folder &lt;b&gt;views&lt;/b&gt; in WEB-INF. This folder will contain Velocity templates. Application has 2 pages: list and detail, both having the same layout.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s create layout file &lt;b&gt;layout.vm&lt;/b&gt; and put it into &lt;b&gt;WEB-INF/views&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;Spring MVC and Velocity&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;Spring MVC and Velocity&amp;lt;/h1&amp;gt;

        $screen_content

        &amp;lt;hr /&amp;gt;
        Copyright &amp;amp;copy 2012 Sergej Sizov
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
As you may see &lt;b&gt;$screen_content&lt;/b&gt; variable contains the content of pages.&lt;br /&gt;
&lt;br /&gt;
Now let&#39;s create &lt;b&gt;list.vm&lt;/b&gt; and &lt;b&gt;detail.vm&lt;/b&gt; pages:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;h2&amp;gt;List of Feeds&amp;lt;/h2&amp;gt;
&amp;lt;ul&amp;gt;
    #foreach($feed in $feeds)
        &amp;lt;li&amp;gt;&amp;lt;a href=&quot;/feed/${feed.id}&quot;&amp;gt;${feed.title}&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    #end
&amp;lt;/ul&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;h2&amp;gt;Detail of Feed&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;Id: ${feed.id}&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Title: ${feed.title}&amp;lt;/p&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Configuring Spring MVC to use Velocity templates&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Create a file &lt;b&gt;servlet-context.xml&lt;/b&gt; in &lt;b&gt;WEB-INF&lt;/b&gt; and put the following lines into it:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
    xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
    xsi:schemaLocation=&quot;http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd&quot;&amp;gt;

    &amp;lt;mvc:annotation-driven /&amp;gt;

    &amp;lt;bean id=&quot;velocityConfig&quot;
        class=&quot;org.springframework.web.servlet.view.velocity.VelocityConfigurer&quot;&amp;gt;
        &amp;lt;property name=&quot;resourceLoaderPath&quot; value=&quot;/WEB-INF/views/&quot; /&amp;gt;
    &amp;lt;/bean&amp;gt;

    &amp;lt;bean id=&quot;viewResolver&quot;
        class=&quot;org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver&quot;&amp;gt;
        &amp;lt;property name=&quot;cache&quot; value=&quot;true&quot; /&amp;gt;
        &amp;lt;property name=&quot;prefix&quot; value=&quot;&quot; /&amp;gt;
        &amp;lt;property name=&quot;layoutUrl&quot; value=&quot;layout.vm&quot;/&amp;gt;
        &amp;lt;property name=&quot;suffix&quot; value=&quot;.vm&quot; /&amp;gt;
    &amp;lt;/bean&amp;gt;

    &amp;lt;context:component-scan base-package=&quot;com.blogspot.vozis.springvelocity.web&quot; /&amp;gt;

&amp;lt;/beans&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then register Spring Servlet to intercept all HTTP request. You need to add the following lines into &lt;b&gt;web.xml&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;    &amp;lt;servlet&amp;gt;
        &amp;lt;servlet-name&amp;gt;servlet&amp;lt;/servlet-name&amp;gt;
        &amp;lt;servlet-class&amp;gt;org.springframework.web.servlet.DispatcherServlet&amp;lt;/servlet-class&amp;gt;
        &amp;lt;init-param&amp;gt;
            &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;
            &amp;lt;param-value&amp;gt;/WEB-INF/servlet-context.xml&amp;lt;/param-value&amp;gt;
        &amp;lt;/init-param&amp;gt;
        &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;
    &amp;lt;/servlet&amp;gt;
   
    &amp;lt;servlet-mapping&amp;gt;
        &amp;lt;servlet-name&amp;gt;servlet&amp;lt;/servlet-name&amp;gt;
        &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
    &amp;lt;/servlet-mapping&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Programming the Controller&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Create a new java class WebController:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package com.blogspot.vozis.springvelocity.web;

import com.blogspot.vozis.springvelocity.data.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Controller
 *
 * @author sergej.sizov
 */
@Controller
public class WebController {

    private FeedService feedService;

    @Autowired
    public WebController(FeedService feedService) {
        this.feedService = feedService;
    }

    @RequestMapping(&quot;/&quot;)
    public String list(ModelMap model) {
        model.put(&quot;feeds&quot;, feedService.getFeeds());
        return &quot;list&quot;;
    }

    @RequestMapping(&quot;/feed/{id}&quot;)
    public String detail(@PathVariable(value = &quot;id&quot;) Integer feedId, ModelMap model) {
        model.put(&quot;feed&quot;, feedService.getFeedById(feedId));
        return &quot;detail&quot;;
    }
}


&lt;/pre&gt;
&lt;br /&gt;
Spring Controller is annotated with @Controller. Spring MVC automatically scans for @Controllers and creates mapping to methods according to @RequestMapping URL pattern.&lt;br /&gt;
&lt;br /&gt;
ModelMap map contains data to be passed to Velocity, the return parameter defines the template to be used (e.g. list, detail). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/2781835594332542349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/07/spring-mvc-and-velocity-webapp.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2781835594332542349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2781835594332542349'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/07/spring-mvc-and-velocity-webapp.html' title='Spring MVC and Velocity WebApp'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-4651622970016506596</id><published>2012-05-08T12:39:00.000-07:00</published><updated>2015-01-03T06:02:13.477-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ifnull"/><category scheme="http://www.blogger.com/atom/ns#" term="mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="nulls on top"/><category scheme="http://www.blogger.com/atom/ns#" term="sort"/><title type='text'>MySQL: Sort by column with NULLs on top</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Common task, when you need to sort table data by a column, that may have &lt;b&gt;null values&lt;/b&gt; and you want to put nulls to the top.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Example table&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;id | name | salary
---|------|-------
 1 | John | 1000
 2 | Mary | 500
 3 | Paul | null
&lt;/pre&gt;
&lt;br /&gt;
The following SQL request sorts the table by salary putting the nulls on top:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;SELECT * FROM table ORDER BY IFNULL(salary, 0)
&lt;/pre&gt;
&lt;br /&gt;
Using &lt;b&gt;IFNULL(column, 0)&lt;/b&gt; we simply substitute null with &lt;b&gt;zero&lt;/b&gt;, so it gets to the top.&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;id | name | salary
---|------|-------
 3 | Paul | null
 2 | Mary | 500 
 1 | John | 1000
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/4651622970016506596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/05/mysql-sort-by-column-with-nulls-on-top.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4651622970016506596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4651622970016506596'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/05/mysql-sort-by-column-with-nulls-on-top.html' title='MySQL: Sort by column with NULLs on top'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-6059683233737029840</id><published>2012-04-27T05:13:00.000-07:00</published><updated>2012-04-27T05:13:00.237-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="hook"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="release"/><category scheme="http://www.blogger.com/atom/ns#" term="resources"/><category scheme="http://www.blogger.com/atom/ns#" term="runtime"/><category scheme="http://www.blogger.com/atom/ns#" term="shutdown"/><title type='text'>Java: Release resources on program termination</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Applications use different resources (database connections, file descriptors, etc.) that need to be released when program terminates. &lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Java application can register a ShutdownHook that would be executed before the last non-daemon process is terminated. It also works when application is terminated by &lt;b&gt;System.exit() &lt;/b&gt;or external interrupt request (kill).&lt;br /&gt;
&lt;br /&gt;
Java &lt;b&gt;Runtime &lt;/b&gt;has a method &lt;b&gt;addShutdownHook &lt;/b&gt;that registers a &lt;b&gt;Thread &lt;/b&gt;that would be started to do the cleaning up.&lt;br /&gt;
&lt;br /&gt;
Example, closing database connection on shutdown:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;// register shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread() {
&amp;nbsp;&amp;nbsp; @Override
&amp;nbsp;&amp;nbsp; public void run() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DatabaseFactory.shutdown();
&amp;nbsp;&amp;nbsp; }
});
&lt;/pre&gt;
&lt;br /&gt;
N.B.: ShutdownHook may not be executed if VM crashes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/6059683233737029840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/04/java-release-resources-on-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/6059683233737029840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/6059683233737029840'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/04/java-release-resources-on-program.html' title='Java: Release resources on program termination'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-2907434336275063274</id><published>2012-03-18T10:35:00.000-07:00</published><updated>2012-03-18T10:35:29.605-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ajax"/><category scheme="http://www.blogger.com/atom/ns#" term="tinymce"/><category scheme="http://www.blogger.com/atom/ns#" term="wicket"/><category scheme="http://www.blogger.com/atom/ns#" term="wysiwyg"/><title type='text'>Wicket TinyMCE and AjaxButton</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
TinyMCE is a &lt;b&gt;WYSIWYG editor&lt;/b&gt;, that can be used in Wicket to replace your TextAreas with a Rich Text Editor.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;You need to add the following dependency into you pom.xml:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;nbsp;&amp;lt;dependency&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.wicketstuff&amp;lt;/groupId&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;wicketstuff-tinymce&amp;lt;/artifactId&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;1.5.4&amp;lt;/version&amp;gt;
&amp;nbsp;&amp;lt;/dependency&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
This would allow you to add &lt;b&gt;TinyMceBehaviour&lt;/b&gt; to a TextArea that you want to transform into a&amp;nbsp; WYSIWYG editor.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;TextArea contentTextArea = new TextArea(&quot;content&quot;);
// tinymce
contentTextArea.add(new TinyMceBehavior(new TinyMCESettings(Theme.advanced)));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
contentTextArea.add(StringValidator.maximumLength(4000));
form.add(contentTextArea); 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Your &lt;b&gt;&amp;lt;textarea&amp;gt;&lt;/b&gt; tag will be replaced by TinyMCE editor. On form submit, the content of TinyMCE editor is converted back into &amp;lt;textarea&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
TinyMCE does not work with AjaxButtons, as AjaxButton does not call Form submit event. There is a work around for this. You need to call &lt;b&gt;triggerSave&lt;/b&gt; on your AjaxButton onClick event:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;tinyMCE.triggerSave(true,true);
&lt;/pre&gt;
&lt;br /&gt;

To do this just add  &lt;b&gt;TinyMceAjaxSubmitModifier&lt;/b&gt; to your AjaxButton:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;ajaxButton.add(new TinyMceAjaxSubmitModifier()); 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/2907434336275063274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/03/wicket-tinymce-and-ajaxbutton.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2907434336275063274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2907434336275063274'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/03/wicket-tinymce-and-ajaxbutton.html' title='Wicket TinyMCE and AjaxButton'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-4027730367385746493</id><published>2012-03-13T11:29:00.003-07:00</published><updated>2012-03-13T11:29:29.698-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="class-path"/><category scheme="http://www.blogger.com/atom/ns#" term="jar"/><category scheme="http://www.blogger.com/atom/ns#" term="main-class"/><category scheme="http://www.blogger.com/atom/ns#" term="manifest"/><category scheme="http://www.blogger.com/atom/ns#" term="maven"/><category scheme="http://www.blogger.com/atom/ns#" term="plugin"/><title type='text'>Maven Jar Plugin: Setting Main-Class and Class-Path in Manifest</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
To start a Java application you have to tell JVM where is the program&#39;s entry point (Main Class) and where to search for classes (Class Path). Main class must have a public static method called &lt;b&gt;main&lt;/b&gt;, which takes command arguments and does the initialization. If your Java application is packaged into JAR you can define &lt;b&gt;Main-Class&lt;/b&gt; and &lt;b&gt;Class-Path&lt;/b&gt; in &lt;b&gt;manifest&lt;/b&gt; file, so that JVM would be able to find your program&#39;s entry point and load necessary classes. To do so you can use &lt;b&gt;maven-jar-plugin&lt;/b&gt; that generates manifest with Main-Class and Class-Path parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Add the following lines into your &lt;b&gt;pom.xml&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;plugin&amp;gt;
&amp;nbsp; &amp;lt;artifactId&amp;gt;maven-jar-plugin&amp;lt;/artifactId&amp;gt;
&amp;nbsp; &amp;lt;configuration&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;archive&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;manifest&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;addDefaultImplementationEntries&amp;gt;false&amp;lt;/addDefaultImplementationEntries&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;addClasspath&amp;gt;true&amp;lt;/addClasspath&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;mainClass&amp;gt;com.blogspot.vozis.MyApp&amp;lt;/mainClass&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/manifest&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;manifestEntries&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Class-Path&amp;gt;../conf/&amp;lt;/Class-Path&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/manifestEntries&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/archive&amp;gt;
&amp;nbsp; &amp;lt;/configuration&amp;gt;
&amp;lt;/plugin&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
Definition of the parameters:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;addClasspath &lt;/b&gt;- generates Class Path from your dependencies&lt;/li&gt;
&lt;li&gt;&lt;b&gt;mainClass &lt;/b&gt;- name of the main class including package&lt;/li&gt;
&lt;li&gt;&lt;b&gt;manifestEntries &lt;/b&gt;/ &lt;b&gt;Class-Path&lt;/b&gt; - adds extra places to be added to Class Path&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Resulting manifest file:
&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: sergej.sizov
Build-Jdk: 1.5.0_22
Main-Class: com.blogspot.vozis.MyApp
Class-Path: ../conf/ xmlrpc-2.0.jar commons-httpclient-3.0.jar log4j-1.2.15.jar 
&lt;/pre&gt;
&lt;br /&gt;
To start the application you no more need to define Main Class or Class Path using &lt;b&gt;-cp&lt;/b&gt; parameter.&lt;br /&gt;
&lt;br /&gt;
Just run&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;java -jar MyApp.jar&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/4027730367385746493/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/03/maven-jar-plugin-setting-main-class-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4027730367385746493'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4027730367385746493'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/03/maven-jar-plugin-setting-main-class-and.html' title='Maven Jar Plugin: Setting Main-Class and Class-Path in Manifest'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-2353845523008921500</id><published>2012-03-13T04:50:00.000-07:00</published><updated>2012-03-13T05:14:46.034-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="debugging"/><category scheme="http://www.blogger.com/atom/ns#" term="netbeans"/><category scheme="http://www.blogger.com/atom/ns#" term="php"/><category scheme="http://www.blogger.com/atom/ns#" term="xdebug"/><title type='text'>Debugging PHP with Xdebug and Netbeans</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Many PHP programmers tend to develop their applications without special debugging tools. PHP developers are happy, because they do not have to wait for recompilation and redeploy, they just press F5 and see the results in browser. I call it &lt;b&gt;&quot;echo&quot; debug&lt;/b&gt; method :). In this article I will show you how easy it is to install &lt;b&gt;Xdebug &lt;/b&gt;debugger tool and use it in &lt;b&gt;Netbeans &lt;/b&gt;IDE.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;I use stand-alone installation of Apache 2.2, PHP 5.3 thread-safe. We need to download &lt;b&gt;Xdebug 2.1.4&lt;/b&gt; that supports PHP 5.3 from &lt;a href=&quot;http://xdebug.org/download.php&quot;&gt;http://xdebug.org/download.php&lt;/a&gt;. Linux users have to download Xdebug&#39;s source code and build it for concrete system. Windows users can download already compiled binaries.&lt;br /&gt;
&lt;br /&gt;
You have to choose the right version according to your version of PHP. To find out your version of PHP use &lt;b&gt;php -i&lt;/b&gt; command or &lt;b&gt;phpinfo()&lt;/b&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Xdebug 2.1.4&lt;/b&gt; is available for :&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;PHP 5.2 VC9 (32 bit),&lt;/li&gt;
&lt;li&gt;PHP 5.2 VC9 TS (32 bit),&lt;/li&gt;
&lt;li&gt;PHP 5.3 VC9 (64 bit),&lt;/li&gt;
&lt;li&gt;PHP 5.3 VC9 (32 bit),&lt;/li&gt;
&lt;li&gt;PHP 5.3 VC9 TS (64 bit),&lt;/li&gt;
&lt;li&gt;PHP 5.3 VC9 TS (32 bit)&lt;/li&gt;
&lt;/ul&gt;
According to &lt;b&gt;phpinfo()&lt;/b&gt; I use PHP version 5.3 thread-safe (TS), 32 bit x86, so I have to download PHP 5.3 VC9 TS (32 bit). &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;: do not use older version of Xdebug 2.1.3 as it crashes on &lt;b&gt;simplexml &lt;/b&gt;object inspection.&lt;br /&gt;
&lt;br /&gt;
Then we put the downloaded library (extension) into PHP&#39;s extensions directory. In my case it is &lt;b&gt;d:\php5\ext&lt;/b&gt; and I would rename it to &lt;b&gt;php_xdebug.dll&lt;/b&gt; for short name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Xdebug PHP extension configuration&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Open &lt;b&gt;php.ini&lt;/b&gt; configuration file and add the following line:
&lt;br /&gt;
&lt;pre&gt;zend_extension=d:\php5\ext\php_xdebug.dll&lt;/pre&gt;
&lt;br /&gt;
Then we need to allow remote debugging
&lt;br /&gt;
&lt;pre&gt;xdebug.remote_enable=1&lt;/pre&gt;
&lt;br /&gt;
and configure&lt;b&gt; host, protocol and port&lt;/b&gt; that would listen for debugger:
&lt;br /&gt;
&lt;pre&gt;xdebug.remote_host=127.0.0.1
xdebug.remote_handler=dbgp 
xdebug.remote_port=9000&amp;nbsp;&lt;/pre&gt;
In some cases we need to see the debugger logs, e.g. when Xdebud crashes. You can turn on Xdebug logging with the following line:
&lt;br /&gt;
&lt;pre&gt;xdebug.remote_log=d:\php5\xdebug.log&lt;/pre&gt;
&lt;br /&gt;
Save changes and restart Apache server.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Netbeans configuration&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In your Netbeans IDE open &lt;b&gt;Tools &amp;gt; Options &amp;gt; PHP&lt;/b&gt;, select &lt;b&gt;Debugging &lt;/b&gt;tab. Check that &lt;b&gt;Debugger port&lt;/b&gt; is the the same as your &lt;b&gt;xdebug.remote_port&lt;/b&gt; parameter (9000). &lt;br /&gt;
&lt;br /&gt;
If you want the debugger to stop at the first line of your application check &quot;Stop at first line&quot;, otherwise debugger will stop at the first reached breakpoint.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxU1dkT0CYeoJbwLaLMQr4Sw9V1CtRngmG0eQkxC1fXLUhyphenhyphenoVq57tw1qAn1j629_kbTmaz-UiYzy2_ulb0IYdYqj2Hh2LehZ38nHxY3OYDfDK_hdbOR6viPf7ZthEUNM5IMXgQc6YmxZvy/s1600/nb1.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;488&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxU1dkT0CYeoJbwLaLMQr4Sw9V1CtRngmG0eQkxC1fXLUhyphenhyphenoVq57tw1qAn1j629_kbTmaz-UiYzy2_ulb0IYdYqj2Hh2LehZ38nHxY3OYDfDK_hdbOR6viPf7ZthEUNM5IMXgQc6YmxZvy/s640/nb1.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Open your PHP project in Netbeans IDE, set a &lt;b&gt;breakpoint &lt;/b&gt;by double-clicking on the row number or by pressing Ctrl + F8 and then start debugging by pressing Ctrl + F5.&lt;br /&gt;
&lt;br /&gt;
Use the following keyboard shortcuts to debug your code:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;Ctrl + F5&lt;/b&gt;&amp;nbsp; - Start debugging main project&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + Shift + F5&lt;/b&gt; - Start debugging main project&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Shift + F5 / F5&lt;/b&gt; - Stop / Continue&lt;/li&gt;
&lt;li&gt;&lt;b&gt;F4&lt;/b&gt; - Run to cursor location&lt;/li&gt;
&lt;li&gt;&lt;b&gt;F7 / F8&lt;/b&gt; - Step into / over&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + F7&lt;/b&gt; - Step out&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + Alt + Up&lt;/b&gt; - Go to called method&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + Alt + Down&lt;/b&gt; - Go to calling method&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + F9&lt;/b&gt; - Evaluate Expression&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ctrl + F8&lt;/b&gt; - Toggle breakpoint&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Lucky debugging!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/2353845523008921500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/03/debugging-php-with-xdebug-and-netbeans.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2353845523008921500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2353845523008921500'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/03/debugging-php-with-xdebug-and-netbeans.html' title='Debugging PHP with Xdebug and Netbeans'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxU1dkT0CYeoJbwLaLMQr4Sw9V1CtRngmG0eQkxC1fXLUhyphenhyphenoVq57tw1qAn1j629_kbTmaz-UiYzy2_ulb0IYdYqj2Hh2LehZ38nHxY3OYDfDK_hdbOR6viPf7ZthEUNM5IMXgQc6YmxZvy/s72-c/nb1.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-3872136361452571181</id><published>2012-02-29T07:05:00.000-08:00</published><updated>2012-02-29T07:05:33.688-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="integer"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="overflow"/><title type='text'>Integer overflow in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
What would happen in Java when you &lt;b&gt;increment &lt;/b&gt;an Integer variable that already contains its &lt;b&gt;maximum &lt;/b&gt;value?&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;No, Java would not throw any exception :(&lt;br /&gt;
&lt;br /&gt;
Lets write a simple program to test it:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;    
public static void main(String[] args) {
   int i = Integer.MAX_VALUE;
   System.out.println(&quot;Before: &quot; + i 
             + &quot; = &quot; + Integer.toBinaryString(i));

   i++;
   System.out.println(&quot;After: &quot; + i 
             + &quot; = &quot; + Integer.toBinaryString(i));        
}
&lt;/pre&gt;
&lt;br /&gt;
The output is:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Before: 2147483647 = 01111111111111111111111111111111
After: -2147483648 = 10000000000000000000000000000000
&lt;/pre&gt;
&lt;br /&gt;
As you see, it is &lt;b&gt;negative&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
So, when you increment &lt;b&gt;Integer.MAX_VALUE&lt;/b&gt; by 1 it becomes &lt;b&gt;Integer.MIN_VALUE&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/3872136361452571181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/integer-overflow-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/3872136361452571181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/3872136361452571181'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/integer-overflow-in-java.html' title='Integer overflow in Java'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-4252638281820725943</id><published>2012-02-21T12:41:00.000-08:00</published><updated>2012-02-21T12:41:19.896-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="card"/><category scheme="http://www.blogger.com/atom/ns#" term="filter"/><category scheme="http://www.blogger.com/atom/ns#" term="log4j"/><category scheme="http://www.blogger.com/atom/ns#" term="pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="pci"/><category scheme="http://www.blogger.com/atom/ns#" term="security"/><title type='text'>Log4j filter to mask Payment Card numbers (PCI DSS)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
According to PCI DSS (Payment Card Industry Data Security Standard) your application &lt;b&gt;must not&lt;/b&gt; store payment card numbers. This requirement includes database, files and logs. The following filter will allow you to mask card numbers in your logs &lt;b&gt;on the fly&lt;/b&gt;, so even if you accidentally turned &lt;b&gt;debug mode&lt;/b&gt; on for network communication, you can be confident that your data is PCI compliant.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Log4j allows you to configure &lt;b&gt;PatternLayout&lt;/b&gt; that processes your log records. The idea is simple, out filter would match payment card numbers and replace them with masked values. Card number is usually a number of 15-19 digits.&lt;br /&gt;
&lt;br /&gt;
I am going to use &lt;b&gt;regular expression&lt;/b&gt; to match possible card numbers and replace them with masked values. I leave unmasked the beginning (6 digits) and the ending (4 digits), replacing the middle part with &amp;lt;HIDDEN&amp;gt; text. So, instead of 123456789012345678, I will get 123456&amp;lt;HIDDEN&amp;gt;5678 in my logs.&lt;br /&gt;
&lt;br /&gt;
The following class implements &lt;b&gt;PatternLayout&lt;/b&gt; with overriden &lt;b&gt;format()&lt;/b&gt; method that does filtering:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package vozis.logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

/**
&amp;nbsp;* Credit Card Filtering Layout
&amp;nbsp;* @author sergej.sizov
&amp;nbsp;*/
public class CreditCardFilteringLayout extends PatternLayout {
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;private static final String MASKCARD = &quot;$1&amp;lt;HIDDEN&amp;gt;$2&quot;;
&amp;nbsp;private static final Pattern PATTERNCARD =&amp;nbsp;
    Pattern.compile(&quot;([0-9]{6})[0-9]{0,9}([0-9]{4})&quot;);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;@Override
&amp;nbsp;public String format(LoggingEvent event) {
&amp;nbsp;&amp;nbsp; if (event.getMessage() instanceof String) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String message = event.getRenderedMessage();

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Matcher matcher = PATTERNCARD.matcher(message);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (matcher.find()) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String maskedMessage = matcher.replaceAll(MASKCARD);

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Throwable throwable =&amp;nbsp;
             event.getThrowableInformation() != null ?
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; event.getThrowableInformation().getThrowable() : null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LoggingEvent maskedEvent = new LoggingEvent(
                 event.fqnOfCategoryClass,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Logger.getLogger(event.getLoggerName()),&amp;nbsp;
                 event.timeStamp, 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; event.getLevel(),&amp;nbsp;
                 maskedMessage,&amp;nbsp;
                 throwable);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return super.format(maskedEvent);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp; return super.format(event);

&amp;nbsp;}
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we need to configure Log4j to use CreditCardFilteringLayout. You need to override &lt;b&gt;layout&lt;/b&gt; property for every &lt;b&gt;appender&lt;/b&gt; in &lt;b&gt;log4j.properties&lt;/b&gt; as it is shown below:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
&lt;b&gt;log4j.appender.stdout.layout=vozis.logger.CreditCardFilteringLayout&lt;/b&gt;
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1} - %m%n
log4j.appender.stdout.Threshold=info

log4j.appender.TEMP=org.apache.log4j.RollingFileAppender
log4j.appender.TEMP.File=temp.log 
log4j.appender.TEMP.MaxFileSize=5MB
log4j.appender.TEMP.MaxBackupIndex=1
&lt;b&gt;log4j.appender.TEMP.layout=vozis.logger.CreditCardFilteringLayout&lt;/b&gt;
log4j.appender.TEMP.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss,SSS} %C{1}:%M(line %L) - %m%n

&lt;/pre&gt;
&lt;br /&gt;
This idea can be used not only for credit card numbers, but also for &lt;b&gt;Social Security number (SSN)&lt;/b&gt; or any other data that you consider sensitive. The benefit of this solution is that it is a one place change and it is easier than checking every logger.log() invocation in your application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/4252638281820725943/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/log4j-filter-to-mask-payment-card.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4252638281820725943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/4252638281820725943'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/log4j-filter-to-mask-payment-card.html' title='Log4j filter to mask Payment Card numbers (PCI DSS)'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-2571659621510692310</id><published>2012-02-20T05:38:00.001-08:00</published><updated>2012-02-20T05:39:29.953-08:00</updated><title type='text'>Software Engineer - Weekends!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
What we do at weekends :)&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Click to enlarge&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAp0bL_XA9AvaGNFqeVZj2cDINsrne95LT876lsAif53ZkH0x6nWuJPa29SO55ahI1ja5Jq92tCQRyM2gRtou_I1TQvAeqnhObxWwmfHj6PthVGEcdZXPOZW63JP_Rgo9frnmNZJkonAu-/s1600/software-engineer.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;298&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAp0bL_XA9AvaGNFqeVZj2cDINsrne95LT876lsAif53ZkH0x6nWuJPa29SO55ahI1ja5Jq92tCQRyM2gRtou_I1TQvAeqnhObxWwmfHj6PthVGEcdZXPOZW63JP_Rgo9frnmNZJkonAu-/s400/software-engineer.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/2571659621510692310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/software-engineer-weekends.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2571659621510692310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2571659621510692310'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/software-engineer-weekends.html' title='Software Engineer - Weekends!'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAp0bL_XA9AvaGNFqeVZj2cDINsrne95LT876lsAif53ZkH0x6nWuJPa29SO55ahI1ja5Jq92tCQRyM2gRtou_I1TQvAeqnhObxWwmfHj6PthVGEcdZXPOZW63JP_Rgo9frnmNZJkonAu-/s72-c/software-engineer.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-7069188623600910214</id><published>2012-02-19T14:58:00.001-08:00</published><updated>2012-02-20T04:04:58.960-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="apache commons"/><category scheme="http://www.blogger.com/atom/ns#" term="commons"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="utils"/><title type='text'>Stop reinventing wheel, use Apache Commons</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Developer&#39;s life is full of routine tasks. We need to validate strings, convert data from different formats, compare objects and so on. Some developers tend to write everything on their own, reinventing common algorithms again and again. Every Java developer should look at &lt;b&gt;Apache Commons Lang&lt;/b&gt; library as it already has everything we need every day.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;When we work with Strings, we usually need to validate them (check if they are not null, not empty or do not contain some specific characters), transform, split, etc. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These tasks can be easily done with the following classes: &lt;b&gt;StringUtils&lt;/b&gt;, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils. &lt;br /&gt;
&lt;br /&gt;
How often do you check this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;if (str != null &amp;amp;&amp;amp; str.trim().length() &amp;gt; 0) {

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
when it can be easily done by:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;if (StringUtils.isNotBlank(str)) {

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
List of methods available from &lt;b&gt;StringUtils&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;IsEmpty/IsBlank&lt;/b&gt; - checks if a String contains text&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Trim&lt;/b&gt;/&lt;b&gt;Strip &lt;/b&gt;- removes leading and trailing whitespace&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Equals &lt;/b&gt;- compares two strings null-safe&lt;/li&gt;
&lt;li&gt;&lt;b&gt;IndexOf&lt;/b&gt;/&lt;b&gt;LastIndexOf&lt;/b&gt;/&lt;b&gt;Contains &lt;/b&gt;- null-safe index-of checks&lt;/li&gt;
&lt;li&gt;&lt;b&gt;IndexOfAny&lt;/b&gt;/&lt;b&gt;LastIndexOfAny&lt;/b&gt;/&lt;b&gt;IndexOfAnyBut&lt;/b&gt;/&lt;b&gt;LastIndexOfAnyBut &lt;/b&gt;- index-of any of a set of Strings&lt;/li&gt;
&lt;li&gt;&lt;b&gt;ContainsOnly&lt;/b&gt;/&lt;b&gt;ContainsNone &lt;/b&gt;- does String contains only/none of these characters&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Substring&lt;/b&gt;/&lt;b&gt;Left&lt;/b&gt;/&lt;b&gt;Right&lt;/b&gt;/&lt;b&gt;Mid &lt;/b&gt;- null-safe substring extractions&lt;/li&gt;
&lt;li&gt;&lt;b&gt;SubstringBefore&lt;/b&gt;/&lt;b&gt;SubstringAfter&lt;/b&gt;/&lt;b&gt;SubstringBetween &lt;/b&gt;- substring extraction relative to other strings&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Split&lt;/b&gt;/&lt;b&gt;Join &lt;/b&gt;- splits a String into an array of substrings and vice versa&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Remove&lt;/b&gt;/&lt;b&gt;Delete &lt;/b&gt;- removes part of a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Replace&lt;/b&gt;/&lt;b&gt;Overlay &lt;/b&gt;- Searches a String and replaces one String with another&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chomp&lt;/b&gt;/&lt;b&gt;Chop &lt;/b&gt;- removes the last part of a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;LeftPad&lt;/b&gt;/&lt;b&gt;RightPad&lt;/b&gt;/&lt;b&gt;Center&lt;/b&gt;/&lt;b&gt;Repeat &lt;/b&gt;- pads a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;UpperCase&lt;/b&gt;/&lt;b&gt;LowerCase&lt;/b&gt;/&lt;b&gt;SwapCase&lt;/b&gt;/&lt;b&gt;Capitalize&lt;/b&gt;/&lt;b&gt;Uncapitalize &lt;/b&gt;- changes the case of a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;CountMatches &lt;/b&gt;- counts the number of occurrences of one String in another&lt;/li&gt;
&lt;li&gt;&lt;b&gt;IsAlpha&lt;/b&gt;/&lt;b&gt;IsNumeric&lt;/b&gt;/&lt;b&gt;IsWhitespace&lt;/b&gt;/&lt;b&gt;IsAsciiPrintable &lt;/b&gt;- checks the characters in a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;DefaultString &lt;/b&gt;- protects against a null input String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Reverse&lt;/b&gt;/&lt;b&gt;ReverseDelimited &lt;/b&gt;- reverses a String&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Abbreviate &lt;/b&gt;- abbreviates a string using ellipsis&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Difference &lt;/b&gt;- compares two Strings and reports on their differences&lt;/li&gt;
&lt;li&gt;&lt;b&gt;LevensteinDistance &lt;/b&gt;- the number of changes needed to change one String into another &lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
Now let&#39;s look at arrays. We check that arrays are not empty, we search for an element in array, we add/remove elements.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ArrayUtils&lt;/b&gt; class helps with arrays. My favourite method is &lt;b&gt;ArrayUtils.contains()&lt;/b&gt; that checks if an element is in array.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;if (ArrayUtils.contains(array, element)) {

} 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
ArrayUtils contains commons operations for arrays of every primitive type and arrays of Objects:&lt;br /&gt;
&lt;br /&gt;
Object[] &lt;b&gt;add&lt;/b&gt;(Object[] array, int index, Object element)&lt;br /&gt;
Inserts the specified element at the specified position in the array.&lt;br /&gt;
&lt;br /&gt;
Object[] &lt;b&gt;add&lt;/b&gt;(Object[] array, Object element)&lt;br /&gt;
Copies the given array and adds the given element at the end of the new array.&lt;br /&gt;
&lt;br /&gt;
Object[] &lt;b&gt;addAll&lt;/b&gt;(Object[] array1, Object[] array2)&lt;br /&gt;
Adds all the elements of the given arrays into a new array.&lt;br /&gt;
&lt;br /&gt;
Object[] &lt;b&gt;clone&lt;/b&gt;(Object[] array)&lt;br /&gt;
Shallow clones an array returning a typecast result and handling null.&lt;br /&gt;
&lt;br /&gt;
boolean &lt;b&gt;contains&lt;/b&gt;(Object[] array, Object objectToFind)&lt;br /&gt;
Checks if the object is in the given array.&lt;br /&gt;
&lt;br /&gt;
int &lt;b&gt;getLength&lt;/b&gt;(Object array)&lt;br /&gt;
Returns the length of the specified array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
int &lt;b&gt;hashCode&lt;/b&gt;(Object array)&lt;br /&gt;
Get a hashCode for an array handling multi-dimensional arrays correctly.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
int &lt;b&gt;indexOf&lt;/b&gt;(Object[] array, Object objectToFind)&lt;br /&gt;
Finds the index of the given object in the array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
int &lt;b&gt;indexOf&lt;/b&gt;(Object[] array, Object objectToFind, int startIndex)&lt;br /&gt;
Finds the index of the given object in the array starting at the given index.&lt;br /&gt;
&lt;br /&gt;
boolean &lt;b&gt;isEmpty&lt;/b&gt;(Object[] array)&lt;br /&gt;
Checks if an array of Objects is empty or null.&lt;br /&gt;
&lt;br /&gt;
boolean &lt;b&gt;isEquals&lt;/b&gt;(Object array1, Object array2)&lt;br /&gt;
Compares two arrays, using equals(), handling multi-dimensional arrays correctly.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
boolean &lt;b&gt;isSameLength&lt;/b&gt;(Object[] array1, Object[] array2)&lt;br /&gt;
Checks whether two arrays are the same length, treating null arrays as length 0.&lt;br /&gt;
&lt;br /&gt;
boolean &lt;b&gt;isSameType&lt;/b&gt;(Object array1, Object array2)&lt;br /&gt;
Checks whether two arrays are the same type taking into account multi-dimensional arrays.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
int &lt;b&gt;lastIndexOf&lt;/b&gt;(Object[] array, Object objectToFind)&lt;br /&gt;
Finds the last index of the given object within the array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
int &lt;b&gt;lastIndexOf&lt;/b&gt;(Object[] array, Object objectToFind, int startIndex)&lt;br /&gt;
Finds the last index of the given object in the array starting at the given index.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
Object[] &lt;b&gt;remove&lt;/b&gt;(Object[] array, int index)&lt;br /&gt;
Removes the element at the specified position from the specified array.&lt;br /&gt;
&lt;br /&gt;
Object[] &lt;b&gt;removeElement&lt;/b&gt;(Object[] array, Object element)&lt;br /&gt;
Removes the first occurrence of the specified element from the specified array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
void &lt;b&gt;reverse&lt;/b&gt;(Object[] array)&lt;br /&gt;
Reverses the order of the given array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
Object[] &lt;b&gt;subarray&lt;/b&gt;(Object[] array, int startIndexInclusive, int endIndexExclusive)&lt;br /&gt;
Produces a new array containing the elements between the start and end indices.&lt;br /&gt;
&lt;br /&gt;
Map &lt;b&gt;toMap&lt;/b&gt;(Object[] array)&lt;br /&gt;
Converts the given array into a Map.&lt;br /&gt;
&lt;br /&gt;
Integer[] &lt;b&gt;toObject&lt;/b&gt;(int[] array)&lt;br /&gt;
Converts an array of primitive ints to objects.&lt;br /&gt;
&lt;br /&gt;
int[] &lt;b&gt;toPrimitive&lt;/b&gt;(Integer[] array)&lt;br /&gt;
Converts an array of object Integers to primitives.&lt;br /&gt;
&lt;br /&gt;
int[] &lt;b&gt;toPrimitive&lt;/b&gt;(Integer[] array, int valueForNull)&lt;br /&gt;
Converts an array of object Integer to primitives handling null.&lt;br /&gt;
&lt;br /&gt;
String &lt;b&gt;toString&lt;/b&gt;(Object array)&lt;br /&gt;
Outputs an array as a String, treating null as an empty array.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
String &lt;b&gt;toString&lt;/b&gt;(Object array, String stringIfNull)&lt;br /&gt;
Outputs an array as a String handling nulls.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Bonus :) The following classes help with building &lt;b&gt;toString&lt;/b&gt;(), &lt;b&gt;hashCode&lt;/b&gt;() and &lt;b&gt;equals&lt;/b&gt;() methods: &lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;EqualsBuilder&lt;/li&gt;
&lt;li&gt;HashCodeBuilder&lt;/li&gt;
&lt;li&gt;ToStringBuilder&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
For more detailed info see: &lt;a href=&quot;http://commons.apache.org/lang/userguide.html&quot;&gt;http://commons.apache.org/lang/userguide.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/7069188623600910214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/stop-reinventing-wheel-use-apache.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7069188623600910214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7069188623600910214'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/stop-reinventing-wheel-use-apache.html' title='Stop reinventing wheel, use Apache Commons'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-1922702065169591762</id><published>2012-02-08T04:03:00.000-08:00</published><updated>2012-02-08T04:03:09.409-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="netbeans highlighting template"/><title type='text'>NetBeans: syntax highlighting for html templates with unsupported file extension</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;NetBeans &lt;/b&gt;editor supports syntax highlighting and validation for several languages as Java, PHP, JavaScript, HTML. But if your file has different extension e.g. .template, NetBeans is unable to recognise the language. What you have to do if you want to turn on highlighting for &lt;b&gt;Smarty &lt;/b&gt;or &lt;b&gt;Velocity templates&lt;/b&gt;? &lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;In Netbeans, choose &lt;b&gt;Options &lt;/b&gt;from &lt;b&gt;Tools &lt;/b&gt;menu. Choose &lt;b&gt;Miscellaneous&lt;/b&gt;, then &lt;b&gt;Files&lt;/b&gt;. On this tab you have the list of all file extensions that are supported by NetBeans editor.&lt;br /&gt;
&lt;br /&gt;
Click &lt;b&gt;New... &lt;/b&gt;button and enter new file extension e.g. &quot;template&quot;. Then choose corresponding Associated File Type (MIME): &lt;b&gt;HTML Files (text/html)&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
All .template files will be recognised as html with syntax highlighting and validation.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/1922702065169591762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/netbeans-syntax-highlighting-for-html.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/1922702065169591762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/1922702065169591762'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/netbeans-syntax-highlighting-for-html.html' title='NetBeans: syntax highlighting for html templates with unsupported file extension'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-8860063396762760539</id><published>2012-02-08T02:45:00.000-08:00</published><updated>2012-02-15T06:39:38.717-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="closeutils java resources"/><title type='text'>CloseUtils: closing resources silently</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
When working with &lt;b&gt;resources in Java&lt;/b&gt; it is important that they are closed after use. Usually it is done by calling &lt;b&gt;close()&lt;/b&gt; method in finally block. The problem is that close() method can throw an Exception. The following class allows you to close resources silently, so you save some try-catch lines.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Resources, since Java 1.5, implement &lt;b&gt;java.io.Closeable&lt;/b&gt; interface:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
interface Closeable {
   public void close() throws IOException;
}
&lt;/pre&gt;
&lt;br /&gt;
So you can make a method, that would close any Closeable resource.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
public class CloseUtils {
   private static final Logger logger = Logger.getLogger(CloseUtils.class);

       public static void close(java.io.Closeable closeable) {
         if (closeable != null) {
            try {
                closeable.close();
            } catch (java.io.IOException e) {
                logger.error(&quot;Error closing resourse/stream &quot;, e);
            }
         }
      }
}
&lt;/pre&gt;
&lt;br /&gt;
So, instead of having finally block like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
InputStream is = ...
try{
...
} finally {
   if (is != null) {
      try {
            is.close()
      } catch (IOException e) {
            logger.error(e);
      }
   }
}
&lt;/pre&gt;
&lt;br /&gt;
you can use the following code&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
InputStream is = ...
try {
...
} finally {
   CloseUtils.close(is);
}
&lt;/pre&gt;
&lt;br /&gt;
P.S. Java 7 comes with a new construction called &quot;&lt;b&gt;try with resources&lt;/b&gt;&quot; that automatically closes the resources that were declared in try block. See: &lt;a href=&quot;http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html&quot; target=&quot;_blank&quot;&gt;The try-with-resources Statement&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/8860063396762760539/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/closeutils-closing-resources-silently.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/8860063396762760539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/8860063396762760539'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/closeutils-closing-resources-silently.html' title='CloseUtils: closing resources silently'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-7528824337492483435</id><published>2012-02-05T15:09:00.000-08:00</published><updated>2012-02-16T03:53:54.458-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="integration"/><category scheme="http://www.blogger.com/atom/ns#" term="jetty"/><category scheme="http://www.blogger.com/atom/ns#" term="spring"/><category scheme="http://www.blogger.com/atom/ns#" term="wicket"/><title type='text'>Integrating Wicket with Spring</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In this short example I will show you how to integrate &lt;b&gt;Wicket 1.5&lt;/b&gt; with &lt;b&gt;Spring 3&lt;/b&gt;. Wicket is a rich web framework that allows creating web applications using simple html and Java code. Wicket has its own IOC/DI engine, but Spring has become a de-facto standard for service layer.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Let&#39;s start with a new Maven project. First we need to setup dependencies to be loaded from repositories.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Wicket Framework:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;wicket-core&lt;/li&gt;
&lt;li&gt;wicket-util&lt;/li&gt;
&lt;li&gt;wicket-ioc&lt;/li&gt;
&lt;li&gt;wicket-request&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;brush: xml&quot;&gt; &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;wicket-core&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;${wicket.version}&amp;lt;/version&amp;gt;
 &amp;lt;/dependency&amp;gt;
 &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;wicket-util&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;${wicket.version}&amp;lt;/version&amp;gt;
 &amp;lt;/dependency&amp;gt;
 &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;wicket-ioc&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;${wicket.version}&amp;lt;/version&amp;gt;
 &amp;lt;/dependency&amp;gt;
 &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;wicket-request&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;${wicket.version}&amp;lt;/version&amp;gt;
 &amp;lt;/dependency&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;b&gt;Spring Framework:&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;spring-context&lt;/li&gt;
&lt;li&gt;spring-web&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;dependency&amp;gt;            
  &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;            
  &amp;lt;artifactId&amp;gt;spring-context&amp;lt;/artifactId&amp;gt;            
  &amp;lt;version&amp;gt;${spring.version}&amp;lt;/version&amp;gt;            
  &amp;lt;exclusions&amp;gt;              
    &amp;lt;exclusion&amp;gt;                                    
      &amp;lt;groupId&amp;gt;commons-logging&amp;lt;/groupId&amp;gt;               
      &amp;lt;artifactId&amp;gt;commons-logging&amp;lt;/artifactId&amp;gt;                               
    &amp;lt;/exclusion&amp;gt;                         
  &amp;lt;/exclusions&amp;gt;                   
&amp;lt;/dependency&amp;gt;                   
&amp;lt;dependency&amp;gt;                       
  &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;                       
  &amp;lt;artifactId&amp;gt;spring-context-support&amp;lt;/artifactId&amp;gt;                       
  &amp;lt;version&amp;gt;${spring.version}&amp;lt;/version&amp;gt;                   
&amp;lt;/dependency&amp;gt;                   
&amp;lt;dependency&amp;gt;                       
  &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;                       
  &amp;lt;artifactId&amp;gt;spring-web&amp;lt;/artifactId&amp;gt;                       
  &amp;lt;version&amp;gt;${spring.version}&amp;lt;/version&amp;gt;               
&amp;lt;/dependency&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Wicket-Spring Integration and Annotation Support:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;dependency&amp;gt;
  &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;wicket-spring&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;${wicket.version}&amp;lt;/version&amp;gt;
  &amp;lt;!-- exclude spring framework that wicket pulls in --&amp;gt;
  &amp;lt;exclusions&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
  &amp;lt;/exclusions&amp;gt;
&amp;lt;/dependency&amp;gt;
&amp;lt;dependency&amp;gt;
  &amp;lt;groupId&amp;gt;org.apache.wicket&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;wicket-spring-annot&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;${wicket-spring-annot.version}&amp;lt;/version&amp;gt;
  &amp;lt;!-- exclude spring framework that wicket pulls in --&amp;gt;
  &amp;lt;exclusions&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
  &amp;lt;/exclusions&amp;gt;
&amp;lt;/dependency&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add these version constants to your POM properties:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;properties&amp;gt;       
 &amp;lt;spring.version&amp;gt;3.0.6.RELEASE&amp;lt;/spring.version&amp;gt;
 &amp;lt;wicket.version&amp;gt;1.5.4&amp;lt;/wicket.version&amp;gt;
 &amp;lt;wicket-spring-annot.version&amp;gt;1.3.7&amp;lt;/wicket-spring-annot.version&amp;gt;
&amp;lt;/properties&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we need to register a listener that would load Spring context and a Wicket filter to process requests. Add the following lines to &lt;b&gt;web.xml&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;context-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;param-value&amp;gt;classpath:context.xml&amp;lt;/param-value&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;/context-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;servlet&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;servlet-name&amp;gt;wicket&amp;lt;/servlet-name&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;servlet-class&amp;gt;org.apache.wicket.protocol.http.WicketServlet&amp;lt;/servlet-class&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;init-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-name&amp;gt;applicationFactoryClassName&amp;lt;/param-name&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-value&amp;gt;org.apache.wicket.spring.SpringWebApplicationFactory&amp;lt;/param-value&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;/init-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;init-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-name&amp;gt;applicationBean&amp;lt;/param-name&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-value&amp;gt;wicketApplication&amp;lt;/param-value&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;/init-param&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;/servlet&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;servlet-mapping&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;servlet-name&amp;gt;wicket&amp;lt;/servlet-name&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;/servlet-mapping&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;listener&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; &amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;lt;/listener&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we need to configure Spring Context, to turn on the annotation-driven configuration. Create a new file &lt;b&gt;context.xml&lt;/b&gt; in your default package and put the following lines into it:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
        xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;&amp;gt;

    &amp;lt;context:annotation-config /&amp;gt;
    &amp;lt;context:component-scan base-package=&quot;web&quot;/&amp;gt;

&amp;lt;/beans&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
Then create a Java Class called &lt;b&gt;WicketApplication&lt;/b&gt; in &lt;b&gt;web&lt;/b&gt; package. This class will be our main Wicket Application class.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package web;

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.springframework.stereotype.Component;

/**
&amp;nbsp;* Wicket Application
&amp;nbsp;*
&amp;nbsp;* @author sergej.sizov
&amp;nbsp;*/
@Component(&quot;wicketApplication&quot;)
public class WicketApplication extends WebApplication {

&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override
&amp;nbsp;&amp;nbsp;&amp;nbsp; public Class&amp;lt;? extends Page&amp;gt; getHomePage() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return HomePage.class;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override
&amp;nbsp;&amp;nbsp;&amp;nbsp; protected void init() {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // !!! - do not forget to call init on parent
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; super.init();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initialize Spring
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; getComponentInstantiationListeners().add(new SpringComponentInjector(this));
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets create a Spring Service that would return a message to be shown on our page.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package web;

import org.springframework.stereotype.Component;
/**
&amp;nbsp;* Hello Service
&amp;nbsp;*
&amp;nbsp;* @author sergej.sizov
&amp;nbsp;*/
@Component(&quot;helloService&quot;)
public class HelloService {

&amp;nbsp; public String getMessage() {
&amp;nbsp;&amp;nbsp;&amp;nbsp; return &quot;Hello World!&quot;;
&amp;nbsp; }

}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we need to create a Home Page that will be shown as a default page. We need to create a HTML template and a Java class that would process actions and data binding.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;web/HomePage.html&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&amp;gt;
&amp;lt;html xmlns:wicket=&quot;http://wicket.apache.org&quot;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;wicket:head&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;Home Page&amp;lt;/title&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/wicket:head&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;body&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;span wicket:id=&quot;msg&quot; /&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;web/HomePage.java&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;package web;&lt;br /&gt;&lt;br /&gt;import org.apache.wicket.markup.html.WebPage;&lt;br /&gt;import org.apache.wicket.markup.html.basic.Label;&lt;br /&gt;import org.apache.wicket.request.mapper.parameter.PageParameters;&lt;br /&gt;import org.apache.wicket.spring.injection.annot.SpringBean;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;&amp;nbsp;* Home Page&lt;br /&gt;&amp;nbsp;*&lt;br /&gt;&amp;nbsp;* @author sergej.sizov&lt;br /&gt;&amp;nbsp;*/&lt;br /&gt;public class HomePage extends WebPage {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @SpringBean&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected HelloService helloService;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public HomePage(final PageParameters parameters) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add(new Label(&quot;msg&quot;, helloService.getMessage());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
HelloService will be injected into Wicket Page using &lt;b&gt;@SpringBean&lt;/b&gt; annotation.&lt;br /&gt;
&lt;br /&gt;
Integration is done. To see if it works we need to run it in a web container e.g. tomcat or jetty.&lt;br /&gt;
&lt;br /&gt;
If you want to test your Wicket application with &lt;b&gt;Jetty&lt;/b&gt;, you can add the following lines into your build plugins:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: xml&quot;&gt;&amp;lt;plugin&amp;gt;               
  &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;               
  &amp;lt;artifactId&amp;gt;maven-jetty-plugin&amp;lt;/artifactId&amp;gt;               
  &amp;lt;version&amp;gt;6.1.16&amp;lt;/version&amp;gt;               
  &amp;lt;configuration&amp;gt;                   
    &amp;lt;contextPath&amp;gt;/&amp;lt;/contextPath&amp;gt;                   
    &amp;lt;scanTargets&amp;gt;                       
      &amp;lt;scanTarget&amp;gt;target/classes/&amp;lt;/scanTarget&amp;gt;                   
    &amp;lt;/scanTargets&amp;gt;                   
    &amp;lt;scanIntervalSeconds&amp;gt;5&amp;lt;/scanIntervalSeconds&amp;gt;               
  &amp;lt;/configuration&amp;gt;           
&amp;lt;/plugin&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then just run Maven with &lt;i&gt;jetty:run&lt;/i&gt; parameter:&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;mvn jetty:run&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/7528824337492483435/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/02/integrating-wicket-with-spring.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7528824337492483435'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/7528824337492483435'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/02/integrating-wicket-with-spring.html' title='Integrating Wicket with Spring'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1256788220020556950.post-2247489400282196909</id><published>2012-01-31T04:42:00.000-08:00</published><updated>2012-01-31T04:45:38.073-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="conversion"/><category scheme="http://www.blogger.com/atom/ns#" term="parameters"/><category scheme="http://www.blogger.com/atom/ns#" term="wicket"/><title type='text'>My Wicket improvement for StringValue was supported!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Working with Wicket I was sadly surprised by the behaviour of &lt;b&gt;StringValue&lt;/b&gt; class. I needed to get &lt;b&gt;optional parameters&lt;/b&gt; from URL, such as page number or document id. This parameters may have values, but can be omited and should be replaced by &lt;b&gt;default value&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
So I used this code
to get page number, default page is 1.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace; font-size: x-small;&quot;&gt;PageParameters parameters = ...
&lt;br /&gt;
int currentPage = parameters.get(&quot;page&quot;).toInt(1);&amp;nbsp; // default value is 1&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunatelly, &lt;b&gt;toInt(1)&lt;/b&gt; throwed &lt;b&gt;StringValueConversionException&lt;/b&gt; instead of returning 1.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
I submitted this improvement to Wicket Jira.&amp;nbsp; &lt;a href=&quot;https://issues.apache.org/jira/browse/WICKET-4356&quot;&gt;WICKET-4356 : StringValueConversionException thrown when calling toInt(final int defaultValue) on StringValue. Why not to return the default value?&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Wicket user Robert McGuinness experienced the same:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
i ran into this issue as well, kinda wish the api would fail silently upon conversion and return defaults. Ex:&lt;br /&gt;
http://wicket-stateless.herokuapp.com/?counter=8 (works)&lt;br /&gt;
but I forgot to check for non ints so the following will fail (will be fixed soon)&lt;br /&gt;
http://wicket-stateless.herokuapp.com/?counter=fails&lt;/blockquote&gt;
&lt;br /&gt;
This issue was also discussed in &lt;span class=&quot;info&quot;&gt;&lt;a href=&quot;http://markmail.org/thread/4brd7ywrxt3vcyo2&quot;&gt;&lt;b&gt;org.apache.wicket.dev&lt;/b&gt;&lt;/a&gt; discussion group.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;info&quot;&gt;Martin Grigorov:&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
Hi devs,&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
At https://issues.apache.org/jira/browse/WICKET-4356 the reporter asks a reasonable question: why toXyz(default) throws a FormatException instead of returning the default value ? I agree with him. Is there a reason for the current behavior - the default value is used only if the text is null or empty but if non-empty and with the wrong format then the methods throw exception instead of logging it (DEBUG) and returning the default value.&lt;br /&gt;
&lt;br /&gt;
If you also agree then the next question is: should we change this behavior in 1.5.x or just in 6.0?&amp;nbsp;&amp;nbsp;&lt;/blockquote&gt;
&lt;br /&gt;
Sven Meier:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
I don&#39;t know the reason for this. Let&#39;s change it for 1.5.x too.&lt;/blockquote&gt;
&lt;br /&gt;
Igor Vaynberg:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
+1&lt;/blockquote&gt;
&lt;br /&gt;
Peter Ertl:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
+1 avoiding try..catch() for the simplest of use cases makes sense for me&lt;/blockquote&gt;
&lt;br /&gt;
And finally, the issue was solved by &lt;span class=&quot;info&quot;&gt;Martin Grigorov&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
The behavior of all methods &quot;toXyz(default)&quot; has been changed as requested.&amp;nbsp;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;br /&gt;
P.S. I am really happy to know that my idea was supported by the developers/maintainers of Wicket, because this small change would save many lines of try-catch, make code more secure and conversions would behave in a natural way.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vozis.blogspot.com/feeds/2247489400282196909/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vozis.blogspot.com/2012/01/my-wicket-improvement-for-stringvalue.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2247489400282196909'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1256788220020556950/posts/default/2247489400282196909'/><link rel='alternate' type='text/html' href='http://vozis.blogspot.com/2012/01/my-wicket-improvement-for-stringvalue.html' title='My Wicket improvement for StringValue was supported!'/><author><name>Sergej</name><uri>http://www.blogger.com/profile/15188174520807951751</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>