Skip to main content

Posts

Showing posts from December, 2014

How to apply Constraints in Command Objects

Points To Remember You need to add the  @Validateable  annotation to the command class. Use  importFrom  in the constraints block with the domain name to import the constraints from domain. How to add Constraints to the Command Object. Lets us look at the example below. We have created a Domain by name User and a Command object by name UserCommand. We have created the constraints for email, password, first name and last name in User domain. We will import these constraints from domain to the command object and add some more constraints like verify password( if password and confirm password match). User.groovy package com.ekiras.domain class User { String email String password String firstName String lastName static constraints = { email(nullable: false, blank: false, unique: true, email:true) password(nullable: false, blank: false) firstName(nullable: false, blank: false) lastName(nullable: false, blank: false) } } UserCommand.gro

How to create SSL Certificate and add it to Keystore

Points To Remember You need to have a SSL certificate o run an application/ server on https. You can create a SSL certificate using keytool. The keystore file for Java is available in Java/jre/lib/security/cacerts. Default password of the keystore is "changeit". Create a Keystore You can make a SSL certificate by following the steps below. Open a Command-line or Terminal window and make sure you're in your home directory. Execute the following command in the terminal keytool -genkey -alias tomcat -keyalg RSA -validity 365 NOTE, the validity parameter allows you to specify, in the number of days, how long the certificate should be valid for. The longer the time period, the less likely you are to need to recreate it. To recreate it, you'd need to delete the old one and then follow these instructions again. The response will look something like this: Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: $MACHINE_NAME What is the

Struts : Setup Hello World Example

Points To Remember Download the struts required jars and add to the lib folder. Setup :  Struts Hello World Example web.xml File <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app&

How to make a Class Singleton

Points To Remember To make a class Singleton the class must satisfy the following points No class should be able to make the object of this class. Only one object of this class must me there that is used by all the other classes. The class must have a private constructor so that no class should be able to make object of this class. Class should make a final object of the class itself and return it whenever asked for. Class should have a static method that returns the object of this class. Demo : Create a Singleton Class Class : Singleton.java public class Singleton{ private String name = "Singleton Object"; private static final Singleton object = new Singleton(); private Singleton(){ } public static Singleton getObject(){ return object; } public String getName(){ return name; } } Class : SingletonTest.java class SingletonTest{ public static void main(String args[]){ Singleton obj = Singleton.getObject(); System.out.println(obj.getName()); } } T

How to flatten a Nested List in Java

Points To Remember See  How to create a Nested List from a String input.  We use this class to create a nested list and then we try to flatten this list. Program : Flatten a Nested List  Here we write a Java function that will accept a nested ist upto any level of nesting and flatten this list. import java.util.List; import java.util.LinkedList; import java.util.Scanner; class FlattenList{ public static void main(String args[]){ CreateList obj = new CreateList(); // Get the nested list sample List<Object> nestedList = obj.createNestedList(); System.out.println("-----------------------------------"); // Print the nested list System.out.println("Nested List = " + nestedList); // Print the sample nested list after flattening the list System.out.println("Flatten List = " + new FlattenList().flattenList(nestedList)); } public List<Integer> flattenList(List<Object> nestedList){ List<Integer>

How to create a Nested List upto any level of nesting

Points To Remember The Correct Datatype for creating a Nested List will be List<Object>,  because we have to store either a integer number or a list inside the list. We can not have a nested list like  List<List<Integer>>  because it will not be able to hold the integer values. It can only hold a list. The List we create can be nested to any level. Program : Create A nested list upto any level Here we suppose that we get a String input of the list that we have to create. The following program accepts the list as a String and returns after converting the list to a nested list up to any level. import java.util.*; import java.util.LinkedList; import java.util.Scanner; public class CreateList{ public static void main(String args[]){ CreateList obj = new CreateList(); System.out.println(obj.createNestedList()); } public List<Object> createNestedList(){ Scanner sc = new Scanner(System.in); System.out.println("Enter the Nested List");

Test CAS Rest API from Java Code

Points To Remember Make sure that your CAS Server is up and running. How to set up CAS Rest api with JDBC Authentication. You have created a database and have dummy data in it. Program : Test CAS Rest Api from a Java Code You can use the following piece of code to test the CAS Rest API. You need to follow the following points for Authentication a user on CAS You need to make a GET or POST call depending on your CAS server setup. If the Username and Password are correct then you will get a TGT (Ticket Granting Token) Now we will make a call to the service url of our application to get the Service Ticket. On success you will get a Service Ticket If you have service the Service Token,  then you have successfully authenticated the user. Save this service ticket in a cookie or session, since a service ticket can be used only once You will get the following type of response if everything is working fine. string s = username=admin%40gmail.com&password=igdefault 201 https://ekansh:8443/c

Finding number of factors of a number

Concepts Examples If a number x divides another number y completely then x is a divisor of y. If x is a prime number then x is called the prime factor of number x. e.g 1,2,3,6 are the factors of 6 and 2,3 are the prime factors of 6. If any composite number N which can be expressed as N= $a^x\cdot b^y\cdot c^z...$ a x · b y · c z . . . , where a,b,c are the prime numbers and x,y,z are the powers of a,b,c respectively and so on. then the number of factors of N =(x+1)×(y+1)×(z+1)... e.g Find the number of factors of 24 24=2 2 ×3 1 ∴ number of factors of 24 = (3+1)(1+1) = 8 Question 1 Find the total number of factors of 1024 except 1 and 1024 itself. 8

CAS Rest API Integration and Testing

Points To Remember Please refer to blog How to set up CAS server with database . Now we are going to use the same authentication process through rest api and test it. Step 1: Adding dependency in pom.xml First thing that we need to do here is to add the CAS Rest Api  dependencies to pom.xml so that we can get the related jars in the class path. <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-integration-restlet</artifactId> <version>${cas.version}</version> <type>jar</type> </dependency> Note : Here we are using CAS v4.0.0 Step 2: Mapping Servlet in web.xml Now, we need to map the restlet servlet in the web.xml. For this copy the previously generated web.xml from the war file and add the following lines in the web.xml file. This will register the servlet and the url pattern with which this servlet can be accessed. Here we are using v1/*  as the url mapping, you can customize it accor

How to setup & configure CAS with Database

Points To Remember You can user MYSQL, Oracle or PostgreSQL connectors as the database. Make sure you have created a SSL certificate to run the CAS server on browser. Step 1: Adding Dependencies in pom.xml Add the following dependencies to the pom.xml that we created in this tutorial.  These are the dependencies that are required to authenticate the user using the database. <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-support-generic</artifactId> <version>${cas.version}</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-support-jdbc</artifactId> <version>${cas.version}</version> </dependency> <dependency> <groupId>c3p0</groupI

How to setup and configure CAS - Central Authentication Service

Points To Remember CAS works on Maven WAR overlay  technique. You need to create a SSL certificate to use CAS and run it over https to enable SSO(single sign on). How to create a SSL certificate and add it to keystore. You can use LDAP, Database and Active Directory techniques to authenticate the user. Requirements You need to have Java SDK v1.6 or greater. Servlet Containers like Tomcat , Websphere, JBoss etc.  Apache Maven v3.0 or greater. Authentication technique related dependencies. For the sake of simplicity let us assume that out application path is PROJECT = /home/ekiras/cas and our tomcat path is TOMCAT = /home/ekiras/apache Step 1 : Create pom.xml You need to create a pom.xml to create the war file and package it. lets create the pom file in the project directory i.e. PROJECT/pom.xml Add the following code to the pom. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:

How to Deploy a war on Tomcat

Points To Remember You should have a war file that can be deployed to the tomcat. You should have tomcat installed on your machine. Deploying a war file on Tomcat. We hope you have a installed the tomcat or have the tomcat bundle extracted on your system. In your case we just downloaded the tomcat zip and unzipped at home location. Step 1 : Change Tomcat User Name Password and Roles This is done so that you can login to the Tomcat Application Manager . All you need to do is find the file tomcat-user.xml at location path-to-tomcat/conf . Replace the following lines <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="role1" password="tomcat" roles="role1"/> --> with the following lines < rol