Author: scottbw
Date: Wed Mar 28 13:38:26 2012
New Revision: 1306319

URL: http://svn.apache.org/viewvc?rev=1306319&view=rev
Log:
Added JSON support for the Participants API, and rewrote the helper to use JDOM 
and the JSON lib rather than concatenate Strings.

Modified:
    
incubator/wookie/trunk/src/org/apache/wookie/controller/ParticipantsController.java
    incubator/wookie/trunk/src/org/apache/wookie/helpers/ParticipantHelper.java

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/controller/ParticipantsController.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/ParticipantsController.java?rev=1306319&r1=1306318&r2=1306319&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/controller/ParticipantsController.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/controller/ParticipantsController.java
 Wed Mar 28 13:38:26 2012
@@ -34,8 +34,7 @@ import org.apache.wookie.helpers.Partici
 
 /**
  * Implementation of the REST API for working with Participants. For a 
description of the methods implemented by this controller see 
- * http://incubator.apache.org/wookie/wookie-rest-api.html 
- * @author Scott Wilson
+ * http://incubator.apache.org/wookie/docs/api.html
  *
  */
 public class ParticipantsController extends Controller {
@@ -72,7 +71,11 @@ public class ParticipantsController exte
                IWidgetInstance instance = 
WidgetInstancesController.findWidgetInstance(request);
                if (instance == null) throw new ResourceNotFoundException();
                IParticipant[] participants = new 
SharedContext(instance).getParticipants();
-               
returnXml(ParticipantHelper.createXMLParticipantsDocument(participants), 
response);
+    switch (format(request)) {
+       case XML: 
returnXml(ParticipantHelper.createXMLParticipantsDocument(participants),response);break;
+       case JSON: 
returnJson(ParticipantHelper.createJSONParticipantsDocument(participants),response);break;
+       default: 
returnXml(ParticipantHelper.createXMLParticipantsDocument(participants),response);break;
+    }
        }
 
        @Override

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/helpers/ParticipantHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/ParticipantHelper.java?rev=1306319&r1=1306318&r2=1306319&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/ParticipantHelper.java 
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/ParticipantHelper.java 
Wed Mar 28 13:38:26 2012
@@ -13,21 +13,26 @@
  */
 package org.apache.wookie.helpers;
 
+import org.apache.log4j.Logger;
 import org.apache.wookie.beans.IParticipant;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.output.XMLOutputter;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 /**
  * A helper for Participants
- * @author scott wilson
- *
  */
 public class ParticipantHelper {
-       
-       private static final String XMLDECLARATION = "<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>";
+  
+  static Logger logger = Logger.getLogger(ParticipantHelper.class.getName());
        
        /**
-        * Generate a Participants representation doc in XML for a single 
participant
+        * Create XML representation for a single participant
         * @param participant
-        * @return
+        * @return a String representing the participant in XML
         */
        public static String createXMLParticipantsDocument(IParticipant 
participant){
                IParticipant[] participants = {participant};
@@ -35,61 +40,83 @@ public class ParticipantHelper {
        }
        
        /**
-        * Generate a Widgets representation doc in XML for an array of 
widgets, e.g. a catalogue
+        * Create XML representation of an array of participants
         * 
-        * @param widgets
-        * @param localIconPath
-        * @return
+        * @param participants the participants
+        * @return a String representing the participants in XML 
         */
        public static String createXMLParticipantsDocument(IParticipant[] 
participants){
-               String document = XMLDECLARATION;
-               document += "\n<participants>\n"; //$NON-NLS-1$
+               
+               Document document = new Document();
+               Element root = new Element("participants");
+               document.setRootElement(root);
                for (IParticipant participant:participants){
-                       document += toXml(participant);
+                 root.addContent(toXml(participant));
                }
-               document += "</participants>\n"; //$NON-NLS-1$
-               return document;
+               return new XMLOutputter().outputString(document);
        }
        
+       /**
+        * Create a JSON representation of a single participant
+        * @param participant
+        * @return JSON output as a String of the participant
+        */
        public static String createJSONParticipantDocument(IParticipant 
participant){
-                return 
"{\"Participant\":"+ParticipantHelper.toJson(participant)+"}"; //$NON-NLS-1$
+    JSONObject json = new JSONObject();
+    try {
+      json.put("Participant",toJson(participant));
+    } catch (JSONException e) {
+      logger.error("Problem marshalling participants to JSON", e);
+    }
+    return json.toString();
        }
        
+       /**
+        * Create a JSON representation of an array of participants
+        * @param participants
+        * @return JSON output as a String
+        */
        public static String createJSONParticipantsDocument(IParticipant[] 
participants){
-               String json = "{\"Participants\":[";//$NON-NLS-1$
-               String delimit = "";
-               for (IParticipant participant: participants){
-                       json+=delimit+toJson(participant);
-                       delimit = ","; //$NON-NLS-1$
-               }
-               json+="]}"; //$NON-NLS-1$
-               return json;
+         JSONArray arr = new JSONArray();
+               try {
+      for (IParticipant participant: participants){
+          arr.put(toJson(participant));
+      }
+      JSONObject json = new JSONObject();
+      json.put("Participants", arr);
+      return json.toString();
+    } catch (JSONException e) {
+      logger.error("Problem marshalling participants to JSON", e);
+      return null;
+    }
+
        }
        
        /**
-        * Returns an XML representation of the given participant.
-        * 
-        * @param participant the participant to represent
-        * @return the XML representation of the participant
+        * Returns an XML element for the given participant.
+        * @param participant
+        * @return an XML Element representing the participant
         */
-       public static String toXml(IParticipant participant){
-                       return "<participant 
id=\""+participant.getParticipantId()+
-                                                               "\" 
display_name=\""+participant.getParticipantDisplayName()+
-                                                               "\" 
thumbnail_url=\""+participant.getParticipantThumbnailUrl()+
-                                       "\" />"; //$NON-NLS-1$ //$NON-NLS-2$ 
//$NON-NLS-3$ //$NON-NLS-4$
+       private static Element toXml(IParticipant participant){
+         Element element = new Element("participant");
+         element.setAttribute("id", participant.getParticipantId());
+         element.setAttribute("display_name", 
participant.getParticipantDisplayName());
+         element.setAttribute("thumbnail_url", 
participant.getParticipantThumbnailUrl());
+         return element;
        }
 
        /**
-        * Converts a participant to JSON representation
+        * Creates a JSONObject for the given participant
         * @param participant
-        * @return
+        * @return a JSONObject representing the participant
+        * @throws JSONException 
         */
-       public static String toJson(IParticipant participant){
-               String json = "{"+
-               "\"participant_id\":\""+participant.getParticipantId()+
-               "\", 
\"participant_display_name\":\""+participant.getParticipantDisplayName()+
-               "\", 
\"participant_thumbnail_url\":\""+participant.getParticipantThumbnailUrl()+"\"}";
-               return json; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
//$NON-NLS-4$
+       private static JSONObject toJson(IParticipant participant) throws 
JSONException{
+    JSONObject obj = new JSONObject();
+    obj.put("participant_id", participant.getParticipantId());
+    obj.put("participant_display_name", 
participant.getParticipantDisplayName());
+    obj.put("participant_thumbnail_url", 
participant.getParticipantThumbnailUrl());
+    return obj;
        }
 
 }


Reply via email to