Commit 70c7ea2b authored by Dustin Jenkins's avatar Dustin Jenkins
Browse files

Story 1848: JSON fixes for inputter.

parent 5fe594dd
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -223,10 +223,10 @@ public class GMSClient implements TransferListener
     * @return List of HTTP Principal users.
     * @throws IOException Any errors in reading.
     */
    public List<User<HttpPrincipal>> getDisplayUsers() throws IOException
    public List<User<? extends Principal>> getDisplayUsers() throws IOException
    {
        final List<User<HttpPrincipal>> webUsers =
                new ArrayList<User<HttpPrincipal>>();
        final List<User<? extends Principal>> webUsers =
                new ArrayList<User<? extends Principal>>();
        final HttpDownload httpDownload =
                    createDisplayUsersHTTPDownload(webUsers);

@@ -272,7 +272,7 @@ public class GMSClient implements TransferListener
     * @throws IOException      Any writing/reading errors.
     */
    HttpDownload createDisplayUsersHTTPDownload(
            final List<User<HttpPrincipal>> webUsers) throws IOException
            final List<User<? extends Principal>> webUsers) throws IOException
    {
        final URL usersListURL = new URL(this.baseURL + "/users");
        return new HttpDownload(usersListURL,
+7 −55
Original line number Diff line number Diff line
@@ -68,29 +68,23 @@

package ca.nrc.cadc.ac.client;

import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.ac.json.JsonUserListReader;
import ca.nrc.cadc.net.InputStreamWrapper;
import ca.nrc.cadc.util.StringUtil;
import org.apache.log4j.Logger;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.Principal;
import java.util.List;


public class JsonUserListInputStreamWrapper implements InputStreamWrapper
{
    private static final Logger LOGGER = Logger
            .getLogger(JsonUserListInputStreamWrapper.class);
    private final List<User<HttpPrincipal>> output;
    private final List<User<? extends Principal>> output;


    public JsonUserListInputStreamWrapper(
        final List<User<HttpPrincipal>> output)
            final List<User<? extends Principal>> output)
    {
        this.output = output;
    }
@@ -105,50 +99,8 @@ public class JsonUserListInputStreamWrapper implements InputStreamWrapper
    @Override
    public void read(final InputStream inputStream) throws IOException
    {
        String line = null;

        try
        {
            final InputStreamReader inReader =
                    new InputStreamReader(inputStream);
            final BufferedReader reader = new BufferedReader(inReader);

            while (StringUtil.hasText(line = reader.readLine()))
            {
                // Deal with arrays stuff.
                while (line.startsWith("[") || line.startsWith(","))
                {
                    line = line.substring(1);
                }

                while (line.endsWith("]") || line.endsWith(","))
                {
                    line = line.substring(0, (line.length() - 1));
                }
        final JsonUserListReader reader = new JsonUserListReader();

                if (StringUtil.hasText(line))
                {
                    LOGGER.debug(String.format("Reading: %s", line));

                    final JSONObject jsonObject = new JSONObject(line);
                    final User<HttpPrincipal> webUser =
                            new User<HttpPrincipal>(
                                    new HttpPrincipal(jsonObject
                                                              .getString("id")));
                    final String firstName = jsonObject.getString("firstName");
                    final String lastName = jsonObject.getString("lastName");

                    webUser.details
                            .add(new PersonalDetails(firstName, lastName));

                    output.add(webUser);
                }
            }
        }
        catch (Exception bug)
        {
            throw new IOException(bug + (StringUtil.hasText(line)
                                         ? "Error line is " + line : ""));
        }
        output.addAll(reader.read(inputStream));
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -76,7 +76,6 @@ import org.jdom2.Document;
import org.json.JSONException;

import java.io.Reader;
import java.net.URISyntaxException;
import java.security.Principal;
import java.util.List;
import java.util.Scanner;
@@ -93,11 +92,10 @@ public class JsonUserListReader extends UserListReader
     * @param reader Reader.
     * @return users List of Users.
     * @throws ReaderException
     * @throws URISyntaxException
     */
    @Override
    public List<User<Principal>> read(Reader reader)
        throws URISyntaxException, ReaderException
        throws ReaderException
    {
        if (reader == null)
        {
@@ -112,6 +110,7 @@ public class JsonUserListReader extends UserListReader
            JsonInputter jsonInputter = new JsonInputter();
            jsonInputter.getListElementMap().put("identities", "identity");
            jsonInputter.getListElementMap().put("details", "userDetails");
            jsonInputter.getListElementMap().put("users", "user");

            Document document = jsonInputter.input(json);
            return getUserList(document.getRootElement());
+3 −5
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ public class UserListReader extends AbstractReaderWriter
     * @throws java.net.URISyntaxException
     */
    public List<User<Principal>> read(InputStream in)
        throws ReaderException, IOException, URISyntaxException
        throws ReaderException, IOException
    {
        if (in == null)
        {
@@ -146,10 +146,9 @@ public class UserListReader extends AbstractReaderWriter
     * @return List of Users.
     * @throws ReaderException
     * @throws java.io.IOException
     * @throws java.net.URISyntaxException
     */
    public List<User<Principal>> read(Reader reader)
        throws ReaderException, IOException, URISyntaxException
        throws ReaderException, IOException
    {
        if (reader == null)
        {
@@ -185,11 +184,10 @@ public class UserListReader extends AbstractReaderWriter
     *
     * @param element The Users JDOM element.
     * @return A List of User objects.
     * @throws URISyntaxException
     * @throws ReaderException
     */
    protected final List<User<Principal>> getUserList(Element element)
        throws URISyntaxException, ReaderException
        throws ReaderException
    {
        List<User<Principal>> users = new ArrayList<User<Principal>>();

+2 −1
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ package ca.nrc.cadc.ac.client;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.Principal;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
@@ -129,7 +130,7 @@ public class GMSClientTest
                {
                    @Override
                    HttpDownload createDisplayUsersHTTPDownload(
                            List<User<HttpPrincipal>> webUsers) throws IOException
                            List<User<? extends Principal>> webUsers) throws IOException
                    {
                        return mockHTTPDownload;
                    }
Loading