diff --git a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/UserPersistence.java b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/UserPersistence.java index 599f98586cf90640981da00f41f525f23974745e..6ee6c39295949ef969c3b58e7f235dfb94edf35c 100755 --- a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/UserPersistence.java +++ b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/UserPersistence.java @@ -80,6 +80,7 @@ import ca.nrc.cadc.net.TransientException; import com.unboundid.ldap.sdk.DN; + public interface UserPersistence<T extends Principal> { /** diff --git a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConfig.java b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConfig.java index 54c2676d46f9dc060d05de9b314ee99cebb4fc5c..e6c0d5c7dbf1a50a8a5238897a6b27b8e6eb12c6 100755 --- a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConfig.java +++ b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/ldap/LdapConfig.java @@ -128,7 +128,7 @@ public class LdapConfig public static LdapConfig getLdapConfig(final String ldapProperties) { - logger.debug("Reading LDAP properties from: " + ldapProperties); + logger.info("Reading LDAP properties from: " + ldapProperties); PropertiesReader pr = new PropertiesReader(ldapProperties); MultiValuedProperties config = pr.getAllProperties(); diff --git a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUserNamesAction.java b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUsersAction.java similarity index 84% rename from projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUserNamesAction.java rename to projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUsersAction.java index bd2e78594f9f05346d732e0aefc5fe4b4bf76a7b..41b7d96520dac85c26da35a8b96e7ec5bc447bf1 100644 --- a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUserNamesAction.java +++ b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/GetUsersAction.java @@ -69,19 +69,18 @@ package ca.nrc.cadc.ac.server.web.users; -import java.io.Writer; -import java.util.Collection; import org.apache.log4j.Logger; import ca.nrc.cadc.ac.server.UserPersistence; -public class GetUserNamesAction extends UsersAction + +public class GetUsersAction extends UsersAction { - private static final Logger log = Logger.getLogger(GetUserNamesAction.class); + private static final Logger log = Logger.getLogger(GetUsersAction.class); - GetUserNamesAction(UserLogInfo logInfo) + GetUsersAction(UserLogInfo logInfo) { super(logInfo); } @@ -89,23 +88,9 @@ public class GetUserNamesAction extends UsersAction public Object run() throws Exception { - UserPersistence userPersistence = getUserPersistence(); - Collection<String> users = userPersistence.getUserNames(); - log.debug("Found " + users.size() + " user names"); - response.setContentType("text/plain"); - log.debug("Set content-type to text/plain"); - Writer writer = response.getWriter(); - boolean start = true; - for (final String user : users) - { - if (!start) - { - writer.write("\r\n"); - } - writer.write(user); - start = false; - } - + final UserPersistence userPersistence = getUserPersistence(); + + writeUsers(userPersistence.getUserNames()); return null; } } diff --git a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersAction.java b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersAction.java index 3a83c96e67cbca4221a40787f16bd00d796be666..c9f97284278858c31beb1efa75c31a15c24a6699 100644 --- a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersAction.java +++ b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersAction.java @@ -75,6 +75,7 @@ import java.security.AccessControlException; import java.security.Principal; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.Collection; import javax.security.auth.Subject; import javax.servlet.http.HttpServletResponse; @@ -251,42 +252,33 @@ public abstract class UsersAction } /** - * Read a user from the HTTP Request's stream. + * Write a user to the response's writer. * - * @param inputStream The Input Stream to read from. - * @return User instance. - * @throws IOException Any reading error(s) + * @param user The user object to marshall and write out. + * @throws IOException Any writing errors. */ - protected final User<Principal> readUser( - final InputStream inputStream) throws IOException + protected final <T extends Principal> void writeUser(final User<T> user) + throws IOException { - final User<Principal> user; + response.setContentType(acceptedContentType); + final Writer writer = response.getWriter(); if (acceptedContentType.equals(DEFAULT_CONTENT_TYPE)) { - user = ca.nrc.cadc.ac.xml.UserReader.read(inputStream); + ca.nrc.cadc.ac.xml.UserWriter.write(user, writer); } else if (acceptedContentType.equals(JSON_CONTENT_TYPE)) { - user = ca.nrc.cadc.ac.json.UserReader.read(inputStream); - } - else - { - // Should never happen. - throw new IOException("Unknown content being asked for: " - + acceptedContentType); + ca.nrc.cadc.ac.json.UserWriter.write(user, writer); } - - return user; } /** - * Write a user to the response's writer. + * Write out a list of users as this Action's specified content type. * - * @param user The user object to marshall and write out. - * @throws IOException Any writing errors. + * @param users The Collection of user entries. */ - protected final <T extends Principal> void writeUser(final User<T> user) + protected final void writeUsers(final Collection<String> users) throws IOException { response.setContentType(acceptedContentType); @@ -294,11 +286,11 @@ public abstract class UsersAction if (acceptedContentType.equals(DEFAULT_CONTENT_TYPE)) { - ca.nrc.cadc.ac.xml.UserWriter.write(user, writer); + ca.nrc.cadc.ac.xml.UsersWriter.write(users, writer); } else if (acceptedContentType.equals(JSON_CONTENT_TYPE)) { - ca.nrc.cadc.ac.json.UserWriter.write(user, writer); + ca.nrc.cadc.ac.json.UsersWriter.write(users, writer); } } } diff --git a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersActionFactory.java b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersActionFactory.java index d1f1a9b4f5fd65f36d5bc8cadb214cc89c15359a..37ddf3638cae55e5556f6c10afbeecf00bea82f1 100644 --- a/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersActionFactory.java +++ b/projects/cadcAccessControl-Server/src/ca/nrc/cadc/ac/server/web/users/UsersActionFactory.java @@ -124,7 +124,7 @@ public class UsersActionFactory { if (method.equals("GET")) { - action = new GetUserNamesAction(logInfo); + action = new GetUsersAction(logInfo); } else if (method.equals("PUT")) { diff --git a/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUsersActionTest.java b/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUsersActionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..36f11a496be001d2898e098d898565ffd9e7f637 --- /dev/null +++ b/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUsersActionTest.java @@ -0,0 +1,208 @@ +/* + ************************************************************************ + ******************* CANADIAN ASTRONOMY DATA CENTRE ******************* + ************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES ************** + * + * (c) 2014. (c) 2014. + * Government of Canada Gouvernement du Canada + * National Research Council Conseil national de recherches + * Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6 + * All rights reserved Tous droits réservés + * + * NRC disclaims any warranties, Le CNRC dénie toute garantie + * expressed, implied, or énoncée, implicite ou légale, + * statutory, of any kind with de quelque nature que ce + * respect to the software, soit, concernant le logiciel, + * including without limitation y compris sans restriction + * any warranty of merchantability toute garantie de valeur + * or fitness for a particular marchande ou de pertinence + * purpose. NRC shall not be pour un usage particulier. + * liable in any event for any Le CNRC ne pourra en aucun cas + * damages, whether direct or être tenu responsable de tout + * indirect, special or general, dommage, direct ou indirect, + * consequential or incidental, particulier ou général, + * arising from the use of the accessoire ou fortuit, résultant + * software. Neither the name de l'utilisation du logiciel. Ni + * of the National Research le nom du Conseil National de + * Council of Canada nor the Recherches du Canada ni les noms + * names of its contributors may de ses participants ne peuvent + * be used to endorse or promote être utilisés pour approuver ou + * products derived from this promouvoir les produits dérivés + * software without specific prior de ce logiciel sans autorisation + * written permission. préalable et particulière + * par écrit. + * + * This file is part of the Ce fichier fait partie du projet + * OpenCADC project. OpenCADC. + * + * OpenCADC is free software: OpenCADC est un logiciel libre ; + * you can redistribute it and/or vous pouvez le redistribuer ou le + * modify it under the terms of modifier suivant les termes de + * the GNU Affero General Public la “GNU Affero General Public + * License as published by the License” telle que publiée + * Free Software Foundation, par la Free Software Foundation + * either version 3 of the : soit la version 3 de cette + * License, or (at your option) licence, soit (à votre gré) + * any later version. toute version ultérieure. + * + * OpenCADC is distributed in the OpenCADC est distribué + * hope that it will be useful, dans l’espoir qu’il vous + * but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE + * without even the implied GARANTIE : sans même la garantie + * warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ + * or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF + * PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence + * General Public License for Générale Publique GNU Affero + * more details. pour plus de détails. + * + * You should have received Vous devriez avoir reçu une + * a copy of the GNU Affero copie de la Licence Générale + * General Public License along Publique GNU Affero avec + * with OpenCADC. If not, see OpenCADC ; si ce n’est + * <http://www.gnu.org/licenses/>. pas le cas, consultez : + * <http://www.gnu.org/licenses/>. + * + * $Revision: 4 $ + * + ************************************************************************ + */ +package ca.nrc.cadc.ac.server.web.users; + + +import ca.nrc.cadc.ac.server.UserPersistence; +import ca.nrc.cadc.auth.HttpPrincipal; +import org.apache.log4j.Level; + +import org.json.JSONArray; + +import ca.nrc.cadc.util.Log4jInit; + +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collection; + +import static org.easymock.EasyMock.*; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import org.skyscreamer.jsonassert.JSONAssert; + +/** + * + * @author adriand + */ +public class GetUsersActionTest +{ + @BeforeClass + public static void setUpClass() + { + Log4jInit.setLevel("ca.nrc.cadc.ac", Level.INFO); + } + + @Test + @SuppressWarnings("unchecked") + public void testWriteUsersJSON() throws Exception + { + final HttpServletResponse mockResponse = + createMock(HttpServletResponse.class); + final UserPersistence<HttpPrincipal> mockUserPersistence = + createMock(UserPersistence.class); + final Collection<String> userEntries = new ArrayList<String>(); + + for (int i = 1; i <= 13; i++) + { + userEntries.add("USER_" + i); + } + + final GetUsersAction testSubject = new GetUsersAction(null) + { + @Override + UserPersistence<HttpPrincipal> getUserPersistence() + { + return mockUserPersistence; + } + }; + + testSubject.setAcceptedContentType(UsersAction.JSON_CONTENT_TYPE); + + final Writer writer = new StringWriter(); + final PrintWriter printWriter = new PrintWriter(writer); + + expect(mockUserPersistence.getUserNames()).andReturn( + userEntries).once(); + expect(mockResponse.getWriter()).andReturn(printWriter).once(); + mockResponse.setContentType("application/json"); + expectLastCall().once(); + + replay(mockResponse, mockUserPersistence); + testSubject.doAction(null, mockResponse); + + final JSONArray expected = + new JSONArray("['USER_1','USER_2','USER_3','USER_4','USER_5','USER_6','USER_7','USER_8','USER_9','USER_10','USER_11','USER_12','USER_13']"); + final JSONArray result = new JSONArray(writer.toString()); + + JSONAssert.assertEquals(expected, result, true); + verify(mockResponse, mockUserPersistence); + } + + @Test + @SuppressWarnings("unchecked") + public void testWriteUsersXML() throws Exception + { + final HttpServletResponse mockResponse = + createMock(HttpServletResponse.class); + final UserPersistence<HttpPrincipal> mockUserPersistence = + createMock(UserPersistence.class); + final Collection<String> userEntries = new ArrayList<String>(); + + for (int i = 1; i <= 13; i++) + { + userEntries.add("USER_" + i); + } + + final GetUsersAction testSubject = new GetUsersAction(null) + { + @Override + UserPersistence<HttpPrincipal> getUserPersistence() + { + return mockUserPersistence; + } + }; + + final Writer writer = new StringWriter(); + final PrintWriter printWriter = new PrintWriter(writer); + + expect(mockUserPersistence.getUserNames()).andReturn( + userEntries).once(); + expect(mockResponse.getWriter()).andReturn(printWriter).once(); + mockResponse.setContentType("text/xml"); + expectLastCall().once(); + + replay(mockResponse, mockUserPersistence); + testSubject.doAction(null, mockResponse); + + final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + + "<users>\r\n" + + " <USER_1 />\r\n" + + " <USER_2 />\r\n" + + " <USER_3 />\r\n" + + " <USER_4 />\r\n" + + " <USER_5 />\r\n" + + " <USER_6 />\r\n" + + " <USER_7 />\r\n" + + " <USER_8 />\r\n" + + " <USER_9 />\r\n" + + " <USER_10 />\r\n" + + " <USER_11 />\r\n" + + " <USER_12 />\r\n" + + " <USER_13 />\r\n" + + "</users>\r\n"; + final String result = writer.toString(); + + assertEquals("Wrong XML", expected, result); + verify(mockResponse, mockUserPersistence); + } +} diff --git a/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/UserActionFactoryTest.java b/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/UserActionFactoryTest.java index 3c78050fc4be572407d879036d8e63a039867da3..4241c015a6a3f7a2d21b0a0e41f0fac1ff37d45b 100644 --- a/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/UserActionFactoryTest.java +++ b/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/UserActionFactoryTest.java @@ -67,7 +67,6 @@ package ca.nrc.cadc.ac.server.web.users; -import ca.nrc.cadc.ac.server.web.RemoveUserMemberAction; import ca.nrc.cadc.util.Log4jInit; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Level; @@ -160,7 +159,7 @@ public class UserActionFactoryTest EasyMock.replay(request); UsersAction action = UsersActionFactory.getUsersAction(request, null); EasyMock.verify(request); - Assert.assertTrue("Wrong action", action instanceof GetUserNamesAction); + Assert.assertTrue("Wrong action", action instanceof GetUsersAction); } catch (Throwable t) { diff --git a/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUserIDsActionTest.java b/projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/UsersWriter.java similarity index 58% rename from projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUserIDsActionTest.java rename to projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/UsersWriter.java index 1113657209b897c5032234fe3b7acfd19822b41b..cd9894453959f894a6886627433a224ffd805078 100644 --- a/projects/cadcAccessControl-Server/test/src/ca/nrc/cadc/ac/server/web/users/GetUserIDsActionTest.java +++ b/projects/cadcAccessControl/src/ca/nrc/cadc/ac/json/UsersWriter.java @@ -3,7 +3,7 @@ ******************* CANADIAN ASTRONOMY DATA CENTRE ******************* ************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES ************** * - * (c) 2014. (c) 2014. + * (c) 2015. (c) 2015. * Government of Canada Gouvernement du Canada * National Research Council Conseil national de recherches * Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6 @@ -62,105 +62,53 @@ * <http://www.gnu.org/licenses/>. pas le cas, consultez : * <http://www.gnu.org/licenses/>. * - * $Revision: 4 $ * ************************************************************************ */ -package ca.nrc.cadc.ac.server.web.users; -import static org.junit.Assert.fail; +package ca.nrc.cadc.ac.json; -import java.io.PrintWriter; -import java.security.Principal; -import java.util.ArrayList; -import java.util.Collection; - -import javax.servlet.http.HttpServletResponse; +import org.json.JSONException; +import org.json.JSONWriter; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.easymock.EasyMock; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; -import ca.nrc.cadc.ac.server.GroupPersistence; -import ca.nrc.cadc.ac.server.UserPersistence; -import ca.nrc.cadc.ac.server.web.GetGroupNamesAction; -import ca.nrc.cadc.ac.server.web.GroupLogInfo; -import ca.nrc.cadc.auth.HttpPrincipal; -import ca.nrc.cadc.util.Log4jInit; -import ca.nrc.cadc.uws.server.SyncOutput; /** - * - * @author adriand + * Class to write out, as JSON, a list of user entries. */ -public class GetUserIDsActionTest +public class UsersWriter { - private final static Logger log = Logger.getLogger(GetUserIDsActionTest.class); - - @BeforeClass - public static void setUpClass() + public static void write(final Collection<String> users, + final Writer writer) throws IOException { - Log4jInit.setLevel("ca.nrc.cadc.ac", Level.INFO); - } + final JSONWriter jsonWriter = new JSONWriter(writer); - @Test - @Ignore - public void testRun() throws Exception - { - /* try { - Collection<HttpPrincipal> userIDs = new ArrayList<HttpPrincipal>(); - userIDs.add(new HttpPrincipal("foo")); - userIDs.add(new HttpPrincipal("bar")); - - final UserPersistence mockPersistence = EasyMock.createMock(UserPersistence.class); - EasyMock.expect(mockPersistence.getCadcIDs()).andReturn(userIDs).once(); - - final PrintWriter mockWriter = EasyMock.createMock(PrintWriter.class); - mockWriter.write("foo", 0, 3); - EasyMock.expectLastCall(); - mockWriter.write(44); - EasyMock.expectLastCall(); - mockWriter.write("bar", 0, 3); - EasyMock.expectLastCall(); - mockWriter.write("\n"); - EasyMock.expectLastCall(); - - final SyncOutput mockSyncOutput = - EasyMock.createMock(SyncOutput.class); + jsonWriter.array(); - mockSyncOutput.setHeader("Content-Type", "text/csv"); - - final HttpServletResponse mockResponse = EasyMock.createMock(HttpServletResponse.class); - mockResponse.setContentType("text/csv"); - EasyMock.expectLastCall(); - EasyMock.expect(mockResponse.getWriter()).andReturn(mockWriter).once(); - - UserLogInfo mockLog = EasyMock.createMock(UserLogInfo.class); - - EasyMock.replay(mockPersistence, mockWriter, mockResponse, mockLog); - - GetUserIDsAction action = new GetUserIDsAction(mockLog) + for (final String s : users) { - @Override - <T extends Principal> UserPersistence<T> getUserPersistence() - { - return mockPersistence; - }; - }; - - action.run(); + jsonWriter.value(s); + } } - catch (Throwable t) + catch (JSONException e) { - log.error(t.getMessage(), t); - fail("unexpected error: " + t.getMessage()); + throw new IOException(e); + } + finally + { + try + { + jsonWriter.endArray(); + } + catch (JSONException e) + { + throw new IOException(e); + } } - */ } - } diff --git a/projects/cadcAccessControl/src/ca/nrc/cadc/ac/xml/UsersWriter.java b/projects/cadcAccessControl/src/ca/nrc/cadc/ac/xml/UsersWriter.java new file mode 100644 index 0000000000000000000000000000000000000000..7a0e356fab53fe79b256df5e3e5b28b0154e7928 --- /dev/null +++ b/projects/cadcAccessControl/src/ca/nrc/cadc/ac/xml/UsersWriter.java @@ -0,0 +1,106 @@ +/* + ************************************************************************ + ******************* CANADIAN ASTRONOMY DATA CENTRE ******************* + ************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES ************** + * + * (c) 2015. (c) 2015. + * Government of Canada Gouvernement du Canada + * National Research Council Conseil national de recherches + * Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6 + * All rights reserved Tous droits réservés + * + * NRC disclaims any warranties, Le CNRC dénie toute garantie + * expressed, implied, or énoncée, implicite ou légale, + * statutory, of any kind with de quelque nature que ce + * respect to the software, soit, concernant le logiciel, + * including without limitation y compris sans restriction + * any warranty of merchantability toute garantie de valeur + * or fitness for a particular marchande ou de pertinence + * purpose. NRC shall not be pour un usage particulier. + * liable in any event for any Le CNRC ne pourra en aucun cas + * damages, whether direct or être tenu responsable de tout + * indirect, special or general, dommage, direct ou indirect, + * consequential or incidental, particulier ou général, + * arising from the use of the accessoire ou fortuit, résultant + * software. Neither the name de l'utilisation du logiciel. Ni + * of the National Research le nom du Conseil National de + * Council of Canada nor the Recherches du Canada ni les noms + * names of its contributors may de ses participants ne peuvent + * be used to endorse or promote être utilisés pour approuver ou + * products derived from this promouvoir les produits dérivés + * software without specific prior de ce logiciel sans autorisation + * written permission. préalable et particulière + * par écrit. + * + * This file is part of the Ce fichier fait partie du projet + * OpenCADC project. OpenCADC. + * + * OpenCADC is free software: OpenCADC est un logiciel libre ; + * you can redistribute it and/or vous pouvez le redistribuer ou le + * modify it under the terms of modifier suivant les termes de + * the GNU Affero General Public la “GNU Affero General Public + * License as published by the License” telle que publiée + * Free Software Foundation, par la Free Software Foundation + * either version 3 of the : soit la version 3 de cette + * License, or (at your option) licence, soit (à votre gré) + * any later version. toute version ultérieure. + * + * OpenCADC is distributed in the OpenCADC est distribué + * hope that it will be useful, dans l’espoir qu’il vous + * but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE + * without even the implied GARANTIE : sans même la garantie + * warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ + * or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF + * PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence + * General Public License for Générale Publique GNU Affero + * more details. pour plus de détails. + * + * You should have received Vous devriez avoir reçu une + * a copy of the GNU Affero copie de la Licence Générale + * General Public License along Publique GNU Affero avec + * with OpenCADC. If not, see OpenCADC ; si ce n’est + * <http://www.gnu.org/licenses/>. pas le cas, consultez : + * <http://www.gnu.org/licenses/>. + * + * + ************************************************************************ + */ + +package ca.nrc.cadc.ac.xml; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; + +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; + +public class UsersWriter +{ + /** + * Write the Collection of String entries as XML. + * + * @param users The Collection of User strings. + * @param writer The Writer to output to. + * @throws IOException Any writing errors. + */ + public static void write(final Collection<String> users, + final Writer writer) throws IOException + { + // Create the root users Element. + final Element usersElement = new Element("users"); + + for (final String s : users) + { + final Element userEntryElement = new Element(s); + usersElement.addContent(userEntryElement); + } + + final XMLOutputter output = new XMLOutputter(); + + output.setFormat(Format.getPrettyFormat()); + output.output(new Document(usersElement), writer); + } +}