Author: scottbw
Date: Sun Feb 28 21:44:25 2010
New Revision: 917299

URL: http://svn.apache.org/viewvc?rev=917299&view=rev
Log:
Fixed the problem in connecting to REST participants service, and added a 
getUsers(instance) method with test case, for checking that participants are 
correctly set up on the service. Also corrected a typo in getCurrentUser test 
case.

Modified:
    
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/connector/framework/impl/WookieConnectorService.java
    
incubator/wookie/trunk/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java

Modified: 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/connector/framework/impl/WookieConnectorService.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/connector/framework/impl/WookieConnectorService.java?rev=917299&r1=917298&r2=917299&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/connector/framework/impl/WookieConnectorService.java
 (original)
+++ 
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/connector/framework/impl/WookieConnectorService.java
 Sun Feb 28 21:44:25 2010
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.util.HashMap;
 
+import org.apache.wookie.connector.framework.User;
 import org.apache.wookie.connector.framework.Widget;
 import org.apache.wookie.connector.framework.WidgetInstance;
 import org.apache.wookie.connector.framework.WookieConnectorException;
@@ -41,7 +42,7 @@
   
   @Test
   public void getUser() {
-    assertNotNull("Test user is null", service.getUser("test"));
+    assertNotNull("Test user is null", service.getUser("testuser"));
   }
   
   @Test
@@ -57,4 +58,18 @@
     WidgetInstance instance = 
service.getOrCreateInstance((Widget)widgets.values().toArray()[0]);
     assertNotNull("Retrieved widget instance is null", instance);
   }
+  
+  @Test
+  public void addParticipant() throws WookieConnectorException, IOException {
+       HashMap<String, Widget> widgets = service.getAvailableWidgets();
+       WidgetInstance instance = 
service.getOrCreateInstance((Widget)widgets.values().toArray()[0]);
+    assertNotNull("Retrieved widget instance is null", instance);
+    
+       User user = new User("test1","test user 1");
+    service.addParticipant(instance, user);
+    User[] users = service.getUsers(instance);
+    assertTrue("Wrong number of users returned",users.length==2);
+    assertTrue("Wrong user returned", 
users[0].getLoginName().equals("testuser"));
+    assertTrue("Wrong user returned", users[1].getLoginName().equals("test1"));
+  }
 }

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java?rev=917299&r1=917298&r2=917299&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java
 Sun Feb 28 21:44:25 2010
@@ -106,14 +106,7 @@
     return instance;
   }
 
-  /**
-   * @refactor At time of writing the REST API for adding a participant is 
broken so we are
-   * using the non-REST approach. The code for REST API is commented out and 
should be used
-   * in the future.
-   */
   public void addParticipant(WidgetInstance widget, User user) throws 
WookieConnectorException {
-    /* 
-     * REST API approach - REST API is broken at time of writing
     StringBuilder postdata;
     try {
       postdata = new StringBuilder("api_key=");
@@ -137,18 +130,12 @@
     URL url = null;
     try {
       url = new URL(conn.getURL() + "/participants");
-      URLConnection conn = url.openConnection();
+      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
       conn.setDoOutput(true);
       OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
       wr.write(postdata.toString());
       wr.flush();
-  
-      InputStream is = conn.getInputStream();
-  
-      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-      DocumentBuilder db;
-      db = dbf.newDocumentBuilder();
-      Document xml = db.parse(is);
+      if (conn.getResponseCode() > 201) throw new 
IOException(conn.getResponseMessage());
     } catch (MalformedURLException e) {
       throw new WookieConnectorException("Participants rest URL is incorrect: 
" + url, e);
     } catch (IOException e) {
@@ -158,47 +145,7 @@
       sb.append(" data: ");
       sb.append(postdata);
       throw new WookieConnectorException(sb.toString(), e);
-    } catch (ParserConfigurationException e) {
-      throw new RuntimeException("Unable to configure XML parser", e);
-    } catch (SAXException e) {
-      throw new RuntimeException("Problem parsing XML from Wookie Server", e);
-    }
-    */
-    
-    StringBuilder url;
-    try {
-      url = new StringBuilder(conn.getURL());
-      url.append("/WidgetServiceServlet?");
-      url.append("requestid=addparticipant");
-      url.append("&api_key=");
-      url.append(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
-      url.append("&shareddatakey=");
-      url.append(URLEncoder.encode(getConnection().getSharedDataKey(), 
"UTF-8"));
-      url.append("&userid=");
-      url.append(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
-      url.append("&widgetid=");
-      url.append(URLEncoder.encode(widget.getId(), "UTF-8"));
-      url.append("&participant_id=");
-      url.append(URLEncoder.encode(user.getLoginName(), "UTF-8"));
-      url.append("&participant_display_name=");
-      url.append(URLEncoder.encode(user.getScreenName(), "UTF-8"));
-      url.append("&participant_thumbnail_url=");
-      url.append(URLEncoder.encode(user.getThumbnailUrl(), "UTF-8"));
-    } catch (UnsupportedEncodingException e) {
-      throw new WookieConnectorException("Must support UTF-8 encoding", e);
-    }
-    
-    try {
-      HttpURLConnection conn = (HttpURLConnection)new 
URL(url.toString()).openConnection();
-      conn.disconnect();
-    } catch (MalformedURLException e) {
-      throw new WookieConnectorException("Participants rest URL is incorrect: 
" + url, e);
-    } catch (IOException e) {
-      StringBuilder sb = new StringBuilder("Problem adding a participant. ");
-      sb.append("URL: ");
-      sb.append(url);
-      throw new WookieConnectorException(sb.toString(), e);
-    }
+    } 
   }
   
   /**
@@ -258,6 +205,65 @@
   public WidgetInstances getInstances() {
     return instances;
   }
+  
+  /**
+   * Get the array of users for a widget instance
+   * @param instance
+   * @return an array of users
+   * @throws WookieConnectorException
+   */
+  public User[] getUsers(WidgetInstance instance) throws 
WookieConnectorException{
+           String queryString;
+           try {
+               queryString = new String("?api_key=");
+               queryString+=(URLEncoder.encode(getConnection().getApiKey(), 
"UTF-8"));
+               queryString+=("&shareddatakey=");
+               
queryString+=(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
+               queryString+=("&userid=");
+               
queryString+=(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
+               queryString+=("&widgetid=");
+               queryString+=(URLEncoder.encode(instance.getId(), "UTF-8"));
+           } catch (UnsupportedEncodingException e) {
+             throw new WookieConnectorException("Must support UTF-8 encoding", 
e);
+           }
+           
+           URL url = null;
+           try {
+             url = new URL(conn.getURL() + "/participants"+queryString);
+             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+             InputStream is = conn.getInputStream();
+             if (conn.getResponseCode() > 200) throw new 
IOException(conn.getResponseMessage());
+             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+             DocumentBuilder db = dbf.newDocumentBuilder();
+             Document widgetsDoc = db.parse(is);
+             Element root = widgetsDoc.getDocumentElement();
+             NodeList participantsList = 
root.getElementsByTagName("participant");
+             if (participantsList == null || participantsList.getLength() == 
0) return new User[0];
+             User[] users = new User[participantsList.getLength()];
+             for (int idx = 0; idx < participantsList.getLength(); idx = idx + 
1) {
+               Element participantEl = (Element) participantsList.item(idx);
+               String id = participantEl.getAttribute("id");
+               String name = participantEl.getAttribute("display_name");
+               //FIXME implement: String thumbnail = 
participantEl.getAttribute("thumbnail_url");
+               User user = new User(id,name);
+               users[idx] = user;
+             }
+             return users;
+           } catch (MalformedURLException e) {
+             throw new WookieConnectorException("Participants rest URL is 
incorrect: " + url, e);
+           } catch (IOException e) {
+             StringBuilder sb = new StringBuilder("Problem getting 
participants. ");
+             sb.append("URL: ");
+             sb.append(url);
+             sb.append(" data: ");
+             sb.append(queryString);
+             throw new WookieConnectorException(sb.toString(), e);
+           } catch (ParserConfigurationException e) {
+                     throw new WookieConnectorException("Problem parsing data: 
" + url, e);
+               } catch (SAXException e) {
+                     throw new WookieConnectorException("Problem parsing data: 
" + url, e);
+               } 
+  }
 
   
 }


Reply via email to