You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Felix Meschberger <fm...@gmail.com> on 2008/01/21 09:39:30 UTC

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Hi David,

Am Montag, den 21.01.2008, 08:25 +0100 schrieb David Nuescheler:
> Hi guys,
> 
> I have been trying to access the JCR session from my .esp script
> to create content for an import script that needs to create many
> nodes in the repository.
> 
> I think my initial thought was to get access to the session from
> the node on the resource via the .getSession() method on Node, but
> since the actual node was hidden by ScriptableNode I didn't find a quick
> way to do that.
> 
> Then I thought that we might even want to expose the session in a more
> direct way. With my "JCR framework"-hat on I would obviously expect
> something on a per request basis that let's me access the jcr session
> that is associated with a specific request, given the authentication
> information etc. on the http requests.
> 
> Maybe we should also get the JCR session from the resource though,
> which I think would fit the current (more abstract) design of Sling better.
> 
> Is there a current solution that I missed?
> What would be a possible future way of doing that?

There are two ways: The better way is to do:

   Session session =
request.getResourceResolver().adaptTo(Session.class);

the second best way is to get the javax.jcr.Session request attribute:

   Session session = (Session)
request.getAttribute(Session.class.getName());

Hope, this helps.

Regards
Felix

> 
> regards,
> david


Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Carsten Ziegeler <cz...@apache.org>.
Bertrand Delacretaz wrote:
> On Jan 21, 2008 9:39 AM, Felix Meschberger <fm...@gmail.com> wrote:
>> ...The better way is to do:
>>
>>    Session session =
>> request.getResourceResolver().adaptTo(Session.class);...
> 
> If the Resource is a JCR node, it would be convenient to have direct
> access to a "session" variable, or "JcrSession" to avoid confusion
> with the HTTP Session.
> 
> Currently these scripting variables are set in SlingScriptServlet:
> 
>             SlingBindings props = new SlingBindings();
>             props.put(REQUEST, req);
>             props.put(RESPONSE, res);
>             props.put(FLUSH, TRUE);
> 
> To avoid hardcoding additional values here, maybe this could ask the
> current Resource for more variables to bind? A JcrResource could then
> provide the JCR session, workspace, queryManager or whatever's
> convenient, in a way that's localized to the JcrResource's
> JCR-specific code.
> 
> WDYT?
> 
I think that making additional variables makes sense as it simplifies 
writing scripts. But I don't think that this is the task of a resource.
For instance, if you have a non-jcr-resource this resource does not have
a jcr session, but still you might want to use a jcr session in your 
script. But you don't want to care what kind of resource you have just 
in order to get a jcr session.

So I think it's more the task of the resource resolver. Perhaps together 
with the new adapter factory this can be made easily plugable/extensible?

Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Jan 21, 2008 11:17 AM, Felix Meschberger <fm...@gmail.com> wrote:

> ...Coming back to David's initial problem, that the ScriptableNode does not
> provide the getSession() method: We have SLING-154 [1], which is
> concerned with completing the ScriptableNode implementation, and there
> is a way to access the Session, I think, we do not need to do anything
> else at the moment....

You're right, that's good enough for now - I seemed to remember such a
proposal but didn't look in the right place this morning.

-Bertrand

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by David Nuescheler <da...@day.com>.
> I think you missinterpreted my comment and quoted my statement out of
> context.
;) i competely agree...

regards,
david

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Carsten Ziegeler <cz...@apache.org>.
David Nuescheler wrote
> 
> <pointless-rant>
> Carsten says:
>> For instance, if you have a non-jcr-resource this resource ...
> 
> Can we please not use that as an argument... I really think
> we should focus on the real use-cases we have at hand.
> I know I sound like a broken record regarding the abstraction
> of Sling from JCR. Getting access to session and node is
> just the beginning.
> 
> Personally, I think it makes little sense to promote abstraction on
> the Sling API layer, if we re-introduce all the JCR dependencies
> "rightfully" and based on use-cases through the back door into
> all the scripting layers.
> 
> I guess what beats me is, why we can't build abstraction into the
> system as we go along and just cover our current needs for now.
> 
> YAGNI. Scratch your own itch. Flexibility Syndrome.
> </pointless-rant>
> 
I think you missinterpreted my comment and quoted my statement out of
context.

Anyways, it seems we have a solution now that we can all agree on - and
that's the important part :)

Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org


Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by David Nuescheler <da...@day.com>.
Hi Guys,

Felix says:
> Coming back to David's initial problem, that the ScriptableNode does not
> provide the getSession() method: We have SLING-154 [1], which is
> concerned with completing the ScriptableNode implementation, and there
> is a way to access the Session, I think, we do not need to do anything
> else at the moment.
Well my problem that goes even beyond that.
Even if I would manage to acquired a Session somehow all the
nodes that come back from calls will be ScriptableNodes and
will hide the methods that I need to
manipulate the node.

I agree with Felix that
https://issues.apache.org/jira/browse/SLING-154
resolves my Issue in a pragmatic fashion.

...on a more general level, though.

<pointless-rant>
Carsten says:
> For instance, if you have a non-jcr-resource this resource ...

Can we please not use that as an argument... I really think
we should focus on the real use-cases we have at hand.
I know I sound like a broken record regarding the abstraction
of Sling from JCR. Getting access to session and node is
just the beginning.

Personally, I think it makes little sense to promote abstraction on
the Sling API layer, if we re-introduce all the JCR dependencies
"rightfully" and based on use-cases through the back door into
all the scripting layers.

I guess what beats me is, why we can't build abstraction into the
system as we go along and just cover our current needs for now.

YAGNI. Scratch your own itch. Flexibility Syndrome.
</pointless-rant>

Feel free to ignore the above rant since I think we discussed
this matter on this list many times over...

regards,
david

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Carsten Ziegeler <cz...@apache.org>.
Felix Meschberger wrote:
> Hi,
> 
> Am Montag, den 21.01.2008, 10:51 +0100 schrieb Bertrand Delacretaz:
>> On Jan 21, 2008 10:37 AM, Carsten Ziegeler <cz...@apache.org> wrote:
>>
>>> Felix Meschberger wrote:
>>>> ....Sure, still this would all just be convenience :-)
>> Of course, we WANT Sling to be convenient to use ;-)
> 
> Sure, but where do we stop ?? 
> 
>>> ...For jsp we could hard-code this in the sling taglib for the
>>> defineObjects tag and we could do similar for esp.
>> Sounds good,but how would you do it for ESP? Currently we don't have a
>> "standard included script" for ESP.
>>
>>> ...This has the minor
>>> disadvantage that this has to be done for each scripting implementation
>>> independently...
>> No big deal, I'd be happy with that.
> 
> Coming back to David's initial problem, that the ScriptableNode does not
> provide the getSession() method: We have SLING-154 [1], which is
> concerned with completing the ScriptableNode implementation, and there
> is a way to access the Session, I think, we do not need to do anything
> else at the moment.
> 
+1

Carsten	

-- 
Carsten Ziegeler
cziegeler@apache.org

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Tobias Bocanegra <to...@day.com>.
> Coming back to David's initial problem, that the ScriptableNode does not
> provide the getSession() method: We have SLING-154 [1], which is
> concerned with completing the ScriptableNode implementation, and there
> is a way to access the Session, I think, we do not need to do anything
> else at the moment.
+1
-- 
-----------------------------------------< tobias.bocanegra@day.com >---
Tobias Bocanegra, Day Management AG, Barfuesserplatz 6, CH - 4001 Basel
T +41 61 226 98 98, F +41 61 226 98 97
-----------------------------------------------< http://www.day.com >---

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

Am Montag, den 21.01.2008, 10:51 +0100 schrieb Bertrand Delacretaz:
> On Jan 21, 2008 10:37 AM, Carsten Ziegeler <cz...@apache.org> wrote:
> 
> > Felix Meschberger wrote:
> > > ....Sure, still this would all just be convenience :-)
> 
> Of course, we WANT Sling to be convenient to use ;-)

Sure, but where do we stop ?? 

> 
> > ...For jsp we could hard-code this in the sling taglib for the
> > defineObjects tag and we could do similar for esp.
> 
> Sounds good,but how would you do it for ESP? Currently we don't have a
> "standard included script" for ESP.
> 
> > ...This has the minor
> > disadvantage that this has to be done for each scripting implementation
> > independently...
> 
> No big deal, I'd be happy with that.

Coming back to David's initial problem, that the ScriptableNode does not
provide the getSession() method: We have SLING-154 [1], which is
concerned with completing the ScriptableNode implementation, and there
is a way to access the Session, I think, we do not need to do anything
else at the moment.

Regards
Felix

[1] http://issues.apache.org/jira/browse/SLING-154


Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Carsten Ziegeler <cz...@apache.org>.
Bertrand Delacretaz wrote:
> On Jan 21, 2008 10:37 AM, Carsten Ziegeler <cz...@apache.org> wrote:
> 
>> Felix Meschberger wrote:
>>> ....Sure, still this would all just be convenience :-)
> 
> Of course, we WANT Sling to be convenient to use ;-)
> 
>> ...For jsp we could hard-code this in the sling taglib for the
>> defineObjects tag and we could do similar for esp.
> 
> Sounds good,but how would you do it for ESP? Currently we don't have a
> "standard included script" for ESP.
We could do it in the esp script engine (I guess/hope)

Carsten

> 
>> ...This has the minor
>> disadvantage that this has to be done for each scripting implementation
>> independently...
> 
> No big deal, I'd be happy with that.
> 
> -Bertrand
> 


-- 
Carsten Ziegeler
cziegeler@apache.org

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Jan 21, 2008 10:37 AM, Carsten Ziegeler <cz...@apache.org> wrote:

> Felix Meschberger wrote:
> > ....Sure, still this would all just be convenience :-)

Of course, we WANT Sling to be convenient to use ;-)

> ...For jsp we could hard-code this in the sling taglib for the
> defineObjects tag and we could do similar for esp.

Sounds good,but how would you do it for ESP? Currently we don't have a
"standard included script" for ESP.

> ...This has the minor
> disadvantage that this has to be done for each scripting implementation
> independently...

No big deal, I'd be happy with that.

-Bertrand

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Carsten Ziegeler <cz...@apache.org>.
Felix Meschberger wrote:
> Hi,
> 
> Am Montag, den 21.01.2008, 10:27 +0100 schrieb Bertrand Delacretaz:
>> On Jan 21, 2008 10:15 AM, Felix Meschberger <fm...@gmail.com> wrote:
>>
>>> ...Having this in mind, retrieving the session from the Resource is not
>>> appropriate. For this reason, the Session is available from the
>>> ResourceResolver's adaptTo method. ...
>> So (as Carsten also suggests), we could ask the ResourceResolver(s) to
>> provide additional variables that are to be bound for scripts, right?
>> By adapting it to a BindingProvider interface or something.
>>
>> Right now, the only concrete use-case that I see is providing the JCR
>> Session, but it seems like the mechanism could help in providing a
>> richer scripting environment.
> 
> Sure, still this would all just be convenience :-)
> 
Yepp :)

Rethinking this, I'm not sure anymore if we really need an automatic 
mechanism or direct support for this.

For jsp we could hard-code this in the sling taglib for the 
defineObjects tag and we could do similar for esp. This has the minor 
disadvantage that this has to be done for each scripting implementation 
independently.

Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

Am Montag, den 21.01.2008, 10:27 +0100 schrieb Bertrand Delacretaz:
> On Jan 21, 2008 10:15 AM, Felix Meschberger <fm...@gmail.com> wrote:
> 
> > ...Having this in mind, retrieving the session from the Resource is not
> > appropriate. For this reason, the Session is available from the
> > ResourceResolver's adaptTo method. ...
> 
> So (as Carsten also suggests), we could ask the ResourceResolver(s) to
> provide additional variables that are to be bound for scripts, right?
> By adapting it to a BindingProvider interface or something.
> 
> Right now, the only concrete use-case that I see is providing the JCR
> Session, but it seems like the mechanism could help in providing a
> richer scripting environment.

Sure, still this would all just be convenience :-)

Regards
Felix


Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Jan 21, 2008 10:15 AM, Felix Meschberger <fm...@gmail.com> wrote:

> ...Having this in mind, retrieving the session from the Resource is not
> appropriate. For this reason, the Session is available from the
> ResourceResolver's adaptTo method. ...

So (as Carsten also suggests), we could ask the ResourceResolver(s) to
provide additional variables that are to be bound for scripts, right?
By adapting it to a BindingProvider interface or something.

Right now, the only concrete use-case that I see is providing the JCR
Session, but it seems like the mechanism could help in providing a
richer scripting environment.

-Bertrand

Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Felix Meschberger <fm...@gmail.com>.
Hi, 

Am Montag, den 21.01.2008, 09:58 +0100 schrieb Bertrand Delacretaz:
> On Jan 21, 2008 9:39 AM, Felix Meschberger <fm...@gmail.com> wrote:
> > ...The better way is to do:
> >
> >    Session session =
> > request.getResourceResolver().adaptTo(Session.class);...
> 
> If the Resource is a JCR node, it would be convenient to have direct
> access to a "session" variable, or "JcrSession" to avoid confusion
> with the HTTP Session.
> 
> Currently these scripting variables are set in SlingScriptServlet:
> 
>             SlingBindings props = new SlingBindings();
>             props.put(REQUEST, req);
>             props.put(RESPONSE, res);
>             props.put(FLUSH, TRUE);

There are more: OUT is the same as response.getWriter() and RESOURCE is
the same request.getResource() and SLING is the SlingScriptHelper
object.

> 
> To avoid hardcoding additional values here, maybe this could ask the
> current Resource for more variables to bind? A JcrResource could then
> provide the JCR session, workspace, queryManager or whatever's
> convenient, in a way that's localized to the JcrResource's
> JCR-specific code.
> 
> WDYT?

-1, let me explain:

JcrNodeResource (as is JcrPropertyResource) are private classes to the
jcr/resource project. So there is no way of exposing special methods. In
fact, this would not be nice as there would be a need for cast.

To still be able to provide special "views" of the Resource (as well as
the ResourceResovler and even more in the future, see the other thread
on the "Everything is a Resource" concept), we introduced the adaptTo()
method. The intent is to provide other views of the object.

Having this in mind, retrieving the session from the Resource is not
appropriate. For this reason, the Session is available from the
ResourceResolver's adaptTo method. The workspace need not be provided,
as this is accessible from the session.

The queryManager is special and respective simplified API is available
from the ResourceResolver in terms of the findResources and
queryResources method.

Regards
Felix


Re: Sling Launchpad: How to get access to the current JCR session from .esp

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Jan 21, 2008 9:39 AM, Felix Meschberger <fm...@gmail.com> wrote:
> ...The better way is to do:
>
>    Session session =
> request.getResourceResolver().adaptTo(Session.class);...

If the Resource is a JCR node, it would be convenient to have direct
access to a "session" variable, or "JcrSession" to avoid confusion
with the HTTP Session.

Currently these scripting variables are set in SlingScriptServlet:

            SlingBindings props = new SlingBindings();
            props.put(REQUEST, req);
            props.put(RESPONSE, res);
            props.put(FLUSH, TRUE);

To avoid hardcoding additional values here, maybe this could ask the
current Resource for more variables to bind? A JcrResource could then
provide the JCR session, workspace, queryManager or whatever's
convenient, in a way that's localized to the JcrResource's
JCR-specific code.

WDYT?

-Bertrand