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

ashokkumarmarni

13-08-2019

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?

Accepted Solutions (1)

Accepted Solutions (1)

ashokkumarmarni

23-08-2019

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

Answers (6)

Answers (6)

Jörg_Hoh

Employee

13-08-2019

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

19-08-2019

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

15-08-2019

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

15-08-2019

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