Monday, October 27, 2014

Configure Google Analytics access from Tomcat SSL

My recent web application had a requirement to publish the user requests to Google Analytics (GA) who has confirmed the registration. This is because there can be users who have visited the registration page but not confirm the registration or end up with errors even they click the registration button. Fortunately, this web application is using Spring Web Flow therefore I managed to implement the helper method that will send the confirmation information to the GA as a very last even.
I've used the Apache HTTP Client to send a http request to GA using Google Measurement Protocol. In my case, I don't use Java general security store, instead use the custom keystore for Tomcat. This situation is very common when you need to configure the developer environment for the secure web applications development.
First you have to enter the following URL to the Firfox browser. Then click the upper left corner pad lock icon: you will get the More information button.

In the next window, click the button "View Certificate".
As shown in the above screenshot, you have to find the value of the Serial Number. This number is very important. In the certificate hierarchy, each tree item has different serial number. Here we select the top one in the chain.

Importing Certificate from general key store

Now the time to find the certificate in the java general certificate store. First you need to change to the certificate store. Generally that is c:\Program Files...\java\jre\lib\security and the keystore is cacerts.
You have to execute the following command to get the certificate information to the t.txt text file:

keytool -list -v -keystore cacerts > t.txt

Open the t.txt file in the notepad.exe and look for the 35def4cf that is the number where all the colons are removed.
Now export the certificate using following keytool command.

keytool -export -keystore cacerts -alias equifaxsecureca -file equifaxsecureca.cer

Use the following command to import this certificate to your keystore.

keytool -import -trustcacerts -alias equifaxsecureca  -file equifaxsecureca.cer -keystore truststore.jks

Truststore is the one used to store all the third party certificates in the tomcat.

Direct import

This is the easiest if you follow the above steps correctly. Again, select the top of the certificate chain (verify by the serial number) as shown in the above screenshot and click the "Export" button to export directly from the FireFox. I've exported the certificate as ga.crt in this case instead of equifaxsecureca.cer file. And import as alias ga as well:
keytool -import -trustcacerts -alias ga  -file ga.crt -keystore truststore.jks

Using above command import the ga.crt.

keytool -list  -v -keystore truststore.jks -alias ga > n.txt
If you have use the above command to extract the certificates, you will find both the way same certificate is imported.

Friday, May 02, 2014

Java Trick: Generic Array Converter

I had a problem of increasing size of the array. Of course I have to copy the array. But it is not easy with java Generics, because of the following problem:

public class ArrayUtils<t>{
 public T[] resizeArray(T[] source, int offset){
  int length = source.length+offset;
  Object[] aNew = new Object[length];
  System.arraycopy(source, 0, aNew, 0, source.length);
  return (T[])aNew;

Of course you can compile the ArrayUtils class, but you cannot use this method as shown in the following code segment:
 public static void main(String[] args){
  Integer[] ints = new Integer[2];
  Integer[] newInts =  new ArrayUtils<integer>().resizeArray(ints, 3);

Here the error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

the problem, aNew never is possible to cast to the type T because it is created as an Object type. I found a very good answer with help of the book: Core Java: Volume I—Fundamentals, Ninth Edition. It is easy with java reflections as shown in the line# 7 to 9. getClass().getComponentType() return the type of the array elements.

import java.lang.reflect.Array;

public class ArrayUtils<t>{
 public T[] resizeArray(T[] source, int offset){
  int length = source.length+offset;
  Class arrayClass =  source.getClass();
  Class elementType =  arrayClass.getComponentType();
  Object aNew = Array.newInstance(elementType, length);
  System.arraycopy(source, 0, aNew, 0, source.length);
  return (T[])aNew;

If you need you can make the method static by changing it signature to
public <t> T[] resizeArray(T[] source, int offset){ 

Thursday, May 01, 2014

Java Trick: Double brace initialization

This is a trick I found from the Core Java™: Volume I—Fundamentals, Ninth Edition, 6.4.6. Anonymous Inner Classes.
import java.util.ArrayList;
import java.util.List;

public class TestArrayList {
 private String s1;
 private static String s2;
 public static void main(String[] args) {
  TestArrayList tl = new TestArrayList();
  tl.printList(new ArrayList(){
   {add("a"); add("b"); add(s2);}


 public void printList(/*only final variables are accessible to local inner classes*/final List sList){
  printDynamicList(new ArrayList(){{add(s1); addAll(sList);}});
 public void printDynamicList(List dList){
As shown in the above code:

  • As shown in the line# 10, you can add elements to the anonymous array: this is called Double brace initialization.
  • Again in the line# 16, in the double brace initialization, s1 is added to the array which is a property fo the outer class.
  • To add the parameter in line# 16, it should be final, this is the rule of local inner classes.
If you have more info please add to this blog.