Sunday, June 23, 2013

Installing Godady SSL Certificate keytool error: java.lang.Exception: Failed to establish chain from reply - fixed



Problem: keytool error: java.lang.Exception: Failed to establish chain from reply 

error while importing Godaddy SSL certificate into the keystore file


Server: Tomcat 7

Solution: Make sure all the certificates from the chain are imported into the keystore. You 

can identify the certificates from the chain by opening the certificate received from the 

CA. Double click the file and go to the certification path tab. You should be able to get 

the path chain from there. Check https://certs.godaddy.com/anonymous/repository.pki 

in order to obtain the individual certificates.


Details:


1) Open the certificate issued by Godaddy, which is named after your domain name



2) The chain is that you need to install "Go Daddy Root Certificate Authority - G2" root 


certificate & then "Go Daddy Secure Certificate Authority - G2" intermediate certificate 

and then finally install "test.co.in" certificate.


3) The Question where do we find the first two certificates. Go to https://certs.godaddy.com/anonymous/repository.pki, check for the above two certificates as in below image








4) You need to follow the following command line instructions to install the standard 

certificate issued by Go Daddy with the files (or) procedure mentioned above.


keytool -import -alias root -keystore tomcat.keystore -trustcacerts -file gdroot-g2.crt


keytool -import -alias intermed -keystore tomcat.keystore -trustcacerts -file gdig2.crt


keytool -import -alias tomcat -keystore tomcat.keystore -trustcacerts -file test.co.in


The above process completely resolved the Chain exception and was able to install the SSL certificate into tomcat successfully.

Tuesday, May 28, 2013

Developing Web Application with Apache Wicket + JPA (Eclipselink) without dependency injection - Easy way

It's been a while I was struggling to get Apache Wicket working with JPA (Eclipselink). Initially tried a lot to get working solution with Wicket 6 + JPA + Spring DI/AOP & later with Wicket 6 + JPA + Google Guice, but the solutions wereworking with lot of restrictions since Apache wicket is an unmanaged Framework ( https://cwiki.apache.org/WICKET/spring.html ).

Finally found out a very simple & elegant solution to make this work seamlessly without any Dependency Injection frameworks.

Here we go,  what is that we really require ? Creating an Entity Manager for each request and closing it at the end of the request like OpenEntityManagerInViewFilter filter which is implemented in Spring.

Somewhere I saw a good saying about wicket is that use Wicket framework itself to do whatever you want it, like for Custom Session Management use wickets WebSession, and don't create any more filters rather use Wickets Request Life Cycle framework etc.

At last that is where I found a way to implement JPA based web application using Wicket, lets dive into the steps without digging into further details

1) Configuration Required in Wicket Application class

WicketApplication extends AuthenticatedWebApplication{
    EntityManagerFactory entityManagerFactory;
    @Override
    public void init() {
        ------------
        ------------
        entityManagerFactory=Persistence.createEntityManagerFactory("persistence_unit");

        getRequestCycleListeners().add(new OpenEntityManagerListener()); //---->
    }

    public EntityManagerFactory getEntityManagerFactory(){
        return entityManagerFactory;
    }
}

2) Create a class OpenEntityManagerListener which implements IRequestCycleListener

OpenEntityManagerListener implements IRequestCycleListener{

    ThreadLocal<EntityManager> 
                   entityManagerThreadLocal = new ThreadLocal<EntityManager>();

      @Override
      public void onBeginRequest(RequestCycle cycle) {
EntityManagerFactory 
                    entityManagerFactory= ((WicketApplication)Application.get()).getEntityManagerFactory();   
                entityManagerThreadLocal.set(entityManagerFactory.createEntityManager());    
                getEntityManager().getTransaction().begin();
      }

      @Override
       public void onEndRequest(RequestCycle cycle) {
                EntityManager entityManager= entityManagerThreadLocal.get();
                if(entityManager!=null && entityManager.isOpen()){
entityManager.getTransaction().rollback();
entityManager.close();
}
       }

       @Override
public void onDetach(RequestCycle cycle) {
                entityManagerThreadLocal.remove();
        }

        public EntityManager getEntityManager() {
                return entityManagerThreadLocal.get();
        }

}

3. Now we need a way to access EntityManager which is created at beginning of request, here we go

public class OpenEntityManager {

public static EntityManager getEntityManager() {
Iterator<IRequestCycleListener> listenerIterator = RequestCycle.get().getListeners().iterator();

while (listenerIterator.hasNext()) {
IRequestCycleListener listener = listenerIterator.next();

if (listener instanceof RequestCycleListenerCollection) {
Iterator<IRequestCycleListener> iterator = ((RequestCycleListenerCollection) listener).iterator();

while (iterator.hasNext()) {
IRequestCycleListener appListener = iterator.next();

if (appListener instanceof OpenEntityManagerListener) {

return ((OpenEntityManagerListener) appListener).getEntityManager();

}
}
}
}
return null;
}
}

4. What's next? Lest's use the EntityManager in Wicket Pages, Forms, SortableDataProviders, etc.

public class CoursePage extends TemplatePage {
       -------------
       -------------
       @Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
                EntityManager entityManager = OpenEntityManager.getEntityManager();
                CourseService service = new CourseService(entityManager);
                service.create(formModel);
       }

}


The above is the first version, there is a flaw here i.e. irrespective of whether we use EntityManager (or) not we are creating an instance of EntityManager. Let's fix this so that EntityManager instance will be created only when we try to use EntityManager at first time during the request processing. The change is required in OpenEntityManagerListener class. The below is the refined class

public class OpenEntityManagerListener implements IRequestCycleListener{
ThreadLocal<EntityManager> entityManagerThreadLocal=new ThreadLocal<EntityManager>();

@Override
public void onBeginRequest(RequestCycle cycle) {

}

@Override
public void onEndRequest(RequestCycle cycle) {
System.out.println("inside OpenEntityManagerListener onEndRequest");
EntityManager entityManager= entityManagerThreadLocal.get();
if(entityManager!=null && entityManager.isOpen()){
entityManager.getTransaction().commit();
entityManager.close();
System.out.println("inside OpenEntityManagerListener onEndRequest== close");
}

}

@Override
public void onDetach(RequestCycle cycle) {
System.out.println("inside OpenEntityManagerListener onDetach");
entityManagerThreadLocal.remove();
}

        --------------------------
public EntityManager getEntityManager() {
synchronized (entityManagerThreadLocal) {
if(entityManagerThreadLocal.get()==null){
EntityManagerFactory entityManagerFactory =       
                                     ((WicketApplication)Application.get()).getEntityManagerFactory();
entityManagerThreadLocal.set(entityManagerFactory.createEntityManager());

getEntityManager().getTransaction().begin();

System.out.println("inside OpenEntityManagerListener getEntityManager == create entity manager if null");
}
return entityManagerThreadLocal.get();
}
}
}

Hope this helps !!!!

Tuesday, March 26, 2013

Tomcat7 setup in Ubuntu 12.04 LTS

Installing Tomcat is easy using any linux package management tools, but once it is installed on Ubuntu 12.04 LTS you may not be able to access the deployed web application from outside that computer. It is because of firewall configuration, for this you need to allow the HTTP port on which tomcat is running.

ufw allow <port_number>

Install tomcat 7 using:
sudo apt-get install tomcat7

Tomcat7 web app location: /var/lib/tomcat7/webapps
Tomcat7 configuration location: /var/lib/tomcat7/conf

Tomcat7 Start/Stop:
sudo /etc/init.d/tomcat7 [stop/start/restart]

Allow Tomcat7 port (assume it is configured on 6040) for accessing deployed web application outside the hosted computer:
sudo ufw allow 6040