Author: scottbw
Date: Wed Mar 28 12:32:59 2012
New Revision: 1306289
URL: http://svn.apache.org/viewvc?rev=1306289&view=rev
Log:
Added JSON support to the /keys REST API
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ApiKeyControllerTest.java
incubator/wookie/trunk/src/org/apache/wookie/controller/ApiKeyController.java
incubator/wookie/trunk/src/org/apache/wookie/helpers/ApiKeyHelper.java
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ApiKeyControllerTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ApiKeyControllerTest.java?rev=1306289&r1=1306288&r2=1306289&view=diff
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ApiKeyControllerTest.java
(original)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ApiKeyControllerTest.java
Wed Mar 28 12:32:59 2012
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
@@ -33,9 +34,9 @@ import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
-import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;
+import org.mortbay.util.ajax.JSON;
/**
* Functional tests for API key management
@@ -43,15 +44,6 @@ import org.junit.Test;
public class ApiKeyControllerTest extends AbstractControllerTest {
private static final String APIKEY_SERVICE_LOCATION_VALID =
TEST_SERVER_LOCATION + "keys";
-
-
-// @AfterClass
-// public static void cleanUp() throws HttpException, IOException{
-// HttpClient client = new HttpClient();
-// DeleteMethod del = new DeleteMethod(APIKEY_SERVICE_LOCATION_VALID + "/"
+ "DUPLICATION_TEST");
-// setAuthenticationCredentials(client);
-// client.executeMethod(del);
-// }
/**
* Attempt to get the list of API keys without having authenticated first
@@ -83,6 +75,51 @@ public class ApiKeyControllerTest extend
int code = get.getStatusCode();
assertEquals(200, code);
}
+
+ /**
+ * Get the set of API keys in JSON using default admin credentials
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @SuppressWarnings("unchecked")
+ @Test
+ public void getKeysJson() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(APIKEY_SERVICE_LOCATION_VALID);
+ //
+ // Set the accepts header to JSON
+ //
+ get.addRequestHeader("accepts", "application/json");
+ setAuthenticationCredentials(client);
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+
+ //
+ // Parse the response and check the values
+ //
+ Object[] keys = (Object[]) JSON.parse(get.getResponseBodyAsStream());
+ assertEquals("TEST", ((Map<String, String>)keys[0]).get("id"));
+ assertEquals("TEST", ((Map<String, String>)keys[0]).get("value"));
+
+ //
+ // Try again using ?format param overriding the accepts header
+ //
+ get = new GetMethod(APIKEY_SERVICE_LOCATION_VALID+"?format=json");
+ get.addRequestHeader("accepts", "text/xml");
+ setAuthenticationCredentials(client);
+ client.executeMethod(get);
+ code = get.getStatusCode();
+ assertEquals(200, code);
+
+ //
+ // Parse the response and check the values
+ //
+ keys = (Object[]) JSON.parse(get.getResponseBodyAsStream());
+ assertEquals("TEST", ((Map<String, String>)keys[0]).get("id"));
+ assertEquals("TEST", ((Map<String, String>)keys[0]).get("value"));
+ }
/**
* Add a new key
Modified:
incubator/wookie/trunk/src/org/apache/wookie/controller/ApiKeyController.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/ApiKeyController.java?rev=1306289&r1=1306288&r2=1306289&view=diff
==============================================================================
---
incubator/wookie/trunk/src/org/apache/wookie/controller/ApiKeyController.java
(original)
+++
incubator/wookie/trunk/src/org/apache/wookie/controller/ApiKeyController.java
Wed Mar 28 12:32:59 2012
@@ -55,7 +55,11 @@ public class ApiKeyController extends Co
@Override
protected void index(HttpServletRequest request, HttpServletResponse
response)
throws UnauthorizedAccessException, IOException {
-
returnXml(ApiKeyHelper.createXML(ApiKeys.getInstance().getKeys()),response);
+ switch (format(request)) {
+ case XML:
returnXml(ApiKeyHelper.toXml(ApiKeys.getInstance().getKeys()),response);break;
+ case JSON:
returnJson(ApiKeyHelper.toJson(ApiKeys.getInstance().getKeys()),response);break;
+ default:
returnXml(ApiKeyHelper.toXml(ApiKeys.getInstance().getKeys()),response);break;
+ }
}
/* (non-Javadoc)
Modified: incubator/wookie/trunk/src/org/apache/wookie/helpers/ApiKeyHelper.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/helpers/ApiKeyHelper.java?rev=1306289&r1=1306288&r2=1306289&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/helpers/ApiKeyHelper.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/helpers/ApiKeyHelper.java Wed
Mar 28 12:32:59 2012
@@ -18,21 +18,27 @@
package org.apache.wookie.helpers;
import org.apache.wookie.server.security.ApiKey;
+import org.apache.log4j.Logger;
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;
/**
* View helper for API keys
*/
public class ApiKeyHelper {
+
+ static Logger logger = Logger.getLogger(ApiKeyHelper.class.getName());
/**
* Create an XML representation of a set of API keys
* @param keys the keys to serialize
* @return a String containing the XML serialization of the API keys
*/
- public static String createXML(ApiKey[] keys){
+ public static String toXml(ApiKey[] keys){
Document document = new Document();
Element keysElement = new Element("keys");
@@ -47,4 +53,25 @@ public class ApiKeyHelper {
XMLOutputter outputter = new XMLOutputter();
return outputter.outputString(document);
}
+
+ /**
+ * Create a JSON representation of a set of API keys
+ * @param keys the keys to serialize
+ * @return a JSON String of the keys
+ */
+ public static String toJson(ApiKey[] keys){
+ JSONArray json = new JSONArray();
+ for(ApiKey key: keys){
+ JSONObject jsonKey = new JSONObject();
+ try {
+ jsonKey.put("id", key.getId());
+ jsonKey.put("value", key.getValue());
+ jsonKey.put("email", key.getEmail());
+ } catch (JSONException e) {
+ logger.error("Problem rendering json for ApiKey object", e);
+ }
+ json.put(jsonKey);
+ }
+ return json.toString();
+ }
}