changes to bring SWORD into line with current working practices on the trunk

git-svn-id: http://scm.dspace.org/svn/repo/trunk@2911 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Richard Jones
2008-04-16 14:32:35 +00:00
parent 9c3f4c2dc3
commit 4ea0b2f94c
7 changed files with 169 additions and 246 deletions

View File

@@ -48,7 +48,9 @@ import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.handle.HandleManager; import org.dspace.uri.IdentifierService;
import org.dspace.uri.IdentifierException;
import org.dspace.uri.ResolvableIdentifier;
/** /**
* This class provides a single point of contact for * This class provides a single point of contact for
@@ -75,7 +77,7 @@ public class CollectionLocation
public String getLocation(Collection collection) public String getLocation(Collection collection)
throws DSpaceSWORDException throws DSpaceSWORDException
{ {
return this.getBaseUrl() + "/" + collection.getHandle(); return IdentifierService.getURL(this.getBaseUrl(), collection);
} }
/** /**
@@ -97,31 +99,23 @@ public class CollectionLocation
{ {
throw new DSpaceSWORDException("The deposit URL is incomplete"); throw new DSpaceSWORDException("The deposit URL is incomplete");
} }
String handle = location.substring(baseUrl.length());
if (handle.startsWith("/")) ResolvableIdentifier ri = IdentifierService.resolve(context, location);
{ DSpaceObject dso = (DSpaceObject) IdentifierService.getResource(context, ri);
handle = handle.substring(1);
}
if ("".equals(handle))
{
throw new DSpaceSWORDException("The deposit URL is incomplete");
}
DSpaceObject dso = HandleManager.resolveToObject(context, handle);
if (!(dso instanceof Collection)) if (!(dso instanceof Collection))
{ {
throw new DSpaceSWORDException("The deposit URL does not resolve to a valid collection"); throw new DSpaceSWORDException("The deposit URL does not resolve to a valid collection");
} }
return (Collection) dso; return (Collection) dso;
} }
catch (SQLException e) catch (IdentifierException e)
{ {
log.error("Caught exception:", e); log.error("Caught exception:", e);
throw new DSpaceSWORDException("There was a problem resolving the collection", e); throw new DSpaceSWORDException("There was a problem resolving the collection", e);
} }
} }
/** /**
* Get the base deposit URL for the DSpace SWORD implementation. This * Get the base deposit URL for the DSpace SWORD implementation. This

View File

@@ -47,7 +47,7 @@ import org.dspace.content.DCDate;
import org.dspace.content.DCValue; import org.dspace.content.DCValue;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.handle.HandleManager; import org.dspace.uri.IdentifierService;
import org.purl.sword.base.SWORDEntry; import org.purl.sword.base.SWORDEntry;
@@ -192,17 +192,14 @@ public class DSpaceATOMEntry
{ {
if (!noOp) if (!noOp)
{ {
if (item.getHandle() != null) handle = IdentifierService.getCanonicalForm(item);
{
handle = item.getHandle();
}
if (handle != null && !"".equals(handle)) if (handle != null && !"".equals(handle))
{ {
Content content = new Content(); Content content = new Content();
// content.setType("application/zip"); // content.setType("application/zip");
content.setType("text/html"); content.setType("text/html");
content.setSource(HandleManager.getCanonicalForm(handle)); content.setSource(handle);
entry.setContent(content); entry.setContent(content);
} }
} }
@@ -247,14 +244,11 @@ public class DSpaceATOMEntry
// it's possible that the item hasn't been assigned a handle yet // it's possible that the item hasn't been assigned a handle yet
if (!noOp) if (!noOp)
{ {
if (item.getHandle() != null) handle = IdentifierService.getCanonicalForm(item);
{
handle = item.getHandle();
}
if (handle != null && !"".equals(handle)) if (handle != null && !"".equals(handle))
{ {
entry.setId(HandleManager.getCanonicalForm(handle)); entry.setId(handle);
return; return;
} }
} }
@@ -277,35 +271,26 @@ public class DSpaceATOMEntry
protected void addLinks(String handle) protected void addLinks(String handle)
throws DSpaceSWORDException throws DSpaceSWORDException
{ {
try // if there is no handle, we can't generate links
{ if (handle == null)
// if there is no handle, we can't generate links {
if (handle == null) return;
{ }
return;
} // in the default set up we just pass urls to all of the
// inidivual files in the item
String base = ConfigurationManager.getProperty("dspace.url"); Bundle[] bundles = item.getBundles("ORIGINAL");
for (int i = 0; i < bundles.length ; i++)
// in the default set up we just pass urls to all of the {
// inidivual files in the item Bitstream[] bss = bundles[i].getBitstreams();
Bundle[] bundles = item.getBundles("ORIGINAL"); for (int j = 0; j < bss.length; j++)
for (int i = 0; i < bundles.length ; i++) {
{ Link link = new Link();
Bitstream[] bss = bundles[i].getBitstreams(); String url = IdentifierService.getURL(bss[j]).toString(); // base + "/bitstream/" + handle + "/" + bss[j].getSequenceID() + "/" + bss[j].getName();
for (int j = 0; j < bss.length; j++) link.setHref(url);
{ entry.addLink(link);
Link link = new Link(); }
String url = base + "/bitstream/" + handle + "/" + bss[j].getSequenceID() + "/" + bss[j].getName(); }
link.setHref(url);
entry.addLink(link);
}
}
}
catch (SQLException e)
{
throw new DSpaceSWORDException(e);
}
} }
/** /**
@@ -338,43 +323,28 @@ public class DSpaceATOMEntry
*/ */
protected void addRights(String handle) protected void addRights(String handle)
{ {
try // if there's no handle, we can't give a link
{ if (handle == null)
// if there's no handle, we can't give a link {
if (handle == null) return;
{ }
return;
} StringBuilder rightsString = new StringBuilder();
Bundle[] bundles = item.getBundles("LICENSE");
String base = ConfigurationManager.getProperty("dspace.url"); for (int i = 0; i < bundles.length; i++)
{
// if there's no base URL, we are stuck Bitstream[] bss = bundles[i].getBitstreams();
if (base == null) for (int j = 0; j < bss.length; j++)
{ {
return; String url = IdentifierService.getURL(bss[j]).toString(); //base + "/bitstream/" + handle + "/" + bss[j].getSequenceID() + "/" + bss[j].getName();
} rightsString.append(url + " ");
}
StringBuilder rightsString = new StringBuilder(); }
Bundle[] bundles = item.getBundles("LICENSE");
for (int i = 0; i < bundles.length; i++) Rights rights = new Rights();
{ rights.setContent(rightsString.toString());
Bitstream[] bss = bundles[i].getBitstreams(); rights.setType(ContentType.TEXT);
for (int j = 0; j < bss.length; j++) entry.setRights(rights);
{
String url = base + "/bitstream/" + handle + "/" + bss[j].getSequenceID() + "/" + bss[j].getName();
rightsString.append(url + " ");
}
}
Rights rights = new Rights();
rights.setContent(rightsString.toString());
rights.setType(ContentType.TEXT);
entry.setRights(rights);
}
catch (SQLException e)
{
// do nothing
}
} }
/** /**

View File

@@ -256,73 +256,61 @@ public class DSpaceSWORDServer implements SWORDServer
} }
log.info(LogManager.getHeader(context, "sword_authenticate", "username=" + un + ",on_behalf_of=" + obo)); log.info(LogManager.getHeader(context, "sword_authenticate", "username=" + un + ",on_behalf_of=" + obo));
try
{ // attempt to authenticate the primary user
// attempt to authenticate the primary user SWORDContext sc = new SWORDContext();
SWORDContext sc = new SWORDContext(); SWORDAuthentication auth = new SWORDAuthentication();
SWORDAuthentication auth = new SWORDAuthentication(); EPerson ep = null;
EPerson ep = null; boolean authenticated = false;
boolean authenticated = false; if (auth.authenticates(this.context, un, pw))
if (auth.authenticates(this.context, un, pw)) {
{ // if authenticated, obtain the eperson object
// if authenticated, obtain the eperson object ep = EPerson.findByEmail(context, un);
ep = EPerson.findByEmail(context, un);
if (ep != null)
if (ep != null) {
{ authenticated = true;
authenticated = true; sc.setAuthenticated(ep);
sc.setAuthenticated(ep); }
}
// if there is an onBehalfOfuser, then find their eperson
// if there is an onBehalfOfuser, then find their eperson // record, and if it exists set it. If not, then the
// record, and if it exists set it. If not, then the // authentication process fails
// authentication process fails if (obo != null)
if (obo != null) {
{ EPerson epObo= EPerson.findByEmail(this.context, obo);
EPerson epObo= EPerson.findByEmail(this.context, obo); if (epObo != null)
if (epObo != null) {
{ sc.setOnBehalfOf(epObo);
sc.setOnBehalfOf(epObo); }
} else
else {
{ authenticated = false;
authenticated = false; }
} }
} }
}
// deal with the context or throw an authentication exception
// deal with the context or throw an authentication exception if (ep != null && authenticated)
if (ep != null && authenticated) {
{ this.context.setCurrentUser(ep);
this.context.setCurrentUser(ep); log.info(LogManager.getHeader(context, "sword_set_authenticated_user", "user_id=" + ep.getID()));
log.info(LogManager.getHeader(context, "sword_set_authenticated_user", "user_id=" + ep.getID())); }
} else
else {
{ // decide what kind of error to throw
// decide what kind of error to throw if (ep != null)
if (ep != null) {
{ log.info(LogManager.getHeader(context, "sword_unable_to_set_user", "username=" + un));
log.info(LogManager.getHeader(context, "sword_unable_to_set_user", "username=" + un)); throw new SWORDAuthenticationException("Unable to authenticate the supplied used");
throw new SWORDAuthenticationException("Unable to authenticate the supplied used"); }
} else
else {
{ log.info(LogManager.getHeader(context, "sword_unable_to_set_on_behalf_of", "username=" + un + ",on_behalf_of=" + obo));
log.info(LogManager.getHeader(context, "sword_unable_to_set_on_behalf_of", "username=" + un + ",on_behalf_of=" + obo)); throw new SWORDAuthenticationException("Unable to authenticate the onBehalfOf account");
throw new SWORDAuthenticationException("Unable to authenticate the onBehalfOf account"); }
} }
}
return sc;
return sc;
}
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new SWORDException("There was a problem accessing the repository user database", e);
}
catch (AuthorizeException e)
{
log.error("caught exception: ", e);
throw new SWORDAuthenticationException("There was a problem authenticating or authorising the user", e);
}
} }
} }

View File

@@ -39,7 +39,6 @@
package org.dspace.sword; package org.dspace.sword;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@@ -266,11 +265,6 @@ public class DepositManager
log.error("authentication problem; caught exception: ", e); log.error("authentication problem; caught exception: ", e);
throw new DSpaceSWORDException(e); throw new DSpaceSWORDException(e);
} }
catch (SQLException e)
{
log.error("caught exception: ", e);
throw new DSpaceSWORDException(e);
}
} }

View File

@@ -104,27 +104,19 @@ public class SWORDContext
* Is the authenticated user a DSpace administrator? This translates * Is the authenticated user a DSpace administrator? This translates
* as asking the question of whether the given eperson is a member * as asking the question of whether the given eperson is a member
* of the special DSpace group Administrator, with id 1 * of the special DSpace group Administrator, with id 1
* *
* @param eperson
* @return true if administrator, false if not * @return true if administrator, false if not
* @throws SQLException * @throws SQLException
*/ */
public boolean isUserAdmin(Context context) public boolean isUserAdmin(Context context)
throws DSpaceSWORDException throws DSpaceSWORDException
{ {
try if (this.authenticated != null)
{ {
if (this.authenticated != null) Group admin = Group.find(context, 1);
{ return admin.isMember(this.authenticated);
Group admin = Group.find(context, 1); }
return admin.isMember(this.authenticated); return false;
}
return false;
}
catch (SQLException e)
{
throw new DSpaceSWORDException(e);
}
} }
/** /**
@@ -132,26 +124,18 @@ public class SWORDContext
* as asking the question of whether the given eperson is a member * as asking the question of whether the given eperson is a member
* of the special DSpace group Administrator, with id 1 * of the special DSpace group Administrator, with id 1
* *
* @param eperson
* @return true if administrator, false if not * @return true if administrator, false if not
* @throws SQLException * @throws SQLException
*/ */
public boolean isOnBehalfOfAdmin(Context context) public boolean isOnBehalfOfAdmin(Context context)
throws DSpaceSWORDException throws DSpaceSWORDException
{ {
try if (this.onBehalfOf != null)
{ {
if (this.onBehalfOf != null) Group admin = Group.find(context, 1);
{ return admin.isMember(this.onBehalfOf);
Group admin = Group.find(context, 1); }
return admin.isMember(this.onBehalfOf); return false;
}
return false;
}
catch (SQLException e)
{
throw new DSpaceSWORDException(e);
}
} }
/** /**
@@ -237,49 +221,42 @@ public class SWORDContext
public Collection[] getAllowedCollections(Context context) public Collection[] getAllowedCollections(Context context)
throws DSpaceSWORDException throws DSpaceSWORDException
{ {
try // locate the collections to which the authenticated user has ADD rights
{ Collection[] cols = Collection.findAuthorized(context, null, Constants.ADD);
// locate the collections to which the authenticated user has ADD rights
Collection[] cols = Collection.findAuthorized(context, null, Constants.ADD);
// if there is no onBehalfOf user, just return the list // if there is no onBehalfOf user, just return the list
if (this.getOnBehalfOf() == null) if (this.getOnBehalfOf() == null)
{ {
return cols; return cols;
} }
// if the onBehalfOf user is an administrator, return the list // if the onBehalfOf user is an administrator, return the list
if (this.isOnBehalfOfAdmin(context)) if (this.isOnBehalfOfAdmin(context))
{ {
return cols; return cols;
} }
// if we are here, then we have to filter the list of collections // if we are here, then we have to filter the list of collections
List<Collection> colList = new ArrayList<Collection>(); List<Collection> colList = new ArrayList<Collection>();
for (int i = 0; i < cols.length; i++) for (int i = 0; i < cols.length; i++)
{ {
// we check each collection to see if the onBehalfOf user // we check each collection to see if the onBehalfOf user
// is permitted to deposit // is permitted to deposit
// urgh, this is so inefficient, but the authorisation API is // urgh, this is so inefficient, but the authorisation API is
// a total hellish nightmare // a total hellish nightmare
Group subs = cols[i].getSubmitters(); Group subs = cols[i].getSubmitters();
if (isOnBehalfOfInGroup(subs)) if (isOnBehalfOfInGroup(subs))
{ {
colList.add(cols[i]); colList.add(cols[i]);
} }
} }
// now create the new array and return that // now create the new array and return that
Collection[] newCols = new Collection[colList.size()]; Collection[] newCols = new Collection[colList.size()];
newCols = colList.toArray((Collection[]) newCols); newCols = colList.toArray((Collection[]) newCols);
return newCols; return newCols;
}
catch (SQLException e)
{
throw new DSpaceSWORDException(e);
}
} }
/** /**

View File

@@ -54,9 +54,9 @@ import org.dspace.content.packager.PackageParameters;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.core.PluginManager; import org.dspace.core.PluginManager;
import org.dspace.handle.HandleManager;
import org.dspace.workflow.WorkflowItem; import org.dspace.workflow.WorkflowItem;
import org.dspace.workflow.WorkflowManager; import org.dspace.workflow.WorkflowManager;
import org.dspace.uri.IdentifierService;
import org.purl.sword.base.Deposit; import org.purl.sword.base.Deposit;
@@ -90,7 +90,7 @@ public class SWORDMETSIngester implements SWORDIngester
CollectionLocation cl = new CollectionLocation(); CollectionLocation cl = new CollectionLocation();
Collection collection = cl.getCollection(context, loc); Collection collection = cl.getCollection(context, loc);
message("Performing deposit using location: " + loc + "; "); message("Performing deposit using location: " + loc + "; ");
message("Location resolves to collection with handle: " + collection.getHandle() + message("Location resolves to collection with identifier: " + IdentifierService.getCanonicalForm(collection) +
" and name: " + collection.getMetadata("name") + "; "); " and name: " + collection.getMetadata("name") + "; ");
// load the plugin manager for the required configuration // load the plugin manager for the required configuration
@@ -143,9 +143,10 @@ public class SWORDMETSIngester implements SWORDIngester
// for some reason, DSpace will not give you the handle automatically, // for some reason, DSpace will not give you the handle automatically,
// so we have to look it up // so we have to look it up
String handle = HandleManager.findHandle(context, installedItem); // String handle = HandleManager.findHandle(context, installedItem);
String handle = IdentifierService.getCanonicalForm(installedItem);
message("Ingest successful; ");
message("Ingest successful; ");
message("Item created with internal identifier: " + installedItem.getID() + "; "); message("Item created with internal identifier: " + installedItem.getID() + "; ");
if (handle != null) if (handle != null)
{ {

View File

@@ -44,7 +44,6 @@ import org.apache.log4j.Logger;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.eperson.EPerson; import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group; import org.dspace.eperson.Group;