0

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?

anse
  • 87
  • 1
  • 8

1 Answers1

4

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

Oliver Gebert
  • 1,166
  • 1
  • 6
  • 20
  • The approach is good, however the last sentence turned out to be the pitfall. Anyways I ended up using the RequestResponseFactory to create internal requests. – anse Sep 15 '20 at 05:54