MemoryArchive archive = new MemoryArchive(true);
InputStreamPump pump = new InputStreamPump(in , archive);
// this will cause the input stream to be consumed and the memory archive being initialized.
Binary bin = session.getValueFactory().createBinary(pump);
if (pump.getError() != null) {
Exception error = pump.getError();
log.error("Error while reading from input stream.", error);
bin.dispose();
throw new IOException("Error while reading from input stream", error);
}
if (archive.getJcrRoot() == null) {
String msg = "Stream is not a content package. Missing 'jcr_root'.";
log.error(msg);
bin.dispose();
throw new IOException(msg);
}
final MetaInf inf = archive.getMetaInf();
PackagePropertiesImpl props = new PackagePropertiesImpl() {
@Override
protected Properties getPropertiesMap() {
return inf.getProperties();
}
};
PackageId pid = props.getId();
// invalidate pid if path is unknown
if (pid == null || pid.getInstallationPath().equals(ZipVaultPackage.UNKNOWN_PATH)) {
bin.dispose();
throw new IOException("Package does not contain a path specification or valid package id.");
}
if (!pid.isValid()) {
throw new RepositoryException("Unable to create package. Illegal package name.");
}
// create parent node
String path = pid.getInstallationPath() + ".zip";
String parentPath = Text.getRelativeParent(path, 1);
String name = Text.getName(path);
Node parent = mkdir(parentPath, false);
// remember installation state properties (GRANITE-2018)
JcrPackageDefinitionImpl.State state = null;
if (parent.hasNode(name)) {
JcrPackage oldPackage = new JcrPackageImpl(parent.getNode(name));
JcrPackageDefinitionImpl oldDef = (JcrPackageDefinitionImpl) oldPackage.getDefinition();
if (oldDef != null) {
state = oldDef.getState();
}
if (replace) {
parent.getNode(name).remove();
} else {
throw new ItemExistsException("Package already exists: " + path);
}
}
JcrPackage jcrPack = null;
try {
jcrPack = JcrPackageImpl.createNew(parent, pid, bin, archive);
if (jcrPack != null) {
JcrPackageDefinitionImpl def = (JcrPackageDefinitionImpl) jcrPack.getDefinition();
if (state != null) {
def.setState(state);
}
}
return jcrPack;
} finally {
bin.dispose();
if (jcrPack == null) {
session.refresh(false);
} else {
session.save();
}