Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

resource.adaptTo(File.class) returning null for all the resources present in the DAM

Avatar

Level 2

I want to use a particular service for which I need to pass a CSV file object as argument e.g:-

try {
cl = new LookupService(new File("/content/dam/location.csv"), LookupService.GEOIP_MEMORY_CACHE);
} catch (IOException e) {
log.info("File NOt found"+e.getMessage());
}

I tried the following but getting null object

        ResourceResolver res = request.getResourceResolver();
        Resource resource = res
                .getResource("/content/dam/geoLocation.csv");
        if (resource != null) {
            Asset asset = resource.adaptTo(Asset.class);
            if (asset != null) {
                Rendition rend = asset.getOriginal();
                File file = null;
                if (rend != null) {
                    file = rend.adaptTo(File.class);
                }
                if (file != null) {
                    // to do
                } else {
                    log.info("File Null");
                }

            } else {
                log.info("Asset null");
            }
        }

The primary type of .csv file is nt:file. Please let me know how can I get the File object or if I'm doing it wrongly

1 Accepted Solution

Avatar

Correct answer by
Employee

In short, you can't. Resources and Assets aren't adaptable to java.io.File objects. The LookupService API really should change to allow you to pass in an InputStream. If that's impossible, you will need to create a temp file and then write the contents of the InputStream to that temp file. Although that's pretty ugly. 

A File object in Java is really intended to represent an actual file on a filesystem. This doesn't apply to anything in the JCR which is a higher-level abstration.

View solution in original post

3 Replies

Avatar

Level 8

At what point are you getting a null object, there are multiple points where you are doing a null check. 

  1. If you are getting a null resource object in your first null check then the likely reason is that your path to the resource is incorrect. I notice you have two paths /content/dam/location.csv and /content/dam/geoLocation.csv. If you are getting a null resource object it's likely because your path is wrong. Your code isn't logging anything for this so I am not sure if this is your issue. 
  2. If you are getting a null Asset then the problem is likely that your resource isn't DAM asset. You said that the primary type of the .csv file is nt:file - do you mean that the primary type of /content/dam/geoLocation.csv is nt:file. If so that's the problem. In order to get and Asset from a Resource the resource needs needs to be of type dam:Asset. 
  3. If your rendition is null that would be unusual. The only reason I can think of for that to happen is if the underlying structure of the Asset were not correct - if for example someone had deleted the original rendition of changed its name somehow. 
  4. If you file is null but everything else exists then I think the problem is how you are trying to create the File object. I don't believe the Rendition is adaptable to File. Take a look at http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/dam/api/Asset.html#getOriginal() for a code fragment from getting an InputStream from and Asset. Then convert the InputStream to a File - that would probably be more reliable. 

Avatar

Level 2

Thanks for the update orotas

I am getting null at (Rendition.adapTo(File.class))

 if (rend != null) {
               File file = rend.adaptTo(File.class);
  }


 Basically , I want a file object to be passed to cl = new LookUpService(File file)

So when I am giving direct path as "/content/dam/location.csv" or "/etc/location.csv" ,it's not working

that is , cl = new LookUpService(new File("/content/dam/location.csv")) or cl = new LookUpService(new File("/content/dam/location.csv")) is thorwing null pointer exception

So I tried with ResourceResolver and tried adapting the resolver to file.class but no success.

Please let me know where can I put my csv file. or how can I adapt this resource to file.class

Thanks,

Avatar

Correct answer by
Employee

In short, you can't. Resources and Assets aren't adaptable to java.io.File objects. The LookupService API really should change to allow you to pass in an InputStream. If that's impossible, you will need to create a temp file and then write the contents of the InputStream to that temp file. Although that's pretty ugly. 

A File object in Java is really intended to represent an actual file on a filesystem. This doesn't apply to anything in the JCR which is a higher-level abstration.