com.allenzheng.twittersyn.utility.impl.TwitterAPIImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.allenzheng.twittersyn.utility.impl.TwitterAPIImpl.java

Source

/*
 *  Copyright (c) 2009, Steven Wang
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 *  twitterSina at http://twitterSina.appspot.com
 *  twitterSina code at http://twitterSina.googlecode.com
 *    
 */
package com.allenzheng.twittersyn.utility.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.allenzheng.twittersyn.controller.AccountServlet;
import com.allenzheng.twittersyn.utility.TwitterAPI;

import twitter4j.*;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

/**
 * APITwitter?
 * @author Steven Wang <http://steven-wang.appspot.com>
 */
public class TwitterAPIImpl implements TwitterAPI 
{
   private static String callbackUrl;
   private static final Log logger = LogFactory.getLog(TwitterAPIImpl.class);

   
   public TwitterAPIImpl(){}
   
   /**
   * loginTwitter
   * @param userNametwitter?
   * @param userPwdtwitter?
    * @throws TwitterException 
   * @return??
   */
   
   
   public String getConsumerKey(){
      
      Properties prop = loadProperties();
      
      return prop.getProperty("twitter.consumerkey");
      
   }
   
   public String getConsumerSecret(){
      Properties prop = loadProperties();
      
      return prop.getProperty("twitter.consumersecret");
   
   }
   
   public String getReqTokenUrl(){
      Properties prop = loadProperties();
      
      return prop.getProperty("twitter.reqtokenurl");
   }
   
   public String getAccTokenUrl(){
      Properties prop = loadProperties();
      
      return prop.getProperty("twitter.acctokenurl");
   }
   
   public String getCallBackUrl(){
      
      Properties prop = loadProperties();
      
      return prop.getProperty("twitter.callbackurl");
   }
   
   public String getAuthorisationUrl() throws TwitterException{
      Properties prop = loadProperties();
      
         Twitter twitter = new TwitterFactory().getInstance();
         twitter.setOAuthConsumer(prop.getProperty("twitter.consumerkey"), 
               prop.getProperty("twitter.consumersecret"));
         try {
            RequestToken requestToken;
            if (callbackUrl == null) {
               requestToken = twitter.getOAuthRequestToken();
            } else {
               requestToken = twitter
                     .getOAuthRequestToken(callbackUrl);
            }
            String authorisationUrl = requestToken
                  .getAuthorizationURL();
//            session.setAttribute(ATTR_TWITTER, twitter);
//            session.setAttribute(ATTR_REQUEST_TOKEN, requestToken);

            logger.debug("Redirecting user to " + authorisationUrl);
//            response.sendRedirect(authorisationUrl);
            return authorisationUrl;
         } catch (TwitterException e) {
            logger.error("Sign in with Twitter failed - "
                  + e.getMessage());
            e.printStackTrace();
            throw new TwitterException(e);
         }
      
      
   }
//   public boolean loginTwitter(String userName, String userPwd)
//   {
//      try
//      {
//         User user = new Twitter(userName,userPwd).verifyCredentials();
//         if(user != null)
//            return true;
//      }
//      catch(TwitterException e){}
//      return false;
////      return true;
//   }
   
   
   
   /**
   * ?Twitter?
   * @param twitterUserNametwitter?
   * @param twitterUserPwdtwitter?
   * @param publishContent?
   * @return???
   */
//   public boolean publishTwitter(String twitterUserName, String twitterUserPwd, String publishContent)
//   {
//      //Twitter twitter = new Twitter(twitterUserName,twitterUserPwd);
//      Twitter twitter = new TwitterFactory().getInstance();
//      
//      try
//      {
//         twitter = new Twitter(twitterUserName,twitterUserPwd);
////         twitter.setClientURL("http://twittersina.appspot.com");
////         twitter.setClientVersion("http://twittersina.appspot.com");
////         twitter.setSource("twittersina");
//         twitter.updateStatus(publishContent);
//         return true;
//      }
//      catch(TwitterException e)
//      {
//         return false;
//      }
////      return false;
//   }
   
   private Properties loadProperties(){
      InputStream inputStream = this.getClass().
         getClassLoader().getResourceAsStream("oauth.properties");
      Properties prop = new Properties();
      try{
         prop.load(inputStream);
      }catch(IOException ex){
         ex.printStackTrace();
      }
      
      return prop;
   }
}