de.dev.eth0.retweeter.AbstractTwitterClient.java Source code

Java tutorial

Introduction

Here is the source code for de.dev.eth0.retweeter.AbstractTwitterClient.java

Source

package de.dev.eth0.retweeter;

import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE":
* <amuthmann@dev-eth0.de> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return
* ----------------------------------------------------------------------------
*/
/**
 * Abstract class for twitterclients
 *
 * @author deveth0
 */
public abstract class AbstractTwitterClient {

    private final Config config;
    private Twitter twitter;

    /**
     * Builds an AbstractTwitterClient
     *
     * @param _config
     */
    public AbstractTwitterClient(Config _config) {
        this.config = _config;
    }

    /**
     * returns the config
     *
     * @return config
     */
    protected Config getConfig() {
        return config;
    }

    /**
     * Builds a twitter object to access twitter, including all configs
     *
     * @return twitter
     */
    protected Twitter getTwitter() {
        if (twitter == null) {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setOAuthAccessToken(config.getAccessToken());
            cb.setOAuthAccessTokenSecret(config.getAccessSecret());
            cb.setOAuthConsumerKey(config.getConsumerKey());
            cb.setOAuthConsumerSecret(config.getConsumerSecret());
            twitter = new TwitterFactory(cb.build()).getInstance();
        }
        return twitter;
    }
}