Challenge Engineer Life !


Integrating PrimeFaces Terminal sample with Twitter4J.

I'm glad Mr.Cagatay Civici who is lead of PrimeFaces picked up my blog entry yesterday.

But it's just only Japanese. So, I try to rewrite the entry in English.

What is Terminal of PrimeFaces ?

Terminal component is similar to Unix/Linux terminal.
Here is showcase link of Terminal component.

You can input two command in the PrimeFaces showcase sample.

  • greet
  • date

If you input 'greet' command, then terminal responses 'Hello Stranger'.
If you add your name after greet command, for example 'greet kikutaro', then terminal responses 'Hello kikutaro'.
Finally, you input 'date' command, then it returns now date.


Make Twitter4sh

I thought what is good usage of PrimeFaces terminal.
Finally I tried to make collaboration between PF terminal and Twitter4j.

Twitter4j is great library for tweet by using Java.
Here is a English site link of Twitter4j.

I named this collaboration sample 'Twitter4sh' :)

My code is very simple, if you are interested in this, please enhance!

Twitter4sh has only two command.

  • user : command for user information
  • tweet : command for tweet

That's all.

User command has '-i' option and target.

  • user -i name : show Twitter account name
  • user -i follow : show total count of follow
  • user -i follower : show total count of follower


I made on this environment.

OS :Windows 7 Professional
Java version :JDK 7
IDE :NetBeans 7.3
Framework :Java EE 6
Application Server :GlassFish 3.1.2
Library:Twitter4j,PrimeFaces 3.5


I inputed each command.

Check my tweet.



Creating view is very easy.
Only set terminal component of PrimeFaces.

I set 'prompt' attribute, it means head of prompt message in each line.
Also set 'welcomeMessage' attribute, it's first message of showing the page.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>Facelet Title</title>
            <p:terminal commandHandler="#{terminalBean.handleCommand}" 
                        welcomeMessage="Welcome to Twitter4sh!"/>

JSF Managed Bean is here.

package com.mycompany.primefacesterminal;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;

public class TerminalBean implements Serializable{
    private Twitter twitter;
    public void init(){
        twitter = new TwitterFactory().getInstance();

    public String handleCommand(String command, String[] params) {
        try {
            if(command == null){
                return "Please input twitter4sh command";
            }else switch(command){
                case "user":
                    User user = twitter.verifyCredentials();
                    if(params.length == 2){
                                case "name":
                                    return user.getName();
                                case "follow":
                                    return Integer.toString(user.getFriendsCount());
                                case "follower":
                                    return Integer.toString(user.getFollowersCount());
                            return "You only use -i option.";
                        return "Usage:user -i name/follow/follower";
                case "tweet":
                    if(params.length > 0){
                        String myTweet = "";
                        for(int i = 0; i < params.length; i++){
                            myTweet = (i==0) ? myTweet + params[i] : myTweet + " " + params[i];
                        return "Your tweet'" + myTweet + "'" + "is success.";
                        return "Usage:tweet 'message'.";
                    return "There are only two command user or tweet.";
        } catch (TwitterException ex) {
            return "Error" + ex.getErrorMessage();

This is useful for tweeting by command line!
I can make this code in 10 minutes, thanks to PrimeFaces and Twitter4j :)

にほんブログ村 IT技術ブログへ
にほんブログ村 にほんブログ村 IT技術ブログ Javaへ