Example usage for twitter4j TwitterException getMessage

List of usage examples for twitter4j TwitterException getMessage

Introduction

In this page you can find the example usage for twitter4j TwitterException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:org.sakaiproject.profile2.logic.ProfileExternalIntegrationLogicImpl.java

License:Educational Community License

/**
  * {@inheritDoc}/*from  w w  w  . j av a 2 s.  c o m*/
  */
public void sendMessageToTwitter(final String userUuid, String message) {
    //setup class thread to call later
    class TwitterUpdater implements Runnable {
        private Thread runner;
        private String userUuid;
        private String userToken;
        private String userSecret;
        private String message;

        public TwitterUpdater(String userUuid, String userToken, String userSecret, String message) {
            this.userUuid = userUuid;
            this.userToken = userToken;
            this.userSecret = userSecret;
            this.message = message;

            runner = new Thread(this, "Profile2 TwitterUpdater thread");
            runner.start();
        }

        //do it!
        public synchronized void run() {

            //global config
            Map<String, String> config = getTwitterOAuthConsumerDetails();

            //token for user
            AccessToken accessToken = new AccessToken(userToken, userSecret);

            //setup
            TwitterFactory factory = new TwitterFactory();
            Twitter twitter = factory.getInstance();
            twitter.setOAuthConsumer(config.get("key"), config.get("secret"));
            twitter.setOAuthAccessToken(accessToken);

            try {
                twitter.updateStatus(message);
                log.info("Twitter status updated for: " + userUuid);

                //post update event
                sakaiProxy.postEvent(ProfileConstants.EVENT_TWITTER_UPDATE, "/profile/" + userUuid, true);
            } catch (TwitterException e) {
                log.error(
                        "ProfileLogic.sendMessageToTwitter() failed. " + e.getClass() + ": " + e.getMessage());
            }
        }
    }

    //is twitter enabled
    if (!sakaiProxy.isTwitterIntegrationEnabledGlobally()) {
        return;
    }

    //get user info
    ExternalIntegrationInfo info = getExternalIntegrationInfo(userUuid);
    String token = info.getTwitterToken();
    String secret = info.getTwitterSecret();
    if (StringUtils.isBlank(token) || StringUtils.isBlank(secret)) {
        return;
    }

    //PRFL-423 limit to 140 chars
    //Hardcoded limit because 140 is the Twitter requirement so no need to make configurable
    message = ProfileUtils.truncate(message, 140, false);

    //instantiate class to send the data
    new TwitterUpdater(userUuid, token, secret, message);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Looks for users with a twitter web account in the data set, gets their
 * followers and following and creates connections
 * /*  w ww .  j  av  a  2  s .  c om*/
 */
private void interconnectPersons() {

    MetaTag twitterTag = source.getDataSet().getMetaTag(TwitterTags.TWITTER);
    if (twitterTag == null) {
        // no tag defined, so no accounts
        return;
    }

    EList<WebAccount> twitterAccounts = twitterTag.getWebAccounts();
    if (twitterAccounts == null || twitterAccounts.isEmpty()) {
        // no accounts defined
        return;
    }

    for (WebAccount twitterAccount : twitterAccounts) {
        // assume screen name in twitter account
        String screenName = twitterAccount.getUsername();

        if (screenName == null || screenName.equals("")) {
            // skip this one
            continue;
        }

        // get twitter user with this screenname
        User twitterUser = null;
        try {
            twitterUser = twitterAPI.showUser(screenName);
        } catch (TwitterException e) {
            log("Could not get User with screename: " + screenName + " (" + e.getMessage() + ")",
                    LogService.LOG_DEBUG);
            continue;
        }

        if (twitterUser == null) {
            log("Got no User with screename: " + screenName, LogService.LOG_DEBUG);
            continue;
        }

        // add connection to extisting followers and following without
        // creating new users
        if (interconnectFollowers()) {
            addFollower(twitterUser, false);
        }
        if (interconnectFollowing()) {
            addFollowing(twitterUser, false);
        }
    }

}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

private void addFollowing(User twitterUser, boolean createNewUsers) {

    if (twitterUser == null) {
        return;//  w w  w.j  a v  a2  s .  c  om
    }

    IDs following;
    long cursor = -1;

    // look up person for twitter user and connect
    Person connectToPerson = getPersonForTwitterUser(twitterUser);

    do {
        try {
            following = twitterAPI.getFriendsIDs(twitterUser.getId(), cursor);
            log("Retrieving following with cursor: " + cursor, LogService.LOG_INFO);
        } catch (TwitterException e) {
            log("Could not get more following Users from twitter. (Cursor: " + cursor + "): " + e.getMessage(),
                    LogService.LOG_DEBUG);
            break;
        }

        if (following == null) {
            return;
        }

        if (connectToPerson != null) {
            connectPersonsForTwitterUserIds(following.getIDs(), connectToPerson, true, createNewUsers);
        }

        // next cursor
        cursor = following.getNextCursor();
    } while (following.hasNext());
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

private void addFollower(User twitterUser, boolean createNewUsers) {

    if (twitterUser == null) {
        return;//from  w  ww . j  a v  a 2 s .  com
    }

    int followersMax = 1000;

    int followersCount = twitterUser.getFollowersCount();
    if (followersCount > followersMax) {
        // ignore users with to much followers
        log("Ignoring the " + followersCount + " Followers of " + twitterUser.getName());
        return;
    }

    IDs followers;
    long cursor = -1;

    do {
        try {
            followers = twitterAPI.getFollowersIDs(twitterUser.getId(), cursor);
            log("Retrieving followers with cursor: " + cursor, LogService.LOG_INFO);
        } catch (TwitterException e) {
            log("Could not get more followers from twitter. (Cursor: " + cursor + "): " + e.getMessage(),
                    LogService.LOG_DEBUG);
            break;
        }

        if (followers == null) {
            return;
        }

        // look up person for twitter user and connect
        Person connectToPerson = getPersonForTwitterUser(twitterUser);
        if (connectToPerson != null) {
            connectPersonsForTwitterUserIds(followers.getIDs(), connectToPerson, false, createNewUsers);
        }
        // next cursor
        cursor = followers.getNextCursor();
    } while (followers.hasNext());
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Looks if the person for the twitter user already exists or otherwise loads it using the api.
 * /*from   w  ww. j a v  a 2s  . c  om*/
 * @param id Twitter user id
 * @return The Person for the twitter user or null in error case.
 */
private Person getPersonForTwitterUserId(long id) {
    Person person = getExistingPersonForTwitterUserId(id);

    if (person == null) {
        ResponseList<User> twitterUsers = null;

        // put id in an array to lookuup
        long[] userIdArray = new long[] { id };
        try {
            log("Looking up twitter user for id " + id, LogService.LOG_DEBUG);
            twitterUsers = twitterAPI.lookupUsers(userIdArray);
        } catch (TwitterException e) {
            log("Could not lookup single user " + id + " (" + e.getMessage() + ")", LogService.LOG_WARNING);
            return null;
        }

        if (twitterUsers != null && !twitterUsers.isEmpty()) {
            // only one entry
            person = createPersonFromTwitterUser(twitterUsers.get(0));
        }
    }
    return person;
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Searches for the query defined in the configuration and adds the results.
 *//*from www.j  av a 2  s.c  o  m*/
private void search() {
    // get query from configuration
    String query = source.getPropertyValue(TwitterProperties.SEARCH_PROPERTY);

    if (query == null || query.isEmpty()) {
        return;
    }

    QueryResult searchResult = null;

    try {
        Query twitterQuery = new Query(query);
        // set requested number of tweets
        twitterQuery.setCount(getNumberOfSearchTweets());

        // add possible geo location parameter
        addSearchGeoLocation(twitterQuery);

        // if defined set since id
        String sinceId = source.getPropertyValue(TwitterProperties.SEARCH_SINCE_ID_PROPERTY);
        if (sinceId != null && !sinceId.isEmpty()) {
            long sinceIdVal = new Long(sinceId);
            twitterQuery.setSinceId(sinceIdVal);
        }
        searchResult = twitterAPI.search(twitterQuery);
    } catch (TwitterException e) {
        log("Could not search for " + query + "(" + e.getMessage() + ")", LogService.LOG_WARNING);
        return;
    }

    if (searchResult == null) {
        return;
    }

    String sinceId = parseSinceId(searchResult);

    if (sinceId != null) {
        // set it in configuration
        source.addProperty(TwitterProperties.SEARCH_SINCE_ID_PROPERTY, sinceId);
    }

    List<Status> tweets = searchResult.getTweets();

    log("Got " + tweets.size() + " tweets for search " + query, LogService.LOG_DEBUG);

    // add all tweets
    addTweetList(tweets);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * create the hometimeline/*from   w  w w. j  a  v a  2s .  c  om*/
 */
private void addHomeTimeline() {

    List<Status> homeTimeline = null;

    // List of statuses (homeTimeline)
    try {
        homeTimeline = twitterAPI.getHomeTimeline();
    } catch (TwitterException te) {
        log("Could not get Home Timeline from Twitter. (" + te.getMessage() + ")", LogService.LOG_ERROR);
        return;
    }

    addTweetList(homeTimeline);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Adds the User Timeline/*from   w  w  w . ja  v a2  s.  com*/
 */
private void addUserTimeline() {

    List<Status> userTimeline = null;

    // List of statuses
    try {
        Paging userTimelineParam = new Paging();
        userTimelineParam.setCount(getNumberOfUserTimelineTweets());
        userTimeline = twitterAPI.getUserTimeline(userTimelineParam);
    } catch (TwitterException te) {
        log("Could not get User Timeline from Twitter. (" + te.getMessage() + ")", LogService.LOG_ERROR);
        return;
    }

    addTweetList(userTimeline);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Adds all direct messages sent from other users to the owner of this
 * account and the ones sent from this user to others.
 *//*  ww  w.j a  v a2 s  .c om*/
private void addDirectMessages() {

    // add DirectMessages
    ResponseList<DirectMessage> directMessages = null;

    if (loadDirectMessages()) {
        try {
            directMessages = twitterAPI.getDirectMessages();
        } catch (TwitterException e) {
            log("Could not get received Direct Messages from Twitter. (" + e.getMessage() + ")",
                    LogService.LOG_ERROR);
            directMessages = null;
        }

        if (directMessages != null) {
            addDirectMessages(directMessages);
        }
    }

    if (loadSentDirectMessages()) {
        try {
            directMessages = twitterAPI.getSentDirectMessages();
        } catch (TwitterException e) {
            log("Could not get sent Direct Messages from Twitter. (" + e.getMessage() + ")",
                    LogService.LOG_ERROR);
            directMessages = null;
        }

        if (directMessages != null) {
            addDirectMessages(directMessages);
        }
    }

}

From source file:org.sonar.plugins.twitter.TwitterPublisher.java

License:Open Source License

public final void executeOn(Project project, SensorContext context) {
    Configuration configuration = project.getConfiguration();
    String hostUrl = configuration.getString(HOST_PROPERTY, HOST_DEFAULT_VALUE);

    try {/*ww  w  . java2 s.c  o m*/
        AccessToken accessToken = loadAccessToken(configuration);
        if (accessToken.getToken() == null || accessToken.getTokenSecret() == null) {
            authenticate(configuration);
            accessToken = loadAccessToken(configuration);
        }
        StringBuilder url = new StringBuilder(hostUrl).append(PROJECT_INDEX_URI).append(project.getKey());
        String status = String.format("Sonar analysis of %s is available at %s", project.getName(), url);
        updateStatus(status);
    } catch (TwitterException e) {
        LOG.error("Exception updating Twitter status: " + e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception updating Twitter status: " + e.getMessage());
    }
}