12 Mayıs 2014 Pazartesi

Container Managed Authentication

Authentication and authorization is not so easy work-load for programmers who are not expert on JSF framework. Understanding different solutions and choosing the right one based on requirements is very critic in this context. Solutions I mention can be categorized such as;

A - Java EE Container Managed Authentication
B - Homegrown a Servlet Filter
C - 3rd Party Java EE Authentication Frameworks

While I was searching the right solution, I worked on small examples on JSF 2.x, Tomcat 7.0 and Hibernate. One of them was developed based on container managed authentication. I think that this solution is the easiest to implement on Web applications. There are five steps to adapt this solution into your application;

 1) Configure server.xml  on the Tomcat directory;
<Realm className="org.apache.catalina.realm.JDBCRealm"
       driverName="com.mysql.jdbc.Driver"
       connectionURL="jdbc:mysql://localhost:3306/authentication_db"
       connectionName="..." connectionPassword="..."
       userTable="user" userNameCol="USER_NAME" userCredCol="PASSWORD" 
       userRoleTable="usergroup" roleNameCol="USER_GROUP_NAME" />

2) Create entities (User and User Group) based on JDBCRealm implementation on the link - https://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#Standard_Realm_Implementations

3) Not to forget updating hibernate mapping files of entities based on changes on entities after Step-2. JDBCRealm Implementation needs many-to-many relationship between User and User Group tables, that's why related tags on mapping files should be configured properly.

4) Configure web.xml file;
    <security-constraint>
        <display-name>Restricted</display-name>
        <web-resource-collection>
            <web-resource-name>Restricted Area</web-resource-name>
            <url-pattern>/authorized/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>

5) Lastly, create your login function;
public String login(){

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        try {
            //Login via the Servlet Context
            request.login(getLoginName(), getLoginPass());


            return "success";
        } catch (ServletException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Login", null));
            e.printStackTrace();
        }
        return "failure";
}

Optimization can be made based on reqirements of an application, but the development-process will be similar I present above for container managed authentication.

20 Aralık 2013 Cuma

Hibernate - JSF Project Error : JavaReflectionManager cannot be cast to MetadataProviderInjector

During working on Hibernate-JSF project on Eclipse, I got such errors;
java.lang.ClassCastException: org.hibernate.annotations.common.reflection.java.JavaReflectionManager cannot be cast to org.hibernate.annotations.common.reflection.MetadataProviderInjector
java.lang.NoSuchFieldError: INSTANCE
For this situation, two annotation modules are detected in library files that are already added into the project for Hibernate processes. It causes conflict. Since "hibernateX.jar - hibernate core library" can perform annotation task without other dependency, you should delete both "hibernate-annotations.jar" and "hibernate-commons-annotations.jar" files to solve these kinds of problems.

12 Kasım 2013 Salı

Referenced file contains errors (jar:file:/.../plugins/org.jboss.tools.jst.web_3.5.0.Final-v20130717-0309-B75.jar!/catalog/web-facesconfig_2_2.xsd)

When I create new JavaServer Faces v2.2 project on Eclipse Kepler, I get an error in faces-config.xml file such as; 

"Referenced file contains errors (jar:file:/.../plugins/org.jboss.tools.jst.web_3.5.0.Final-v20130717-0309-B75.jar!/catalog/web-facesconfig_2_2.xsd)."

faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">

</faces-config>

It is obvious that http://xmlns.jcp.org/xml/ns/javaee is generated twice. To handle this error, one of them should be removed. Matching library versions with the XSD version is important, that's why removing first appearance is more proper action.

26 Şubat 2013 Salı

Weka GUI - OutOfMemory

"Not enough memory. Please load a smaller dataset or use a larger heap size."

Weka sometimes gives this error, if you use large datasets. To handle it, specify a larger heap size by changing the RunWeka.ini configuration file. The file is in the Weka folder of your Program Files directory on Windows. You should increment the value of the variable which regulates maximum heap size, such as "maxheap=1024m".

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

While using eclipse to run Java program, I was faced with the error below;
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Reason;
Eclipse uses default value of memory, if you don't change it. While using large data in your program, this default value is not enough.
Solution;
Eclipse>Windows> Preferences> Java> Installed JREs> Select the JRE> Click edit> Write in the Default VM Arguments: -Xmx1024M or any other amount of memory ...

20 Kasım 2012 Salı

Installing Java SE Development Kit on Ubuntu

- Download Java SE 7u7 from Oracle and uncompress it;

$ tar xf jdk-7u7-linux-i586.tar.gz

- JDK 7 package is extracted into jdk1.7.0_07 directory. Move this directory to "/usr/lib/jvm/";

$ sudo mv ./jdk1.7.0_07 /usr/lib/jvm/jdk1.7.0_07-sun

- Then, run;

$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_07-sun/bin/java" 1

$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_07-sun/bin/javac" 1

$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_07-sun/bin/javaws" 1

- Set newly installed java as default;

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config javaws

- Check the version of new JDK 7 installation;

$ java -version

References;
(1)http://askubuntu.com/questions/55848/how-do-i-install-oracle-java-jdk-7
(2)http://greenmang0.wordpress.com/2012/05/28/installing-oracle-jdk-7-on-ubuntu-12-04

26 Temmuz 2012 Perşembe

Displaying external webpage on Samsung Smart Tv's screen

One more tip about Samsung Smart Tv SDK [2.5]...

Displaying external webpage on Samsung's screen is not so easy. Samsung Smart Tv uses Maple Browser and this browser does not support IFrame. It leads us on using JavaScript window.open method. However, it may not solve your problem!
External website may not display correctly on Maple which is not a full fledged browser. You may either take data from the website and push it in your application using customized UI following Maple Browser Specs of displaying a webpage.