0
votes

Inside of an OSGi component / service I'd need a JSON-representation of a resource (pages, CF etc) exactly like retrieving it via Sling model selector (resource.model.json).

Unfortunately being inside an OSGi component or service, there's no (sling) request object available.

Is there a way to get the json representation (with all the component's model exporters) without creating a http-request to localhost?

1

1 Answers

4
votes

that's not a problem as long as you have access to the resource.

First you need to make sure that your Model can deliver the json via a method call. See Get .model.json as String for an explanation on how to do this.

If you are done with that, use the ModelFactory to "getModelFromResource". This will create an instance of your SlingModel for the given resource. Just call the method you created before to get your json. See https://sling.apache.org/apidocs/sling10/org/apache/sling/models/factory/ModelFactory.html

Your model should probably have adaptables= {Resource.class} - if you adapt from Request there might be trouble ahead.

HTH, OliG