From ba46d452de94bb9fe0405febc511be7223850220 Mon Sep 17 00:00:00 2001 From: Jamal Kaabi-Mofrad Date: Thu, 2 Jun 2016 21:18:03 +0000 Subject: [PATCH] Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2) 125453 jvonka: RA-779 / RA-780: Sites API - initial commit for create site [default/functional Share site ;-)] & delete site -add new files, doh ! git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127529 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../api/impl/SiteImportPackageHandler.java | 79 +++++++++++ .../rest/api/impl/SiteSurfConfig.java | 125 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 source/java/org/alfresco/rest/api/impl/SiteImportPackageHandler.java create mode 100644 source/java/org/alfresco/rest/api/impl/SiteSurfConfig.java diff --git a/source/java/org/alfresco/rest/api/impl/SiteImportPackageHandler.java b/source/java/org/alfresco/rest/api/impl/SiteImportPackageHandler.java new file mode 100644 index 0000000000..fc39dc9894 --- /dev/null +++ b/source/java/org/alfresco/rest/api/impl/SiteImportPackageHandler.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.rest.api.impl; + +import org.alfresco.service.cmr.view.ImportPackageHandler; +import org.alfresco.service.cmr.view.ImporterException; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.io.UnsupportedEncodingException; + +// note: based on HomeSiteImportPackageHandler in Cloud/Thor module +public class SiteImportPackageHandler implements ImportPackageHandler +{ + private final static String SITEID_PLACEHOLDER = "${siteId}"; + + private SiteSurfConfig config; + private String siteId; + + public SiteImportPackageHandler(SiteSurfConfig config, String siteId) + { + this.config = config; + this.siteId = siteId; + } + + @Override + public void startImport() + { + } + + @Override + public Reader getDataStream() + { + return new StringReader(config.getImportView()); + } + + @Override + public InputStream importStream(String contentPath) + { + String content = config.getImportContent(contentPath); + if (content == null) + { + return null; + } + + String siteContent = content.replace(SITEID_PLACEHOLDER, siteId); + try + { + return new ByteArrayInputStream(siteContent.getBytes("UTF-8")); + } + catch(UnsupportedEncodingException e) + { + throw new ImporterException("Failed to read content " + contentPath, e); + } + } + + @Override + public void endImport() + { + } +} \ No newline at end of file diff --git a/source/java/org/alfresco/rest/api/impl/SiteSurfConfig.java b/source/java/org/alfresco/rest/api/impl/SiteSurfConfig.java new file mode 100644 index 0000000000..661085b752 --- /dev/null +++ b/source/java/org/alfresco/rest/api/impl/SiteSurfConfig.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.rest.api.impl; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.extensions.webscripts.ClassPathStoreResourceResolver; +import org.springframework.util.FileCopyUtils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +// note: based on HomeSiteSurfConfig in Cloud/Thor module +public class SiteSurfConfig implements ApplicationContextAware, InitializingBean +{ + private ApplicationContext applicationContext; + private String configPath; + private String packageName = "surf-config"; + private String importView; + private Map importContent; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + this.applicationContext = applicationContext; + } + + public void setConfigPath(String configPath) + { + if (!configPath.endsWith("/")) + { + configPath = configPath + "/"; + } + this.configPath = configPath; + } + + public void setPackageName(String packageName) + { + this.packageName = packageName; + } + + @Override + public void afterPropertiesSet() throws Exception + { + if (this.configPath == null) + { + setConfigPath("alfresco/bootstrap/site"); + } + importView = loadImportView(); + importContent = loadContent(); + } + + public String getImportView() + { + return importView; + } + + public String getImportContent(String contentPath) + { + return importContent.get(contentPath); + } + + private String loadImportView() throws IOException + { + ClassPathResource importViewResource = new ClassPathResource(configPath + packageName + ".xml"); + if (!importViewResource.exists()) + { + throw new AlfrescoRuntimeException("Cannot find site config " + importViewResource.getPath()); + } + return convert(importViewResource.getInputStream()); + } + + private Map loadContent() throws IOException + { + ClassPathStoreResourceResolver resourceResolver = new ClassPathStoreResourceResolver(applicationContext); + Resource[] contentResources = resourceResolver.getResources("classpath*:" + configPath + packageName + "/*.*"); + Map content = new HashMap(); + for (Resource contentResource : contentResources) + { + String fileName = contentResource.getFilename(); + // ignore hidden directories / files + if (fileName.startsWith(".")) + { + continue; + } + String key = packageName + "/" + fileName; + String value = convert(contentResource.getInputStream()); + content.put(key, value); + } + return content; + } + + private String convert(InputStream contentInputStream) throws IOException + { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + FileCopyUtils.copy(contentInputStream, os); + byte[] bytes = os.toByteArray(); + String content = new String(bytes, "UTF-8"); + return content; + } +}