Merge pull request #1396 from Georgetown-University-Libraries/DS-3140

DS-3140 METSRightsCrosswalk NPE During AIP Restore - No Anonymous Read
This commit is contained in:
Tim Donohue
2016-07-15 10:25:00 -05:00
committed by GitHub

View File

@@ -475,179 +475,136 @@ public class METSRightsCrosswalk
{ {
//get what class of context this is //get what class of context this is
String contextClass = element.getAttributeValue("CONTEXTCLASS"); String contextClass = element.getAttributeValue("CONTEXTCLASS");
if ((element.getAttributeValue("start-date") != null) ResourcePolicy rp = resourcePolicyService.create(context);
|| (element.getAttributeValue("end-date") != null) SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
|| (element.getAttributeValue("rpName") != null))
{ // get reference to the <Permissions> element
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); // Note: we are assuming here that there will only ever be ONE <Permissions>
try { // element. Currently there are no known use cases for multiple.
ResourcePolicy rp = resourcePolicyService.create(context); Element permsElement = element.getChild("Permissions", METSRights_NS);
if (element.getAttributeValue("CONTEXTCLASS").equalsIgnoreCase("GENERAL PUBLIC")) { if(permsElement == null) {
Group anonGroup = groupService.findByName(context, Group.ANONYMOUS); log.error("No <Permissions> element was found. Skipping this <Context> element.");
rp.setGroup(anonGroup); continue;
}
else
{
if (element.getAttributeValue("CONTEXTCLASS").equalsIgnoreCase("REPOSITORY MGR")) {
Group adminGroup = groupService.findByName(context, Group.ADMIN);
rp.setGroup(adminGroup);
}
}
if (element.getAttributeValue("rpName") != null)
{
rp.setRpName(element.getAttributeValue("rpName"));
}
try {
if (element.getAttributeValue("start-date") != null)
{
rp.setStartDate(sdf.parse(element.getAttributeValue("start-date")));
}
if (element.getAttributeValue("end-date") != null)
{
rp.setEndDate(sdf.parse(element.getAttributeValue("end-date")));
}
}catch (ParseException ex) {
java.util.logging.Logger.getLogger(METSRightsCrosswalk.class.getName()).log(Level.SEVERE, null, ex);
}
List<Element> le = new ArrayList<Element>(element.getChildren());
for (Element el : le)
{
if ((el.getAttributeValue("DISCOVER").equalsIgnoreCase("true"))
&& (el.getAttributeValue("DISPLAY").equalsIgnoreCase("true")))
{
if (el.getAttributeValue("DELETE").equalsIgnoreCase("false"))
{
if (el.getAttributeValue("MODIFY").equalsIgnoreCase("false"))
{
rp.setAction(Constants.READ);
}
else
{
rp.setAction(Constants.WRITE);
}
}
else
{
if (el.getAttributeValue("MODIFY").equalsIgnoreCase("true"))
{
rp.setAction(Constants.DELETE);
if ((el.getAttributeValue("COPY").equalsIgnoreCase("true"))
&&(el.getAttributeValue("DUPLICATE").equalsIgnoreCase("true"))
&&(el.getAttributeValue("PRINT").equalsIgnoreCase("true")))
{
rp.setAction(Constants.ADMIN);
}
}
}
}
}
policies.add(rp);
} catch (NullPointerException ex) {
java.util.logging.Logger.getLogger(METSRightsCrosswalk.class.getName()).log(Level.SEVERE, null, ex);
}
assignPermissions(context, dso, policies);
} }
else
if (element.getAttributeValue("rpName") != null)
{ {
//also get reference to the <Permissions> element rp.setRpName(element.getAttributeValue("rpName"));
Element permsElement = element.getChild("Permissions", METSRights_NS); }
try {
//Check if this permission pertains to Anonymous users if (element.getAttributeValue("start-date") != null)
if(ANONYMOUS_CONTEXTCLASS.equals(contextClass))
{ {
//get DSpace Anonymous group, ID=0 rp.setStartDate(sdf.parse(element.getAttributeValue("start-date")));
Group anonGroup = groupService.findByName(context, Group.ANONYMOUS); }
if(anonGroup==null) if (element.getAttributeValue("end-date") != null)
{
throw new CrosswalkInternalException("The DSpace database has not been properly initialized. The Anonymous Group is missing from the database.");
}
assignPermissions(context, dso, anonGroup, permsElement);
} // else if this permission declaration pertains to Administrators
else if(ADMIN_CONTEXTCLASS.equals(contextClass))
{ {
//get DSpace Administrator group, ID=1 rp.setEndDate(sdf.parse(element.getAttributeValue("end-date")));
Group adminGroup = groupService.findByName(context, Group.ADMIN); }
if(adminGroup==null) }catch (ParseException ex) {
{ log.error("Failed to parse embargo date. The date needs to be in the format 'yyyy-MM-dd'.", ex);
throw new CrosswalkInternalException("The DSpace database has not been properly initialized. The Administrator Group is missing from the database."); }
}
//Check if this permission pertains to Anonymous users
assignPermissions(context, dso, adminGroup, permsElement); if(ANONYMOUS_CONTEXTCLASS.equals(contextClass))
} // else if this permission pertains to another DSpace group {
else if(GROUP_CONTEXTCLASS.equals(contextClass)) //get DSpace Anonymous group, ID=0
Group anonGroup = groupService.findByName(context, Group.ANONYMOUS);
if(anonGroup==null)
{ {
try throw new CrosswalkInternalException("The DSpace database has not been properly initialized. The Anonymous Group is missing from the database.");
{ }
//we need to find the name of DSpace group it pertains to
//Get the text within the <UserName> child element,
// this is the group's name
String groupName = element.getChildTextTrim("UserName", METSRights_NS);
//Translate Group name back to internal ID format (e.g. COLLECTION_<ID>_ADMIN) rp.setGroup(anonGroup);
// from its external format (e.g. COLLECTION_<handle>_ADMIN) } // else if this permission declaration pertains to Administrators
groupName = PackageUtils.translateGroupNameForImport(context, groupName); else if(ADMIN_CONTEXTCLASS.equals(contextClass))
{
//Check if this group exists in DSpace already //get DSpace Administrator group, ID=1
Group group = groupService.findByName(context, groupName); Group adminGroup = groupService.findByName(context, Group.ADMIN);
if(adminGroup==null)
//if not found, throw an error -- user should restore group from the SITE AIP
if(group==null)
{
throw new CrosswalkInternalException("Cannot restore Group permissions on object ("
+ "type=" + Constants.typeText[dso.getType()] + ", "
+ "handle=" + dso.getHandle() + ", "
+ "ID=" + dso.getID()
+ "). The Group named '" + groupName + "' is missing from DSpace. "
+ "Please restore this group using the SITE AIP, or recreate it.");
}
//assign permissions to group on this object
assignPermissions(context, dso, group, permsElement);
}
catch(PackageException pe)
{
//A PackageException will only be thrown if translateDefaultGroupName() fails
//We'll just wrap it as a CrosswalkException and throw it upwards
throw new CrosswalkException(pe);
}
}//end if Group
else if(PERSON_CONTEXTCLASS.equals(contextClass))
{ {
//we need to find the person it pertains to throw new CrosswalkInternalException("The DSpace database has not been properly initialized. The Administrator Group is missing from the database.");
// Get the text within the <UserName> child element, }
// this is the person's email address
String personEmail = element.getChildTextTrim("UserName", METSRights_NS);
//Check if this person exists in DSpace already rp.setGroup(adminGroup);
EPerson person = ePersonService.findByEmail(context, personEmail); } // else if this permission pertains to another DSpace group
else if(GROUP_CONTEXTCLASS.equals(contextClass))
{
try
{
//we need to find the name of DSpace group it pertains to
//Get the text within the <UserName> child element,
// this is the group's name
String groupName = element.getChildTextTrim("UserName", METSRights_NS);
//If cannot find by email, try by netID //Translate Group name back to internal ID format (e.g. COLLECTION_<ID>_ADMIN)
//(though METSRights should contain email if it was exported by DSpace) // from its external format (e.g. COLLECTION_<handle>_ADMIN)
if(person==null) groupName = PackageUtils.translateGroupNameForImport(context, groupName);
//Check if this group exists in DSpace already
Group group = groupService.findByName(context, groupName);
//if not found, throw an error -- user should restore group from the SITE AIP
if(group==null)
{ {
person = ePersonService.findByNetid(context, personEmail); throw new CrosswalkInternalException("Cannot restore Group permissions on object ("
}
//if not found, throw an error -- user should restore person from the SITE AIP
if(person==null)
{
throw new CrosswalkInternalException("Cannot restore Person permissions on object ("
+ "type=" + Constants.typeText[dso.getType()] + ", " + "type=" + Constants.typeText[dso.getType()] + ", "
+ "handle=" + dso.getHandle() + ", " + "handle=" + dso.getHandle() + ", "
+ "ID=" + dso.getID() + "ID=" + dso.getID()
+ "). The Person with email/netid '" + personEmail + "' is missing from DSpace. " + "). The Group named '" + groupName + "' is missing from DSpace. "
+ "Please restore this Person object using the SITE AIP, or recreate it."); + "Please restore this group using the SITE AIP, or recreate it.");
} }
//assign permissions to person on this object //assign group to policy
assignPermissions(context, dso, person, permsElement); rp.setGroup(group);
}//end if Person }
else catch(PackageException pe)
log.error("Unrecognized CONTEXTCLASS: " + contextClass); {
//A PackageException will only be thrown if translateDefaultGroupName() fails
//We'll just wrap it as a CrosswalkException and throw it upwards
throw new CrosswalkException(pe);
}
}// else if this permission pertains to a DSpace person
else if(PERSON_CONTEXTCLASS.equals(contextClass))
{
//we need to find the person it pertains to
// Get the text within the <UserName> child element,
// this is the person's email address
String personEmail = element.getChildTextTrim("UserName", METSRights_NS);
//Check if this person exists in DSpace already
EPerson person = ePersonService.findByEmail(context, personEmail);
//If cannot find by email, try by netID
//(though METSRights should contain email if it was exported by DSpace)
if(person==null)
{
person = ePersonService.findByNetid(context, personEmail);
}
//if not found, throw an error -- user should restore person from the SITE AIP
if(person==null)
{
throw new CrosswalkInternalException("Cannot restore Person permissions on object ("
+ "type=" + Constants.typeText[dso.getType()] + ", "
+ "handle=" + dso.getHandle() + ", "
+ "ID=" + dso.getID()
+ "). The Person with email/netid '" + personEmail + "' is missing from DSpace. "
+ "Please restore this Person object using the SITE AIP, or recreate it.");
}
//assign person to the policy
rp.setEPerson(person);
}//end if Person
else {
log.error("Unrecognized CONTEXTCLASS: " + contextClass);
} }
//set permissions on policy and add to object
rp.setAction(parsePermissions(permsElement));
policies.add(rp);
assignPermissions(context, dso, policies);
} //end if "Context" element } //end if "Context" element
}//end while loop }//end while loop
} }