Thursday, October 29, 2015

Setting width Inplace Component Primefaces


In an hour ago I have a  problem with inplace component then Aceh Gayo coffee was gave me the idea.

taken from www.foodspotting.com

There is no properties for setting width in inplace component, but if you look at html code that produced by primefaces you will find the span element for wrapping the text in inplace component.

Then you just need little html knowledge to setting span width in other words is inplace width.
Here the code :

 <p:inplace id="inOutput4" editor="true" style="display:inline-block;width:250px;overflow:hidden">  
   <p:inputText id="output4" value="#{bean.output}" required="true" />  
 </p:inplace>  

Yes, I use style="display:inline-block;width:250px;overflow:hidden"  for set the width.




Saturday, October 10, 2015

Jsf, Getting url parameter value

Source image: wallpaperseries.com
Good morning, people!
Have you drink your coffee today? I'm drinking a cup of americano from nearby store. :)


Let say you have to get param value in http://localhost:8080/apps/faces/test.xhtml?param=hello.
All you need is just add view param in your xhtml file and mapped it with a variable in managed bean.

xhtml Code :
 <f:metadata>  
     <f:viewParam name="param" value="#{testBean.message}" />  
 </f:metadata>  

Managed Bean Code :
 @ManagedBean(name = "testBean")  
 @ViewScoped  
 public class TestBean implements Serializable {  
   private String message;  
   public String getMessage(){  
     return message;  
   }  
   public String setMessage(String message){  
     this.message = message;  
   }  
 }  

Saturday, September 26, 2015

Securing Web Application in Tomcat 7 using jdbc realm and j_security_check


Use case : 

a Web Application contains master and transaction modules. Those modules need to be secured using Admin role.

Implementation :

  1.  Prepare database table, we can use either table or view. The following script is the minimum requirement, we can use either table and view.

     CREATE TABLE "USER_MANAGEMENT"  
       (      "ID" NUMBER(5,0) NOT NULL ENABLE,   
             "USER_ID" VARCHAR2(50 BYTE) NOT NULL ENABLE,   
             "USER_NAME" VARCHAR2(100 BYTE) NOT NULL ENABLE,   
             "PASSWORD" VARCHAR2(1000 BYTE) NOT NULL ENABLE,   
             "ROLE_NAME" VARCHAR2(20 BYTE),   
              PRIMARY KEY ("ID")  
       );  
    
  2.    

  3. Configure $CATALINA_HOME /conf/server.xml, add the following :

     <Realm className="org.apache.catalina.realm.JDBCRealm" connectionName="ORACLE_SCHEMA" connectionPassword="ORACLE_PASSWORD"  
      connectionURL="jdbc:oracle:thin:@localhost:1521:xe" driverName="oracle.jdbc.driver.OracleDriver"   
      dataSourceName="jdbc/authority" userTable="USER_MANAGEMENT" userNameCol="USER_ID" userCredCol="PASSWORD"  
      roleNameCol="ROLE_NAME" userRoleTable="USER_MANAGEMENT"   
     />  
    

    short explanation about the configuration :
    • className is the JDBCRealm class. When we use glassfish, it must be different
    • connectionName is oracle schema's name
    • connectionPassword is  oracle schema's password
    • connectionURL is the jdbc oracle thin url that contains ip or machine name, port and SID of Oracle Server.
    • driverName is JDBC Driver
    • dataSourceName is data source name, the name will be used later on.
    • userTable is table or view, this is the database table that we created before (USER_MANAGEMENT). Container will look up to this table for checking user privilege.
    • userNameCol is a column for storing user id in the application.
    • userCredCol is a column for storing password of user id in the application.
    • userRoleTable is a table or view for storing the role of the application. To make this simple we'll use the same table as userTable, in the real world we could use different table or view.
    • roleNameCol is a column on the USER_MANAGEMENT table. This column let container knows the role of user id.
  4. Configure web.xml
    • Define security constrain:
       <security-constraint>  
                 <web-resource-collection>  
                      <web-resource-name>Master and transaction</web-resource-name>  
                      <url-pattern>/faces/master/*</url-pattern>  
                      <url-pattern>/faces/transaction/*</url-pattern>  
                      <url-pattern>/faces/login.xhtml</url-pattern>  
                       <url-pattern>/faces/index.xhtml</url-pattern>  
                 </web-resource-collection>  
                 <auth-constraint>  
                      <role-name>admin</role-name>  
                 </auth-constraint>            
       </security-constraint>  
      
    • Define security role
       <security-role>  
        <security-role>  
         <description></description>  
         <role-name>admin</role-name>  
       </security-role>  
      
    • login-config
       <login-config>  
                 <auth-method>FORM</auth-method>  
                 <realm-name>jdbc/authority</realm-name>  
                 <form-login-config>  
                      <form-login-page>/faces/login.xhtml</form-login-page>  
                      <form-error-page>/faces/error/LoginError.xhtml</form-error-page>  
                 </form-login-config>  
            </login-config>  
      

  5. Put your JDBC driver into Tomcat lib directory : $CATALINA_HOME/lib/
  6. Restart tomcat and publish the apps


Wednesday, September 2, 2015

Saturday, January 3, 2015

Dependency Injection with Spring Framework

Before you read this post I hope you understand about dependency injection  and  Spring Framework Initial Configuration.

On my previous post we tried simple dependency injection in Java, let's call it Programmatic way. Sometimes we need to make it configurable. Spring offers dependency injection with Declarative way.

Here we go :

1. Jacket.java

Create Java class in package com.brewer :

 package com.brewer;  

 public class Jacket {  

      private String color;  //will be injected

      public Jacket(){  
      }  
      public Jacket(String color){  //used for constructor injection
           this.color = color;  
      }  
      public String getColor() {  
           return color;  
      }  
      public void setColor(String color) { //used for setter method injection  
           this.color = color;  
      }  
      @Override  
      public String toString() {  
           return "Jacket [color=" + color + "]";  
      }  
 }  



2.  bean.xml

Create bean.xml in package com.brewer :

 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
      <bean id="jacketConst" class="com.brewer.Jacket">  
           <constructor-arg>  
                <value>blue#87CEEB</value>  
           </constructor-arg>  
      </bean>  
      <bean id="jacketSetter" class="com.brewer.Jacket">  
           <property name="color" value="blue#87CEEB" />  
      </bean>   
 </beans>  

bean.xml contains list of beans and will loaded by Spring. Spring will save those beans in Application Context.


3. Test.Java

Create Test class in package com.brewer :

 package com.brewer;  

 import org.springframework.context.ApplicationContext;  
 import org.springframework.context.support.ClassPathXmlApplicationContext;  

 public class Test {  
      public static void main(String[] args) {  
           ApplicationContext appContext = new ClassPathXmlApplicationContext("com/brewer/bean.xml"); 
           Jacket jacket = (Jacket) appContext.getBean("jacketConst");  
           System.out.println("Constructor injection : "+jacket.toString());  
           Jacket jacket2 = (Jacket) appContext.getBean("jacketSetter");  
           System.out.println("Method Setter injection : "+jacket2.toString());  
      }  
 }  




Happy Coding :)