Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Create page/site from a simple java project?

Avatar

Level 1

Hi Team,

 

My usecase is to create page/site in AEM in a simple java project. However I am unable to find a optimal way to do so.

 

Purpose: Creating testdata for my project as a part of Automation Testing.

 

Solutions tried:

 

1)Page Manager APIs to create pages, the issue faced in this is unable to get ResourceResolver instance.

java.lang.NullPointerException
at com.aem.create_page.createPage(create_page.java:53)
at com.aem.create_page.main(create_page.java:116)

 

Below is the code screenshot, where this error is occurring:

vishakha_sonawale_1-1655285692621.png

 

As per my understanding to get the resource resolver instance we need to create OSGi or Sling project, which I am trying to avoid since I am not sure how it will fit in our existing framework.

 

2) Tried to identify REST API for pages/sites, however couldn't find any similar to Assets in aem.

 

Any pointers on the above usecase would be helpful.

 

Thanks in advance.

 

Regards,

Vishakha

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

From what I read, you are trying to run a Java application (standalone, that means not deployed into AEM) which connects to AEM to create test cntent.

 

I would use the AEM Testing Clients, which can be used to do that. See [1] for the github project. The README of the underlying Sling Testing Client library [2] has some examples how you can make it run.

 

But the main purpose of these libraries is to create Integration Tests for AEM; so creating test content is just a small part of its functionality.

 

 

[1] https://github.com/adobe/aem-testing-clients

[2] https://github.com/apache/sling-org-apache-sling-testing-clients

View solution in original post

4 Replies

Avatar

Employee Advisor

Hi @vishakha_sonawale ,

For #1 -> you are right, you can get resourceResolver when using osgi
I think you can establish a javax.jcr.Session using basic auth and perform CRUD operarions on repository :

public Session getAemSession() {
		Session session = null;
		try {
			Repository aemrepository = JcrUtils.getRepository(this.getRepositoryname());
			session = aemrepository.login(
					new SimpleCredentials(this.getRepositoryusername(), this.getRepositorypassword().toCharArray()));
		} catch (RepositoryException e) {
			LOGGER.debug("RepositoryException::Exception::{}", e.getMessage());
		}
		return session;
	}


This we you can access the AEM JCR from external application.

Here's the reference link :
https://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/access-jcr.html?la...

 

 

Thanks,

Milind

Avatar

Community Advisor

Hi,

 

Over here first you need to get the service resource resolver in your java class using the below code snipped:

 

Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "writeService");

ResourceResolver resolver = null;

try { resolver = resolverFactory.getServiceResourceResolver(param);

}  catch (Exception e) { log.error("Exception",e); } finally{ if(resolver != null && resolver.isLive()){ resolver.close(); } }

 

And for the steps to create the system user, and the user mapping service, you can refer to the document 

https://one-inside.com/aem6-resourceresolver-access-in-services

 

Thanks 

 

Avatar

Community Advisor

Hi @vishakha_sonawale ,
Let me talk to your solutions first. 
1. You will get null as per your screenshoot because you are passing null to get resource resolver. You should pass map which contains a service user. That service user should have proper permission to perform any action. 
2. Yes, you can make a call from java project(outside aem) to create page. Still not sure why do you need this. But my further answer having way to make rest call. 

If you are writing automation, you must be having some framework to get AEM related objects. You should be able to get resource resolver. Can't add much information as I am not sure about what frameworks you are using. 

Adding two solution as per what I understood. 
1. You should get resource resolver using service/system user. This is recommended way. If you are having existing project. you must be having service user and a way to get resource resolver. 
Here is one link https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-initialize-resource...

 

2. Creating page from outside AEM (Rest call). I am explaining using postman. you can easily convert this into rest call using any Java API. You can get java code from postman itself.
1. Create a post call for URL - http://localhost:4502/libs/wcm/core/content/sites/createpagewizard/_jcr_content

2. In Authentication  tab -> Choose Basic Auth -> your credential

3. in Body Tab -> Add parameters use to create page. I added only which needed. You can add others as well.
Adding a screenshoot. 
Again, this is basic way to authenticate. There are other ways too. Use as per your need. 
Postman_Page.png

Avatar

Correct answer by
Employee Advisor

From what I read, you are trying to run a Java application (standalone, that means not deployed into AEM) which connects to AEM to create test cntent.

 

I would use the AEM Testing Clients, which can be used to do that. See [1] for the github project. The README of the underlying Sling Testing Client library [2] has some examples how you can make it run.

 

But the main purpose of these libraries is to create Integration Tests for AEM; so creating test content is just a small part of its functionality.

 

 

[1] https://github.com/adobe/aem-testing-clients

[2] https://github.com/apache/sling-org-apache-sling-testing-clients