use of org.opencms.file.CmsObject in project opencms-ideconnector by mediaworx.
the class IDEConnectorService method login.
private void login() throws ServletException, IOException {
LoginStatus status = new LoginStatusImpl();
CmsObject cmsObject;
try {
cmsObject = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
} catch (CmsException e) {
String message = "ERROR initializing OpenCms: ";
LOG.error(message, e);
status.setLoggedIn(false);
status.setMessage(message + e.getMessage() + ".\n StackTrace available in the OpenCms log file.");
objectMapper.writeValue(out, status);
return;
}
String user = request.getParameter(IDEConnectorConst.PARAM_USER);
String password = request.getParameter(IDEConnectorConst.PARAM_PASSWORD);
try {
cmsObject.loginUser(user, password);
} catch (CmsException e) {
String message = "ERROR logging in to OpenCms: ";
LOG.error(message, e);
status.setLoggedIn(false);
status.setMessage(message + e.getMessage() + ".\n StackTrace available in the OpenCms log file.");
objectMapper.writeValue(out, status);
return;
}
String token = (new CmsUUID()).getStringValue();
storeCmsObject(token, cmsObject);
status.setLoggedIn(true);
status.setMessage("User " + user + " logged in successfully.");
status.setToken(token);
objectMapper.writeValue(out, status);
}
use of org.opencms.file.CmsObject in project opencms-ideconnector by mediaworx.
the class IDEConnectorService method importModule.
private void importModule(ModuleImportInfo importInfo) {
String moduleZipPath = importInfo.getModuleZipPath();
String moduleZipName = StringUtils.substringAfterLast(moduleZipPath, File.separator);
String moduleName = StringUtils.substringBeforeLast(moduleZipName, "_");
out.println("******** Importing module zip " + moduleZipName + " to siteRoot " + importInfo.getImportSiteRoot() + " START ********");
out.flush();
CmsObject cmsObject = getCmsObject();
String siteRootBefore = cmsObject.getRequestContext().getSiteRoot();
cmsObject.getRequestContext().setSiteRoot(importInfo.getImportSiteRoot());
PrintStream ps = new PrintStream(new WriterOutputStream(out));
try {
I_CmsReport report = new CmsPrintStreamReport(ps, cmsObject.getRequestContext().getLocale(), false);
if (OpenCms.getModuleManager().getModule(moduleName) != null) {
OpenCms.getModuleManager().deleteModule(cmsObject, moduleName, true, report);
ps.flush();
out.flush();
}
CmsImportParameters params = new CmsImportParameters(moduleZipPath, "/", true);
OpenCms.getImportExportManager().importData(cmsObject, report, params);
ps.flush();
out.println("******** Importing module zip " + moduleZipName + " to siteRoot " + importInfo.getImportSiteRoot() + " FINISHED ********");
out.flush();
} catch (CmsException e) {
LOG.error("Error importing module " + moduleZipPath, e);
}
cmsObject.getRequestContext().setSiteRoot(siteRootBefore);
}
use of org.opencms.file.CmsObject in project opencms-core by alkacon.
the class CmsVfsSitemapService method createSitemapBrokenLinkBean.
/**
* Creates a "broken link" bean based on a resource.<p>
*
* @param resource the resource
*
* @return the "broken link" bean with the data from the resource
*
* @throws CmsException if something goes wrong
*/
protected CmsBrokenLinkBean createSitemapBrokenLinkBean(CmsResource resource) throws CmsException {
CmsObject cms = getCmsObject();
CmsProperty titleProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TITLE, true);
String defaultTitle = "";
String title = titleProp.getValue(defaultTitle);
String path = cms.getSitePath(resource);
String subtitle = path;
return new CmsBrokenLinkBean(resource.getStructureId(), title, subtitle);
}
use of org.opencms.file.CmsObject in project opencms-core by alkacon.
the class CmsCloneModuleThread method run.
/**
* @see java.lang.Thread#run()
*/
@Override
public void run() {
CmsModule sourceModule = OpenCms.getModuleManager().getModule(m_cloneInfo.getSourceModuleName());
// clone the module object
CmsModule targetModule = sourceModule.clone();
targetModule.setName(m_cloneInfo.getName());
targetModule.setNiceName(m_cloneInfo.getNiceName());
targetModule.setDescription(m_cloneInfo.getDescription());
targetModule.setAuthorEmail(m_cloneInfo.getAuthorEmail());
targetModule.setAuthorName(m_cloneInfo.getAuthorName());
targetModule.setGroup(m_cloneInfo.getGroup());
targetModule.setActionClass(m_cloneInfo.getActionClass());
CmsObject cms = getCms();
CmsProject currentProject = cms.getRequestContext().getCurrentProject();
try {
CmsProject workProject = cms.createProject("Clone_module_work_project", "Clone modulee work project", OpenCms.getDefaultUsers().getGroupAdministrators(), OpenCms.getDefaultUsers().getGroupAdministrators(), CmsProject.PROJECT_TYPE_TEMPORARY);
cms.getRequestContext().setCurrentProject(workProject);
// store the module paths
String sourceModulePath = CmsWorkplace.VFS_PATH_MODULES + sourceModule.getName() + "/";
String targetModulePath = CmsWorkplace.VFS_PATH_MODULES + targetModule.getName() + "/";
// store the package name as path part
String sourcePathPart = sourceModule.getName().replaceAll("\\.", "/");
String targetPathPart = targetModule.getName().replaceAll("\\.", "/");
// store the classes folder paths
String sourceClassesPath = targetModulePath + PATH_CLASSES + sourcePathPart + "/";
String targetClassesPath = targetModulePath + PATH_CLASSES + targetPathPart + "/";
// copy the resources
cms.copyResource(sourceModulePath, targetModulePath);
// check if we have to create the classes folder
if (cms.existsResource(sourceClassesPath)) {
// in the source module a classes folder was defined,
// now create all sub-folders for the package structure in the new module folder
createTargetClassesFolder(targetModule, sourceClassesPath, targetModulePath + PATH_CLASSES);
// delete the origin classes folder
deleteSourceClassesFolder(targetModulePath, sourcePathPart, targetPathPart);
}
// TODO: clone module dependencies
// adjust the export points
cloneExportPoints(sourceModule, targetModule, sourcePathPart, targetPathPart);
// adjust the resource type names and IDs
Map<String, String> descKeys = new HashMap<String, String>();
Map<I_CmsResourceType, I_CmsResourceType> resTypeMap = cloneResourceTypes(sourceModule, targetModule, sourcePathPart, targetPathPart, descKeys);
// adjust the explorer type names and store referred icons and message keys
Map<String, String> iconPaths = new HashMap<String, String>();
cloneExplorerTypes(targetModule, iconPaths, descKeys);
// rename the icon file names
cloneExplorerTypeIcons(iconPaths);
// adjust the module resources
adjustModuleResources(sourceModule, targetModule, sourcePathPart, targetPathPart, iconPaths);
// search and replace the localization keys
if (getCms().existsResource(targetClassesPath)) {
List<CmsResource> props = cms.readResources(targetClassesPath, CmsResourceFilter.DEFAULT_FILES);
replacesMessages(descKeys, props);
}
int type = OpenCms.getResourceManager().getResourceType(CmsVfsBundleManager.TYPE_XML_BUNDLE).getTypeId();
CmsResourceFilter filter = CmsResourceFilter.requireType(type);
List<CmsResource> resources = cms.readResources(targetModulePath, filter);
replacesMessages(descKeys, resources);
renameXmlVfsBundles(resources, targetModule, sourceModule.getName());
List<CmsResource> allModuleResources = cms.readResources(targetModulePath, CmsResourceFilter.ALL);
replacePath(sourceModulePath, targetModulePath, allModuleResources);
// search and replace paths
replaceModuleName();
// replace formatter paths
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_cloneInfo.getFormatterTargetModule()) && !targetModule.getResourceTypes().isEmpty()) {
replaceFormatterPaths(targetModule);
}
adjustConfigs(targetModule, resTypeMap);
// now unlock and publish the project
getReport().println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
cms.unlockProject(workProject.getUuid());
OpenCms.getPublishManager().publishProject(cms, getReport());
OpenCms.getPublishManager().waitWhileRunning();
getReport().println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_END_0), I_CmsReport.FORMAT_HEADLINE);
// add the imported module to the module manager
OpenCms.getModuleManager().addModule(cms, targetModule);
// reinitialize the resource manager with additional module resource types if necessary
if (targetModule.getResourceTypes() != Collections.EMPTY_LIST) {
OpenCms.getResourceManager().initialize(cms);
}
// reinitialize the workplace manager with additional module explorer types if necessary
if (targetModule.getExplorerTypes() != Collections.EMPTY_LIST) {
OpenCms.getWorkplaceManager().addExplorerTypeSettings(targetModule);
}
// re-initialize the workplace
OpenCms.getWorkplaceManager().initialize(cms);
// fire "clear caches" event to reload all cached resource bundles
OpenCms.fireCmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap<String, Object>());
// following changes will not be published right now, switch back to previous project
cms.getRequestContext().setCurrentProject(currentProject);
// change resource types and schema locations
if (isTrue(m_cloneInfo.getChangeResourceTypes())) {
changeResourceTypes(resTypeMap);
}
// adjust container pages
CmsObject cloneCms = OpenCms.initCmsObject(cms);
if (isTrue(m_cloneInfo.getApplyChangesEverywhere())) {
cloneCms.getRequestContext().setSiteRoot("/");
}
if (m_cloneInfo.isRewriteContainerPages()) {
CmsResourceFilter f = CmsResourceFilter.requireType(CmsResourceTypeXmlContainerPage.getContainerPageTypeId());
List<CmsResource> allContainerPages = cloneCms.readResources("/", f);
replacePath(sourceModulePath, targetModulePath, allContainerPages);
}
} catch (Throwable e) {
LOG.error(e.getLocalizedMessage(), e);
getReport().addError(e);
} finally {
cms.getRequestContext().setCurrentProject(currentProject);
}
}
use of org.opencms.file.CmsObject in project opencms-core by alkacon.
the class CmsCloneModuleThread method adjustConfigs.
/**
* Adjusts the module configuration file and the formatter configurations.<p>
*
* @param targetModule the target module
* @param resTypeMap the resource type mapping
*
* @throws CmsException if something goes wrong
* @throws UnsupportedEncodingException if the file content could not be read with the determined encoding
*/
private void adjustConfigs(CmsModule targetModule, Map<I_CmsResourceType, I_CmsResourceType> resTypeMap) throws CmsException, UnsupportedEncodingException {
String modPath = CmsWorkplace.VFS_PATH_MODULES + targetModule.getName() + "/";
CmsObject cms = getCms();
if (((m_cloneInfo.getSourceNamePrefix() != null) && (m_cloneInfo.getTargetNamePrefix() != null)) || !m_cloneInfo.getSourceNamePrefix().equals(m_cloneInfo.getTargetNamePrefix())) {
// replace resource type names in formatter configurations
List<CmsResource> resources = cms.readResources(modPath, CmsResourceFilter.requireType(OpenCms.getResourceManager().getResourceType(CmsFormatterConfigurationCache.TYPE_FORMATTER_CONFIG)));
String source = "<Type><!\\[CDATA\\[" + m_cloneInfo.getSourceNamePrefix();
String target = "<Type><!\\[CDATA\\[" + m_cloneInfo.getTargetNamePrefix();
Function<String, String> replaceType = new ReplaceAll(source, target);
for (CmsResource resource : resources) {
transformResource(resource, replaceType);
}
resources.clear();
}
// replace resource type names in module configuration
try {
CmsResource config = cms.readResource(modPath + CmsADEManager.CONFIG_FILE_NAME, CmsResourceFilter.requireType(OpenCms.getResourceManager().getResourceType(CmsADEManager.MODULE_CONFIG_TYPE)));
Function<String, String> substitution = Functions.identity();
for (Map.Entry<I_CmsResourceType, I_CmsResourceType> mapping : resTypeMap.entrySet()) {
substitution = Functions.compose(new ReplaceAll(mapping.getKey().getTypeName(), mapping.getValue().getTypeName()), substitution);
}
// Either replace prefix in or prepend it to the folder name value
Function<String, String> replaceFolderName = new ReplaceAll("(<Folder>[ \n]*<Name><!\\[CDATA\\[)(" + m_cloneInfo.getSourceNamePrefix() + ")?", "$1" + m_cloneInfo.getTargetNamePrefix());
substitution = Functions.compose(replaceFolderName, substitution);
transformResource(config, substitution);
} catch (CmsVfsResourceNotFoundException e) {
LOG.info(e.getLocalizedMessage(), e);
}
}
Aggregations