[java-opensaml COMMIT] in /trunk/opensaml-core/src: main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager...
noreply at shibboleth.net
noreply at shibboleth.net
Tue Oct 11 20:37:48 EDT 2016
Author: putmanb
Date: Tue Oct 11 20:37:48 2016
New Revision: 4532
URL: http://svn.shibboleth.net/view/java-opensaml?rev=4532&view=rev
Log:
In support of OSJ-168, update FilesystemLoadSaveManager so saves from XMLObjectSource if present.
Also load an object with attached XMLObjectSource, for symmetry.
Modified:
trunk/opensaml-core/src/main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager.java
trunk/opensaml-core/src/test/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManagerTest.java
Modified: trunk/opensaml-core/src/main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-core/src/main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager.java?rev=4532&r1=4531&r2=4532&view=diff
==============================================================================
--- trunk/opensaml-core/src/main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager.java (original)
+++ trunk/opensaml-core/src/main/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManager.java Tue Oct 11 20:37:48 2016
@@ -17,6 +17,7 @@
package org.opensaml.core.xml.persist;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
@@ -26,6 +27,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
@@ -37,12 +39,14 @@
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.core.xml.io.MarshallingException;
import org.opensaml.core.xml.io.UnmarshallingException;
+import org.opensaml.core.xml.util.XMLObjectSource;
import org.opensaml.core.xml.util.XMLObjectSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
+import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import net.shibboleth.utilities.java.support.collection.Pair;
@@ -163,8 +167,10 @@
return null;
}
try (FileInputStream fis = new FileInputStream(file)) {
- try {
- XMLObject xmlObject = XMLObjectSupport.unmarshallFromInputStream(parserPool, fis);
+ byte[] source = ByteStreams.toByteArray(fis);
+ try (ByteArrayInputStream bais = new ByteArrayInputStream(source)) {
+ XMLObject xmlObject = XMLObjectSupport.unmarshallFromInputStream(parserPool, bais);
+ xmlObject.getObjectMetadata().put(new XMLObjectSource(source));
//TODO via ctor, etc, does caller need to supply a Class so we can can test and throw an IOException,
// rather than an unchecked ClassCastException?
return (T) xmlObject;
@@ -186,15 +192,23 @@
String.format("Target file already exists for key '%s' and overwrite not indicated", key));
}
- //TODO possibly look at objectMetadata for source byte[] and write that rather than marshall+serialize?
-
File file = buildFile(key);
try (FileOutputStream fos = new FileOutputStream(file)) {
- try {
- XMLObjectSupport.marshallToOutputStream(xmlObject, fos);
- } catch (MarshallingException e) {
- throw new IOException(String.format("Error saving target file: %s", file.getAbsolutePath()), e);
- }
+ List<XMLObjectSource> sources = xmlObject.getObjectMetadata().get(XMLObjectSource.class);
+ if (sources.size() == 1) {
+ log.debug("XMLObject contained 1 XMLObjectSource instance, persisting existing byte[]");
+ XMLObjectSource source = sources.get(0);
+ fos.write(source.getObjectSource());
+ } else {
+ log.debug("XMLObject contained {} XMLObjectSource instances, persisting marshalled object",
+ sources.size());
+ try {
+ XMLObjectSupport.marshallToOutputStream(xmlObject, fos);
+ } catch (MarshallingException e) {
+ throw new IOException(String.format("Error saving target file: %s", file.getAbsolutePath()), e);
+ }
+ }
+ fos.flush();
}
}
Modified: trunk/opensaml-core/src/test/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManagerTest.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-core/src/test/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManagerTest.java?rev=4532&r1=4531&r2=4532&view=diff
==============================================================================
--- trunk/opensaml-core/src/test/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManagerTest.java (original)
+++ trunk/opensaml-core/src/test/java/org/opensaml/core/xml/persist/FilesystemLoadSaveManagerTest.java Tue Oct 11 20:37:48 2016
@@ -17,6 +17,7 @@
package org.opensaml.core.xml.persist;
[... 110 lines stripped ...]
More information about the commits
mailing list