Skip to content
Snippets Groups Projects
Commit 096426f3 authored by Sara Bertocco's avatar Sara Bertocco
Browse files

Groups list support added

parent 6012aba8
No related branches found
No related tags found
No related merge requests found
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ca.nrc.cadc.ac.server.web.groups;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.xml.GroupListWriter;
import ca.nrc.cadc.ac.xml.GroupWriter;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.log4j.Logger;
/**
*
* @author bertocco
*/
public class GetGroupsListAction extends AbstractGroupAction {
private static final Logger log = Logger.getLogger(GetGroupsListAction.class);
GetGroupsListAction()
{
super();
}
public void doAction() throws Exception
{
Collection<String> groupNames = groupPersistence.getGroupNames();
Collection<Group> groups = new ArrayList<Group>();
log.debug("Found " + groupNames.size() + " group names");
Group group = new Group();
syncOut.setHeader("Content-Type", "application/xml");
GroupListWriter groupListWriter = new GroupListWriter();
for (final String currentGroup : groupNames)
{
try {
group = groupPersistence.getGroup(currentGroup);
groups.add(group);
} catch (AccessControlException ace) {
// The user can read only groups of which is member or owner
log.info("User can not read group " + currentGroup);
}
}
groupListWriter.write(groups, syncOut.getWriter());
}
}
...@@ -109,12 +109,16 @@ public abstract class GroupsActionFactory ...@@ -109,12 +109,16 @@ public abstract class GroupsActionFactory
{ {
action = new GetGroupNamesAction(); action = new GetGroupNamesAction();
} }
else if (segments.length == 1) else if ((segments.length == 1) && (segments[0].equals("list")))
{
action = new GetGroupsListAction();
}
else if ((segments.length == 1) && (!segments[0].equals("list")))
{ {
String groupName = segments[0]; String groupName = segments[0];
action = new GetGroupAction(groupName); action = new GetGroupAction(groupName);
} }
if (action != null) if (action != null)
{ {
log.debug("Returning action: " + action.getClass()); log.debug("Returning action: " + action.getClass());
......
...@@ -165,9 +165,55 @@ public class GMSClient implements TransferListener ...@@ -165,9 +165,55 @@ public class GMSClient implements TransferListener
* *
* @return The list of groups. * @return The list of groups.
*/ */
public List<Group> getGroups() public List<Group> getGroups() throws GroupNotFoundException, IOException
{ {
throw new UnsupportedOperationException("Not yet implemented");
URL groupsURL = lookupServiceURL(Standards.GMS_GROUPS_01);
URL getGroupListURL = new URL(groupsURL.toExternalForm() + "/list");
log.debug("getGroup request to " + getGroupListURL.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpDownload transfer = new HttpDownload(getGroupListURL, out);
transfer.setSSLSocketFactory(getSSLSocketFactory());
transfer.run();
Throwable error = transfer.getThrowable();
if (error != null)
{
log.debug("getGroup throwable (" + transfer.getResponseCode() + ")", 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() == 400)
{
throw new IllegalArgumentException(error.getMessage());
}
if (transfer.getResponseCode() == 404)
{
throw new GroupNotFoundException(error.getMessage());
}
throw new IOException(error);
}
try
{
String groupsXML = new String(out.toByteArray(), "UTF-8");
log.debug("getGroups returned: " + groupsXML);
GroupListReader groupListReader = new GroupListReader();
List<Group> groupsList = groupListReader.read(groupsXML);
return groupListReader.read(groupsXML);
}
catch (Exception bug)
{
log.error("Unexpected exception", bug);
throw new RuntimeException(bug);
}
} }
/** /**
......
...@@ -112,6 +112,8 @@ public class Main implements PrivilegedAction<Object> ...@@ -112,6 +112,8 @@ public class Main implements PrivilegedAction<Object>
public static final String ARG_USERID = "userid"; public static final String ARG_USERID = "userid";
public static final String ARG_GROUP = "group"; public static final String ARG_GROUP = "group";
public static final String ARG_LIST = "list";
public static final String ARG_HELP = "help"; public static final String ARG_HELP = "help";
public static final String ARG_VERBOSE = "verbose"; public static final String ARG_VERBOSE = "verbose";
public static final String ARG_DEBUG = "debug"; public static final String ARG_DEBUG = "debug";
...@@ -186,6 +188,9 @@ public class Main implements PrivilegedAction<Object> ...@@ -186,6 +188,9 @@ public class Main implements PrivilegedAction<Object>
if (argMap.isSet(ARG_DEL_ADMIN)) if (argMap.isSet(ARG_DEL_ADMIN))
return ARG_DEL_ADMIN; return ARG_DEL_ADMIN;
if (argMap.isSet(ARG_LIST))
return ARG_LIST;
throw new IllegalArgumentException("No valid commands"); throw new IllegalArgumentException("No valid commands");
} }
...@@ -195,6 +200,8 @@ public class Main implements PrivilegedAction<Object> ...@@ -195,6 +200,8 @@ public class Main implements PrivilegedAction<Object>
System.out.println("--get --group=<uri>"); System.out.println("--get --group=<uri>");
System.out.println("--delete --group=<uri>"); System.out.println("--delete --group=<uri>");
System.out.println(); System.out.println();
System.out.println("--list --group=<uri>");
System.out.println();
System.out.println("--add-member --group=<uri> --userid=<u>"); System.out.println("--add-member --group=<uri> --userid=<u>");
System.out.println("--remove-member --group=<uri> --userid=<u>"); System.out.println("--remove-member --group=<uri> --userid=<u>");
System.out.println(); System.out.println();
...@@ -362,6 +369,13 @@ public class Main implements PrivilegedAction<Object> ...@@ -362,6 +369,13 @@ public class Main implements PrivilegedAction<Object>
client.deleteGroup(group); client.deleteGroup(group);
} }
else if (command.equals(ARG_LIST))
{
if (group == null)
throw new IllegalArgumentException("No group specified");
return client.getGroups();
}
return null; return null;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment