Skip to content
Snippets Groups Projects
Commit 67c5adc7 authored by Brian Major's avatar Brian Major
Browse files

s1666 - start of work to list all group names

parent d89a7a2c
No related branches found
No related tags found
No related merge requests found
...@@ -81,6 +81,16 @@ import ca.nrc.cadc.net.TransientException; ...@@ -81,6 +81,16 @@ import ca.nrc.cadc.net.TransientException;
public abstract interface GroupPersistence<T extends Principal> public abstract interface GroupPersistence<T extends Principal>
{ {
/**
* Get all group names.
*
* @return A collection of strings.
* @throws TransientException If an temporary, unexpected problem occurred.
* @throws AccessControlException If the operation is not permitted.
*/
public Collection<String> getGroupNames()
throws TransientException, AccessControlException;
/** /**
* Get the group with the given Group ID. * Get the group with the given Group ID.
* *
......
...@@ -305,6 +305,61 @@ public class LdapGroupDAO<T extends Principal> extends LdapDAO ...@@ -305,6 +305,61 @@ public class LdapGroupDAO<T extends Principal> extends LdapDAO
throw new RuntimeException("Unexpected LDAP exception", e); throw new RuntimeException("Unexpected LDAP exception", e);
} }
} }
/**
* Get all group names.
*
* @return A collection of strings
*
* @throws TransientException If an temporary, unexpected problem occurred.
*/
public Collection<String> getGroupNames() throws TransientException,
AccessControlException
{
try
{
Filter filter = null;
String [] attributes = new String[] {"cn", "nsaccountlock"};
SearchRequest searchRequest =
new SearchRequest(config.getGroupsDN(),
SearchScope.SUB, filter, attributes);
searchRequest.addControl(
new ProxiedAuthorizationV2RequestControl("dn:" +
getSubjectDN().toNormalizedString()));
SearchResult searchResult = null;
try
{
searchResult = getConnection().search(searchRequest);
}
catch (LDAPSearchException e)
{
if (e.getResultCode() == ResultCode.NO_SUCH_OBJECT)
{
logger.debug("Count not find groups root", e);
throw new IllegalStateException("Count not find groups root");
}
}
LdapDAO.checkLdapResult(searchResult.getResultCode());
List<String> groupNames = new ArrayList<String>();
for (SearchResultEntry next : searchResult.getSearchEntries())
{
groupNames.add(next.getAttributeValue("cn"));
}
return groupNames;
}
catch (LDAPException e1)
{
LdapDAO.checkLdapResult(e1.getResultCode());
throw new IllegalStateException("Unexpected exception: " + e1.getMatchedDN(), e1);
}
}
/** /**
* Get the group with the given Group ID. * Get the group with the given Group ID.
......
...@@ -93,7 +93,32 @@ public class LdapGroupPersistence<T extends Principal> ...@@ -93,7 +93,32 @@ public class LdapGroupPersistence<T extends Principal>
{ {
config = LdapConfig.getLdapConfig(); config = LdapConfig.getLdapConfig();
} }
public Collection<String> getGroupNames() throws TransientException,
AccessControlException
{
LdapGroupDAO<T> groupDAO = null;
LdapUserDAO<T> userDAO = null;
try
{
userDAO = new LdapUserDAO<T>(config);
groupDAO = new LdapGroupDAO<T>(config, userDAO);
Collection<String> ret = groupDAO.getGroupNames();
return ret;
}
finally
{
if (groupDAO != null)
{
groupDAO.close();
}
if (userDAO != null)
{
userDAO.close();
}
}
}
public Group getGroup(String groupName) public Group getGroup(String groupName)
throws GroupNotFoundException, TransientException, throws GroupNotFoundException, TransientException,
AccessControlException AccessControlException
......
...@@ -68,12 +68,15 @@ ...@@ -68,12 +68,15 @@
*/ */
package ca.nrc.cadc.ac.server.web; package ca.nrc.cadc.ac.server.web;
import ca.nrc.cadc.util.StringUtil;
import java.io.IOException; import java.io.IOException;
import java.net.URLDecoder; import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import ca.nrc.cadc.util.StringUtil;
public class GroupsActionFactory public class GroupsActionFactory
{ {
private static final Logger log = Logger.getLogger(GroupsActionFactory.class); private static final Logger log = Logger.getLogger(GroupsActionFactory.class);
...@@ -109,7 +112,7 @@ public class GroupsActionFactory ...@@ -109,7 +112,7 @@ public class GroupsActionFactory
{ {
if (method.equals("GET")) if (method.equals("GET"))
{ {
action = new ListGroupsAction(logInfo); action = new GetGroupNamesAction(logInfo);
} }
else if (method.equals("PUT")) else if (method.equals("PUT"))
{ {
......
...@@ -189,7 +189,7 @@ public class GroupActionFactoryTest ...@@ -189,7 +189,7 @@ public class GroupActionFactoryTest
} }
@Test @Test
public void testCreateListGroupsAction() public void testCreateGetGroupNamesAction()
{ {
try try
{ {
...@@ -199,7 +199,7 @@ public class GroupActionFactoryTest ...@@ -199,7 +199,7 @@ public class GroupActionFactoryTest
EasyMock.replay(request); EasyMock.replay(request);
GroupsAction action = GroupsActionFactory.getGroupsAction(request, null); GroupsAction action = GroupsActionFactory.getGroupsAction(request, null);
EasyMock.verify(request); EasyMock.verify(request);
Assert.assertTrue("Wrong action", action instanceof ListGroupsAction); Assert.assertTrue("Wrong action", action instanceof GetGroupNamesAction);
} }
catch (Throwable t) catch (Throwable t)
{ {
......
...@@ -68,9 +68,12 @@ ...@@ -68,9 +68,12 @@
*/ */
package ca.nrc.cadc.ac.client; package ca.nrc.cadc.ac.client;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
...@@ -79,6 +82,7 @@ import java.security.AccessControlContext; ...@@ -79,6 +82,7 @@ import java.security.AccessControlContext;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.security.AccessController; import java.security.AccessController;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -106,6 +110,8 @@ import ca.nrc.cadc.net.HttpPost; ...@@ -106,6 +110,8 @@ import ca.nrc.cadc.net.HttpPost;
import ca.nrc.cadc.net.HttpUpload; import ca.nrc.cadc.net.HttpUpload;
import ca.nrc.cadc.net.NetUtil; import ca.nrc.cadc.net.NetUtil;
import com.csvreader.CsvReader;
/** /**
* Client class for performing group searching and group actions * Client class for performing group searching and group actions
* with the access control web service. * with the access control web service.
...@@ -293,6 +299,78 @@ public class GMSClient ...@@ -293,6 +299,78 @@ public class GMSClient
throw new RuntimeException(bug); throw new RuntimeException(bug);
} }
} }
/**
* Get the all group names.
*
* @return The list of names.
* @throws AccessControlException If unauthorized to perform this operation.
* @throws java.io.IOException
*/
public List<String> getGroupNames()
throws AccessControlException, IOException
{
URL getGroupNamesURL = new URL(this.baseURL + "/groups");
log.debug("getGroupNames request to " + getGroupNamesURL.toString());
HttpURLConnection conn =
(HttpURLConnection) getGroupNamesURL.openConnection();
conn.setRequestMethod("GET");
SSLSocketFactory sf = getSSLSocketFactory();
if ((sf != null) && ((conn instanceof HttpsURLConnection)))
{
((HttpsURLConnection) conn)
.setSSLSocketFactory(sf);
}
int responseCode = -1;
try
{
responseCode = conn.getResponseCode();
}
catch(Exception e)
{
throw new AccessControlException(e.getMessage());
}
if (responseCode != 200)
{
String errMessage = NetUtil.getErrorBody(conn);
log.debug("deleteGroup response " + responseCode + ": " +
errMessage);
if ((responseCode == 401) || (responseCode == 403) ||
(responseCode == -1))
{
throw new AccessControlException(errMessage);
}
if (responseCode == 400)
{
throw new IllegalArgumentException(errMessage);
}
throw new IOException("HttpResponse (" + responseCode + ") - " + errMessage);
}
try
{
List<String> groupNames = new ArrayList<String>();
Reader ioReader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(ioReader);
CsvReader reader = new CsvReader(br);
for (int i=0; i<reader.getColumnCount(); i++)
{
groupNames.add(reader.get(i));
}
return groupNames;
}
catch (Exception bug)
{
log.error("Unexpected exception", bug);
throw new RuntimeException(bug);
}
}
/** /**
* Update a group. * Update a group.
...@@ -948,6 +1026,7 @@ public class GMSClient ...@@ -948,6 +1026,7 @@ public class GMSClient
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
{ {
this.sslSocketFactory = sslSocketFactory; this.sslSocketFactory = sslSocketFactory;
clearCache();
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment