Skip to content
GMSClient.java 44.4 KiB
Newer Older
    public Group getMembership(String groupName, Role role)
        throws UserNotFoundException, AccessControlException, IOException
            throw new IllegalArgumentException("groupName and role are required.");
        Principal userID = getCurrentUserID();
        if (userID != null)
            Group cachedGroup = getCachedGroup(userID, groupName, role);
            if (cachedGroup != null)
            {
                return cachedGroup;
            }
        //String idType = AuthenticationUtil.getPrincipalType(userID);
        //String id = userID.getName();
        String roleString = role.getValue();
        StringBuilder searchGroupURL = new StringBuilder(this.baseURL);
        searchGroupURL.append("/search?");
        //searchGroupURL.append("ID=").append(NetUtil.encode(id));
        //searchGroupURL.append("&IDTYPE=").append(NetUtil.encode(idType));
        searchGroupURL.append("&ROLE=").append(NetUtil.encode(roleString));
        searchGroupURL.append("&GROUPID=").append(NetUtil.encode(groupName));
        log.debug("getMembership request to " + searchGroupURL.toString());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        URL url = new URL(searchGroupURL.toString());
        HttpDownload transfer = new HttpDownload(url, out);

        transfer.setSSLSocketFactory(getSSLSocketFactory());
        transfer.run();

        Throwable error = transfer.getThrowable();
        if (error != null)
        {
            log.debug("getMembership throwable", error);
            // transfer returns a -1 code for anonymous access.
            if ((transfer.getResponseCode() == -1) ||
                (transfer.getResponseCode() == 401) ||
                (transfer.getResponseCode() == 403))
            {
                throw new AccessControlException(error.getMessage());
            }
            if (transfer.getResponseCode() == 404)
            {
                throw new UserNotFoundException(error.getMessage());
            }
            if (transfer.getResponseCode() == 400)
            {
                throw new IllegalArgumentException(error.getMessage());
            }
            throw new IOException(error);
        }

        try
        {
            String groupsXML = new String(out.toByteArray(), "UTF-8");
            log.debug("getMembership returned: " + groupsXML);
Jeff Burke's avatar
Jeff Burke committed
            GroupListReader groupListReader = new GroupListReader();
            List<Group> groups = groupListReader.read(groupsXML);
            {
                return null;
            }
            if (groups.size() == 1)
            {
                Group ret = groups.get(0);
                addCachedGroup(userID, ret, role);
                return ret;
            }
            throw new IllegalStateException(
                    "Duplicate membership for " + userID + " in group " + groupName);
        }
        catch (Exception bug)
        {
            log.error("Unexpected exception", bug);
            throw new RuntimeException(bug);
        }
    /**
     * Check group membership of the current Subject.
     * @param groupName
     * @return
     * @throws UserNotFoundException
     * @throws AccessControlException
Brian Major's avatar
Brian Major committed
     * @throws IOException
     */
    public boolean isMember(String groupName)
        throws UserNotFoundException, AccessControlException, IOException
    {
     * 
     * @param groupName
     * @param role
     * @return
     * @throws UserNotFoundException
     * @throws AccessControlException
     * @throws IOException 
    public boolean isMember(String groupName, Role role)
        throws UserNotFoundException, AccessControlException, IOException
        return isMember(getCurrentUserID(), groupName, role);
    private boolean isMember(Principal userID, String groupName, Role role)
        throws UserNotFoundException, AccessControlException, IOException
        Group group = getMembership(groupName, role);
        return group != null;
    }
Jeff Burke's avatar
Jeff Burke committed
    /**
     * @param sslSocketFactory the sslSocketFactory to set
     */
    public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
    {
        if (mySocketFactory != null)
            throw new IllegalStateException("Illegal use of GMSClient: "
                    + "cannot set SSLSocketFactory after using one created from Subject");
Jeff Burke's avatar
Jeff Burke committed
        this.sslSocketFactory = sslSocketFactory;
    private SSLSocketFactory getSSLSocketFactory()
    {
        AccessControlContext ac = AccessController.getContext();
        Subject s = Subject.getSubject(ac);
        // no real Subject: can only use the one from setSSLSocketFactory
        if (s == null || s.getPrincipals().isEmpty())
        {
            return sslSocketFactory;
        }
        // lazy init
        if (this.mySocketFactory == null)
        {
            log.debug("getSSLSocketFactory: " + s);
            this.mySocketFactory = SSLUtil.getSocketFactory(s);
            this.subjectHashCode = s.hashCode();
        }
        else
            int c = s.hashCode();
            if (c != subjectHashCode)
                throw new IllegalStateException("Illegal use of "
                        + this.getClass().getSimpleName()
                        + ": subject change not supported for internal SSLSocketFactory");
    protected void clearCache()
    {
        AccessControlContext acContext = AccessController.getContext();
        Subject subject = Subject.getSubject(acContext);
        if (subject != null)
        {
            subject.getPrivateCredentials().remove(new GroupMemberships());
    protected GroupMemberships getGroupCache(Principal userID)
    {
        AccessControlContext acContext = AccessController.getContext();
        Subject subject = Subject.getSubject(acContext);
        // only consult cache if the userID is of the calling subject
        if (userIsSubject(userID, subject))
            Set<GroupMemberships> gset = subject.getPrivateCredentials(GroupMemberships.class);
            if (gset == null || gset.isEmpty())
                GroupMemberships mems = new GroupMemberships(userID);
                subject.getPrivateCredentials().add(mems);
                return mems;
            GroupMemberships mems = gset.iterator().next();
            return mems;
    protected Group getCachedGroup(Principal userID, String groupID, Role role)
    {
        List<Group> groups = getCachedGroups(userID, role, false);
        if (groups == null)
            return null; // no cache
        for (Group g : groups)
        {
            if (g.getID().equals(groupID))
                return g;
        }
        return null;
    }
    protected List<Group> getCachedGroups(Principal userID, Role role, boolean complete)
    {
        GroupMemberships mems = getGroupCache(userID);
        if (mems == null)
            return null; // no cache
        Boolean cacheState = mems.isComplete(role);
        if (!complete || Boolean.TRUE.equals(cacheState))
    protected void addCachedGroup(Principal userID, Group group, Role role)
        GroupMemberships mems = getGroupCache(userID);
        if (mems == null)
            return; // no cache
Patrick Dowler's avatar
Patrick Dowler committed
        mems.add(group, role);
    protected void setCachedGroups(Principal userID, List<Group> groups, Role role)
        GroupMemberships mems = getGroupCache(userID);
        if (mems == null)
            return; // no cache
Patrick Dowler's avatar
Patrick Dowler committed
        mems.add(groups, role);
    protected boolean userIsSubject(Principal userID, Subject subject)
    {
        if (userID == null || subject == null)
        {
            return false;
        }
Dustin Jenkins's avatar
Dustin Jenkins committed
        for (Principal subjectPrincipal : subject.getPrincipals())
            if (AuthenticationUtil.equals(subjectPrincipal, userID))
        return false;