upd: make output of Find-Agent-info JSON-formatted
authorFelix Dörre <felix@dogcraft.de>
Fri, 23 Sep 2016 16:57:16 +0000 (18:57 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 29 Sep 2016 15:33:10 +0000 (17:33 +0200)
Change-Id: I773aaff596314e83b63e8555ff8e85fce1c2cf55

src/org/cacert/gigi/api/FindAgent.java
tests/org/cacert/gigi/api/TestFindAgent.java
tests/org/cacert/gigi/testUtils/IOUtils.java

index e64326b..a78cd65 100644 (file)
@@ -1,6 +1,7 @@
 package org.cacert.gigi.api;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
@@ -14,6 +15,7 @@ import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.email.EmailProvider;
 import org.cacert.gigi.pages.account.FindAgentAccess;
 import org.cacert.gigi.util.ServerConstants;
+import org.json.JSONWriter;
 
 public class FindAgent extends APIPoint {
 
@@ -69,16 +71,29 @@ public class FindAgent extends APIPoint {
             resp.setContentType("text/plain; charset=UTF-8");
             resp.getWriter().print(us.getId());
         } else if (pi.equals(PATH_INFO)) {
-            resp.setContentType("text/plain; charset=UTF-8");
+            resp.setContentType("application/json; charset=UTF-8");
+            PrintWriter out = resp.getWriter();
             String[] uids = req.getParameterValues("id");
+            JSONWriter jw = new JSONWriter(out);
+            jw.array();
             for (String i : uids) {
                 User u1 = User.getById(Integer.parseInt(i));
                 if ( !u1.isInGroup(Group.LOCATE_AGENT)) {
                     continue;
                 }
                 // date, recheck(?), name
-                resp.getWriter().println(i + "," + u1.canAssure() + "," + u1.getPreferredName().toAbbreviatedString());
+                jw.object();
+                jw.key("id");
+                jw.value(u1.getId());
+
+                jw.key("canAssure");
+                jw.value(u1.canAssure());
+
+                jw.key("name");
+                jw.value(u1.getPreferredName().toAbbreviatedString());
+                jw.endObject();
             }
+            jw.endArray();
         } else if (pi.equals(PATH_MAIL)) {
             String id = req.getParameter("from");
             String rid = req.getParameter("to");
index d721396..71bad94 100644 (file)
@@ -7,6 +7,7 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.security.GeneralSecurityException;
+import java.util.Arrays;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.Certificate;
@@ -20,6 +21,9 @@ import org.cacert.gigi.pages.account.FindAgentAccess;
 import org.cacert.gigi.testUtils.IOUtils;
 import org.cacert.gigi.testUtils.RestrictedApiTest;
 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.JSONTokener;
 import org.junit.Test;
 
 public class TestFindAgent extends RestrictedApiTest {
@@ -78,10 +82,21 @@ public class TestFindAgent extends RestrictedApiTest {
         int u2 = createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD);
 
         String res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
-        assertEquals(res, "");
+        res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
+        assertEquals(new JSONArray().toString(), new JSONArray(new JSONTokener(res)).toString());
         grant(u, Group.LOCATE_AGENT);
         grant(User.getById(u2), Group.LOCATE_AGENT);
         res = IOUtils.readURL(doApi(FindAgent.PATH_INFO, "id=" + id + "&id=" + u2)).replace("\r", "");
-        assertEquals(id + ",true," + u.getPreferredName().toAbbreviatedString() + "\n" + u2 + ",false," + User.getById(u2).getPreferredName().toAbbreviatedString() + "\n", res);
+        JSONTokener jt = new JSONTokener(res);
+        JSONObject j1 = new JSONObject();
+        j1.put("id", id);
+        j1.put("canAssure", true);
+        j1.put("name", u.getPreferredName().toAbbreviatedString());
+        JSONObject j2 = new JSONObject();
+        j2.put("id", u2);
+        j2.put("canAssure", false);
+        j2.put("name", User.getById(u2).getPreferredName().toAbbreviatedString());
+        JSONArray ja = new JSONArray(Arrays.asList(j1, j2));
+        assertEquals(ja.toString(), new JSONArray(jt).toString());
     }
 }
index 23db401..61cd178 100644 (file)
@@ -17,7 +17,7 @@ public class IOUtils {
 
     public static String readURL(URLConnection in) {
         try {
-            if ( !in.getContentType().equals("text/html; charset=UTF-8") && !in.getContentType().equals("text/plain; charset=UTF-8")) {
+            if ( !in.getContentType().equals("text/html; charset=UTF-8") && !in.getContentType().equals("text/plain; charset=UTF-8") && !in.getContentType().equals("application/json; charset=UTF-8")) {
                 if (in instanceof HttpURLConnection && ((HttpURLConnection) in).getResponseCode() != 200) {
                     System.err.println(readURL(new InputStreamReader(((HttpURLConnection) in).getErrorStream(), "UTF-8")));
                 }