/** * Rewrites the content of the given file.<p> * * @param resource the resource to rewrite the content for * * @throws CmsException if something goes wrong */ private static void hardTouch(CmsObject cms, CmsResource resource) throws CmsException { CmsFile file = cms.readFile(resource); file.setContents(file.getContents()); cms.writeFile(file); }
/** * Replaces the messages for the given resources.<p> * * @param descKeys the replacement mapping * @param resources the resources to consult * * @throws CmsException if something goes wrong * @throws UnsupportedEncodingException if the file content could not be read with the determined encoding */ private void replacesMessages(Map<String, String> descKeys, List<CmsResource> resources) throws CmsException, UnsupportedEncodingException { for (CmsResource resource : resources) { CmsFile file = getCms().readFile(resource); String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file); String content = new String(file.getContents(), encoding); for (Map.Entry<String, String> entry : descKeys.entrySet()) { content = content.replaceAll(entry.getKey(), entry.getValue()); } file.setContents(content.getBytes(encoding)); getCms().writeFile(file); } }
/** * Rewrites the links included in the content itself.<p> * * @param file the file for which the links should be replaced * @param relations the original relations * * @throws CmsException if something goes wrong */ protected void rewriteContent(CmsFile file, Collection<CmsRelation> relations) throws CmsException { LOG.info("Rewriting in-content links for " + file.getRootPath()); CmsPair<String, String> contentAndEncoding = decode(file); String content = contentAndEncoding.getFirst(); String encodingForSave = contentAndEncoding.getSecond(); String newContent = rewriteContentString(content); byte[] newContentBytes; try { newContentBytes = newContent.getBytes(encodingForSave); } catch (UnsupportedEncodingException e) { newContentBytes = newContent.getBytes(); } file.setContents(newContentBytes); m_cms.writeFile(file); }
/** * @see org.opencms.file.types.A_CmsResourceType#writeFile(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsFile) */ @Override public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException { // actualize the link paths and/or ids CmsJspLinkMacroResolver macroResolver = new CmsJspLinkMacroResolver(cms, resource.getRootPath(), false); String encoding = CmsLocaleManager.getResourceEncoding(cms, resource); String content = CmsEncoder.createString(resource.getContents(), encoding); content = macroResolver.resolveMacros(content); try { resource.setContents(content.getBytes(encoding)); } catch (UnsupportedEncodingException e) { // this should usually never happen since the encoding is already used before resource.setContents(content.getBytes()); } // write the content with the 'right' links Set<String> references = getReferencingStrongLinks(cms, resource); CmsFile file = super.writeFile(cms, securityManager, resource); removeReferencingFromCache(references); return file; }
/** * @see org.opencms.file.types.A_CmsResourceType#writeFile(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsFile) */ @Override public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException { // actualize the link paths and/or ids CmsJspLinkMacroResolver macroResolver = new CmsJspLinkMacroResolver(cms, resource.getRootPath(), false); String encoding = CmsLocaleManager.getResourceEncoding(cms, resource); String content = CmsEncoder.createString(resource.getContents(), encoding); content = macroResolver.resolveMacros(content); try { resource.setContents(content.getBytes(encoding)); } catch (UnsupportedEncodingException e) { // this should usually never happen since the encoding is already used before resource.setContents(content.getBytes()); } // write the content with the 'right' links Set references = getReferencingStrongLinks(cms, resource); CmsFile file = super.writeFile(cms, securityManager, resource); removeReferencingFromCache(references); return file; }
/** * Saves given container page in the current locale, and not only in memory but also to VFS.<p> * * @param cms the current cms context * @param cntPage the container page to save * @param ifChangedOnly <code>true</code> to only write the file if the content has changed * * @throws CmsException if something goes wrong */ public void save(CmsObject cms, CmsContainerPageBean cntPage, boolean ifChangedOnly) throws CmsException { CmsFile file = getFile(); byte[] data = createContainerPageXml(cms, cntPage); if (ifChangedOnly && Arrays.equals(file.getContents(), data)) { return; } // lock the file cms.lockResourceTemporary(file); file.setContents(data); cms.writeFile(file); }
/** * Save the values to the bundle descriptor. * @throws CmsException thrown if saving fails. */ private void saveToBundleDescriptor() throws CmsException { if (null != m_descFile) { m_removeDescriptorOnCancel = false; updateBundleDescriptorContent(); m_descFile.getFile().setContents(m_descContent.marshal()); m_cms.writeFile(m_descFile.getFile()); } }
/** * Rewrites the content of the given file.<p> * * @param resource the resource to rewrite the content for * * @throws CmsException if something goes wrong */ private void hardTouch(CmsResource resource) throws CmsException { CmsFile file = getCms().readFile(resource); file.setContents(file.getContents()); getCms().writeFile(file); } }
/** * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List) */ @Override public CmsResource createResource( CmsObject cms, String resourcename, int type, byte[] content, List<CmsProperty> properties) throws CmsException, CmsIllegalArgumentException { if (matchParentPath(IMPORT_PATH, resourcename)) { CmsResource res = createFakeBinaryFile(resourcename, 0); CmsFile file = new CmsFile(res); file.setContents(content); OpenCms.getModuleManager().getImportExportRepository().importModule( CmsResource.getName(resourcename), content); m_importFileUpdateCache.put(resourcename, Long.valueOf(System.currentTimeMillis())); return file; } else { return super.createResource(cms, resourcename, type, content, properties); } }
/** * Rewrites the content of the given file.<p> * * @param cms the CmsObject * @param resource the resource to rewrite the content for * * @throws CmsException if something goes wrong */ private void hardTouch(CmsObject cms, CmsResource resource) throws CmsException { CmsFile file = cms.readFile(resource); cms = OpenCms.initCmsObject(cms); cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE); file.setContents(file.getContents()); cms.writeFile(file); }
/** * Saves the current editor content.<p> */ void save() { try { byte[] content = m_codeMirror.getValue().getBytes(m_file.getEncoding()); m_file.getFile().setContents(content); A_CmsUI.getCmsObject().writeFile(m_file.getFile()); m_changed = false; m_save.setEnabled(false); m_saveAndExit.setEnabled(false); } catch (Exception e) { CmsErrorDialog.showErrorDialog(e); } }
/** * Writes the xml content to the vfs and re-initializes the member variables.<p> * * @throws CmsException if writing the file fails */ private void writeContent() throws CmsException { String decodedContent = m_content.toString(); try { m_file.setContents(decodedContent.getBytes(getFileEncoding())); } catch (UnsupportedEncodingException e) { throw new CmsException(Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()), e); } // the file content might have been modified during the write operation m_file = getCloneCms().writeFile(m_file); m_content = CmsXmlContentFactory.unmarshal(getCloneCms(), m_file); } }
/** * Rewrites the content of the given file.<p> * * @param resource the resource to rewrite the content for * * @throws CmsException if something goes wrong */ private void hardTouch(CmsResource resource) throws CmsException { CmsFile file = m_context.getCms().readFile(resource); CmsObject cms = OpenCms.initCmsObject(m_context.getCms()); cms.getRequestContext().setAttribute(CmsXmlContent.AUTO_CORRECTION_ATTRIBUTE, Boolean.TRUE); file.setContents(file.getContents()); cms.writeFile(file); }
/** * @see org.opencms.gwt.shared.rpc.I_CmsVfsService#createNewExternalLink(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ public void createNewExternalLink(String title, String link, String resourceName, String parentFolderPath) throws CmsRpcException { CmsObject cms = getCmsObject(); try { CmsProperty titleProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, title, null); @SuppressWarnings("deprecation") CmsResource resource = cms.createResource( CmsStringUtil.joinPaths(parentFolderPath, resourceName), CmsResourceTypePointer.getStaticTypeId(), new byte[0], Collections.singletonList(titleProp)); CmsFile file = cms.readFile(resource); file.setContents(link.getBytes(CmsLocaleManager.getResourceEncoding(cms, resource))); cms.writeFile(file); tryUnlock(resource); // update the offline search indices OpenCms.getSearchManager().updateOfflineIndexes(); } catch (Exception e) { error(e); } }
/** * Reads a file into a string, applies a transformation to the string, and writes the string back to the file.<p> * * @param resource the resource to transform * @param transformation the transformation to apply * @throws CmsException if something goes wrong * @throws UnsupportedEncodingException in case the encoding is not supported */ private void transformResource(CmsResource resource, Function<String, String> transformation) throws CmsException, UnsupportedEncodingException { CmsFile file = getCms().readFile(resource); if (CmsResourceTypeXmlContent.isXmlContent(file)) { CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file); xmlContent.setAutoCorrectionEnabled(true); file = xmlContent.correctXmlStructure(getCms()); } String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file); String content = new String(file.getContents(), encoding); content = transformation.apply(content); file.setContents(content.getBytes(encoding)); lockResource(getCms(), file); getCms().writeFile(file); } }
/** * Writes the new detail page information to the configuration file.<p> * * @param infos the new detail page information * @param newId the id to use for new pages * * @throws CmsException if something goes wrong */ public void updateAndSave(List<CmsDetailPageInfo> infos, CmsUUID newId) throws CmsException { //lock(m_cms, m_resource); getDocument(); removeOldValues(); writeDetailPageInfos(infos, newId); m_document.setAutoCorrectionEnabled(true); m_document.correctXmlStructure(m_cms); byte[] content = m_document.marshal(); m_file.setContents(content); m_cms.writeFile(m_file); //m_cms.unlockResource(m_cms.getSitePath(m_resource)); }
/** * Writes the xml content to the vfs and re-initializes the member variables.<p> * * @throws CmsException if writing the file fails */ private void writeContent() throws CmsException { String decodedContent = m_content.toString(); try { m_file.setContents(decodedContent.getBytes(getFileEncoding())); } catch (UnsupportedEncodingException e) { throw new CmsException(Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()), e); } // the file content might have been modified during the write operation CmsObject cloneCms = getCloneCms(); CmsUUID tempProjectId = OpenCms.getWorkplaceManager().getTempFileProjectId(); cloneCms.getRequestContext().setCurrentProject(getCms().readProject(tempProjectId)); m_file = cloneCms.writeFile(m_file); m_content = CmsXmlContentFactory.unmarshal(cloneCms, m_file); } }
/** * Changes the link target of the pointer.<p> * * @throws JspException if inclusion of error dialog fails */ public void actionChangeLinkTarget() throws JspException { try { // check the resource lock state checkLock(getParamResource()); // change the link target CmsFile editFile = getCms().readFile(getParamResource()); editFile.setContents(getParamLinkTarget().getBytes()); getCms().writeFile(editFile); // close the dialog window actionCloseDialog(); } catch (Throwable e) { // error changing link target, show error dialog setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_CHANGE_LINK_TARGET_0)); includeErrorpage(this, e); } }
/** * Changes the link target of the pointer.<p> * * @throws JspException if inclusion of error dialog fails */ public void actionChangeLinkTarget() throws JspException { try { // check the resource lock state checkLock(getParamResource()); // change the link target CmsFile editFile = getCms().readFile(getParamResource()); editFile.setContents(getParamLinkTarget().getBytes()); getCms().writeFile(editFile); // close the dialog window actionCloseDialog(); } catch (Throwable e) { // error changing link target, show error dialog setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_CHANGE_LINK_TARGET_0)); includeErrorpage(this, e); } }
/** * Saves the inheritance group.<p> * * @param resource the inheritance group resource * @param inheritanceContainer the inherited group container data * * @throws CmsException if something goes wrong */ private void saveInheritanceGroup(CmsResource resource, CmsInheritanceContainer inheritanceContainer) throws CmsException { CmsObject cms = getCmsObject(); CmsFile file = cms.readFile(resource); CmsXmlContent document = CmsXmlContentFactory.unmarshal(cms, file); for (Locale docLocale : document.getLocales()) { document.removeLocale(docLocale); } Locale locale = Locale.ENGLISH; document.addLocale(cms, locale); document.getValue("Title", locale).setStringValue(cms, inheritanceContainer.getTitle()); document.getValue("Description", locale).setStringValue(cms, inheritanceContainer.getDescription()); document.getValue("ConfigName", locale).setStringValue(cms, inheritanceContainer.getName()); byte[] content = document.marshal(); file.setContents(content); cms.writeFile(file); }