You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Christian Eugster <ch...@gmx.net> on 2013/05/31 10:03:37 UTC

access to repository from service

Hi,

I have created an osgi service that should have write permissions to the sling repository. How can I achieve this?

Thank you

Christian

Re: access to repository from service

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Fri, May 31, 2013 at 10:03 AM, Christian Eugster
<ch...@gmx.net> wrote:
> ...I have created an osgi service that should have write permissions to the sling repository. How can I achieve this?...

If your service is processing a request, the Resource that you get in
the request gives you access. There's various ways, for example
resource.adaptTo(Node.class).getSession() provides a JCR session.

If you don't have a request you can use an @Reference annotation to
get a SlingRepository, and SlingRepository.loginAdministrative(...) to
get a Session. Impersonating the Session to a different user is
preferred then, to avoid working as admin. Or get a ResourceResolver
service via ResourceResolverFactory if you want to work with Resources
as opposed to JCR Nodes.

There's tons of examples in the Sling codebase, if you search for the
above code patterns.

-Bertrand