JSF, Spring together with apache CXF

Good tutorials and resources on Apache CXF How Tos are not easy to digg. I had to spend hours searching and reading to make my small application up and running, Integrating Spring with JSF was pretty straightforward, but when it comes to integrating those two with JSF i got stuck.

So this post is about exposing a web service as a web project using JSF front end / Spring backed and CXF for service invocation

before starting I should mention few valuable resource around the net.

The web service u used was the publicly available spelling checker which is used in the netbeans tutorial.

The Step by step guide as follows >>

Step 1 :

Create the classes from the WSDL you can use netbeans for this task or WSDL2JAVA command (wsdl2java [URL]) in the shell.

Continue reading JSF, Spring together with apache CXF

English – Sinhala Unicode Translator (ඉංග්‍රිසි – සිංහල භාෂා පරිවර්තකය)

Lat few days i was stuck with some web dev work for archmage. For the 1st time i had to localize a website in sinhala. Hence i had a longing desire to do something on sinhala i was glad. (Also getting payed for it 🙂 definitely a chance. ) so yeah the site is www.technology.lk still under construction. The site back-end is powered by Joomla CMS and the famous Ecom component virtuemart. I had to prepare a language pack (Sinhala) for virtuemart, not a hard task, yet translating about 200+ words to sinhala is a boring task, so i was googling for an English-Sinhala Translator tool, Found Madura Dictionary. Unfortunately the sinhala words given in madura is not in Unicode. so it was impossible to copy and paste in my language pack php script.

After having a chat with my dear friend sanda, he suggested a FireFox plug-in which was developed by the University of Colombo (UCSC) which does a similar task. the plug-in came with a sqlite database which consisted nearly 50,000 English to sinhala translation words, and in Unicode. I was thrilled to see this. So for my personal satisfaction I just put to gather a small translator tool where you can type an English word and it gives some Sinhala suggestions (in Unicode).

En-Si Trans

Made it in Java, also wanted to test this new swing look and feel called Substance which did work smoothly. so yeah you can download

this tool and use it, modify it or what ever 🙂 .

have fun,

p.s. : I will publish the Virtuemart Sinhala language pack in my next post.

Select Many Problem : JSF

After few days, got some time to write a post… well as i promised in my earlier post.. I thought of writing about the Annoying problem anybody will face while using selectMany component in JSF.

At 1st with out any experience what any one would do is writing both assessor and mutator methods to return and set A LIST of selected objects. some thing like

private List<myClass> selectList;

public List<myClass> getSelectList() {
return selectList;
}

public void setSelectList(List<myClass> selectList) {
this.selectList = selectList;
}

Even though this is the straight forward way, For some reason JSF implementation does not support it. In many places over the NET and in JSF forums, people have advised to use String Arrays, Saying you cannot use Java Collections in this scenario.

But Use of String Arrays are very much annoying and makes your work very messy. After some testing and trying I found this work around to take the selected objects as it is, not just the label string, Its pretty simple. Instead of using String arrays, Just use an array of your own class. Something like this

private myClass[] selectList;

public myClass[] getSelectList() {
return selectList;
}

public void setSelectList(myClass[] selectList) {
this.selectList = selectList;
}

Of-cause you have to use a converter in this case but not difference out there its just a normal converter for your class. So hope this tip will help

cheers !!

“Validation Error: Value is not valid” famous validation error, when using custom converters in JSF.

This is one problem i faced when i worked with select-many and select-one menus in Java Server Faces. For any one who have worked with JSF knows that you have to use custom converters in order to populate select-many and select-on menus with ur own data types.

if i elaborate on this a little bit, Select menus are not just there to show simple value-label pairs. you can give directly an object to its value and one of its fields as the label. for an example,

public ArrayList<SelectItem> getLandSelectList() {
if (landSelectList != null) {
return landSelectList;
}
    landSelectList = new ArrayList<SelectItem>();
List<Land> landList = this.getLandList();
for (int i = 0; i < landList.size(); i++) {
        landSelectList.add(new SelectItem(landList.get(i), landList.get(i)
.getLandsName_DE()));
}
return landSelectList;
}

The returning Select list can be taken to a select one or a select many list box like..

<h:selectOneMenu id=”listBoxLand
value=”#{userManagerBean.land}” required=”true”>
<f:selectItems value=”#{userManagerBean.landSelectList}” />
</h:selectOneMenu>
<h:message for=”listBoxLand” />

This component will set the selected land object directly to the backing bean. If you used simple value(some text or the id) – label pair. you should again query for the object from the selected id and save in the backing bean. but in this way that extra trouble will be handled by JSF.

The problem is if you do it just like this with out anything else.. you will get a wired validation error saying “Validation Error: Value is not valid“. This is where you start googling and debugging. Well after some hours of googling..(couldn’t do much debugging because this is a exception thrown by JSF framework) and reading about 10 to 20 forums i found out that the object which is loaded and the object wich was selected will be compared when setting to the backing bean. So if your object’s Class has not overridden the equals method this error message is shown.

So what you have to do is. if you are using your own data Objects for the select menus or in that case for any other JSF tag where you will use converters. You have to override the Equals method. Probably do the comparison with the Id, or with some unique value in that data Object. That’s it.. The problem solved. In my case the equals methods looks like

// overridden equals method
public boolean equals(Object obj) {
if (!(obj instanceof Land)) {
return false;
}
Land land = (Land) obj;

return (this.id == land.id);

}

Yeah hope this will be useful to some one.. !! I will write another post on how to use Java Collections when working with Select-Many menus.

Joomla Hack! Automated Joomla user registration via JSF form

Well this post is some what continuation of my last post.
What is the use of single sign on if you have to register in two different sites ? yeah this is the solution for that… What i wanted to do is, when a user registers in my java web application i wanted to register the same user in the PHP app. Since these two applications have different user data-tables (well in my project i cannot merge these tables or use one database. if that is your case just ignore this post.)

When a new user registers in my JAVA web app am taking that user form data and insert those to the joomla database. 🙂 (Yup I know.. What is there to blog about this ?)
But what went wrong is joomla use some extra data from 2 other different tables other than jos_users (in joomla database).

those tables are jos_core_acl_aro and jos_core_acl_groups_aro_map so when you are inserting the data to the jos_users table.. also save the data in to the other two tables as well.
there are foreign key constrains over these tables. so

1- Insert the user to the jos_users
2– take the user id from a select query and insert that user to the jos_core_acl_aro
3- takes jos_core_acl_aro id from a select query and insert it in to the jos_core_acl_groups_aro_map

take a look at the three tables then you will realize what you should do.

The other task is password encryption. well Joomla 1.5 uses md5 encryption mechanism to hash the passwords. When a password is created, it is hashed with a 32 character salt that is appended to the end of the password string. The password is stored as {TOTAL HASH}:{ORIGINAL SALT}.

you can see this method at plugins/authentication/joomla.php lines 80-116.

So what you have to do is take your password and make a {TOTAL HASH}:{ORIGINAL SALT} from it and save the created string. I found this information also in a discussion forum. which had shown a java class to do this task.. so yeah it was quite useful..

so that’s all about behind the seen registration 🙂

Have fun !

Single Sign-On between Joomla (PHP) and a custom JSF / JSP login (JAVA)

Single sign-on (SSO) is a method of access control, that enables a user to authenticate once, and gain access to the resources of multiple software systems. Well in my case, the task i have given is to authenticate a user in a PHP and a JAVA (Web) system simultaneously.

My PHP web application is the well known Joomla CMS, and my JAVA web application is based on JSF and custom built. After some thinking and research I found several resources which are worth reading (JOSSO, OneSign ), but i couldn’t take any help from them, mostly those SSO frameworks are complex ( yeah 🙂 I couldn’t understand ) and aimed on a general pourpose and most of them are not for free.

So yeah I thought of doing some Hack to joomla and also make some changes in my Java web app’s authentication method. After talking with some of my geeky Friends (Sandaruwan and Anjana). I came up with two approaches. both are involved in handling the cookies manually up to certain extent.

The 1st approach is (Which i didn’t try and had to give up due to the reason that I am using JSF as the web application framework) to log-in to the Joomla site and after loged in to Joomla create a random named temp file in the server (possibly in /home/secrets with 777) with the user-name (if a valid log in) and set a cookie using set_cookie(“name”,$filename) and direct to a jsp page to do the java side authentication.

in this JSP, page read the secret file name from the cookie and read the file from the http server in-order to take the username of the loged-in user. By passing this to the authentication method of the java web app, the java side also can be authenticated.

yup it is pretty simple, but i had to give it up mainly because I use JSF. if I do the user authentication in the above way in the java side. I cannot add the user object to the FacesContext which will be used by my other java side components. so even though i log in. later on in other jsf pages my loged user cannot be found. (Shortly my java login process is not happening according to the JSF implementation procedures.) and secondly i had to give up this method because my Project manger didn’t like the idea of saving temp files in the server. 🙂

So the Second and the method which i have implemented is, automating the Joomla log-in process by making an http request to the http server from my JSF backing bean. and set the PHP cookie manually via Http Servlet response.

before i explain this method more broadly i have to mention about two nice tools which helped me to monitor the http requests and response.
Apache TCP Monitor
Live Http headers (FireFox ad-on)

Architecture

Implementation

There are two different scenarios.
1. User can visit teh home page of the joomla site 1st and the PHP Cookie is already set.
2. User visit the Java site PHP Cookie is not available.

Continue reading Single Sign-On between Joomla (PHP) and a custom JSF / JSP login (JAVA)

JSF : Setting a custom message from a backing bean

So yeah !! I thought of writing something about JSF. Since am working with it for Nealy 3 months now. So how about custom message handling for starters !! 🙂

JSF message tag is pretty useful in many places. for an example, for the use of validators. but what i wanted to write here is, not how a message is shown after validation.

Think after some database transaction you wanted to say “Transaction is successful or not successful” or in some random scenario if you wanted to show a message in anywhere in your web-page at anytime, you can do that from your backing bean with out much effort.

I will take the tomahawk fileupload tag as an example JSF tag. so in the JSP side it will look like



	
               value="#{bbaBean.theFile}" storage="file"
               styleClass="fileUploadInput"
               required="true">
        

               errorstyle="Color: red;">



                 action="#{bbaBean.uploadFile}" styleclass="linkbox">

And when the upload button is clicked the uploadFile method will be invoked in our backing bean. which will look like..

public String uploadFile throws IOException {

       FacesMessage messageErr =
               new FacesMessage(FacesMessage.SEVERITY_ERROR,
                     "ERROR : ",
                     "XML File you have uploaded is not compatible");

       FacesMessage messageSuc = new FacesMessage(
              "The XML File You have uploaded is successfully
                                           added to the Database");

       FacesContext fc = FacesContext.getCurrentInstance();

       try {
              //TODO: Transaction or logic....
               fc..addMessage("form1:fileupload", messageSuc);
       } catch(Exception e) {
               fc.addMessage("form1:fileupload", messageErr);
       }
}

Simply that is it. So when an exception occurs your JSP page shows the custom error message. One other thing is you can set the message type too. if its and error message as i have shown make it’s SEVERITY to ERROR. and add an error style class for the tag. so your error message will be shown different than the other messages..

so yeah enjoy..

JEvents Hack – Integrating JEvents with Google calender

As i promised today I got some time to write about some PHP programming i did few weeks ago. I had to integrate Google calender with the Sensoria web site, so that the events published in the sensoria site will be automatically published in Sensoria’s public Google calender.

Sensoria Site is created using a famous CMS called Mambo, in Mambo one can install plug-ins for many usage for the customized site they are creating. In this case Sensoria was using Mambo Events Component or now available to download as JEvents in Joomla CMS.

What I did was a small code hack in the eventmanager.php file in com_eventmanager folder in the components directory of Mambo. Its was a pretty easy task, but i had problems while testing. I was behind a proxy and the Zend framework gave a huge trouble when connecting to Google Via an HTTPS connection. After some descutions on Google calender forums.. I found out that it is a bug in the Zend Farmework (Which i could not find a solution and didn’t bother or had time to spend on fixing it). So i had to test it in a live server.

Any how now its all working.. I will publish the code out here.. so anyone who wants to do the same.. please feel free to use it..

eventmanager.php

cheers !!

Hello Java World

322px-java_logosvg.png

I think This is my 1st post on something related to java… Its not that I hated the language before, but I used rather C++ /.NET and PHP for my earlier work. With my Internship, it would be correct if i say that I said Hello World to the actual Java environment. Its true for my collage Assignments, that I have done some java work, but well it cannot be counted… Anyhow Its been almost 2 months, I am having hands on experience on Java programming or rather more in J2ME and j2EE.

With all the APIs and the systematic documentations of those APIs.. and also with plenty of online help..I must say my life was so easy past few weeks.

As a part of my work package I had to develop a tracking system based on GPS positioning. My self and two more guys work on the project and we developed a server and a mobile client for this task. (this is a small part of a European union project called s3ms). What I thought of writing here today… is about the nice Framework we used, to handle the database.

Hibernate01_oben_logo1.gif

Well it’s not something new, its been used in almost all the java projects carried out in my company… Hibernate is an Object to relational Mapping (ORM) system for java. Hibernate provides a framework which maps the object oriented model to a relational database. Some of the main benefits by using Hibernate is that it makes the application portable on any SQL database.

Hibernate keeps the details of the database in its configuration file hibernate.cfg.xml and by editing few fields in this xml file, makes it possible to transport the application from one database to another. Hibernate mapping is done via xml files which is named name.hbm.xml and in these files the mapping of Java data types to SQL data types are done. you can define the relationships of the tables also in these files using relationship tags. (<many-to-one>) .

I have found a good tutorial on Hibernate in Gary’s Blog.. if you are interested more, those tutorials will be a great help. I should also note that Hibernate provides tools (Hibernate tools) for Eclipse IDE as plugins to generate Java Classes and xml mapping files looking at the database tables.

The founders of Hibernate is also providing a similar framework which is very much the same in practice. Its called NHibernate and used in the .NET platform for database handling. Both the Frameworks are Licensed GPL and LGPL respectively.

So for the people who are like me who were doing the database handling in the usual and the traditional way… Hibernate will surely make your work much easer…

UNIX / LINUX based Client & a Server application

Okey so am writing after some time about some valuable thing !! This is about some thing I happened to do as an assignment for one of my degree modules (Network Application Development). The Assignment is to implement a server and a client using both TCP and UDP. The functionalities expected were, a centralized server which keeps records of the online connected buddies in the network (Subscription) Continue reading UNIX / LINUX based Client & a Server application