Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.stronquens.amgtwitter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.logging.Level; import java.util.logging.Logger; import javax.enterprise.context.ApplicationScoped; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import twitter4j.Status; import twitter4j.StatusDeletionNotice; import twitter4j.Twitter; import twitter4j.TwitterFactory; import twitter4j.conf.ConfigurationBuilder; import twitter4j.*; /** * * @author stronquens */ @ApplicationScoped @ServerEndpoint("/twitter/{accesToken}/{accesTokenSecret}") public class SocketTwitter { private Twitter twitter = null; private ConfigurationBuilder configBuilder = new ConfigurationBuilder(); private Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy HH:mm:ss").create(); @OnOpen public void open(Session session, @PathParam("accesToken") String accesToken, @PathParam("accesTokenSecret") String accesTokenSecret) { try { configBuilder.setDebugEnabled(true).setOAuthConsumerKey("nyFJnGU5NfN7MLuGufXhAcPTf") .setOAuthConsumerSecret("QOofP3lOC7ytKutfoexCyh3zDVIFNHoMuuuKI98S78XmeGvqgW") .setOAuthAccessToken(accesToken).setOAuthAccessTokenSecret(accesTokenSecret); /*String result = gson.toJson(twitter.showUser(twitter.getId())); session.getBasicRemote().sendText(result);*/ TwitterStream twitterStream = new TwitterStreamFactory(configBuilder.build()).getInstance(); twitterStream.addListener(listener); twitterStream.user(); } catch (Exception e) { System.out.println(e); } } @OnClose public void close(Session session) { } @OnError public void onError(Throwable error) { Logger.getLogger(SocketTwitter.class.getName()).log(Level.SEVERE, null, error); } @OnMessage public void handleMessage(String message, Session session) { } private static final UserStreamListener listener = new UserStreamListener() { @Override public void onStatus(Status status) { System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText()); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onDeletionNotice(long directMessageId, long userId) { System.out.println("Got a direct message deletion notice id:" + directMessageId); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got a track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onFriendList(long[] friendIds) { System.out.print("onFriendList"); for (long friendId : friendIds) { System.out.print(" " + friendId); } System.out.println(); } @Override public void onFavorite(User source, User target, Status favoritedStatus) { System.out.println("onFavorite source:@" + source.getScreenName() + " target:@" + target.getScreenName() + " @" + favoritedStatus.getUser().getScreenName() + " - " + favoritedStatus.getText()); } @Override public void onUnfavorite(User source, User target, Status unfavoritedStatus) { System.out.println( "onUnFavorite source:@" + source.getScreenName() + " target:@" + target.getScreenName() + " @" + unfavoritedStatus.getUser().getScreenName() + " - " + unfavoritedStatus.getText()); } @Override public void onFollow(User source, User followedUser) { System.out.println( "onFollow source:@" + source.getScreenName() + " target:@" + followedUser.getScreenName()); } @Override public void onUnfollow(User source, User followedUser) { System.out.println( "onFollow source:@" + source.getScreenName() + " target:@" + followedUser.getScreenName()); } @Override public void onDirectMessage(DirectMessage directMessage) { System.out.println("onDirectMessage text:" + directMessage.getText()); } @Override public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) { System.out.println("onUserListMemberAddition added member:@" + addedMember.getScreenName() + " listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) { System.out.println("onUserListMemberDeleted deleted member:@" + deletedMember.getScreenName() + " listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListSubscription(User subscriber, User listOwner, UserList list) { System.out.println("onUserListSubscribed subscriber:@" + subscriber.getScreenName() + " listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListUnsubscription(User subscriber, User listOwner, UserList list) { System.out.println("onUserListUnsubscribed subscriber:@" + subscriber.getScreenName() + " listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListCreation(User listOwner, UserList list) { System.out.println( "onUserListCreated listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListUpdate(User listOwner, UserList list) { System.out.println( "onUserListUpdated listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserListDeletion(User listOwner, UserList list) { System.out.println( "onUserListDestroyed listOwner:@" + listOwner.getScreenName() + " list:" + list.getName()); } @Override public void onUserProfileUpdate(User updatedUser) { System.out.println("onUserProfileUpdated user:@" + updatedUser.getScreenName()); } @Override public void onUserDeletion(long deletedUser) { System.out.println("onUserDeletion user:@" + deletedUser); } @Override public void onUserSuspension(long suspendedUser) { System.out.println("onUserSuspension user:@" + suspendedUser); } @Override public void onBlock(User source, User blockedUser) { System.out.println( "onBlock source:@" + source.getScreenName() + " target:@" + blockedUser.getScreenName()); } @Override public void onUnblock(User source, User unblockedUser) { System.out.println( "onUnblock source:@" + source.getScreenName() + " target:@" + unblockedUser.getScreenName()); } public void onRetweetedRetweet(User source, User target, Status retweetedStatus) { System.out.println( "onRetweetedRetweet source:@" + source.getScreenName() + " target:@" + target.getScreenName() + retweetedStatus.getUser().getScreenName() + " - " + retweetedStatus.getText()); } public void onFavoritedRetweet(User source, User target, Status favoritedRetweet) { System.out.println( "onFavroitedRetweet source:@" + source.getScreenName() + " target:@" + target.getScreenName() + favoritedRetweet.getUser().getScreenName() + " - " + favoritedRetweet.getText()); } public void onQuotedTweet(User source, User target, Status quotingTweet) { System.out.println("onQuotedTweet" + source.getScreenName() + " target:@" + target.getScreenName() + quotingTweet.getUser().getScreenName() + " - " + quotingTweet.getText()); } @Override public void onException(Exception ex) { ex.printStackTrace(); System.out.println("onException:" + ex.getMessage()); } }; }