Friday, September 20, 2013

CAS Client with Java web.xml

We have configured the CAS Server in the previous blog post. Now we will see how to call it from client ie. in Projects where Single Sign On is needed.
 
Download CAS-Client and go to module folder. add all the jar files in the folder to your project library.
 
The following configurations are to be added to your web.xml for adding CAS Single Sign on to your project
  1. AuthenticationFilter
  2. TicketValidationFilter (whichever one is chosen)
  3. HttpServletRequestWrapperFilter
  4. AssertionThreadLocalFilter

Add the below to web.xml file

<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
         <param-name>casServerLoginUrl</param-name>
          <param-value>https://localhost/cas-server-webapp-3.5.2/login</param-value>
</init-param>
<init-param>
          <param-name>serverName</param-name>
          <param-value>http://localhost:8080</param-value>
</init-param>
<init-param>
         <param-name>renew</param-name>
          <param-value>false</param-value>
</init-param>
<init-param>
        <param-name>gateway</param-name>
         <param-value>false</param-value>
</init-param>
</filter>

<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
       <param-name>casServerUrlPrefix</param-name>
       <param-value>https://localhost/cas-server-webapp-3.5.2</param-value>
</init-param>

<init-param>
         <param-name>serverName</param-name>
         <param-value>http://localhost:8080</param-value>
</init-param>

<init-param>
      <param-name>proxyCallbackUrl</param-name>
      <param-value>http://localhost:8080/webappcas2/proxyCallback</param-value>
</init-param>

<init-param>
       <param-name>proxyReceptorUrl</param-name>
       <param-value>/webappcas2/proxyCallback</param-value>
</init-param>

</filter>
 
<filter>
            <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
          <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
 
<filter>
            <filter-name>CAS Assertion Thread Local Filter</filter-name>
            <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
 
<filter-mapping>
         <filter-name>CAS Authentication Filter</filter-name>
          <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
         filter-name>CAS Validation Filter</filter-name>
         <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
      <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
       <filter-name>CAS Assertion Thread Local Filter</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/proxyCallback</url-pattern>
</filter-mapping>

Setting up CAS Server for JDBC Postgres

We are going to see on how to setup CAS Server for Post gres.database. This is for single sign on

Click here to Download CAS Server.

 What you are viewing is the folder structure of the CAS Server you have downloaded.

Go to modules folder.

Take the cas-server-webapp-3.5.2.war and deploy it in your tomcat.

Start the tomcat server. Now the war will be deployed. Now stop the tomcat server and remove the cas-server-webapp-3.5.2.war file from server so that it will not overwrite the folder deployed.

Copy cas-server-support-jdbc-3.5.2.jar from Modules folder and put it inside the webapps\cas-server-webapp-3.5.2\WEB-INF\lib folder

This jar is required for JDBC Connection.

As we are using Postgres we need to download the driver for it. Please Download

We also require 2 more jars. Also download these jars mentioned below.

     commons-dbcp-1.2.2.jar

     commons-pool-1.2.jar

Put all the jars in the same path webapps\cas-server-webapp-3.5.2\WEB-INF\lib


Go to the WEB-INF folder. you will see the folder structure as shown here. Open the deployerConfigContext.xml file and modify the contents as shown below.

Search for the line below

<bean class="org.jasig.cas.authentication.handler.support. SimpleTestUsernamePasswordAuthenticationHandler" />
 </list>
  </property>
 </bean>
Replace it with below code



     <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="select password from user_list where lower(user_name) = lower(?)" />
      </bean>

        
           </list>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName">
      <value>org.postgresql.Driver</value>
     </property>
     <property name="url">
      <value>jdbc:postgresql://localhost:5432/testing</value>
     </property>
     <property name="username">
      <value>postgres</value>
     </property>
     <property name="password">
      <value>postgres</value>
     </property>
    </bean>

Now run the apache tomcat and go to url https://localhost/cas-server-webapp-3.5.2/login
If it is working correctly, you should see authentication screen.

Monday, August 05, 2013

Get Absolute Url in Tapestry4 and Tapestry5

In Tapestry 4 for getting the absolute Url we use
IRequestCycle obCycle = (IRequestCycle)event.getRequestCycle();
obCycle.getAbsoluteURL("/app?page=Login&service=page");
In Tapestry5 we can use the below code to get the absolute url.
@Inject
PageRenderLinkSource linkSource;

Link link = linkSource.createPageRenderLinkWithContext(
                "DepartmentMasterPG",
                5);
String s = link.toAbsoluteURI();

Thursday, August 01, 2013

ValidationDelegate in Tapestry4 is recordError in Tapestry5

In Tapestry4 we are using

ValidationDelegate delegate = (ValidationDelegate)getBeans().getBean("delegate");

to validate and show the error. In Tapestry5 it is obsolete. In Tapestry5 we need to create as shown below

Page

 

<form t:type="form" t:id="departmentMasterList">
<t:errors />

@Component
private Form departmentMasterList;

Java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void onValidateFromDepartmentMasterList() {
 Iterator<DepartmentMasterListVO> iter = deptMasterList.iterator();
 while (iter.hasNext()) {
  DepartmentMasterListVO departmentListVO = iter.next();
  if (!departmentListVO.getCategory().equals("Test")) {
   departmentMasterList.recordError("Category entered is wrong");
   break;
  }
 }
}

In Method onValidateFromDepartmentMasterList DepartmentMasterList is the form Name given in Page.

In the above example I am iterating through a list and checking whether category contains a specific value. This is just for testing only. So now when you press the button, it will go to the above method (OnValidateFrom) if the validation fails, it will display the given message in the location where we have given <t:errors />. If there is any error, the program will not call the OnSuccess

Get Session id and IP Address in Tapestry5

We are going to see on how to get the Session ID and IP Address,

 Please add the below code in the class

@Inject
private HttpServletRequest httpServletRequest;

To get the IP Address

httpServletRequest.getRemoteAddr()

To get the session id

httpServletRequest.getSession().getId()

 

Wednesday, July 31, 2013

Error: Parameter 'translate' of component is bound to null This parameter is not allowed to be null.

Error in Tapestry:
An unexpected application exception has occurred.

Render queue error in BeginRender[DepartmentMasterPG:createddatetextbox]: Parameter 'translate' of component DepartmentMasterPG:createddatetextbox is bound to null. This parameter is not allowed to be null.

Solution:
if the column is a date or timestamp column and if it is editable, then change the textfield to datefield component like shown below

<t:textfield t:id="createdDateTextbox" value="department.createdDate" size="5" />
change to

<t:datefield t:id="createdDateTextbox" value="department.createdDate" size="5" />

Tuesday, July 30, 2013

Multiple Checkboxes inside Grid Control in Tapestry5

I am going to show an example on how to display multiple checkbox(<t:checkbox >) with Grid control(<t:grid). For this I am going to use the same Employee master example which I have shown in my previous article.

Page-EmployeeMasterPG

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html t:type="layout" title="sample Index"
      t:sidebarTitle="Framework Version"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
      xmlns:p="tapestry:parameter">
<form t:type="form" t:id="employeeMasterList">
   <t:grid source="addresses" row ="employee" add="delete,deleteCheck">
      <p:empIdCell>
                <t:pagelink page="CreateEmployeeMasterPG" context="encrypt(employee.id)">${employee.empId}</t:pagelink>
         </p:empIdCell>
          <p:deletecell>
                <t:actionlink t:id="delete" context="employee.id">Delete</t:actionlink>
          </p:deletecell>
          <p:deleteCheckCell>
           <t:checkbox t:id="deleteCheckbox" value ="currentSelected" />
          </p:deleteCheckCell>
         <p:empty>
              <p>There are no users to display; you can <t:pagelink page="createEmployeeMasterPG">add some</t:pagelink>.</p>
          </p:empty>
      </t:grid>
   <br/>
    <t:submit t:id="deleteMultiple" value="Delete"/>    
<a t:type="eventlink" t:event="deleteSelected" href="#">Delete</a>
      <a t:type="eventlink" t:event="CreateEmployee" href="#">Create Employee</a><br/><br/>
   </form>
</html>

Java-EmployeeMasterPG
In the above page we have defined a checkbox , now we will see how to retrieve the values

<t:checkbox t:id="deleteCheckbox" value ="currentSelected" />
The value can be retrieved in the java file with getCurrentSelected and setCurrentSelected accessors

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package org.my.in.sample.pages;

import org.my.in.sample.Util.AESencrp;
import org.my.in.sample.entities.EmployeeMasterEntityEO;
import org.my.in.sample.entities.EmployeeMasterListVO;
import org.my.in.sample.model.EmployeeMasterAM;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.services.Request;
import org.hibernate.Hibernate;
import org.hibernate.Session;


public class EmployeeMasterPG 
{
  @Inject
     private Session session;
  
  @InjectPage
  private CreateEmployeeMasterPG createEmployeeMaster;
  
  @Property
  private EmployeeMasterListVO employee;
  
  @Persist
  private HashSet<Long> selectedSet; 

  private String id;
  
 
  
  public boolean getCurrentSelected() 
  {
   if(selectedSet != null)
    return selectedSet.contains(employee.getId());
   else 
    selectedSet= new HashSet<Long>();
   return false;
  } 
  

      public void setCurrentSelected(boolean value) 
      { 
       if(selectedSet == null)
        selectedSet =new HashSet<Long>();
             if (value) 
             { 
              selectedSet.add(employee.getId()); 
             } 
             else 
             { 
                 selectedSet.remove(employee.getId()); 
             } 
      } 

  @SuppressWarnings("unchecked")
  public List<EmployeeMasterListVO> getAddresses()
     {
   List<EmployeeMasterListVO> emp = session.createSQLQuery("SELECT id, empId, empName, createdBy, createdDate FROM employeemaster")
      .addEntity(EmployeeMasterListVO.class)
      .list();

   return emp;
     }
  
 
  Object onCreateEmployee()
  {
   System.out.println("onCreateEmployee");
   return createEmployeeMaster;
  }
  
  void onActionFromDelete(long id)
  {
   EmployeeMasterAM.delete(id);
  }
  
  void onDeleteSelected()
  {
   if(selectedSet != null)
   {
    System.out.println(selectedSet.size());
    for (Long id : selectedSet) {
        System.out.println(id);
    }
    Iterator iter = selectedSet.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
   }
  }
  
  void onSelectedFromDeleteMultiple()
  {

  }

  
}

In the EmployeeMasterListPG.java  the @Persist needs to be given to retain the values when the form is submitted. The selected Set is the hash set which will store which checkbox have been checked and unchecked

   @Persist
   private HashSet<Long> selectedSet;

We are getting the checkbox value already persisted using the method below. For the first time the SelectedSet initialization will happen. After that, it will check for the id and if present returns.

public boolean getCurrentSelected()
{
if(selectedSet != null)
return selectedSet.contains(employee.getId());
else
selectedSet= new HashSet<Long>();
return false;
}

Likewise for setting the value the user has checked or unchecked.

 public void setCurrentSelected(boolean value)
     {
    if(selectedSet == null)
    selectedSet =new HashSet<Long>();
             if (value)
             {
            selectedSet.add(employee.getId());
             }
             else
             {
                 selectedSet.remove(employee.getId());
             }
     }


You can retrieve the selected values in two ways

1. for (Long id : selectedSet) {
        System.out.println(id);
    }


2.   Iterator iter = selectedSet.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }


Note: Please note that form needs to be submitted to retrieve the values. I tried to call using the event link. But the values are not retained. The tapestry5 understands only if the form where the checkbox is given to be submitted. It can be done either through submit button or through javascript :this.form.submit();

Friday, July 26, 2013

Setup Skeleton Tapestry5 Project For Eclipse

For Tapestry5 Project, you need maven to be installed.
Once maven is installed, create a folder with project Name(eg:Sample)

Go to the command prompt and go to the folder you have created. Copy the below line into the command prompt and execute.

Download Skeleton Project

mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.org
Now
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$ mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.org
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: http://tapestry.apache.org -> org.apache.tapestry:quickstart (Tapestry 5 Quickstart Project)
2: http://tapestry.apache.org -> org.apache.tapestry:tapestry-archetype (Tapestry 4.1.6 Archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Choose version: 
1: 5.0.19
2: 5.1.0.5
3: 5.2.6
4: 5.3.7
Choose a number: 4: 4
Define value for property 'groupId': : org.my.in
Define value for property 'artifactId': : sample
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  org.my.in: : org.my.in.sample
Confirm properties configuration:
groupId: org.my.in
artifactId: sample
version: 1.0-SNAPSHOT
package: com.example.tutorial
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: quickstart:5.3.7
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.my.in
[INFO] Parameter: artifactId, Value: sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: org.my.in.sample
[INFO] Parameter: packageInPathFormat, Value: org/my/in/sample
[INFO] Parameter: package, Value: org.my.in.sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: org.my.in
[INFO] Parameter: artifactId, Value: sample
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/test/java
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/main/webapp
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/main/resources/org/my/in/sample
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/test/resources
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/test/conf
[WARNING] Don't override file /Users/user/workspace/tutorial1/src/site
[INFO] project created from Archetype in dir: /Users/user/workspace/sample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.398s
[INFO] Finished at: Fri Mar 1 11:46:08 PST 2013
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------

Now the project is setup. You will see the folder structure like below.
 
Setup Project for Eclipse

Now to load the project into eclipse, execute the below command from the same folder

$ mvn eclipse:eclipse -DdownloadSources=true

This will start setting the project for eclipse.

1
2
3
4
5
6
[INFO] Scanning for projects...
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom (7 KB at 7.1 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.jar (76 KB at 83.4 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-eclipse-plugin/maven-metadata.xml

Import Project into Eclipse

Once it is fully completed, you need to import the project into your eclipse workspace. Go to Eclipse.File -> Import.

In the window select General -> Existing projects into workspace and select the project folder inside sample folder. Now the project is set up and ready for use.