Changeset 13171

Show
Ignore:
Timestamp:
06/04/08 02:28:29 (5 years ago)
Author:
itengineer
Message:

多分3th

Location:
lang/java/misc/twittercui/src
Files:
3 added
5 modified
1 moved

Legend:

Unmodified
Added
Removed
  • lang/java/misc/twittercui/src/main/java/twitter/TwitterAPIWrapper.java

    r13007 r13171  
    2626        public Timeline getTimeline() throws IOException, ParserConfigurationException, SAXException, TransformerException { 
    2727                String uri = getBaseURI() + "/status/friends_timeline/" + this.id + this.getResponseFormat(); 
    28                 Response response = this.webAgent.get(uri); 
     28                Response response = this.webAgent.get(uri, this.id, this.pw); 
    2929                return new Timeline(response); 
    3030        } 
  • lang/java/misc/twittercui/src/main/java/twitter/WebAgent.java

    r13007 r13171  
    44import java.io.IOException; 
    55import java.io.InputStreamReader; 
     6import java.net.Authenticator; 
    67import java.net.HttpURLConnection; 
     8import java.net.PasswordAuthentication; 
    79import java.net.URL; 
     10 
     11import sun.misc.BASE64Encoder; 
    812 
    913/** 
     
    3135         *  
    3236         * @param path 
     37         * @param id 
     38         * @param pw 
    3339         * @return 
    3440         * @throws IOException 
    3541         */ 
    36         public synchronized Response get(String path) throws IOException { 
     42        public synchronized Response get(String path, String id, String pw) throws IOException { 
    3743                URL url = new URL(path); 
    3844                HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    39                 conn.setRequestMethod("POST");  // (�E�́E) 
     45        HttpAuthenticator http_authenticator = new HttpAuthenticator(id, pw); 
     46        Authenticator.setDefault(http_authenticator); 
     47                conn.setRequestMethod("GET");   // (�E�́E) 
    4048                conn.setInstanceFollowRedirects(false); 
    4149                conn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); 
     50        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     51        conn.setDoOutput(true); 
    4252                conn.connect(); 
    4353        StringBuffer buf = new StringBuffer(); 
     
    5363                return new Response(statusCode ,buf.toString()); 
    5464        } 
     65 
     66        /** 
     67         *  
     68         * @param id 
     69         * @param pw 
     70         * @return 
     71         */ 
     72        protected String createAuthentication(String id, String pw) { 
     73                String s = "Basic" + (new BASE64Encoder().encode((id + ":" + pw).getBytes())); 
     74                return s; 
     75        } 
     76 
     77        /** 
     78         *  
     79         * @author fumi 
     80         * 
     81         */ 
     82        class HttpAuthenticator extends Authenticator { 
     83            private String username; 
     84            private String password; 
     85 
     86            /** 
     87             *  
     88             * @param username 
     89             * @param password 
     90             */ 
     91            public HttpAuthenticator(String username, String password){ 
     92                this.username = username; 
     93                this.password = password; 
     94            } 
     95 
     96            /** 
     97             *  
     98             */ 
     99            protected PasswordAuthentication getPasswordAuthentication(){ 
     100                return new 
     101                    PasswordAuthentication(username, password.toCharArray()); 
     102            } 
     103 
     104            /** 
     105             *  
     106             * @return 
     107             */ 
     108            public String myGetRequestingPrompt(){ 
     109                return super.getRequestingPrompt(); 
     110            } 
     111        } 
    55112} 
  • lang/java/misc/twittercui/src/main/java/twittercui/ConsoleMain.java

    r12581 r13171  
    8888                        e.printStackTrace(); 
    8989                        return; 
     90                } catch (Exception e) { 
     91                        e.printStackTrace(); 
    9092                } 
    9193        } 
  • lang/java/misc/twittercui/src/main/java/twittercui/commands/Command.java

    r12581 r13171  
    3333                if ("showme".equals(commandCode)) { 
    3434                        command = new ShowMeCommand(id, pw); 
     35                } else if ("ls".equals(commandCode)) { 
     36                        command = new GetMyTimelineCommand(id, pw); 
    3537                } 
    3638                return command; 
     
    3941         * ��ۂ̃A���B 
    4042         * @return 
     43         * @throws Exception  
    4144         */ 
    42         abstract public String execute(); 
     45        abstract public String execute() throws Exception; 
    4346} 
  • lang/java/misc/twittercui/src/main/java/twittercui/commands/GetMyTimelineCommand.java

    r12581 r13171  
    11package twittercui.commands; 
     2 
     3import java.util.List; 
     4 
     5import twitter.Status; 
     6import twitter.TwitterAPIWrapper; 
    27 
    38public class GetMyTimelineCommand extends Command { 
     
    813 
    914        @Override 
    10         public String execute() { 
    11                 return null; 
     15        public String execute() throws Exception { 
     16                TwitterAPIWrapper twitter = new TwitterAPIWrapper(this.id, this.pw); 
     17                List<Status> list = twitter.getTimeline().getTimeline(); 
     18                StringBuffer sb = new StringBuffer(); 
     19                for (Status s: list) { 
     20                        sb.append(s.getStatusId()); 
     21                        sb.append(":"); 
     22                        sb.append(s.getStatusText()); 
     23                        sb.append("\n"); 
     24                } 
     25                return sb.toString(); 
    1226        } 
    1327 
  • lang/java/misc/twittercui/src/test/java/twitter/WebAgent_get_Test.java

    r13007 r13171  
    1818import com.sun.org.apache.xpath.internal.XPathAPI; 
    1919 
    20 public class WebAgentTest { 
     20public class WebAgent_get_Test { 
    2121 
    2222        @Test 
     
    2424                WebAgent agent = new WebAgent(); 
    2525                try { 
    26                         String s = agent.get("http://twitter.com/statuses/friends_timeline/itengineer.xml").getContents(); 
     26                        String s = agent.get("http://twitter.com/statuses/friends_timeline/itengineer.xml", "", "").getContents(); 
    2727                        outputTest(new ByteArrayInputStream(s.getBytes())); 
    2828                } catch (IOException e) {