1

I need to fetch a user uploaded XML file from the DAM, parse this file and store the contents in the JCR. Here's what I have so far

public class foo implements Runnable {
private static final Logger log = LoggerFactory
        .getLogger(foo.class);

@Reference
ResourceResolverFactory resourceResolverFactory;
@Reference
ResourceProvider resourceProvider;
ResourceResolver resourceResolver = null;
@Reference
SlingRepository repository;
Session session;

// private static ReadXMLFileUsingDomparserTest readxml;
File tempFile;

public void run(){
    log.info("\n ***  Seems okay ***\n");
    ResourceResolver resourceResolver = null;

    try {
        resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

        Resource resource =  resourceResolver.getResource("/content/dam/foo/file.xml");
        Node node = resource.adaptTo(Node.class);

        boolean isAssest = DamUtil.isAsset(resource);
        if (isAssest) {
            Asset asset = resource.adaptTo(Asset.class);
            List<Rendition> rendition = asset.getRenditions();
            for (Rendition re : rendition) {
                InputStream in = re.getStream();

                File xmlFile = copy(in,tempFile);
                if(filetest.exists()){
                    ReadXMLFileUsingDomparserTest.parseXML(filetest,null);
                }else {
                    log.info("File not found at all");
                }               
            }       
        }

        File xmlFile = copy(in,tempFile);*/

        }catch (Exception e) {
            log.error("Exception while running foo" , e);
        }
}

private File copy(InputStream in, File file) {
    try {
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;
}
}

Although I'm able to pick up the Node object correctly (doing Node.getPath() returns the correct path), I am not able to translate this node into a File object. (cannot be Adapted). I want to access this in terms of a File object for parsing. This is why I went through the renditions of the asset and used the stream to copy it into a file.

However, this always shows null for the above code; the output is always File not found at all.

What is the correct way to get a File object with the requisite data from the DAM so that I can successfully parse it?

Sharath Madappa
  • 3,393
  • 1
  • 24
  • 41
bongman1612
  • 440
  • 1
  • 11
  • 23

1 Answers1

2

Uploaded xml file should have an nt:file node, which has a jcr:content node with jcr:data property. You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();

Here are the build in adapters: http://dev.day.com/docs/en/cq/current/developing/sling-adapters.html

I think you can use InputStream here...

nerd
  • 837
  • 6
  • 21