Search in sources :

Example 1 with CmsException

use of org.opencms.main.CmsException in project opencms-ideconnector by mediaworx.

the class MetaXmlHelper method getResourceInfo.

/**
 * Creates the resource info XML for the VFS file/folder at the given resource path.
 * <br /><br />
 * Sample xml for VFS folders:
 * <pre>
 * &lt;file&gt;
 *     &lt;destination&gt;testfolder&lt;/destination&gt;
 *     &lt;type&gt;folder&lt;/type&gt;
 *     &lt;uuidstructure&gt;41a22af8-4b8f-11e3-b543-210cc9a3bba6&lt;/uuidstructure&gt;
 *     &lt;datelastmodified&gt;Tue, 12 Nov 2013 11:40:40 GMT&lt;/datelastmodified&gt;
 *     &lt;userlastmodified&gt;Admin&lt;/userlastmodified&gt;
 *     &lt;datecreated&gt;Tue, 12 Nov 2013 11:40:40 GMT&lt;/datecreated&gt;
 *     &lt;usercreated&gt;Admin&lt;/usercreated&gt;
 *     &lt;flags&gt;0&lt;/flags&gt;
 *     &lt;properties/&gt;
 *     &lt;relations/&gt;
 *     &lt;accesscontrol/&gt;
 * &lt;/file&gt;
 * </pre>
 * Sample xml for VFS files:
 * <pre>
 * &lt;fileinfo&gt;
 *     &lt;file&gt;
 *         &lt;source&gt;testfolder/testfile.jsp&lt;/source&gt;
 *         &lt;destination&gt;testfolder/testfile.jsp&lt;/destination&gt;
 *         &lt;type&gt;plain&lt;/type&gt;
 *         &lt;uuidstructure&gt;0e436b9f-5c5d-11e3-91b4-210cc9a3bba6&lt;/uuidstructure&gt;
 *         &lt;uuidresource&gt;0e436ba0-5c5d-11e3-91b4-210cc9a3bba6&lt;/uuidresource&gt;
 *         &lt;datelastmodified&gt;Thu, 05 Dec 2013 23:31:52 GMT&lt;/datelastmodified&gt;
 *         &lt;userlastmodified&gt;Admin&lt;/userlastmodified&gt;
 *         &lt;datecreated&gt;Tue, 03 Dec 2013 20:54:09 GMT&lt;/datecreated&gt;
 *         &lt;usercreated&gt;Admin&lt;/usercreated&gt;
 *         &lt;flags&gt;0&lt;/flags&gt;
 *         &lt;properties/&gt;
 *         &lt;relations/&gt;
 *         &lt;accesscontrol/&gt;
 *     &lt;/file&gt;
 *     &lt;siblingcount&gt;1&lt;/siblingcount&gt;
 * &lt;/fileinfo&gt;
 * </pre>
 * @param resourcePath  VFS path of the resource for which meta information should be returned.
 * @return meta info XML for the resource at the given path.
 */
public String getResourceInfo(String resourcePath) {
    try {
        CmsResource resource = cmsObject.readResource(resourcePath);
        if (!resource.isFolder()) {
            Element resourceInfo = DocumentHelper.createElement(NODE_FILE_INFO);
            resourceInfo.add(getFileElement(resource));
            Element siblingCount = resourceInfo.addElement(NODE_SIBLING_COUNT);
            siblingCount.setText(String.valueOf(resource.getSiblingCount()));
            return getFormattedStringForDocument(DocumentHelper.createDocument(resourceInfo));
        } else {
            return getFormattedStringForDocument(DocumentHelper.createDocument(getFileElement(resource)));
        }
    } catch (CmsException e) {
        LOG.error("Resource " + resourcePath + " can't be read", e);
        return null;
    }
}
Also used : Element(org.dom4j.Element) CmsException(org.opencms.main.CmsException)

Example 2 with CmsException

use of org.opencms.main.CmsException in project opencms-ideconnector by mediaworx.

the class MetaXmlHelper method getFileElement.

/**
 * Internal method used to create the XML file node for the given resource. This is a modified version of the
 * standard OpenCms method <code>org.opencms.importexport.CmsExport.appendResourceToManifest</code>.
 * @param resource  the resource for which the file node shold be returned.
 * @return  the file node for the given resource.
 */
private Element getFileElement(CmsResource resource) {
    String rootPath = resource.getRootPath();
    try {
        String fileName = trimResourceName(rootPath);
        // it is not allowed to export organizational unit resources
        if (fileName.startsWith("system/orgunits")) {
            return null;
        }
        // define the file node
        Element fileElement = DocumentHelper.createElement(CmsImportVersion7.N_FILE);
        // only write <source> if resource is a file
        if (resource.isFile()) {
            fileElement.addElement(CmsImportVersion7.N_SOURCE).addText("${" + CmsImportVersion7.N_SOURCE + "}");
        }
        fileElement.addElement(CmsImportVersion7.N_DESTINATION).addText("${" + CmsImportVersion7.N_DESTINATION + "}");
        fileElement.addElement(CmsImportVersion7.N_TYPE).addText(OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getTypeName());
        fileElement.addElement(CmsImportVersion7.N_UUIDSTRUCTURE).addText(useIdVariablesEnabled ? "${" + CmsImportVersion7.N_UUIDSTRUCTURE + "}" : resource.getStructureId().toString());
        if (resource.isFile()) {
            fileElement.addElement(CmsImportVersion7.N_UUIDRESOURCE).addText(useIdVariablesEnabled ? "${" + CmsImportVersion7.N_UUIDRESOURCE + "}" : resource.getResourceId().toString());
        }
        fileElement.addElement(CmsImportVersion7.N_DATELASTMODIFIED).addText(useDateVariablesEnabled ? "${" + CmsImportVersion7.N_DATELASTMODIFIED + "}" : CmsDateUtil.getHeaderDate(resource.getDateLastModified()));
        String userNameLastModified;
        try {
            userNameLastModified = cmsObject.readUser(resource.getUserLastModified()).getName();
        } catch (CmsException e) {
            userNameLastModified = OpenCms.getDefaultUsers().getUserAdmin();
        }
        fileElement.addElement(CmsImportVersion7.N_USERLASTMODIFIED).addText(userNameLastModified);
        fileElement.addElement(CmsImportVersion7.N_DATECREATED).addText(useDateVariablesEnabled ? "${" + CmsImportVersion7.N_DATECREATED + "}" : CmsDateUtil.getHeaderDate(resource.getDateCreated()));
        String userNameCreated;
        try {
            userNameCreated = cmsObject.readUser(resource.getUserCreated()).getName();
        } catch (CmsException e) {
            userNameCreated = OpenCms.getDefaultUsers().getUserAdmin();
        }
        fileElement.addElement(CmsImportVersion7.N_USERCREATED).addText(userNameCreated);
        if (resource.getDateReleased() != CmsResource.DATE_RELEASED_DEFAULT) {
            fileElement.addElement(CmsImportVersion7.N_DATERELEASED).addText(CmsDateUtil.getHeaderDate(resource.getDateReleased()));
        }
        if (resource.getDateExpired() != CmsResource.DATE_EXPIRED_DEFAULT) {
            fileElement.addElement(CmsImportVersion7.N_DATEEXPIRED).addText(CmsDateUtil.getHeaderDate(resource.getDateExpired()));
        }
        int resFlags = resource.getFlags();
        resFlags &= ~CmsResource.FLAG_LABELED;
        fileElement.addElement(CmsImportVersion7.N_FLAGS).addText(Integer.toString(resFlags));
        // properties
        Element propertiesElement = fileElement.addElement(CmsImportVersion7.N_PROPERTIES);
        List<CmsProperty> properties = cmsObject.readPropertyObjects(cmsObject.getSitePath(resource), false);
        Collections.sort(properties);
        for (CmsProperty property : properties) {
            if (property == null) {
                continue;
            }
            addPropertyNode(propertiesElement, property.getName(), property.getStructureValue(), false);
            addPropertyNode(propertiesElement, property.getName(), property.getResourceValue(), true);
        }
        // relations
        List<CmsRelation> relations = cmsObject.getRelationsForResource(resource, CmsRelationFilter.TARGETS.filterNotDefinedInContent());
        Element relationsElement = fileElement.addElement(CmsImportVersion7.N_RELATIONS);
        for (CmsRelation relation : relations) {
            CmsResource target;
            try {
                target = relation.getTarget(cmsObject, CmsResourceFilter.ALL);
            }// if the relation's target is not found, LOG it and skip
             catch (CmsVfsResourceNotFoundException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("relation target " + relation.getTargetPath() + " not found for " + rootPath, e);
                }
                continue;
            }
            addRelationNode(relationsElement, target.getStructureId().toString(), target.getRootPath(), relation.getType().getName());
        }
        // access control
        Element acl = fileElement.addElement(CmsImportVersion7.N_ACCESSCONTROL_ENTRIES);
        // read the access control entries
        List<CmsAccessControlEntry> fileAcEntries = cmsObject.getAccessControlEntries(rootPath, false);
        // create xml elements for each access control entry
        for (CmsAccessControlEntry ace : fileAcEntries) {
            Element accessentry = acl.addElement(CmsImportVersion7.N_ACCESSCONTROL_ENTRY);
            // now check if the principal is a group or a user
            int flags = ace.getFlags();
            String acePrincipalName;
            CmsUUID acePrincipal = ace.getPrincipal();
            if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_ALLOTHERS) > 0) {
                acePrincipalName = CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME;
            } else if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE_ALL) > 0) {
                acePrincipalName = CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME;
            } else if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_GROUP) > 0) {
                // the principal is a group
                acePrincipalName = cmsObject.readGroup(acePrincipal).getPrefixedName();
            } else if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_USER) > 0) {
                // the principal is a user
                acePrincipalName = cmsObject.readUser(acePrincipal).getPrefixedName();
            } else {
                // the principal is a role
                acePrincipalName = CmsRole.PRINCIPAL_ROLE + "." + CmsRole.valueOfId(acePrincipal).getRoleName();
            }
            accessentry.addElement(CmsImportVersion7.N_ACCESSCONTROL_PRINCIPAL).addText(acePrincipalName);
            accessentry.addElement(CmsImportVersion7.N_FLAGS).addText(Integer.toString(flags));
            Element permissionset = accessentry.addElement(CmsImportVersion7.N_ACCESSCONTROL_PERMISSIONSET);
            permissionset.addElement(CmsImportVersion7.N_ACCESSCONTROL_ALLOWEDPERMISSIONS).addText(Integer.toString(ace.getAllowedPermissions()));
            permissionset.addElement(CmsImportVersion7.N_ACCESSCONTROL_DENIEDPERMISSIONS).addText(Integer.toString(ace.getDeniedPermissions()));
        }
        return fileElement;
    } catch (CmsException e) {
        LOG.error("There was a CmsException while trying to genereate the XML info for the resource " + rootPath, e);
        return null;
    }
}
Also used : Element(org.dom4j.Element) CmsAccessControlEntry(org.opencms.security.CmsAccessControlEntry) CmsRelation(org.opencms.relations.CmsRelation) CmsException(org.opencms.main.CmsException) CmsUUID(org.opencms.util.CmsUUID)

Example 3 with CmsException

use of org.opencms.main.CmsException 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 4 with CmsException

use of org.opencms.main.CmsException 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 5 with CmsException

use of org.opencms.main.CmsException 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)

Aggregations

CmsException (org.opencms.main.CmsException)1077 CmsResource (org.opencms.file.CmsResource)476 CmsObject (org.opencms.file.CmsObject)280 ArrayList (java.util.ArrayList)242 CmsUUID (org.opencms.util.CmsUUID)183 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)135 CmsIllegalArgumentException (org.opencms.main.CmsIllegalArgumentException)97 HashMap (java.util.HashMap)96 CmsFile (org.opencms.file.CmsFile)90 CmsProperty (org.opencms.file.CmsProperty)88 Locale (java.util.Locale)87 CmsUser (org.opencms.file.CmsUser)87 IOException (java.io.IOException)82 CmsDataAccessException (org.opencms.file.CmsDataAccessException)80 CmsSecurityException (org.opencms.security.CmsSecurityException)79 CmsProject (org.opencms.file.CmsProject)78 CmsVfsResourceAlreadyExistsException (org.opencms.file.CmsVfsResourceAlreadyExistsException)75 CmsVfsException (org.opencms.file.CmsVfsException)72 CmsLockException (org.opencms.lock.CmsLockException)72 CmsTooManyPublishResourcesException (org.opencms.ade.publish.CmsTooManyPublishResourcesException)65