Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

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

Avatar

Correct answer by
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

7 Replies

Avatar

Employee Advisor

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

Avatar

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

Avatar

Employee Advisor

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);

Avatar

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?

Avatar

Employee Advisor

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...

Avatar

Correct answer by
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();

Avatar

Employee Advisor

Glad you made it work.

Jörg