cancel
Showing results for 
Search instead for 
Did you mean: 

How to read the content of a document?

itzamna
Confirmed Champ
Confirmed Champ
Hi,

I've a configuration file (mymodule-config.properties) uploaded to the Data Dictionary so that the admin is able to override some module settings with it. Now I try to detect in my Webscript code if this file is available at all and if available, I'll read the content to a java.util.Properties object. But I dont get it up and running…

The code to read the content of the document:


   NodeRef nodeDocument = resultSetAllDocuments.getChildAssocRef(0).getChildRef();
   ContentService contentService = serviceRegistry.getContentService();
   ContentReader contentReader = contentService.getReader(nodeDocument, ContentModel.PROP_CONTENT);
   InputStream is = contentReader.getContentInputStream();
   Properties myProps = new Properties();
   myProps.load(is);



Instead the file content I'll get the following:

Error: org.alfresco.service.cmr.repository.ContentIOException: 02200007 Failed to open stream onto channel:
   accessor: ContentAccessor[ contentUrl=store://2014/3/20/14/21/cd19ab69-aaa1-43f1-b17b-caa646bc8965.bin, mimetype=text/plain, size=206, encoding=UTF-8, locale=en_US]


How can I read the file content? I tried also already both versions mentioned in https://wiki.alfresco.com/wiki/NodeRef_cookbook#Reading_the_data_content_of_a_NodeRef_.28plain_text.... but none of them works. Always the same error even if I use the approach with the String:

contentReader.getContentString();


I'm using Alfresco 4.2.1 Enterprise.

Just to complete: The content of the document is something like

mySetting1 = 123
mySetting2 = abc
mySetting3 = false

1 REPLY 1

kaynezhang
World-Class Innovator
World-Class Innovator
Following code is written by me when I learned to write a java backed webscript,it works correctly.There must be something wrong with you code ,you can paste it here.


public class NodeContentGet extends DeclarativeWebScript {
   private static final String PARAM_CONTENT = "content";
   private NodeService nodeService;
   private ContentService contentService;

   public void setContentService(ContentService contentService) {
      this.contentService = contentService;
   }

   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }

   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest req,
         Status status, Cache cache) {
      Map<String, Object> model = new HashMap<String, Object>();
      Map<String, String> templateVars = req.getServiceMatch()
            .getTemplateVars();
      StoreRef store = new StoreRef(templateVars.get("store_type"),
            templateVars.get("store_id"));

      NodeRef nodeRef = new NodeRef(store, templateVars.get("id"));
      if (!nodeService.exists(nodeRef)) {
         String error = "Could not find node: " + nodeRef;
         throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
      }

      String contents;
      ContentReader reader = contentService.getReader(nodeRef,
            ContentModel.PROP_CONTENT);
      if (reader != null) {
         contents = reader.getContentString();
      } else {
         // No content was stored in the version history
         contents = "";
      }
      model.put(PARAM_CONTENT, contents);
      return model;
   }
}