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.
http://www.primefaces.org/showcase/ui/terminal.jsf

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.

f:id:kikutaro777:20130805230220j:plain

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.
http://twitter4j.org/en/index.html

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

Environment

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

Result

I inputed each command.
f:id:kikutaro777:20130806201417j:plain

Check my tweet.

Success!

Code

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <p:terminal commandHandler="#{terminalBean.handleCommand}" 
                        prompt="Twitter4sh"
                        welcomeMessage="Welcome to Twitter4sh!"/>
        </h:form>
    </h:body>
</html>

JSF Managed Bean is here.

package com.mycompany.primefacesterminal;

import java.io.Serializable;
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;

@ManagedBean
@ViewScoped
public class TerminalBean implements Serializable{
    private Twitter twitter;
    
    @PostConstruct
    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){
                        if(params[0].equals("-i")){
                            switch(params[1]){
                                case "name":
                                    return user.getName();
                                case "follow":
                                    return Integer.toString(user.getFriendsCount());
                                case "follower":
                                    return Integer.toString(user.getFollowersCount());
                            }
                        }else{
                            return "You only use -i option.";
                        }
                    }else{
                        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];
                        }
                        twitter.updateStatus(myTweet);
                        return "Your tweet'" + myTweet + "'" + "is success.";
                    }else{
                        return "Usage:tweet 'message'.";
                    }
                default:      
                    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へ
にほんブログ村