Commit 64e8f3a7 authored by Jeff Burke's avatar Jeff Burke
Browse files

s1890: change InternalID to take a URI instead of a UUID and string.

parent 24fbe061
Loading
Loading
Loading
Loading
+73 −43
Original line number Diff line number Diff line
/*
 ************************************************************************
 ****  C A N A D I A N   A S T R O N O M Y   D A T A   C E N T R E  *****
 *******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
 **************  CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES  **************
 *
 * (c) 2014.                            (c) 2014.
 *  (c) 2016.                            (c) 2016.
 *  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 reserves
 *  All rights reserved                  Tous droits réservés
 *
 * NRC disclaims any warranties         Le CNRC denie toute garantie
 * expressed, implied, or statu-        enoncee, implicite ou legale,
 * tory, of any kind with respect       de quelque nature que se soit,
 * to the software, including           concernant le logiciel, y com-
 * without limitation any war-          pris sans restriction toute
 * ranty of merchantability or          garantie de valeur marchande
 * fitness for a particular pur-        ou de pertinence pour un usage
 * pose.  NRC shall not be liable       particulier.  Le CNRC ne
 * in any event for any damages,        pourra en aucun cas etre tenu
 * whether direct or indirect,          responsable de tout dommage,
 * special or general, consequen-       direct ou indirect, particul-
 * tial or incidental, arising          ier ou general, accessoire ou
 * from the use of the software.        fortuit, resultant de l'utili-
 *                                      sation du logiciel.
 *  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.
 *
 * @author adriand
 *  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.
 *
 * @version $Revision: $
 *  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 $
 *
 ****  C A N A D I A N   A S T R O N O M Y   D A T A   C E N T R E  *****
 ************************************************************************
 */

package ca.nrc.cadc.ac;

import java.net.URI;
import java.util.UUID;

/**
@@ -42,36 +78,32 @@ import java.util.UUID;
 */
public class InternalID
{
    private UUID id;
    private String authority;
    private URI uri;
    private UUID uuid;

    /**
     * Ctor
     * @param id unique identifier
     * @param uri unique identifier
     */
    public InternalID(UUID id, String authority)
    {
        if (id == null)
    public InternalID(URI uri)
    {
            throw new IllegalArgumentException("id is null");
        }
        if (authority == null || authority.isEmpty())
        if (uri == null)
        {
            throw new IllegalArgumentException("authority is null or empty");
            throw new IllegalArgumentException("uri is null");
        }

        this.id = id;
        this.authority = authority;
        this.uri = uri;
        uuid = UUID.fromString(uri.getQuery());
    }

    public UUID getId()
    public URI getURI()
    {
        return id;
        return uri;
    }

    public String getAuthority()
    public UUID getUUID()
    {
        return authority;
        return uuid;
    }

    /* (non-Javadoc)
@@ -82,8 +114,7 @@ public class InternalID
    {
        int prime = 31;
        int result = 1;
        result = prime * result + id.hashCode();
        result = prime * result + authority.toLowerCase().hashCode();
        result = prime * result + uri.hashCode();
        return result;
    }

@@ -106,8 +137,7 @@ public class InternalID
            return false;
        }
        InternalID other = (InternalID) obj;
        if (id.equals(other.id) &&
            authority.equalsIgnoreCase(other.authority))
        if (uri.equals(other.uri))
        {
            return true;
        }
@@ -117,7 +147,7 @@ public class InternalID
    @Override
    public String toString()
    {
        return getClass().getSimpleName() + "[" + id + "," + authority + "]";
        return getClass().getSimpleName() + "[" + uri + "]";
    }

}
+37 −0
Original line number Diff line number Diff line
@@ -68,6 +68,9 @@
 */
package ca.nrc.cadc.ac;

import ca.nrc.cadc.auth.HttpPrincipal;
import org.json.HTTP;

import java.security.Principal;
import java.util.Date;
import java.util.HashSet;
@@ -101,6 +104,40 @@ public class User
        return identities;
    }

    public Principal getPrincipal(Class clazz)
    {
        for (Principal principal : getIdentities())
        {
            if (principal.getClass().equals(clazz))
            {
                return principal;
            }
        }
        return null;
    }

    public HttpPrincipal getHttpPrincipal()
    {
        Principal principal = getPrincipal(HttpPrincipal.class);
        if (principal != null)
        {
            return (HttpPrincipal) principal;
        }
        return null;
    }

//    public <S extends Principal> S getIdentity(Class<S> clazz)
//    {
//        for (Principal principal : getIdentities())
//        {
//            if (principal.getClass() == clazz)
//            {
//                return (S) principal;
//            }
//        }
//        return null;
//    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
+4 −0
Original line number Diff line number Diff line
@@ -84,6 +84,10 @@ public class UserRequest
        {
            throw new IllegalArgumentException("null or empty password");
        }
        if (user.getIdentities().isEmpty())
        {
            throw new IllegalArgumentException("user has no identities");
        }
        this.user = user;
        this.password = password;
    }
+22 −23
Original line number Diff line number Diff line
@@ -95,6 +95,8 @@ import javax.security.auth.x500.X500Principal;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.Principal;
import java.text.DateFormat;
import java.text.ParseException;
@@ -658,7 +660,7 @@ public abstract class AbstractReaderWriter

        // identities
        Set<Principal> identities = user.getIdentities();
        if (identities.size() > 1) // includes alternate identities
        if (!identities.isEmpty()) // includes alternate identities
        {
            Element identitiesElement = new Element(IDENTITIES);
            for (Principal identity : identities)
@@ -731,15 +733,10 @@ public abstract class AbstractReaderWriter
        // Create the internalID Element.
        Element internalIDElement = new Element(INTERNAL_ID);

        // id element
        Element idElement = new Element(ID);
        idElement.addContent(internalID.getId().toString());
        internalIDElement.addContent(idElement);

        // authority element
        Element authorityElement = new Element(AUTHORITY);
        authorityElement.setText(internalID.getAuthority());
        internalIDElement.addContent(authorityElement);
        // uri element
        Element uriElement = new Element(URI);
        uriElement.addContent(internalID.getURI().toString());
        internalIDElement.addContent(uriElement);

        return internalIDElement;
    }
@@ -1057,31 +1054,33 @@ public abstract class AbstractReaderWriter
    private void setInternalID(User user, Element element)
        throws ReaderException
    {
        Element idElement = element.getChild(ID);
        if (idElement == null)
        Element uriElement = element.getChild(URI);
        if (uriElement == null)
        {
            String error = "expected id element not found in internalID element";
            String error = "expected uri element not found in internalID element";
            throw new ReaderException(error);
        }
        String id = idElement.getText();
        UUID uuid = UUID.fromString(id);

        Element authorityElement = element.getChild(AUTHORITY);
        if (authorityElement == null)
        String text = uriElement.getText();
        URI uri;
        try
        {
            String error = "expected authority element not found in internalID element";
            throw new ReaderException(error);
            uri = new URI(text);
        }
        catch (URISyntaxException e)
        {
            throw new ReaderException("Invalid InternalID URI " + text, e);
        }
        String authority = authorityElement.getText();

        InternalID internalID = new InternalID(uuid, authority);
        InternalID internalID = new InternalID(uri);

        // set private id field using reflection
        // set private uri field using reflection
        try
        {
            Field field = user.getClass().getDeclaredField(ID);
            field.setAccessible(true);
            field.set(user, internalID);


        }
        catch (NoSuchFieldException e)
        {
+4 −1
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@

package ca.nrc.cadc.ac;

import ca.nrc.cadc.auth.HttpPrincipal;
import org.apache.log4j.Logger;
import org.junit.Test;

@@ -82,7 +83,9 @@ public class UserRequestTest
    @Test
    public void simpleEqualityTests() throws Exception
    {
        UserRequest ur1 = new UserRequest(new User(), "password".toCharArray());
        User user = new User();
        user.getIdentities().add(new HttpPrincipal("foo"));
        UserRequest ur1 = new UserRequest(user, "password".toCharArray());
        UserRequest ur2 = ur1;
        assertEquals(ur1, ur2);
        assertEquals(ur1.getUser(), ur2.getUser());
Loading