Hello All,
I have a requirement that if an asset is modified in one AEM author instance and if it contains specific metadata, we need to send the asset to another AEM author instance. We don't want to write any code in the target AEM author instance.
Our approach was to write a custom java workflow process to be fired on asset modification and use apache http client library to post the asset original rendition to the target AEM instance using assets api (eg http://localhost:4602/api/assets/content/dam/folder/asset.name). Has anyone done this before? Can someone share some code snippet to achieve this? Is there a better approach than this since we are not allowed to deploy any custom code to the target instance?
public void sendAssetToTarget(Asset asset) {
log.info("Send Asset to Target called for asset {}", asset.getName());
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("admin", "admin"));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
CloseableHttpResponse response = null;
try {
URI uri = new URIBuilder()
.setScheme("http")
.setHost("localhost:4602")
.setPath("/api/assets/content/dam/target-assets/new/" + asset.getName())
.build();
log.info("The post request URI is {}", uri.toString());
HttpPost postRequest = new HttpPost(uri);
postRequest.addHeader("content-type", asset.getMimeType());
InputStreamEntity entity = new InputStreamEntity(asset.getOriginal().getStream());
entity.setContentEncoding("binary/octet-stream");
entity.setChunked(true);
postRequest.setEntity(new BufferedHttpEntity(entity));
response = httpClient.execute(postRequest);
log.info("The post request response is: {}", response.toString() );
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
log.error("Error sending asset to Target", e);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
log.error("Error sending asset to Target", e);
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("Error sending asset to Target", e);
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("Error sending asset to Target", e);
}
}
}
The target AEM instance is receiving the post request but it is sending 500 response code and i see following exception in the target instance error.log:
13.05.2020 20:43:56.052 *INFO* [qtp638789605-3595] org.apache.sling.auth.core.impl.SlingAuthenticator getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials
13.05.2020 20:43:56.155 *ERROR* [127.0.0.1 [1589417036057] POST /api/assets/content/dam/target-assets/new/asset_6.png HTTP/1.1] com.adobe.granite.rest.impl.servlet.PostRequest Exception during request processing.
java.lang.NullPointerException: null
at com.adobe.granite.rest.assets.impl.AssetResourceProvider.getDataPropertyKey(AssetResourceProvider.java:412) [com.adobe.granite.rest.assets:1.0.56]
at com.adobe.granite.rest.assets.impl.AssetResourceProvider.create(AssetResourceProvider.java:209) [com.adobe.granite.rest.assets:1.0.56]
at com.adobe.granite.rest.impl.ApiEndpointResourceProvider.create(ApiEndpointResourceProvider.java:107) [com.adobe.granite.rest.api:1.1.10]
at org.apache.sling.resourceresolver.impl.legacy.LegacyResourceProviderAdapter.create(LegacyResourceProviderAdapter.java:168) [org.apache.sling.resourceresolver:1.5.34]
at org.apache.sling.resourceresolver.impl.legacy.LegacyResourceProviderFactoryAdapter.create(LegacyResourceProviderFactoryAdapter.java:109) [org.apache.sling.resourceresolver:1.5.34]
Any help would be greatly appreciated.
-SKMAEM