Tabnine Logo
CmsFile.setContents
Code IndexAdd Tabnine to your IDE (free)

How to use
setContents
method
in
org.opencms.file.CmsFile

Best Java code snippets using org.opencms.file.CmsFile.setContents (Showing top 20 results out of 315)

origin: org.opencms/org.opencms.workplace

/**
 * 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);
}
origin: org.opencms/org.opencms.workplace.tools.modules

/**
 * 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);
  }
}
origin: org.opencms/opencms-core

/**
 * 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);
}
origin: org.opencms/opencms-core

/**
 * @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;
}
origin: org.opencms/opencms-solr

/**
 * @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;
}
origin: org.opencms/opencms-core

/**
 * 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);
}
origin: org.opencms/opencms-core

/**
 * 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());
  }
}
origin: org.opencms/opencms-solr

  /**
   * 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);
  }
}
origin: org.opencms/opencms-core

/**
 * @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);
  }
}
origin: org.opencms/opencms-core

/**
 * 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);
}
origin: org.opencms/opencms-core

/**
 * 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);
  }
}
origin: org.opencms/opencms-solr

  /**
   * 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);
  }
}
origin: org.opencms/opencms-core

/**
 * 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);
}
origin: org.opencms/opencms-core

/**
 * @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);
  }
}
origin: org.opencms/org.opencms.workplace.tools.modules

  /**
   * 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);

  }
}
origin: org.opencms/opencms-core

/**
 * 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));
}
origin: org.opencms/opencms-core

  /**
   * 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);

  }
}
origin: org.opencms/org.opencms.workplace

/**
 * 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);
  }
}
origin: org.opencms/opencms-solr

/**
 * 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);
  }
}
origin: org.opencms/opencms-core

/**
 * 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);
}
org.opencms.fileCmsFilesetContents

Javadoc

Sets the contents of this file.

This will also set the date content, but only if the content is already set.

Popular methods of CmsFile

  • getContents
    Returns the content of this file.
  • getName
  • getRootPath
  • getTypeId
  • getStructureId
  • setDateLastModified
  • <init>
  • clone
    Returns a clone of this Objects instance.
  • getDateCreated
  • getDateLastModified
  • getFlags
  • getLength
  • getFlags,
  • getLength,
  • getProjectLastModified,
  • getResourceId,
  • getState,
  • isFile,
  • isTouched,
  • setDateExpired,
  • setDateReleased

Popular in Java

  • Updating database using SQL prepared statement
  • compareTo (BigDecimal)
  • setContentView (Activity)
  • onRequestPermissionsResult (Fragment)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • GridBagLayout (java.awt)
    The GridBagLayout class is a flexible layout manager that aligns components vertically and horizonta
  • Rectangle (java.awt)
    A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-
  • HttpURLConnection (java.net)
    An URLConnection for HTTP (RFC 2616 [http://tools.ietf.org/html/rfc2616]) used to send and receive d
  • LinkedHashMap (java.util)
    LinkedHashMap is an implementation of Map that guarantees iteration order. All optional operations a
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now