Search in sources :

Example 1 with CmsObject

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);
}
Also used : CmsObject(org.opencms.file.CmsObject) LoginStatusImpl(com.mediaworx.opencms.ideconnector.dataimpl.LoginStatusImpl) CmsException(org.opencms.main.CmsException) LoginStatus(com.mediaworx.opencms.ideconnector.data.LoginStatus) CmsUUID(org.opencms.util.CmsUUID)

Example 2 with CmsObject

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);
}
Also used : PrintStream(java.io.PrintStream) CmsObject(org.opencms.file.CmsObject) CmsPrintStreamReport(org.opencms.report.CmsPrintStreamReport) CmsException(org.opencms.main.CmsException) I_CmsReport(org.opencms.report.I_CmsReport) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) CmsImportParameters(org.opencms.importexport.CmsImportParameters)

Example 3 with CmsObject

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);
}
Also used : CmsObject(org.opencms.file.CmsObject) CmsProperty(org.opencms.file.CmsProperty) CmsBrokenLinkBean(org.opencms.gwt.shared.CmsBrokenLinkBean)

Example 4 with CmsObject

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);
    }
}
Also used : HashMap(java.util.HashMap) CmsExportPoint(org.opencms.db.CmsExportPoint) I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsModule(org.opencms.module.CmsModule) CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) CmsResourceFilter(org.opencms.file.CmsResourceFilter) CmsObject(org.opencms.file.CmsObject) CmsProject(org.opencms.file.CmsProject)

Example 5 with CmsObject

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);
    }
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CmsObject (org.opencms.file.CmsObject)1052 CmsResource (org.opencms.file.CmsResource)483 CmsException (org.opencms.main.CmsException)323 ArrayList (java.util.ArrayList)165 CmsFile (org.opencms.file.CmsFile)145 CmsUUID (org.opencms.util.CmsUUID)129 Locale (java.util.Locale)90 CmsProperty (org.opencms.file.CmsProperty)89 List (java.util.List)81 HashMap (java.util.HashMap)80 CmsShellReport (org.opencms.report.CmsShellReport)77 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)70 CmsProject (org.opencms.file.CmsProject)66 CmsUser (org.opencms.file.CmsUser)60 File (java.io.File)54 CmsXmlEntityResolver (org.opencms.xml.CmsXmlEntityResolver)51 IOException (java.io.IOException)42 CmsIllegalArgumentException (org.opencms.main.CmsIllegalArgumentException)42 CmsLoaderException (org.opencms.loader.CmsLoaderException)41 Map (java.util.Map)40