I am working on a Project to upload files to AEM DAM through Java API. So was going through this tutorial to upload files in DAM.
But I am not able to get this code check as I am getting “Null Pointer Exception” @ ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
Pasting the code as well which I am writing, just to let you know AEM is running on a different server and I am trying to connect to that server from my local, Idea is to deploy it as a Rest API in a different server from AEM.
Thanks in advance any help will be appreciated.
@Reference
private static ResourceResolverFactory resolverFactory;
public static void main(String[] args) {
//InputStream is =null;
try{
File initialFile = new File("C:/Users/502719757/Desktop/DOC0928611r8VCT_750HD_RevDis.pdf");
InputStream targetStream = new FileInputStream(initialFile);
String fileName = "DOC0928611r8VCT_750HD_RevDis.pdf";
String path = "http://uswaupmcvms01l.am.health.ge.com:8001/content/dam/travel";
String mimetype= null;
writeToClientLib(targetStream,fileName,path,mimetype);
}
catch(Exception e){
}
}
//
private static String writeToClientLib(InputStream is, String fileName, String path, String mimetype)
{
try
{
//Inject a ResourceResolver
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
//Use AssetManager to place the file into the AEM DAM
com.day.cq.dam.api.AssetManager assetMgr = resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
String newFile = path+fileName ;
assetMgr.createAsset(newFile, is, mimetype, true);
// Return the path to the document that was stored in CRX.
return newFile;
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return null;
}
zeeshank1500736
zeeshank1500736
20-07-2017
Hi Vishal,
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null); has been diprecated .
You can get service mapping go through below will help
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
22-09-2017
Support
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
20-07-2017
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null); has been diprecated .
You can get service mapping go through below will help
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
20-07-2017
I am working on a Project to upload files to AEM DAM through Java API. So was going through this tutorial to upload files in DAM. "Adobe Experience Manager Help | Uploading files to Adobe Experience…
vishalk87673574
vishalk87673574
20-07-2017
Hi Zeeshan,
I am missing a point here and not able to understand.
On that link too we are using resolverFactory to get the resourceResolver, my problem is I am not getting any such method getServiceResourceResolver() to call on resolverFactory.
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "writeService");
ResourceResolver resolver = null;
try {
ValueMap readMap = res.getValueMap();
log.info(readMap.get("jcr:primaryType", ""));
ModifiableValueMap modMap = res.adaptTo(ModifiableValueMap.class);
zeeshank1500736
zeeshank1500736
20-07-2017
You don't need to get resolverFactory you have required only resource for getting AssetManager object.
resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
for getting resorceResolver follow the below Step you have to get resourceResolver using Service.
AEM6: ResourceResolver access in services - WE ARE INSIDE
vishalk87673574
vishalk87673574
20-07-2017
Thanks for replying.
I cannot see this method is allowed with resolverFactory.
Only two methods are allowed to be used with this
1)getResourceResolver(null);
2)getAdministrativeResourceResolver(null);
I am using org.apache.sling.api-2.4.0 version of jar, please suggest
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
22-09-2017
Support
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
22-09-2017
smacdonald2008
smacdonald2008
21-07-2017
You can use Restful method to upload an asset to AEM - see:
This uses an sample Java Swing client to send a POST to a sling servlet - the only thing you need to change is use System user and Sling Mapping as opposed to the Admin login call.
vishalk87673574
vishalk87673574
21-07-2017
Thanks smacdonald2008 for your suggestions.
Just wanted to be sure I am following the correct approach.
I am deploying an application(may be Rest API) on a different server and AEM is running on a different server.
With this API I want to upload some files to AEM repository(DAM), would this be the efficient method or is there any other way of doing this?
smacdonald2008
smacdonald2008
21-07-2017
We will update that article to show use of this in AEM 6.3.
smacdonald2008
smacdonald2008
21-07-2017
This is for AEM Versions Experience Manager 5.5, 5.6, 6.0, 6.1. In the correct version - you need to use a System user and the Sling Mapping Service. TO learn how to do this -- see this article:
viveksachdeva
viveksachdeva
20-07-2017
As per the documentation, it is available since v 2.4.. What you can do is :
- Open http://localhost:4502/system/console/depfinder
- Search for org.apache.sling.api.resource
- You will see the dependency. Include it in your POM..
- Import maven dependencies again and then check
vishalk87673574
vishalk87673574
20-07-2017
Now it is making sense to me, and sorry to bug you again. I will try to create a service and mapping for a user in AEM.
But the point which I was making in I am not able to see that method getServiceResourceResolver(map) in the recommendations and when I am using it, code is throwing error. Not sure how creating a service and mapping in AEM will effect this piece of code.
Please Suggest.
zeeshank1500736
zeeshank1500736
20-07-2017
your are already using annotation @Reference to inject the ResourceResolverFactory in your code.
@Reference
private static ResourceResolverFactory resolverFactory;
Create a write Service in AEM and put that service name in the "NAME of Your Write Service" in the code . for create service mapping follow the steps of the url Service Users in AEM | Adobe AEM Club
After creating the writeService and mapping with bundle.Below is your modified code in writeToClientLib.
private static String writeToClientLib(InputStream is, String fileName, String path, String mimetype)
{
try
{
Map<String, Object> param = new HashMap<String, Object>();
//Please put your service name in place of NAME of Your Write Service
param.put("NAME of Your Write Service" "writeService");
ResourceResolver resolver = null;
resolver = resolverFactory.getServiceResourceResolver(param);
//Use AssetManager to place the file into the AEM DAM
com.day.cq.dam.api.AssetManager assetMgr = resolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
String newFile = path+fileName ;
assetMgr.createAsset(newFile, is, mimetype, true);
// Return the path to the document that was stored in CRX.
return newFile;
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return null;
}
Kyaw_Zwa_Myint
Kyaw_Zwa_Myint
20-07-2017
resolver = resolverFactory.getServiceResourceResolver(param);
log.info(resolver.getUserID());
Resource res = resolver.getResource("/content/mydata/jcr:content");