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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
At what point are you getting a null object, there are multiple points where you are doing a null check.
Views
Replies
Total Likes
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,
Views
Replies
Total Likes
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.
Views
Replies
Total Likes