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
BedrockMission!

Learn More

View all

Sign in to view all badges

HttpPost to AEM 6.4 Author instance using Assets api through apache http client

Avatar

Avatar
Validate 1
Level 2
skmAem
Level 2

Likes

14 likes

Total Posts

34 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile

Avatar
Validate 1
Level 2
skmAem
Level 2

Likes

14 likes

Total Posts

34 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile
skmAem
Level 2

13-05-2020

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

View Entire Topic

Avatar

Avatar
Validate 1
Level 2
skmAem
Level 2

Likes

14 likes

Total Posts

34 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile

Avatar
Validate 1
Level 2
skmAem
Level 2

Likes

14 likes

Total Posts

34 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile
skmAem
Level 2

13-05-2020

Hi Ka,

I did refer to that article and got some ideas about using httpclient to send the assets, but that article does not have any examples regarding how to access those assets api through java. Also in my code I am creating httpclient using admin:admin credentials, does that not work? Do you have any java code snippets to perform the post operation?

 

Thanks,

SKM