Loading projects/cadcAccessControl/build.xml +1 −1 Original line number Diff line number Diff line Loading @@ -125,7 +125,7 @@ <pathelement path="${build}/test/class"/> <pathelement path="${testingJars}"/> </classpath> <test name="ca.nrc.cadc.ac.json.GroupReaderWriterTest" /> <test name="ca.nrc.cadc.ac.json.JsonGroupReaderWriterTest" /> <formatter type="plain" usefile="false" /> </junit> </target> Loading projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonGroupReader.java +21 −126 Original line number Diff line number Diff line Loading @@ -68,26 +68,20 @@ */ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.AC; import ca.nrc.cadc.ac.Group; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.date.DateUtil; import org.json.JSONArray; import ca.nrc.cadc.ac.xml.GroupReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URISyntaxException; import java.security.Principal; import java.text.DateFormat; import java.text.ParseException; import java.util.Scanner; public class JsonGroupReader public class JsonGroupReader extends GroupReader { /** * Construct a Group from a InputStream. Loading @@ -96,9 +90,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(InputStream in) @Override public Group read(InputStream in) throws ReaderException, IOException { if (in == null) Loading @@ -120,9 +114,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(Reader reader) @Override public Group read(Reader reader) throws ReaderException, IOException { if (reader == null) Loading @@ -143,9 +137,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(String json) @Override public Group read(String json) throws ReaderException, IOException { if (json == null) Loading @@ -156,7 +150,17 @@ public class JsonGroupReader // Create a JSONObject from the JSON try { return parseGroup(new JSONObject(json).getJSONObject("group")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("properties", "property"); jsonInputter.getListElementMap().put("details", "userDetails"); jsonInputter.getListElementMap().put("groupMembers", "group"); jsonInputter.getListElementMap().put("groupAdmins", "group"); jsonInputter.getListElementMap().put("userMembers", "user"); jsonInputter.getListElementMap().put("userAdmins", "user"); Document document = jsonInputter.input(json); return GroupReader.parseGroup(document.getRootElement()); } catch (JSONException e) { Loading @@ -166,113 +170,4 @@ public class JsonGroupReader } } protected static Group parseGroup(JSONObject groupObject) throws ReaderException, JSONException { String uri = groupObject.getString("uri"); // Group groupID int index = uri.indexOf(AC.GROUP_URI); if (index == -1) { String error = "group uri attribute malformed: " + uri; throw new ReaderException(error); } String groupID = uri.substring(AC.GROUP_URI.length()); // Group owner User<? extends Principal> user = null; if (groupObject.has("owner")) { JSONObject ownerObject = groupObject.getJSONObject("owner"); JSONObject userObject = ownerObject.getJSONObject("user"); user = JsonUserReader.parseUser(userObject); } Group group = new Group(groupID, user); // description if (groupObject.has("description")) { group.description = groupObject.getString("description"); } // lastModified if (groupObject.has("lastModified")) { try { DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC); group.lastModified = df.parse(groupObject.getString("lastModified")); } catch (ParseException e) { String error = "Unable to parse group lastModified because " + e.getMessage(); throw new ReaderException(error); } } // properties if (groupObject.has("description")) { JSONArray propertiesArray = groupObject.getJSONArray("properties"); for (int i = 0; i < propertiesArray.length(); i++) { JSONObject propertiesObject = propertiesArray.getJSONObject(i); JSONObject propertyObject = propertiesObject.getJSONObject("property"); group.getProperties().add(JsonGroupPropertyReader.read(propertyObject)); } } // groupMembers if (groupObject.has("groupMembers")) { JSONArray groupMembersArray = groupObject.getJSONArray("groupMembers"); for (int i = 0; i < groupMembersArray.length(); i++) { JSONObject groupMembersObject = groupMembersArray.getJSONObject(i); JSONObject groupMemberObject = groupMembersObject.getJSONObject("group"); group.getGroupMembers().add(parseGroup(groupMemberObject)); } } // userMembers if (groupObject.has("userMembers")) { JSONArray userMembersArray = groupObject.getJSONArray("userMembers"); for (int i = 0; i < userMembersArray.length(); i++) { JSONObject userMemberObject = userMembersArray.getJSONObject(i); JSONObject userObject = userMemberObject.getJSONObject("user"); group.getUserMembers().add(JsonUserReader.parseUser(userObject)); } } // groupAdmins if (groupObject.has("groupAdmins")) { JSONArray groupAdminsArray = groupObject.getJSONArray("groupAdmins"); for (int i = 0; i < groupAdminsArray.length(); i++) { JSONObject groupAdminsObject = groupAdminsArray.getJSONObject(i); JSONObject groupAdminObject = groupAdminsObject.getJSONObject("group"); group.getGroupAdmins().add(parseGroup(groupAdminObject)); } } // userAdmins if (groupObject.has("userAdmins")) { JSONArray userAdminsArray = groupObject.getJSONArray("userAdmins"); for (int i = 0; i < userAdminsArray.length(); i++) { JSONObject userAdminObject = userAdminsArray.getJSONObject(i); JSONObject userObject = userAdminObject.getJSONObject("user"); group.getUserAdmins().add(JsonUserReader.parseUser(userObject)); } } return group; } } projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonGroupWriter.java +26 −123 Original line number Diff line number Diff line Loading @@ -68,16 +68,13 @@ */ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.AC; import ca.nrc.cadc.ac.Group; import ca.nrc.cadc.ac.GroupProperty; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.WriterException; import ca.nrc.cadc.date.DateUtil; import ca.nrc.cadc.ac.xml.GroupWriter; import ca.nrc.cadc.util.StringBuilderWriter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ca.nrc.cadc.xml.JsonOutputter; import org.jdom2.Document; import org.jdom2.Element; import java.io.BufferedWriter; import java.io.IOException; Loading @@ -85,10 +82,8 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.security.Principal; import java.text.DateFormat; public class JsonGroupWriter public class JsonGroupWriter extends GroupWriter { /** * Write a Group to a StringBuilder. Loading @@ -97,7 +92,7 @@ public class JsonGroupWriter * @throws IOException * @throws WriterException */ public static void write(Group group, StringBuilder builder) public void write(Group group, StringBuilder builder) throws IOException, WriterException { write(group, new StringBuilderWriter(builder)); Loading @@ -111,7 +106,8 @@ public class JsonGroupWriter * @throws IOException if the writer fails to write. * @throws WriterException */ public static void write(Group group, OutputStream out) @Override public void write(Group group, OutputStream out) throws IOException, WriterException { OutputStreamWriter outWriter; Loading @@ -134,7 +130,8 @@ public class JsonGroupWriter * @throws IOException if the writer fails to write. * @throws WriterException */ public static void write(Group group, Writer writer) @Override public void write(Group group, Writer writer) throws IOException, WriterException { if (group == null) Loading @@ -142,116 +139,22 @@ public class JsonGroupWriter throw new WriterException("null group"); } try { getGroupObject(group).write(writer); } catch (JSONException e) { final String error = "Unable to create JSON for Group " + " because " + e.getMessage(); throw new WriterException(error, e); } } /** * * @param group * @return * @throws WriterException */ public static JSONObject getGroupObject(Group group) throws WriterException, JSONException { return getGroupObject(group, true); } public static JSONObject getGroupObject(Group group, boolean deepCopy) throws WriterException, JSONException { JSONObject groupObject = new JSONObject(); groupObject.put("uri", AC.GROUP_URI + group.getID()); // Group owner if (group.getOwner() != null) { groupObject.put("owner", JsonUserWriter.getUserObject(group.getOwner())); } if (deepCopy) { // Group description if (group.description != null) { groupObject.put("description", group.description); } // lastModified if (group.lastModified != null) { DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC); groupObject.put("lastModified", df.format(group.lastModified)); } Element children = GroupWriter.getGroupElement(group); Element groupElement = new Element("group"); groupElement.addContent(children); Document document = new Document(); document.setRootElement(groupElement); // Group properties if (!group.getProperties().isEmpty()) { JSONArray propertiesArray = new JSONArray(); for (GroupProperty property : group.getProperties()) { JSONObject propertyObject = new JSONObject(); propertyObject.put("property", JsonGroupPropertyWriter.write(property)); propertiesArray.put(propertyObject); } groupObject.put("properties", propertiesArray); } // Group groupMembers. if ((group.getGroupMembers() != null) && (!group.getGroupMembers().isEmpty())) { JSONArray groupMembersArray = new JSONArray(); for (Group groupMember : group.getGroupMembers()) { groupMembersArray.put(getGroupObject(groupMember, false)); } groupObject.put("groupMembers", groupMembersArray); } // Group userMembers if ((group.getUserMembers() != null) && (!group.getUserMembers().isEmpty())) { JSONArray userMembersArray = new JSONArray(); for (User<? extends Principal> userMember : group.getUserMembers()) { userMembersArray.put(JsonUserWriter.getUserObject(userMember)); } groupObject.put("userMembers", userMembersArray); } // Group groupAdmins. if ((group.getGroupAdmins() != null) && (!group.getGroupAdmins().isEmpty())) { JSONArray groupAdminsArray = new JSONArray(); for (Group groupAdmin : group.getGroupAdmins()) { groupAdminsArray.put(getGroupObject(groupAdmin, false)); } groupObject.put("groupAdmins", groupAdminsArray); } // Group userAdmins if ((group.getUserAdmins() != null) && (!group.getUserAdmins().isEmpty())) { JSONArray userAdminsArray = new JSONArray(); for (User<? extends Principal> userAdmin : group.getUserAdmins()) { userAdminsArray.put(JsonUserWriter.getUserObject(userAdmin)); } groupObject.put("userAdmins", userAdminsArray); } } JsonOutputter jsonOutputter = new JsonOutputter(); jsonOutputter.getListElementNames().add("properties"); jsonOutputter.getListElementNames().add("userMembers"); jsonOutputter.getListElementNames().add("groupMembers"); jsonOutputter.getListElementNames().add("userAdmins"); jsonOutputter.getListElementNames().add("groupAdmins"); jsonOutputter.getListElementNames().add("identities"); jsonOutputter.getListElementNames().add("details"); return new JSONObject().put("group", groupObject); jsonOutputter.output(document, writer); } } projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonUserReader.java +17 −45 Original line number Diff line number Diff line Loading @@ -70,10 +70,10 @@ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.UserDetails; import org.json.JSONArray; import ca.nrc.cadc.ac.xml.UserReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; Loading @@ -81,7 +81,7 @@ import java.io.Reader; import java.security.Principal; import java.util.Scanner; public class JsonUserReader public class JsonUserReader extends UserReader { /** * Construct a User from a InputStream. Loading @@ -91,7 +91,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(InputStream in) @Override public User<Principal> read(InputStream in) throws IOException { if (in == null) Loading @@ -113,7 +114,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(Reader reader) @Override public User<Principal> read(Reader reader) throws IOException { if (reader == null) Loading @@ -135,7 +137,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(String json) @Override public User<Principal> read(String json) throws IOException { if (json == null || json.isEmpty()) Loading @@ -146,7 +149,12 @@ public class JsonUserReader // Create a JSONObject from the JSON try { return parseUser(new JSONObject(json).getJSONObject("user")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("details", "userDetails"); Document document = jsonInputter.input(json); return UserReader.parseUser(document.getRootElement()); } catch (JSONException e) { Loading @@ -156,40 +164,4 @@ public class JsonUserReader } } protected static User<Principal> parseUser(JSONObject userObject) throws ReaderException, JSONException { JSONObject userIDObject = userObject.getJSONObject("userID"); JSONObject userIDIdentityObject = userIDObject.getJSONObject("identity"); Principal userID = JsonIdentityReader.read(userIDIdentityObject); User<Principal> user = new User<Principal>(userID); // identities if (userObject.has("identities")) { JSONArray identitiesArray = userObject.getJSONArray("identities"); for (int i = 0; i < identitiesArray.length(); i++) { JSONObject identitiesObject = identitiesArray.getJSONObject(i); JSONObject identityObject = identitiesObject.getJSONObject(("identity")); user.getIdentities().add(JsonIdentityReader.read(identityObject)); } } // details if (userObject.has("details")) { JSONArray detailsArray = userObject.getJSONArray("details"); for (int i = 0; i < detailsArray.length(); i++) { JSONObject detailsObject = detailsArray.getJSONObject(i); JSONObject userDetailsObject = detailsObject.getJSONObject(UserDetails.NAME); user.details.add(JsonUserDetailsReader.read(userDetailsObject)); } } return user; } } projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonUserRequestReader.java +43 −44 Original line number Diff line number Diff line Loading @@ -69,46 +69,20 @@ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.UserRequest; import ca.nrc.cadc.ac.xml.UserRequestReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.*; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.security.Principal; import java.util.Scanner; public class JsonUserRequestReader public class JsonUserRequestReader extends UserRequestReader { /** * Construct a UserRequest from an JSON String source. * * @param json String of the JSON. * @return UserRequest UserRequest. * @throws IOException */ public static UserRequest<Principal> read(String json) throws IOException { if (json == null) { throw new IllegalArgumentException("JSON must not be null"); } else { try { return parseUserRequest(new JSONObject(json)); } catch (JSONException e) { String error = "Unable to parse JSON to User because " + e.getMessage(); throw new ReaderException(error, e); } } } /** * Construct a User from a InputStream. * Loading @@ -117,7 +91,8 @@ public class JsonUserRequestReader * @throws ReaderException * @throws IOException */ public static UserRequest<Principal> read(InputStream in) @Override public UserRequest<Principal> read(InputStream in) throws IOException { if (in == null) Loading @@ -139,7 +114,8 @@ public class JsonUserRequestReader * @throws ReaderException * @throws IOException */ public static UserRequest<Principal> read(Reader reader) @Override public UserRequest<Principal> read(Reader reader) throws IOException { if (reader == null) Loading @@ -153,16 +129,39 @@ public class JsonUserRequestReader return read(json); } protected static UserRequest<Principal> parseUserRequest( JSONObject userRequestObject) throws ReaderException, JSONException /** * Construct a UserRequest from an JSON String source. * * @param json String of the JSON. * @return UserRequest UserRequest. * @throws IOException */ @Override public UserRequest<Principal> read(String json) throws IOException { if (json == null) { throw new IllegalArgumentException("JSON must not be null"); } else { try { final User<Principal> user = JsonUserReader.parseUser( userRequestObject.getJSONObject("user")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("details", "userDetails"); return new UserRequest<Principal>(user, userRequestObject. getString("password").toCharArray()); Document document = jsonInputter.input(json); return UserRequestReader.parseUserRequest(document.getRootElement()); } catch (JSONException e) { String error = "Unable to parse JSON to User because " + e.getMessage(); throw new ReaderException(error, e); } } } } Loading
projects/cadcAccessControl/build.xml +1 −1 Original line number Diff line number Diff line Loading @@ -125,7 +125,7 @@ <pathelement path="${build}/test/class"/> <pathelement path="${testingJars}"/> </classpath> <test name="ca.nrc.cadc.ac.json.GroupReaderWriterTest" /> <test name="ca.nrc.cadc.ac.json.JsonGroupReaderWriterTest" /> <formatter type="plain" usefile="false" /> </junit> </target> Loading
projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonGroupReader.java +21 −126 Original line number Diff line number Diff line Loading @@ -68,26 +68,20 @@ */ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.AC; import ca.nrc.cadc.ac.Group; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.date.DateUtil; import org.json.JSONArray; import ca.nrc.cadc.ac.xml.GroupReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URISyntaxException; import java.security.Principal; import java.text.DateFormat; import java.text.ParseException; import java.util.Scanner; public class JsonGroupReader public class JsonGroupReader extends GroupReader { /** * Construct a Group from a InputStream. Loading @@ -96,9 +90,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(InputStream in) @Override public Group read(InputStream in) throws ReaderException, IOException { if (in == null) Loading @@ -120,9 +114,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(Reader reader) @Override public Group read(Reader reader) throws ReaderException, IOException { if (reader == null) Loading @@ -143,9 +137,9 @@ public class JsonGroupReader * @return Group Group. * @throws ReaderException * @throws IOException * @throws URISyntaxException */ public static Group read(String json) @Override public Group read(String json) throws ReaderException, IOException { if (json == null) Loading @@ -156,7 +150,17 @@ public class JsonGroupReader // Create a JSONObject from the JSON try { return parseGroup(new JSONObject(json).getJSONObject("group")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("properties", "property"); jsonInputter.getListElementMap().put("details", "userDetails"); jsonInputter.getListElementMap().put("groupMembers", "group"); jsonInputter.getListElementMap().put("groupAdmins", "group"); jsonInputter.getListElementMap().put("userMembers", "user"); jsonInputter.getListElementMap().put("userAdmins", "user"); Document document = jsonInputter.input(json); return GroupReader.parseGroup(document.getRootElement()); } catch (JSONException e) { Loading @@ -166,113 +170,4 @@ public class JsonGroupReader } } protected static Group parseGroup(JSONObject groupObject) throws ReaderException, JSONException { String uri = groupObject.getString("uri"); // Group groupID int index = uri.indexOf(AC.GROUP_URI); if (index == -1) { String error = "group uri attribute malformed: " + uri; throw new ReaderException(error); } String groupID = uri.substring(AC.GROUP_URI.length()); // Group owner User<? extends Principal> user = null; if (groupObject.has("owner")) { JSONObject ownerObject = groupObject.getJSONObject("owner"); JSONObject userObject = ownerObject.getJSONObject("user"); user = JsonUserReader.parseUser(userObject); } Group group = new Group(groupID, user); // description if (groupObject.has("description")) { group.description = groupObject.getString("description"); } // lastModified if (groupObject.has("lastModified")) { try { DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC); group.lastModified = df.parse(groupObject.getString("lastModified")); } catch (ParseException e) { String error = "Unable to parse group lastModified because " + e.getMessage(); throw new ReaderException(error); } } // properties if (groupObject.has("description")) { JSONArray propertiesArray = groupObject.getJSONArray("properties"); for (int i = 0; i < propertiesArray.length(); i++) { JSONObject propertiesObject = propertiesArray.getJSONObject(i); JSONObject propertyObject = propertiesObject.getJSONObject("property"); group.getProperties().add(JsonGroupPropertyReader.read(propertyObject)); } } // groupMembers if (groupObject.has("groupMembers")) { JSONArray groupMembersArray = groupObject.getJSONArray("groupMembers"); for (int i = 0; i < groupMembersArray.length(); i++) { JSONObject groupMembersObject = groupMembersArray.getJSONObject(i); JSONObject groupMemberObject = groupMembersObject.getJSONObject("group"); group.getGroupMembers().add(parseGroup(groupMemberObject)); } } // userMembers if (groupObject.has("userMembers")) { JSONArray userMembersArray = groupObject.getJSONArray("userMembers"); for (int i = 0; i < userMembersArray.length(); i++) { JSONObject userMemberObject = userMembersArray.getJSONObject(i); JSONObject userObject = userMemberObject.getJSONObject("user"); group.getUserMembers().add(JsonUserReader.parseUser(userObject)); } } // groupAdmins if (groupObject.has("groupAdmins")) { JSONArray groupAdminsArray = groupObject.getJSONArray("groupAdmins"); for (int i = 0; i < groupAdminsArray.length(); i++) { JSONObject groupAdminsObject = groupAdminsArray.getJSONObject(i); JSONObject groupAdminObject = groupAdminsObject.getJSONObject("group"); group.getGroupAdmins().add(parseGroup(groupAdminObject)); } } // userAdmins if (groupObject.has("userAdmins")) { JSONArray userAdminsArray = groupObject.getJSONArray("userAdmins"); for (int i = 0; i < userAdminsArray.length(); i++) { JSONObject userAdminObject = userAdminsArray.getJSONObject(i); JSONObject userObject = userAdminObject.getJSONObject("user"); group.getUserAdmins().add(JsonUserReader.parseUser(userObject)); } } return group; } }
projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonGroupWriter.java +26 −123 Original line number Diff line number Diff line Loading @@ -68,16 +68,13 @@ */ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.AC; import ca.nrc.cadc.ac.Group; import ca.nrc.cadc.ac.GroupProperty; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.WriterException; import ca.nrc.cadc.date.DateUtil; import ca.nrc.cadc.ac.xml.GroupWriter; import ca.nrc.cadc.util.StringBuilderWriter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ca.nrc.cadc.xml.JsonOutputter; import org.jdom2.Document; import org.jdom2.Element; import java.io.BufferedWriter; import java.io.IOException; Loading @@ -85,10 +82,8 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.security.Principal; import java.text.DateFormat; public class JsonGroupWriter public class JsonGroupWriter extends GroupWriter { /** * Write a Group to a StringBuilder. Loading @@ -97,7 +92,7 @@ public class JsonGroupWriter * @throws IOException * @throws WriterException */ public static void write(Group group, StringBuilder builder) public void write(Group group, StringBuilder builder) throws IOException, WriterException { write(group, new StringBuilderWriter(builder)); Loading @@ -111,7 +106,8 @@ public class JsonGroupWriter * @throws IOException if the writer fails to write. * @throws WriterException */ public static void write(Group group, OutputStream out) @Override public void write(Group group, OutputStream out) throws IOException, WriterException { OutputStreamWriter outWriter; Loading @@ -134,7 +130,8 @@ public class JsonGroupWriter * @throws IOException if the writer fails to write. * @throws WriterException */ public static void write(Group group, Writer writer) @Override public void write(Group group, Writer writer) throws IOException, WriterException { if (group == null) Loading @@ -142,116 +139,22 @@ public class JsonGroupWriter throw new WriterException("null group"); } try { getGroupObject(group).write(writer); } catch (JSONException e) { final String error = "Unable to create JSON for Group " + " because " + e.getMessage(); throw new WriterException(error, e); } } /** * * @param group * @return * @throws WriterException */ public static JSONObject getGroupObject(Group group) throws WriterException, JSONException { return getGroupObject(group, true); } public static JSONObject getGroupObject(Group group, boolean deepCopy) throws WriterException, JSONException { JSONObject groupObject = new JSONObject(); groupObject.put("uri", AC.GROUP_URI + group.getID()); // Group owner if (group.getOwner() != null) { groupObject.put("owner", JsonUserWriter.getUserObject(group.getOwner())); } if (deepCopy) { // Group description if (group.description != null) { groupObject.put("description", group.description); } // lastModified if (group.lastModified != null) { DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC); groupObject.put("lastModified", df.format(group.lastModified)); } Element children = GroupWriter.getGroupElement(group); Element groupElement = new Element("group"); groupElement.addContent(children); Document document = new Document(); document.setRootElement(groupElement); // Group properties if (!group.getProperties().isEmpty()) { JSONArray propertiesArray = new JSONArray(); for (GroupProperty property : group.getProperties()) { JSONObject propertyObject = new JSONObject(); propertyObject.put("property", JsonGroupPropertyWriter.write(property)); propertiesArray.put(propertyObject); } groupObject.put("properties", propertiesArray); } // Group groupMembers. if ((group.getGroupMembers() != null) && (!group.getGroupMembers().isEmpty())) { JSONArray groupMembersArray = new JSONArray(); for (Group groupMember : group.getGroupMembers()) { groupMembersArray.put(getGroupObject(groupMember, false)); } groupObject.put("groupMembers", groupMembersArray); } // Group userMembers if ((group.getUserMembers() != null) && (!group.getUserMembers().isEmpty())) { JSONArray userMembersArray = new JSONArray(); for (User<? extends Principal> userMember : group.getUserMembers()) { userMembersArray.put(JsonUserWriter.getUserObject(userMember)); } groupObject.put("userMembers", userMembersArray); } // Group groupAdmins. if ((group.getGroupAdmins() != null) && (!group.getGroupAdmins().isEmpty())) { JSONArray groupAdminsArray = new JSONArray(); for (Group groupAdmin : group.getGroupAdmins()) { groupAdminsArray.put(getGroupObject(groupAdmin, false)); } groupObject.put("groupAdmins", groupAdminsArray); } // Group userAdmins if ((group.getUserAdmins() != null) && (!group.getUserAdmins().isEmpty())) { JSONArray userAdminsArray = new JSONArray(); for (User<? extends Principal> userAdmin : group.getUserAdmins()) { userAdminsArray.put(JsonUserWriter.getUserObject(userAdmin)); } groupObject.put("userAdmins", userAdminsArray); } } JsonOutputter jsonOutputter = new JsonOutputter(); jsonOutputter.getListElementNames().add("properties"); jsonOutputter.getListElementNames().add("userMembers"); jsonOutputter.getListElementNames().add("groupMembers"); jsonOutputter.getListElementNames().add("userAdmins"); jsonOutputter.getListElementNames().add("groupAdmins"); jsonOutputter.getListElementNames().add("identities"); jsonOutputter.getListElementNames().add("details"); return new JSONObject().put("group", groupObject); jsonOutputter.output(document, writer); } }
projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonUserReader.java +17 −45 Original line number Diff line number Diff line Loading @@ -70,10 +70,10 @@ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.UserDetails; import org.json.JSONArray; import ca.nrc.cadc.ac.xml.UserReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; Loading @@ -81,7 +81,7 @@ import java.io.Reader; import java.security.Principal; import java.util.Scanner; public class JsonUserReader public class JsonUserReader extends UserReader { /** * Construct a User from a InputStream. Loading @@ -91,7 +91,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(InputStream in) @Override public User<Principal> read(InputStream in) throws IOException { if (in == null) Loading @@ -113,7 +114,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(Reader reader) @Override public User<Principal> read(Reader reader) throws IOException { if (reader == null) Loading @@ -135,7 +137,8 @@ public class JsonUserReader * @throws ReaderException * @throws IOException */ public static User<Principal> read(String json) @Override public User<Principal> read(String json) throws IOException { if (json == null || json.isEmpty()) Loading @@ -146,7 +149,12 @@ public class JsonUserReader // Create a JSONObject from the JSON try { return parseUser(new JSONObject(json).getJSONObject("user")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("details", "userDetails"); Document document = jsonInputter.input(json); return UserReader.parseUser(document.getRootElement()); } catch (JSONException e) { Loading @@ -156,40 +164,4 @@ public class JsonUserReader } } protected static User<Principal> parseUser(JSONObject userObject) throws ReaderException, JSONException { JSONObject userIDObject = userObject.getJSONObject("userID"); JSONObject userIDIdentityObject = userIDObject.getJSONObject("identity"); Principal userID = JsonIdentityReader.read(userIDIdentityObject); User<Principal> user = new User<Principal>(userID); // identities if (userObject.has("identities")) { JSONArray identitiesArray = userObject.getJSONArray("identities"); for (int i = 0; i < identitiesArray.length(); i++) { JSONObject identitiesObject = identitiesArray.getJSONObject(i); JSONObject identityObject = identitiesObject.getJSONObject(("identity")); user.getIdentities().add(JsonIdentityReader.read(identityObject)); } } // details if (userObject.has("details")) { JSONArray detailsArray = userObject.getJSONArray("details"); for (int i = 0; i < detailsArray.length(); i++) { JSONObject detailsObject = detailsArray.getJSONObject(i); JSONObject userDetailsObject = detailsObject.getJSONObject(UserDetails.NAME); user.details.add(JsonUserDetailsReader.read(userDetailsObject)); } } return user; } }
projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/JsonUserRequestReader.java +43 −44 Original line number Diff line number Diff line Loading @@ -69,46 +69,20 @@ package ca.nrc.cadc.ac.json; import ca.nrc.cadc.ac.ReaderException; import ca.nrc.cadc.ac.User; import ca.nrc.cadc.ac.UserRequest; import ca.nrc.cadc.ac.xml.UserRequestReader; import ca.nrc.cadc.xml.JsonInputter; import org.jdom2.Document; import org.json.JSONException; import org.json.JSONObject; import java.io.*; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.security.Principal; import java.util.Scanner; public class JsonUserRequestReader public class JsonUserRequestReader extends UserRequestReader { /** * Construct a UserRequest from an JSON String source. * * @param json String of the JSON. * @return UserRequest UserRequest. * @throws IOException */ public static UserRequest<Principal> read(String json) throws IOException { if (json == null) { throw new IllegalArgumentException("JSON must not be null"); } else { try { return parseUserRequest(new JSONObject(json)); } catch (JSONException e) { String error = "Unable to parse JSON to User because " + e.getMessage(); throw new ReaderException(error, e); } } } /** * Construct a User from a InputStream. * Loading @@ -117,7 +91,8 @@ public class JsonUserRequestReader * @throws ReaderException * @throws IOException */ public static UserRequest<Principal> read(InputStream in) @Override public UserRequest<Principal> read(InputStream in) throws IOException { if (in == null) Loading @@ -139,7 +114,8 @@ public class JsonUserRequestReader * @throws ReaderException * @throws IOException */ public static UserRequest<Principal> read(Reader reader) @Override public UserRequest<Principal> read(Reader reader) throws IOException { if (reader == null) Loading @@ -153,16 +129,39 @@ public class JsonUserRequestReader return read(json); } protected static UserRequest<Principal> parseUserRequest( JSONObject userRequestObject) throws ReaderException, JSONException /** * Construct a UserRequest from an JSON String source. * * @param json String of the JSON. * @return UserRequest UserRequest. * @throws IOException */ @Override public UserRequest<Principal> read(String json) throws IOException { if (json == null) { throw new IllegalArgumentException("JSON must not be null"); } else { try { final User<Principal> user = JsonUserReader.parseUser( userRequestObject.getJSONObject("user")); JsonInputter jsonInputter = new JsonInputter(); jsonInputter.getListElementMap().put("identities", "identity"); jsonInputter.getListElementMap().put("details", "userDetails"); return new UserRequest<Principal>(user, userRequestObject. getString("password").toCharArray()); Document document = jsonInputter.input(json); return UserRequestReader.parseUserRequest(document.getRootElement()); } catch (JSONException e) { String error = "Unable to parse JSON to User because " + e.getMessage(); throw new ReaderException(error, e); } } } }