Changeset 13007

Show
Ignore:
Timestamp:
06/01/08 22:37:21 (6 months ago)
Author:
itengineer
Message:
 
Location:
lang/java/misc/twittercui/src
Files:
7 added
4 modified

Legend:

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

    r12581 r13007  
    11package twitter; 
    22 
     3import org.w3c.dom.Node; 
     4 
    35public class Status { 
    4         public Status(String contents) { 
    5                  
     6 
     7        private String postedDate; 
     8        private String statusId; 
     9        private String statusText; 
     10 
     11        public Status(Node node) { 
     12                this.postedDate = node.getChildNodes().item(1).getTextContent(); 
     13                this.statusId = node.getChildNodes().item(3).getTextContent(); 
     14                this.statusText = node.getChildNodes().item(5).getTextContent(); 
     15        } 
     16 
     17        public String getPostedDate() { 
     18                return postedDate; 
     19        } 
     20        public void setPostedDate(String postedDate) { 
     21                this.postedDate = postedDate; 
     22        } 
     23        public String getStatusId() { 
     24                return statusId; 
     25        } 
     26        public void setStatusId(String statusId) { 
     27                this.statusId = statusId; 
     28        } 
     29        public String getStatusText() { 
     30                return statusText; 
     31        } 
     32        public void setStatusText(String statusText) { 
     33                this.statusText = statusText; 
    634        } 
    735} 
  • lang/java/misc/twittercui/src/main/java/twitter/TwitterAPIWrapper.java

    r12581 r13007  
    11package twitter; 
    22 
    3 import java.util.List; 
     3import java.io.IOException; 
     4 
     5import javax.xml.parsers.ParserConfigurationException; 
     6import javax.xml.transform.TransformerException; 
     7 
     8import org.xml.sax.SAXException; 
    49 
    510public class TwitterAPIWrapper { 
    6         public List<Status> getTimeline(String id) { 
    7                 return null; 
     11 
     12        private WebAgent webAgent; 
     13        private String id; 
     14        private String pw; 
     15 
     16        public TwitterAPIWrapper(String id) { 
     17                this(id, null); 
    818        } 
    9         protected boolean isStatus(String contents) { 
     19 
     20        public TwitterAPIWrapper(String id, String pw) { 
     21                this.webAgent = WebAgent.getInstance(); 
     22                this.id = id; 
     23                this.pw = pw; 
     24        } 
     25 
     26        public Timeline getTimeline() throws IOException, ParserConfigurationException, SAXException, TransformerException { 
     27                String uri = getBaseURI() + "/status/friends_timeline/" + this.id + this.getResponseFormat(); 
     28                Response response = this.webAgent.get(uri); 
     29                return new Timeline(response); 
     30        } 
     31 
     32        protected boolean isStatus(String contents) throws SAXException, IOException, ParserConfigurationException { 
     33                // TODO: 
    1034                return true; 
    1135        } 
     36        protected String getBaseURI() { 
     37                return "http://twitter.com/"; 
     38        } 
     39        protected String getResponseFormat() { 
     40                return ".xml"; 
     41        } 
     42 
     43        public String getId() { 
     44                return id; 
     45        } 
     46        public void setId(String id) { 
     47                this.id = id; 
     48        } 
     49        public String getPw() { 
     50                return pw; 
     51        } 
     52        public void setPw(String pw) { 
     53                this.pw = pw; 
     54        } 
    1255} 
  • lang/java/misc/twittercui/src/main/java/twitter/WebAgent.java

    r12581 r13007  
    1313 */ 
    1414public class WebAgent { 
    15         public synchronized String get(String path) throws IOException { 
     15 
     16        private static WebAgent _instance; 
     17 
     18        /** 
     19         *  
     20         * @return 
     21         */ 
     22        public synchronized static WebAgent getInstance() { 
     23                WebAgent obj = _instance; 
     24                if (obj == null) { 
     25                        obj = new WebAgent(); 
     26                } 
     27                return obj; 
     28        } 
     29 
     30        /** 
     31         *  
     32         * @param path 
     33         * @return 
     34         * @throws IOException 
     35         */ 
     36        public synchronized Response get(String path) throws IOException { 
    1637                URL url = new URL(path); 
    1738                HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     
    2142                conn.connect(); 
    2243        StringBuffer buf = new StringBuffer(); 
     44        int statusCode = conn.getResponseCode(); 
    2345        BufferedReader br = new BufferedReader( 
    2446                        new InputStreamReader(conn.getInputStream(), "UTF-8")); 
     
    2951                conn.disconnect(); 
    3052 
    31                 return buf.toString(); 
     53                return new Response(statusCode ,buf.toString()); 
    3254        } 
    3355} 
  • lang/java/misc/twittercui/src/test/java/twitter/WebAgentTest.java

    r12581 r13007  
    1010import javax.xml.transform.TransformerException; 
    1111 
     12import org.junit.Test; 
    1213import org.w3c.dom.Document; 
    1314import org.w3c.dom.Node; 
     
    1718import com.sun.org.apache.xpath.internal.XPathAPI; 
    1819 
    19 import junit.framework.TestCase; 
     20public class WebAgentTest { 
    2021 
    21 public class WebAgentTest extends TestCase { 
     22        @Test 
    2223        public void testGet() { 
    2324                WebAgent agent = new WebAgent(); 
    2425                try { 
    25                         String s = agent.get("http://twitter.com/statuses/friends_timeline/itengineer.xml"); 
     26                        String s = agent.get("http://twitter.com/statuses/friends_timeline/itengineer.xml").getContents(); 
    2627                        outputTest(new ByteArrayInputStream(s.getBytes())); 
    2728                } catch (IOException e) {