Search in sources :

Example 1 with CmsResource

use of org.opencms.file.CmsResource in project opencms-ideconnector by mediaworx.

the class OpenCmsIDEConnector method publishResources.

/**
 * Publishes all the resources contained in the JSON array that was passed in as request parameter "json".
 * If the request parameter "publishSubResources" was set to "true" sub resources are published as well, otherwise
 * sub resources are not published.
 */
private void publishResources() {
    LOG.info("IntelliJ triggered publish. Publishing the following resources (if necessary):");
    String[] resourcePaths = getStringArrayFromJSON(json);
    boolean publishSubResources = "true".equals(request.getParameter("publishSubResources"));
    List<CmsResource> publishResources = new ArrayList<CmsResource>(resourcePaths.length);
    boolean hasWarnings = false;
    StringBuilder warnings = new StringBuilder();
    for (String resourcePath : resourcePaths) {
        if (cmsObject.existsResource(resourcePath, CmsResourceFilter.ALL)) {
            CmsResource resource;
            try {
                resource = cmsObject.readResource(resourcePath, CmsResourceFilter.ALL);
            } catch (CmsException e) {
                String message = resourcePath + " could not be read from the VFS";
                warnings.append(message).append("\n");
                LOG.warn(message, e);
                hasWarnings = true;
                continue;
            }
            LOG.info("    " + resourcePath);
            publishResources.add(resource);
        }
    }
    if (publishResources.size() > 0) {
        publish: {
            CmsPublishManager publishManager = OpenCms.getPublishManager();
            CmsPublishList publishList;
            try {
                publishList = publishManager.getPublishList(cmsObject, publishResources, false, publishSubResources);
            } catch (CmsException e) {
                String message = "Error retrieving CmsPublishList from OpenCms";
                warnings.append(message).append("\n");
                LOG.warn(message, e);
                hasWarnings = true;
                break publish;
            }
            I_CmsReport report = new CmsLogReport(Locale.ENGLISH, OpenCmsIDEConnector.class);
            try {
                List<CmsResource> resources = publishList.getAllResources();
                for (CmsResource resource : resources) {
                    if (resource.getState().isDeleted()) {
                        LOG.info("DELETED resource " + resource.getRootPath() + " will be published");
                    } else {
                        LOG.info("Resource " + resource.getRootPath() + " will be published");
                    }
                }
                publishManager.publishProject(cmsObject, report, publishList);
            } catch (CmsException e) {
                String message = "Error publishing the resources: " + e.getMessage();
                warnings.append(message).append("\n");
                LOG.warn(message, e);
                hasWarnings = true;
            }
        }
    }
    if (!hasWarnings) {
        println("OK");
    } else {
        println(warnings.toString());
    }
}
Also used : CmsPublishList(org.opencms.db.CmsPublishList) CmsResource(org.opencms.file.CmsResource) ArrayList(java.util.ArrayList) CmsException(org.opencms.main.CmsException) I_CmsReport(org.opencms.report.I_CmsReport) CmsLogReport(org.opencms.report.CmsLogReport) CmsPublishManager(org.opencms.publish.CmsPublishManager)

Example 2 with CmsResource

use of org.opencms.file.CmsResource in project opencms-core by alkacon.

the class CmsAdvancedDirectEditProvider method getResourceInfo.

/**
 * @see org.opencms.workplace.editors.directedit.A_CmsDirectEditProvider#getResourceInfo(java.lang.String)
 *
 * Similar to the method in the superclass, but removes the write permission check, as this is handled differently.
 */
@Override
public CmsDirectEditResourceInfo getResourceInfo(String resourceName) {
    try {
        // first check some simple preconditions for direct edit
        if (m_cms.getRequestContext().getCurrentProject().isOnlineProject()) {
            // don't show direct edit button in online project
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        if (CmsResource.isTemporaryFileName(resourceName)) {
            // don't show direct edit button on a temporary file
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        if (!m_cms.isInsideCurrentProject(resourceName)) {
            // don't show direct edit button on files not belonging to the current project
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        // read the target resource
        CmsResource resource = m_cms.readResource(resourceName, CmsResourceFilter.ALL);
        if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable() && !resource.isFolder()) {
            // don't show direct edit button for non-editable resources
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        // check the resource lock
        CmsLock lock = m_cms.getLock(resource);
        boolean locked = !(lock.isUnlocked() || lock.isOwnedInProjectBy(m_cms.getRequestContext().getCurrentUser(), m_cms.getRequestContext().getCurrentProject()));
        // only if write permissions are granted the resource may be direct editable
        if (locked) {
            // a locked resource must be shown as "disabled"
            return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.DISABLED, resource, lock);
        }
        // if we have write permission and the resource is not locked then direct edit is enabled
        return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.ENABLED, resource, lock);
    } catch (Exception e) {
        // all exceptions: don't mix up the result HTML, always return INACTIVE mode
        if (LOG.isWarnEnabled()) {
            LOG.warn(org.opencms.workplace.editors.Messages.get().getBundle().key(org.opencms.workplace.editors.Messages.LOG_CALC_EDIT_MODE_FAILED_1, resourceName), e);
        }
    }
    // otherwise the resource is not direct editable
    return CmsDirectEditResourceInfo.INACTIVE;
}
Also used : CmsResource(org.opencms.file.CmsResource) CmsLock(org.opencms.lock.CmsLock) JspException(javax.servlet.jsp.JspException) JSONException(org.opencms.json.JSONException) CmsException(org.opencms.main.CmsException)

Example 3 with CmsResource

use of org.opencms.file.CmsResource in project opencms-core by alkacon.

the class A_CmsDirectEditProvider method getResourceInfo.

/**
 * Calculates the direct edit resource information for the given VFS resource.<p>
 *
 * This includes the direct edit permissions.
 * If the permissions are not {@link CmsDirectEditPermissions#INACTIVE}, then the resource and lock
 * information is also included in the result.<p>
 *
 * @param resourceName the name of the VFS resource to get the direct edit info for
 *
 * @return the direct edit resource information for the given VFS resource
 */
public CmsDirectEditResourceInfo getResourceInfo(String resourceName) {
    try {
        // first check some simple preconditions for direct edit
        if (m_cms.getRequestContext().getCurrentProject().isOnlineProject()) {
            // don't show direct edit button in online project
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        if (CmsResource.isTemporaryFileName(resourceName)) {
            // don't show direct edit button on a temporary file
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        if (!m_cms.isInsideCurrentProject(resourceName)) {
            // don't show direct edit button on files not belonging to the current project
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        // read the target resource
        CmsResource resource = m_cms.readResource(resourceName, CmsResourceFilter.ALL);
        if (!OpenCms.getResourceManager().getResourceType(resource.getTypeId()).isDirectEditable() && !resource.isFolder()) {
            // don't show direct edit button for non-editable resources
            return CmsDirectEditResourceInfo.INACTIVE;
        }
        // check the resource lock
        CmsLock lock = m_cms.getLock(resource);
        boolean locked = !(lock.isUnlocked() || lock.isOwnedInProjectBy(m_cms.getRequestContext().getCurrentUser(), m_cms.getRequestContext().getCurrentProject()));
        // check the users permissions on the resource
        if (m_cms.hasPermissions(resource, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.IGNORE_EXPIRATION)) {
            // only if write permissions are granted the resource may be direct editable
            if (locked) {
                // a locked resource must be shown as "disabled"
                return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.DISABLED, resource, lock);
            }
            // if we have write permission and the resource is not locked then direct edit is enabled
            return new CmsDirectEditResourceInfo(CmsDirectEditPermissions.ENABLED, resource, lock);
        }
    } catch (Exception e) {
        // all exceptions: don't mix up the result HTML, always return INACTIVE mode
        if (LOG.isWarnEnabled()) {
            LOG.warn(org.opencms.workplace.editors.Messages.get().getBundle().key(org.opencms.workplace.editors.Messages.LOG_CALC_EDIT_MODE_FAILED_1, resourceName), e);
        }
    }
    // otherwise the resource is not direct editable
    return CmsDirectEditResourceInfo.INACTIVE;
}
Also used : CmsResource(org.opencms.file.CmsResource) CmsLock(org.opencms.lock.CmsLock) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) CmsConfigurationException(org.opencms.configuration.CmsConfigurationException)

Example 4 with CmsResource

use of org.opencms.file.CmsResource in project opencms-core by alkacon.

the class CmsInternalLinkValidationList method fillDetails.

/**
 * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
 */
@Override
protected void fillDetails(String detailId) {
    // get content
    List resourceNames = getList().getAllContent();
    Iterator i = resourceNames.iterator();
    while (i.hasNext()) {
        CmsListItem item = (CmsListItem) i.next();
        CmsResource res = getCollector().getResource(getCms(), item);
        // check if errors are enabled
        StringBuffer html = new StringBuffer();
        // broken links detail is enabled
        if (detailId.equals(LIST_DETAIL_LINKS)) {
            // get all errors for this resource and show them
            List brokenLinks = getValidator().getBrokenLinksForResource(res.getRootPath());
            if (brokenLinks != null) {
                Iterator j = brokenLinks.iterator();
                while (j.hasNext()) {
                    CmsRelation brokenLink = (CmsRelation) j.next();
                    String link = brokenLink.getTargetPath();
                    String siteRoot = OpenCms.getSiteManager().getSiteRoot(link);
                    String siteName = siteRoot;
                    if (siteRoot != null) {
                        String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
                        try {
                            getCms().getRequestContext().setSiteRoot("/");
                            siteName = getCms().readPropertyObject(siteRoot, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(siteRoot);
                        } catch (CmsException e) {
                            siteName = siteRoot;
                        } finally {
                            getCms().getRequestContext().setSiteRoot(storedSiteRoot);
                        }
                        link = link.substring(siteRoot.length());
                    } else {
                        siteName = "/";
                    }
                    if (!getCms().getRequestContext().getSiteRoot().equals(siteRoot)) {
                        link = key(org.opencms.workplace.commons.Messages.GUI_DELETE_SITE_RELATION_2, new Object[] { siteName, link });
                    }
                    html.append(link);
                    html.append("<br>");
                }
                item.set(detailId, html.toString());
            }
        }
    }
}
Also used : CmsListItem(org.opencms.workplace.list.CmsListItem) CmsResource(org.opencms.file.CmsResource) Iterator(java.util.Iterator) CmsException(org.opencms.main.CmsException) CmsRelation(org.opencms.relations.CmsRelation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with CmsResource

use of org.opencms.file.CmsResource 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)

Aggregations

CmsResource (org.opencms.file.CmsResource)1236 CmsException (org.opencms.main.CmsException)575 CmsObject (org.opencms.file.CmsObject)505 ArrayList (java.util.ArrayList)306 CmsUUID (org.opencms.util.CmsUUID)226 CmsFile (org.opencms.file.CmsFile)170 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)163 CmsProperty (org.opencms.file.CmsProperty)125 Locale (java.util.Locale)108 HashMap (java.util.HashMap)97 I_CmsResourceType (org.opencms.file.types.I_CmsResourceType)84 I_CmsResource (org.opencms.file.I_CmsResource)83 List (java.util.List)79 CmsProject (org.opencms.file.CmsProject)65 CmsRelation (org.opencms.relations.CmsRelation)61 IOException (java.io.IOException)60 CmsLoaderException (org.opencms.loader.CmsLoaderException)58 CmsLock (org.opencms.lock.CmsLock)55 HashSet (java.util.HashSet)53 CmsResourceFilter (org.opencms.file.CmsResourceFilter)53