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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Is this possible to get .model.json from content path in Java class instead of hitting absolute url and get response?

ashokkumarmarni
Level 2
Level 2

Hi All,

Is there a way to get the /content/my-project/mypage.model.json directly from content path within AEM OSGI service, instead of hitting absolute url http://localhost:4502/content/my-project/mypage.model.json

We do get html of a page with below code snippet

HttpServletRequest httpRequest = requestResponseFactory.createRequest("GET", location);

HttpServletResponse httpResponse = requestResponseFactory.createResponse(out);

requestProcessor.processRequest(httpRequest, httpResponse, resourceResolver);

Is there a way to get the model.json as well similar to above logic?

1 Accepted Solution
ashokkumarmarni
Correct answer by
Level 2
Level 2

Hi Jörg Hoh,

Thank you for your support !

We have modified sling model adaptables to both request and resource to make sure it works for both. We are able to read required values from sling model as below.

Resource r = resourceResolver.getResource("/content/myproject/mypage");

MyModel model = ModelFactory.createModel (r, MyModel.class);

MyModel model = ModelFactory.createModel (r, MyModel.class);

JSONObject completeObj = new JSONObject();

completeObj.put("title", model.getTitle());

Map<String, ComponentExporter> componentMap = (Map<String, ComponentExporter>) model.getExportedItems();

Set<String> childKeyList = model.getExportedItems().keySet();

JSONArray keys = new JSONArray();

for (String childKey : childKeyList) {

    keys.put(((ChildModelImpl) componentMap.get(childKey)).getElements());

}

completeObj.put("items", keys);

modelJson = completeObj.toString();

View solution in original post

0 Replies
Jörg_Hoh
Employee
Employee

You don't to create a request to get that data, there is a much smoother way to get the model object backing this JSON, see [1].

It can look like this:

Resource r = resourceResolver.getResource("/content/myproject/mypage");

MyModel model = ModelFactory.createModel (r, MyModel.class);

with MyModel being the Model class.

[1] Apache Sling :: Sling Models
ashokkumarmarni
Level 2
Level 2

Hi Jörg Hoh,

I have tried above approach, I am ending up with below exception

Caused by: org.apache.sling.models.factory.PostConstructException: Post-construct method has thrown an exception for model class myproject.slingmodels.core.PageImpl

at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:705)

at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:394)

at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:261)

Caused by: java.lang.NullPointerException

at myproject.slingmodels.core.PageV1Impl.initModel(PageV1Impl.java:69)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.sling.models.impl.ModelAdapterFactory.invokePostConstruct(ModelAdapterFactory.java:896)

at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:703)

... 176 more

NullPointerException is because of null values in below variable in myproject.slingmodels.core.PageImpl

    @ScriptVariable

    protected com.day.cq.wcm.api.Page currentPage;

   

    @Self

    private SlingHttpServletRequest request;

are we missing anything since browser based access is working and not through code

Jörg_Hoh
Employee
Employee

In your case it seems that your model can only be adapted from the request itself. I always prefer the adaptions via the resource, unless you need specifics of the request object itself.

In your case it should look like:

MyModel model = ModelFactory.createModel (request, MyModel.class);

ashokkumarmarni
Level 2
Level 2

Hi Jörg Hoh,

Thank you for your response.

We are able to get the sling model objet from above approach, is there a way to get the json string out of model object in our java class?

Jörg_Hoh
Employee
Employee

not that easy, but you need to run it to the Jackson serializer. I don't have sample code ready, sorry. But at [1] is the Sling implementation for this, maybe this helps.

[1] sling-org-apache-sling-models-jacksonexporter/JacksonExporter.java at master · apache/sling-org-apac...

ashokkumarmarni
Correct answer by
Level 2
Level 2

Hi Jörg Hoh,

Thank you for your support !

We have modified sling model adaptables to both request and resource to make sure it works for both. We are able to read required values from sling model as below.

Resource r = resourceResolver.getResource("/content/myproject/mypage");

MyModel model = ModelFactory.createModel (r, MyModel.class);

MyModel model = ModelFactory.createModel (r, MyModel.class);

JSONObject completeObj = new JSONObject();

completeObj.put("title", model.getTitle());

Map<String, ComponentExporter> componentMap = (Map<String, ComponentExporter>) model.getExportedItems();

Set<String> childKeyList = model.getExportedItems().keySet();

JSONArray keys = new JSONArray();

for (String childKey : childKeyList) {

    keys.put(((ChildModelImpl) componentMap.get(childKey)).getElements());

}

completeObj.put("items", keys);

modelJson = completeObj.toString();

View solution in original post