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 2012/03/28 16:05:44 UTC

svn commit: r1306344 - in /incubator/wookie/trunk/src/org/apache/wookie: controller/WidgetInstancesController.java helpers/WidgetInstanceHelper.java

Author: scottbw
Date: Wed Mar 28 14:05:43 2012
New Revision: 1306344

URL: http://svn.apache.org/viewvc?rev=1306344&view=rev
Log:
Added JSON support to /widgetinstances API

Modified:
    incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
    incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java

Modified: incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java?rev=1306344&r1=1306343&r2=1306344&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetInstancesController.java Wed Mar 28 14:05:43 2012
@@ -119,22 +119,31 @@ public class WidgetInstancesController e
 	 */
 	@Override
 	protected void show(String resourceId, HttpServletRequest request, HttpServletResponse response) throws ResourceNotFoundException, UnauthorizedAccessException, IOException {
-	   IWidgetInstance instance = WidgetInstancesController.getLocalizedWidgetInstance(request);
-	   if (instance == null){
-	     throw new ResourceNotFoundException();
-	   } else {
-	     // Check the API key matches
-	     String apiKey = request.getParameter("api_key");
-	     if (!instance.getApiKey().equals(apiKey)) throw new UnauthorizedAccessException();
-	     // Return the response XML
-	     checkProxy(request);
-	     String url = getUrl(request, instance);
-	     String locale = request.getParameter("locale");//$NON-NLS-1$
-	     response.setContentType(CONTENT_TYPE);
-	     response.setStatus(HttpServletResponse.SC_OK);
-	     PrintWriter out = response.getWriter();
-	     out.println(WidgetInstanceHelper.createXMLWidgetInstanceDocument(instance, url, locale));
-	   }
+	  IWidgetInstance instance = WidgetInstancesController.getLocalizedWidgetInstance(request);
+	  if (instance == null){
+	    throw new ResourceNotFoundException();
+	  } else {
+	    //
+	    // Check the API key matches
+	    //
+	    String apiKey = request.getParameter("api_key");
+	    if (!instance.getApiKey().equals(apiKey)) throw new UnauthorizedAccessException();
+
+	    checkProxy(request);
+	    String url = getUrl(request, instance);
+	    String locale = request.getParameter("locale");//$NON-NLS-1$
+	    response.setStatus(HttpServletResponse.SC_OK);
+
+	    //
+	    // Return XML or JSON 
+	    //
+	    switch(format(request)){
+	    case XML: returnXml(WidgetInstanceHelper.createXMLWidgetInstanceDocument(instance, url, locale), response); break;
+	    case JSON: returnJson(WidgetInstanceHelper.toJson(instance, url, locale), response); break;
+	    default: returnXml(WidgetInstanceHelper.createXMLWidgetInstanceDocument(instance, url, locale), response); break;
+	    }
+
+	  }
 	}
 	
 	/* (non-Javadoc)

Modified: incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java?rev=1306344&r1=1306343&r2=1306344&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/WidgetInstanceHelper.java Wed Mar 28 14:05:43 2012
@@ -14,15 +14,20 @@
 package org.apache.wookie.helpers;
 
 import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.log4j.Logger;
 import org.apache.wookie.beans.IWidget;
 import org.apache.wookie.beans.IWidgetInstance;
 import org.apache.wookie.w3c.IW3CXMLConfiguration;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 /**
  * A helper to create representations of Widget Instance resources
  */
 public class WidgetInstanceHelper {
 	
+  static Logger logger = Logger.getLogger(WidgetInstanceHelper.class.getName());
+  
 	private static final String XMLDECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 
 	/**
@@ -54,4 +59,25 @@ public class WidgetInstanceHelper {
 		return xml;
 	}
 	
+  public static String toJson(IWidgetInstance instance, String url, String locale) {
+    IWidget widget = instance.getWidget();
+    String width = String.valueOf(IW3CXMLConfiguration.DEFAULT_WIDTH_LARGE);
+    String height = String.valueOf(IW3CXMLConfiguration.DEFAULT_HEIGHT_LARGE);
+    if (widget.getWidth() != null && widget.getWidth() > 0)
+      width = widget.getWidth().toString();
+    if (widget.getHeight() != null && widget.getHeight() > 0)
+      height = widget.getHeight().toString();
+    JSONObject json = new JSONObject();
+    try {
+      json.put("url", url);
+      json.put("identifier", instance.getIdKey());
+      json.put("title", widget.getWidgetTitle(locale));
+      json.put("height", height);
+      json.put("width", width);
+    } catch (JSONException e) {
+      logger.error("Problem rendering instance using JSON",e);
+    }
+    return json.toString();
+  }
+	
 }