if (sizes.containsKey(USERS_XML)) {
// Import users first
zis = new NoCloseZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
while (true) {
ZipEntry zipentry = zis.getNextEntry();
if (zipentry == null) break;
String name = zipentry.getName();
if (name.equals(USERS_XML)) {
userProps = importUsers(zis, usersImportHandler);
break;
}
zis.closeEntry();
}
} finally {
zis.reallyClose();
}
}
// Check if it is an 5.x or 6.1 import :
for (Map.Entry<String, Long> entry : sizes.entrySet()) {
if (entry.getKey().startsWith("export_")) {
legacyImport = true;
break;
}
}
if (sizes.containsKey(SITE_PROPERTIES)) {
zis = new NoCloseZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
while (true) {
ZipEntry zipentry = zis.getNextEntry();
if (zipentry == null) break;
String name = zipentry.getName();
if (name.equals(SITE_PROPERTIES)) {
importSiteProperties(zis, site);
break;
}
zis.closeEntry();
}
} finally {
zis.reallyClose();
}
}
if (sizes.containsKey(REPOSITORY_XML)) {
// Import repository content
zis = new NoCloseZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
while (true) {
ZipEntry zipentry = zis.getNextEntry();
if (zipentry == null) break;
String name = zipentry.getName();
if (name.equals(REPOSITORY_XML)) {
DocumentViewImportHandler documentViewImportHandler = new DocumentViewImportHandler(session, null, file, fileList, (site != null ? site.getSiteKey(): null));
documentViewImportHandler.setReferences(references);
documentViewImportHandler.setNoRoot(true);
handleImport(zis, documentViewImportHandler);
session.save(JCRObservationManager.IMPORT);
break;
}
zis.closeEntry();
}
} catch (Exception e) {
logger.error("Cannot import", e);
} finally {
zis.reallyClose();
}
} else {
// No repository descriptor - prepare to import files directly
pathMapping = session.getPathMapping();
pathMapping.put("/", "/sites/"+site.getSiteKey()+"/files/");
}
NodeTypeRegistry reg = NodeTypeRegistry.getInstance();
DefinitionsMapping mapping = null;
// Import additional files - site.properties, old cateogries.xml , sitepermissions.xml
// and eventual plain file from 5.x imports
if (!sizes.containsKey(REPOSITORY_XML) || sizes.containsKey(SITE_PROPERTIES) ||
sizes.containsKey(CATEGORIES_XML)|| sizes.containsKey(SITE_PERMISSIONS_XML) ||
sizes.containsKey(DEFINITIONS_CND)|| sizes.containsKey(DEFINITIONS_MAP)
) {
zis = new NoCloseZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
while (true) {
ZipEntry zipentry = zis.getNextEntry();
if (zipentry == null) break;
String name = zipentry.getName();
if (name.indexOf('\\') > -1) {
name = name.replace('\\', '/');
}
if (name.indexOf('/') > -1) {
if (!sizes.containsKey(REPOSITORY_XML) && !sizes.containsKey(FILESACL_XML)) {
// No repository descriptor - Old import format only
name = "/" + name;
if (name.startsWith("/content/sites")) {
name = pathMapping.get("/")
+ StringUtils.stripStart(name
.replaceFirst("/content/sites/[^/]+/files/", ""),
"/");
} else if (name.startsWith("/users")) {
Matcher m = Pattern.compile("/users/([^/]+)(/.*)?").matcher(name);
if (m.matches()) {
name = ServicesRegistry.getInstance().getJahiaUserManagerService().getUserSplittingRule().getPathForUsername(m.group(1));
name = name + "/files" + ((m.group(2) != null) ? m.group(2) : "");
}
} else if (name.startsWith("/content/users")) {
Matcher m = Pattern.compile("/content/users/([^/]+)(/.*)?").matcher(name);
if (m.matches()) {
name = ServicesRegistry.getInstance().getJahiaUserManagerService().getUserSplittingRule().getPathForUsername(m.group(1));
name = name + ((m.group(2) != null) ? m.group(2) : "");
}
} else {
name = pathMapping.get("/")
+ StringUtils.stripStart(name, "/");
}
if (!zipentry.isDirectory()) {
try {
String filename = name.substring(name.lastIndexOf('/') + 1);
String contentType = JahiaContextLoaderListener.getServletContext().getMimeType(filename);
ensureFile(jcrStoreService.getSessionFactory().getCurrentUserSession(), name, zis, contentType, site);
} catch (Exception e) {
logger.error("Cannot upload file " + zipentry.getName(), e);
}
} else {
ensureDir(jcrStoreService.getSessionFactory().getCurrentUserSession(), name, site);
}
}
} else if (name.equals(CATEGORIES_XML)) {
catProps = importCategoriesAndGetUuidProps(zis, categoriesImportHandler);
} else if (name.equals(DEFINITIONS_CND)) {
reg = new NodeTypeRegistry();
try {
if (legacyImport) {
JahiaCndReaderLegacy r = new JahiaCndReaderLegacy(new InputStreamReader(zis, "UTF-8"),zipentry.getName(), file.getName(), reg);
r.parse();
} else {
JahiaCndReader r = new JahiaCndReader(new InputStreamReader(zis, "UTF-8"),zipentry.getName(), file.getName(), reg);
r.parse();
}
} catch (ParseException e) {
e.printStackTrace();
}
} else if (name.equals(DEFINITIONS_MAP)) {
mapping = new DefinitionsMapping();
mapping.load(zis);
}
zis.closeEntry();
}
} finally {
zis.reallyClose();
}
}
// Import legacy content from 5.x and 6.x
if (legacyImport) {
// Old import
JCRNodeWrapper siteFolder = jcrStoreService.getSessionFactory().getCurrentUserSession().getNode("/sites/" + site.getSiteKey());
zis = new NoCloseZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
while (true) {
ZipEntry zipentry = zis.getNextEntry();
if (zipentry == null) break;
String name = zipentry.getName();
if (name.equals(FILESACL_XML)) {
importFilesAcl(site, file, zis, mapping, fileList);
} else if (name.startsWith("export")) {
String languageCode;
if (name.indexOf("_") != -1) {
languageCode = name.substring(7, name.lastIndexOf("."));
} else {
languageCode = site.getLanguagesAsLocales().iterator().next().toString();
}
zipentry.getSize();
LegacyImportHandler importHandler = new LegacyImportHandler(session,
siteFolder, reg, mapping, LanguageCodeConverters
.languageCodeToLocale(languageCode),
infos != null ? (String)infos.get("originatingJahiaRelease")