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>
* <file>
* <destination>testfolder</destination>
* <type>folder</type>
* <uuidstructure>41a22af8-4b8f-11e3-b543-210cc9a3bba6</uuidstructure>
* <datelastmodified>Tue, 12 Nov 2013 11:40:40 GMT</datelastmodified>
* <userlastmodified>Admin</userlastmodified>
* <datecreated>Tue, 12 Nov 2013 11:40:40 GMT</datecreated>
* <usercreated>Admin</usercreated>
* <flags>0</flags>
* <properties/>
* <relations/>
* <accesscontrol/>
* </file>
* </pre>
* Sample xml for VFS files:
* <pre>
* <fileinfo>
* <file>
* <source>testfolder/testfile.jsp</source>
* <destination>testfolder/testfile.jsp</destination>
* <type>plain</type>
* <uuidstructure>0e436b9f-5c5d-11e3-91b4-210cc9a3bba6</uuidstructure>
* <uuidresource>0e436ba0-5c5d-11e3-91b4-210cc9a3bba6</uuidresource>
* <datelastmodified>Thu, 05 Dec 2013 23:31:52 GMT</datelastmodified>
* <userlastmodified>Admin</userlastmodified>
* <datecreated>Tue, 03 Dec 2013 20:54:09 GMT</datecreated>
* <usercreated>Admin</usercreated>
* <flags>0</flags>
* <properties/>
* <relations/>
* <accesscontrol/>
* </file>
* <siblingcount>1</siblingcount>
* </fileinfo>
* </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;
}
}
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;
}
}
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());
}
}
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);
}
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);
}
Aggregations