You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2011/03/21 15:02:06 UTC

svn commit: r1083787 - in /incubator/wookie/trunk/src/org/apache/wookie: controller/WidgetAccessRequestPolicyController.java helpers/AccessRequestHelper.java

Author: scottbw
Date: Mon Mar 21 14:02:06 2011
New Revision: 1083787

URL: http://svn.apache.org/viewvc?rev=1083787&view=rev
Log:
Added JSON support to the WARP controller

Modified:
    incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetAccessRequestPolicyController.java
    incubator/wookie/trunk/src/org/apache/wookie/helpers/AccessRequestHelper.java

Modified: incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetAccessRequestPolicyController.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetAccessRequestPolicyController.java?rev=1083787&r1=1083786&r2=1083787&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetAccessRequestPolicyController.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetAccessRequestPolicyController.java Mon Mar 21 14:02:06 2011
@@ -58,6 +58,7 @@ public class WidgetAccessRequestPolicyCo
 		switch (format(request)) {
 			case XML: returnXml(AccessRequestHelper.createXMLAccessRequestDocument(accessRequests),response);break;
 			case HTML: returnHtml(AccessRequestHelper.createAccessRequestHTMLTable(accessRequests),response);break;
+			case JSON: returnJson(AccessRequestHelper.createJSON(accessRequests),response);break;
 		}
 	}
 
@@ -72,6 +73,7 @@ public class WidgetAccessRequestPolicyCo
 		switch (format(request)) {
 			case XML: returnXml(AccessRequestHelper.createXMLAccessRequestDocument(new IAccessRequest[]{ar}),response);break;
 			case HTML: returnHtml(AccessRequestHelper.createAccessRequestHTMLTable(new IAccessRequest[]{ar}),response);break;
+			case JSON: returnJson(AccessRequestHelper.createJSON(new IAccessRequest[]{ar}),response);break;
 		}
 	}
 

Modified: incubator/wookie/trunk/src/org/apache/wookie/helpers/AccessRequestHelper.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/AccessRequestHelper.java?rev=1083787&r1=1083786&r2=1083787&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/AccessRequestHelper.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/AccessRequestHelper.java Mon Mar 21 14:02:06 2011
@@ -30,13 +30,29 @@ public class AccessRequestHelper {
 	public static String createXMLAccessRequestDocument(IAccessRequest[] accessRequests){
 		String document = XMLDECLARATION;
 		document += "\n<policies>\n";
-		for (IAccessRequest ar:accessRequests){
-			document += toXml(ar);
+		if (accessRequests != null){
+			for (IAccessRequest ar:accessRequests){
+				document += toXml(ar);
+			}
 		}
 		document += "</policies>\n";
 		return document;
 	}
 	
+	public static String createJSON(IAccessRequest[] accessRequests){
+		String json = "{ \"policies\":[";
+		if (accessRequests != null){
+			for (IAccessRequest ar:accessRequests){
+				json += toJSON(ar);
+			}
+		}
+		// remove last comma
+		json = json.substring(0, json.length()-1);
+		json += "]}";
+		
+		return json;
+	}
+	
 	/**
 	 * Creates a HTML table with controls
 	 * @param accessRequests
@@ -56,6 +72,7 @@ public class AccessRequestHelper {
 		String xml = "\t<policy ";
 		xml += "id=\""+ar.getId()+"\" ";
 		xml += "widget=\""+ar.getWidget().getId()+"\" ";
+		xml += "widget_title=\""+WidgetHelper.getEncodedWidgetTitle(ar.getWidget(), null)+"\" ";
 		xml += "origin=\""+ar.getOrigin()+"\" ";
 		xml += "subdomains=\""+ar.isSubdomains()+"\" ";
 		if (ar.isGranted()) {
@@ -67,6 +84,22 @@ public class AccessRequestHelper {
 		return xml;
 	}
 	
+	private static String toJSON(IAccessRequest ar){
+		String json = "{";
+			json += " \"id\":\"" + ar.getId() + "\""; 
+			json += ",\"widget\":\"" + ar.getWidget().getId() + "\""; 
+			json += ",\"widget_title\":\"" + WidgetHelper.getEncodedWidgetTitle(ar.getWidget(), null) + "\""; 
+			json += ",\"origin\":\"" + ar.getOrigin() + "\""; 	
+			json += ",\"subdomains\":\"" + ar.isSubdomains() + "\""; 	
+			if (ar.isGranted()) {
+				json+= ",\"granted\": \"true\"";
+			} else {
+				json+= ",\"granted\": \"false\"";			
+			}
+		json += "},";
+		return json;
+	}
+	
 	public static String toHtml(IAccessRequest ar){
 		String html = "";
 		if (ar.isGranted()){