Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

finish inline-to-managed filter

  • Loading branch information...
commit 66854090b5096fa6c64a189da73d6fd0de47e46b 1 parent 253ef5b
@cwilper authored
View
23 fcrepo-store-util/src/main/conf/filters/canonicalize-managed-xml.xml
@@ -7,19 +7,26 @@
<bean id="canonicalize-managed-xml"
class="com.github.cwilper.fcrepo.store.util.filters.FilterDatastreams">
<constructor-arg>
- <bean class="com.github.cwilper.ttff.Filters" factory-method="and">
+ <bean class="com.github.cwilper.ttff.Filters" factory-method="or">
<constructor-arg>
<list>
- <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfIdMatches">
+ <bean class="com.github.cwilper.ttff.Filters" factory-method="and">
<constructor-arg>
- <bean class="com.github.cwilper.fcrepo.store.util.IdSpec">
- <constructor-arg value="${datastream-ids}"/>
- </bean>
+ <list>
+ <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfIdMatches">
+ <constructor-arg>
+ <bean class="com.github.cwilper.fcrepo.store.util.IdSpec">
+ <constructor-arg value="${datastream-ids}"/>
+ </bean>
+ </constructor-arg>
+ </bean>
+ <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.CanonicalizeManagedXML">
+ <constructor-arg type="boolean" value="${all-datastream-versions}"/>
+ </bean>
+ </list>
</constructor-arg>
</bean>
- <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.CanonicalizeManagedXML">
- <constructor-arg type="boolean" value="${all-datastream-versions}"/>
- </bean>
+ <ref bean="unfiltered-ds"/>
</list>
</constructor-arg>
</bean>
View
21 fcrepo-store-util/src/main/conf/filters/inline-to-managed-xml.xml
@@ -4,20 +4,27 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
- <bean id="canonicalize-managed-xml"
+ <bean id="inline-to-managed-xml"
class="com.github.cwilper.fcrepo.store.util.filters.FilterDatastreams">
<constructor-arg>
- <bean class="com.github.cwilper.ttff.Filters" factory-method="and">
+ <bean class="com.github.cwilper.ttff.Filters" factory-method="or">
<constructor-arg>
<list>
- <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfIdMatches">
+ <bean class="com.github.cwilper.ttff.Filters" factory-method="and">
<constructor-arg>
- <bean class="com.github.cwilper.fcrepo.store.util.IdSpec">
- <constructor-arg value="${datastream-ids}"/>
- </bean>
+ <list>
+ <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfIdMatches">
+ <constructor-arg>
+ <bean class="com.github.cwilper.fcrepo.store.util.IdSpec">
+ <constructor-arg value="${datastream-ids}"/>
+ </bean>
+ </constructor-arg>
+ </bean>
+ <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.InlineToManagedXML"/>
+ </list>
</constructor-arg>
</bean>
- <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.InlineToManagedXML"/>
+ <ref bean="unfiltered-ds"/>
</list>
</constructor-arg>
</bean>
View
7 fcrepo-store-util/src/main/conf/filters/remove-old-versions.xml
@@ -16,6 +16,13 @@
<bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfIdMatches">
<constructor-arg value="${old-datastream-ids}"/>
</bean>
+ <bean class="com.github.cwilper.fcrepo.store.util.filters.ds.IfControlGroupMatches">
+ <constructor-arg>
+ <bean class="com.github.cwilper.fcrepo.store.util.IdSpec">
+ <constructor-arg value="${control-groups}"/>
+ </bean>
+ </constructor-arg>
+ </bean>
<bean class="com.github.cwilper.fcrepo.store.util.filters.ds.RemoveOldVersions"/>
</list>
</constructor-arg>
View
7 ...-util/src/main/java/com/github/cwilper/fcrepo/store/util/commands/BatchObjectCommand.java
@@ -36,12 +36,13 @@ public void execute() {
}
} else {
for (String pid : pids) {
+ FedoraObject object = null;
try {
- handleObject(source.getObject(pid));
+ object = source.getObject(pid);
} catch (NotFoundException e) {
- logger.warn("Skipped {} (does not exist in source)",
- pid);
+ logger.warn("Skipped {} (not in source)", pid);
}
+ if (object != null) handleObject(object);
}
}
}
View
19 ...o-store-util/src/main/java/com/github/cwilper/fcrepo/store/util/commands/CopyCommand.java
@@ -6,6 +6,7 @@
import com.github.cwilper.fcrepo.dto.core.FedoraObject;
import com.github.cwilper.fcrepo.store.core.ExistsException;
import com.github.cwilper.fcrepo.store.core.FedoraStore;
+import com.github.cwilper.fcrepo.store.core.NotFoundException;
import com.github.cwilper.fcrepo.store.util.IdSpec;
import com.github.cwilper.ttff.Filter;
import org.slf4j.Logger;
@@ -54,15 +55,21 @@ protected void handleFilteredObject(FedoraObject object) {
for (Datastream datastream : object.datastreams().values()) {
if (datastream.controlGroup() == ControlGroup.MANAGED) {
for (DatastreamVersion version : datastream.versions()) {
- InputStream content = source.getContent(
- object.pid(), datastream.id(), version.id());
String info = object.pid() + "/" + datastream.id() +
"/" + version.id();
- if (content != null) {
- destination.setContent(
+ try {
+ InputStream content = source.getContent(
object.pid(), datastream.id(),
- version.id(), content);
- logger.info("Copied content of {}", info);
+ version.id());
+ if (content != null) {
+ destination.setContent(
+ object.pid(), datastream.id(),
+ version.id(), content);
+ logger.info("Copied content of {}", info);
+ }
+ } catch (NotFoundException e) {
+ logger.debug(
+ "Skipped content of {} (not in source)");
}
}
}
View
19 ...src/main/java/com/github/cwilper/fcrepo/store/util/filters/ds/CanonicalizeManagedXML.java
@@ -1,10 +1,12 @@
package com.github.cwilper.fcrepo.store.util.filters.ds;
+import com.github.cwilper.fcrepo.dto.core.ContentDigest;
import com.github.cwilper.fcrepo.dto.core.ControlGroup;
import com.github.cwilper.fcrepo.dto.core.Datastream;
import com.github.cwilper.fcrepo.dto.core.DatastreamVersion;
import com.github.cwilper.fcrepo.dto.core.FedoraObject;
import com.github.cwilper.fcrepo.dto.core.io.XMLUtil;
+import com.github.cwilper.fcrepo.store.core.FedoraStore;
import com.github.cwilper.fcrepo.store.util.commands.CommandContext;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
@@ -14,7 +16,7 @@
import java.io.IOException;
import java.io.InputStream;
-// also sets the size based on the canonicalized byte count
+// also sets size attribute. digest will be re-set if one is already defined.
public class CanonicalizeManagedXML extends MultiVersionFilter {
private static final Logger logger =
LoggerFactory.getLogger(CanonicalizeManagedXML.class);
@@ -22,11 +24,12 @@
public CanonicalizeManagedXML(boolean allDatastreamVersions) {
super(allDatastreamVersions);
}
-
+
@Override
protected void handleVersion(FedoraObject object, Datastream ds,
DatastreamVersion dsv) throws IOException {
- if (CommandContext.getDestination() == null) {
+ FedoraStore destination = CommandContext.getDestination();
+ if (destination == null) {
throw new UnsupportedOperationException("Filter requires content "
+ "write access, but this is a read-only command");
}
@@ -40,10 +43,18 @@ protected void handleVersion(FedoraObject object, Datastream ds,
byte[] oBytes = IOUtils.toByteArray(inputStream);
cBytes = XMLUtil.canonicalize(oBytes);
dsv.size((long) cBytes.length);
+ ContentDigest digest = dsv.contentDigest();
+ if (digest != null) {
+ digest.hexValue(Util.computeFixity(
+ new ByteArrayInputStream(cBytes),
+ digest.type())[1]);
+ }
} finally {
IOUtils.closeQuietly(inputStream);
}
- CommandContext.getDestination().setContent(object.pid(), ds.id(),
+ Util.putObjectIfNoSuchManagedDatastream(object, destination,
+ ds.id());
+ destination.setContent(object.pid(), ds.id(),
dsv.id(), new ByteArrayInputStream(cBytes));
logger.info("Canonicalized {}", info);
}
View
46 ...til/src/main/java/com/github/cwilper/fcrepo/store/util/filters/ds/InlineToManagedXML.java
@@ -1,25 +1,40 @@
package com.github.cwilper.fcrepo.store.util.filters.ds;
+import com.github.cwilper.fcrepo.dto.core.ContentDigest;
import com.github.cwilper.fcrepo.dto.core.ControlGroup;
import com.github.cwilper.fcrepo.dto.core.Datastream;
import com.github.cwilper.fcrepo.dto.core.DatastreamVersion;
import com.github.cwilper.fcrepo.dto.core.FedoraObject;
+import com.github.cwilper.fcrepo.store.core.FedoraStore;
import com.github.cwilper.fcrepo.store.core.StoreException;
import com.github.cwilper.fcrepo.store.util.commands.CommandContext;
import com.github.cwilper.ttff.AbstractFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.net.URI;
+
+// also sets size attribute. digest will be re-set if one is already defined.
+public class InlineToManagedXML extends AbstractFilter<Datastream> {
+ private final static Logger logger =
+ LoggerFactory.getLogger(InlineToManagedXML.class);
-/**
- * A datastream filter that accepts all versions, but may examine or change
- * any of them.
- */
-public abstract class InlineToManagedXML extends AbstractFilter<Datastream> {
@Override
public Datastream accept(Datastream datastream) throws IOException {
+ FedoraStore destination = CommandContext.getDestination();
+ if (destination == null) {
+ throw new UnsupportedOperationException("Filter requires content "
+ + "write access, but this is a read-only command");
+ }
FedoraObject object = CommandContext.getObject();
try {
if (datastream.controlGroup() == ControlGroup.INLINE_XML) {
+ datastream.controlGroup(ControlGroup.MANAGED);
+ // put object first so destination can accept managed content
+ Util.putObjectIfNoSuchManagedDatastream(object, destination,
+ datastream.id());
for (DatastreamVersion datastreamVersion :
datastream.versions()) {
handleVersion(object, datastream, datastreamVersion);
@@ -31,7 +46,22 @@ public Datastream accept(Datastream datastream) throws IOException {
return datastream;
}
- protected abstract void handleVersion(FedoraObject object,
- Datastream datastream, DatastreamVersion datastreamVersion)
- throws IOException;
+ private void handleVersion(FedoraObject object, Datastream datastream,
+ DatastreamVersion datastreamVersion) throws IOException {
+ byte[] bytes = datastreamVersion.inlineXML().bytes();
+ CommandContext.getDestination().setContent(object.pid(),
+ datastream.id(), datastreamVersion.id(),
+ new ByteArrayInputStream(bytes));
+ datastreamVersion.inlineXML(null);
+ datastreamVersion.contentLocation(URI.create(object.pid() + "+" +
+ datastream.id() + "+" + datastreamVersion.id()));
+ datastreamVersion.size((long) bytes.length);
+ ContentDigest digest = datastreamVersion.contentDigest();
+ if (digest != null) {
+ digest.hexValue(Util.computeFixity(new ByteArrayInputStream(bytes),
+ digest.type())[1]);
+ }
+ logger.info("Converted {} to managed content", object.pid() + "/" +
+ datastream.id() + "/" + datastreamVersion.id());
+ }
}
View
19 fcrepo-store-util/src/main/java/com/github/cwilper/fcrepo/store/util/filters/ds/Util.java
@@ -3,8 +3,11 @@
import com.github.cwilper.fcrepo.dto.core.ControlGroup;
import com.github.cwilper.fcrepo.dto.core.Datastream;
import com.github.cwilper.fcrepo.dto.core.DatastreamVersion;
+import com.github.cwilper.fcrepo.dto.core.FedoraObject;
import com.github.cwilper.fcrepo.dto.core.io.ContentResolver;
import com.github.cwilper.fcrepo.dto.core.io.XMLUtil;
+import com.github.cwilper.fcrepo.store.core.FedoraStore;
+import com.github.cwilper.fcrepo.store.core.NotFoundException;
import com.github.cwilper.fcrepo.store.core.StoreException;
import com.github.cwilper.fcrepo.store.util.commands.CommandContext;
import org.apache.commons.io.IOUtils;
@@ -95,4 +98,20 @@ static long computeSize(InputStream inputStream)
IOUtils.closeQuietly(inputStream);
}
}
+
+ // create or update an existing object so it refers to the given
+ // managed datastream -- a prerequisite to putting the content into
+ // the store.
+ static void putObjectIfNoSuchManagedDatastream(FedoraObject object,
+ FedoraStore store, String datastreamId) {
+ try {
+ FedoraObject existing = store.getObject(object.pid());
+ Datastream ds = existing.datastreams().get(datastreamId);
+ if (ds == null || ds.controlGroup() != ControlGroup.MANAGED) {
+ store.updateObject(object);
+ }
+ } catch (NotFoundException e) {
+ store.addObject(object);
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.