You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Stefan Pietschmann <de...@gmail.com> on 2006/01/09 11:24:10 UTC

Access to the AuthenticationContext

I need to access the AuthenticationContext from a component. First things
first: I have it working, but I've got a question anyways:

 

First I tried two ways:

 

1.	Implement Serviceable and get the ServiceManager, then a) lookup the
ContextManager and b) lookup the SessionManager.

 

In short: it didn't work. Both were null and I couldn't get the Context that
way.

 

Then I found http://wiki.apache.org/cocoon/AuthFWWithFlow , which suggests
the following way:

 

ComponentManager componentManager =
CocoonComponentManager.getSitemapComponentManager();
SessionManager sessionManager =
(SessionManager)componentManager.lookup(SessionManager.ROLE);
String username = sessionManager.getContextFragment( "authentication",
"/authentication/ID").getFirstChild().getNodeValue();
 
This works nicely, however the javadoc inside CocoonComponentManager states:
 
"Cocoon Component Manager. This manager extends the
ExcaliburComponentManager by a special lifecycle handling for a
RequestLifecycleComponent and by handling the lookup of the SourceResolver.
WARNING: This is a "private" Cocoon core class - do NOT use this class
directly - and do not assume that a ComponentManager you get via the
compose() method is an instance of CocoonComponentManager."
 
So, am I supposed to use this or not? If not, what would be a better way?
 
Cheers,
Stefan

 


Re: Access to the AuthenticationContext

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Stefan Pietschmann:

> Will try that. Thank you, Jean.

I was expecting this one...

In english it would be « John » rather than « Jean », as I'm not a
girl.  Will it change in a future life, I don't know ;-)

So please call me « John » if  you want to save keystrokes, or the
plain old « Jean-Baptiste ».

All the best,
-- 
Jean-Baptiste Quenot
Systèmes d'Information
ANYWARE TECHNOLOGIES
Tel : +33 (0)5 61 00 52 90
Fax : +33 (0)5 61 00 51 46
http://www.anyware-tech.com/

AW: Access to the AuthenticationContext

Posted by Stefan Pietschmann <de...@gmail.com>.
Thank you, I had even tried that before, but just found out the reason why
it didn't work then - the Context doesn't properly support XPath:

I was requesting

getInputModuleAttribute("authentication//ID");

>From a quick look at the ContextInputModule you will see that this is only
translated into

context = contextManager.getContext("authentication");
something = context.getXML("//ID")
...

The context returns null. However, this works:
getInputModuleAttribute("authentication/authentication/ID");

I tried different ways retrieving information from the context:

context.getXML("authentication/ID"); -> "guest"
context.getXML("/authentication/ID"); -> "guest"
context.getXML("//ID"); --> NULL
context.getXML("authentication//ID"); --> NULL

The last both should be "guest" as well, but it's not working. If those
basics don't work, XPath is pretty useless to me. :(

Stefan


| -----Ursprüngliche Nachricht-----
| Von: Jean-Baptiste Quenot [mailto:jbq@anyware-tech.com]
| Gesendet: Donnerstag, 12. Januar 2006 14:36
| An: dev@cocoon.apache.org
| Betreff: Re: Access to the AuthenticationContext
| 
| * Stefan Pietschmann:
| 
| > So I tried many ways to get just one parameter:
| >
| > getInputModuleAttribute("/authentication/ID"));
| 
| The right one is:
| 
| getInputModuleAttribute("authentication/authentication/ID");
| 
| You can use XPath constructs only after the first component of the
| path.  As stated in the Javadoc:
| 
| This input module provides access to the information of a session
| context using an XPath. The XPath expression that can be used is
| the same as for the session transformer.  The first information in
| the path is the context, ...
| 
| See ContextInputModule.
| --
| Jean-Baptiste Quenot
| Systèmes d'Information
| ANYWARE TECHNOLOGIES
| Tel : +33 (0)5 61 00 52 90
| Fax : +33 (0)5 61 00 51 46
| http://www.anyware-tech.com/


Re: Access to the AuthenticationContext

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Stefan Pietschmann:

> So I tried many ways to get just one parameter:
> 
> getInputModuleAttribute("/authentication/ID"));

The right one is:

getInputModuleAttribute("authentication/authentication/ID");

You can use XPath constructs only after the first component of the
path.  As stated in the Javadoc:

This input module provides access to the information of a session
context using an XPath. The XPath expression that can be used is
the same as for the session transformer.  The first information in
the path is the context, ...

See ContextInputModule.
-- 
Jean-Baptiste Quenot
Systèmes d'Information
ANYWARE TECHNOLOGIES
Tel : +33 (0)5 61 00 52 90
Fax : +33 (0)5 61 00 51 46
http://www.anyware-tech.com/

AW: Access to the AuthenticationContext

Posted by Stefan Pietschmann <de...@gmail.com>.
Will try that. Thank you, Jean.

Stefan

| -----Ursprüngliche Nachricht-----
| Von: Jean-Baptiste Quenot [mailto:jbq@anyware-tech.com]
| Gesendet: Montag, 9. Januar 2006 13:15
| An: dev@cocoon.apache.org
| Betreff: Re: Access to the AuthenticationContext
| 
| * Stefan Pietschmann:
| > | You can also use the « session-context » input module to retrieve
| > | the piece of information more nicely:
| >
| > I would actually call this like
| > getInputModuleAttribute(manager,"session-context","/authentication/id")?
| And
| > it would return the id?
| 
| Yes, quite: getInputModuleAttribute(manager,"session-
| context","authentication/authentication/id")
| 
| > Even if so, I don't always have the full xpath and have to search for
| the
| > node, which would be kinda hard this way.
| 
| You can use an XPath expression like:
| 
| getInputModuleAttribute(manager,"session-context","//id")
| 
| Best regards,
| --
| Jean-Baptiste Quenot
| Systèmes d'Information
| ANYWARE TECHNOLOGIES
| Tel : +33 (0)5 61 00 52 90
| Fax : +33 (0)5 61 00 51 46
| http://www.anyware-tech.com/


AW: Access to the AuthenticationContext

Posted by Stefan Pietschmann <de...@gmail.com>.
Hmpf, I don't get it to work. Btw, I get the context from the
SessionManager, but you made me curious about the session-context input
module (mainly because the XPath). Here's my method (I have the manager
object as an instance variable and only use the session-context, so it
slightly differs from the method proposed by John):

private String getInputModuleAttribute(String path) {
	ServiceSelector selector = (ServiceSelector)
manager.lookup(InputModule.ROLE + "Selector");
      InputModule inputModule = (InputModule)
selector.select("session-context");
      return (String) inputModule.getAttribute(path, null, null);
}

Of course I handle the exceptions (and none is thrown btw), but I left that
out for readability purposes.
So I tried many ways to get just one parameter:

getInputModuleAttribute("//id"));
getInputModuleAttribute("/authentication/ID"));
getInputModuleAttribute("//ID"));
getInputModuleAttribute("session-context:authentication/ID"));
getInputModuleAttribute("session-context:authentication//ID"));
getInputModuleAttribute("session-context:authentication/authentication/ID"))
;

but all I get in return is null.

http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/webapps/session/compo
nents/ContextInputModule.html#getAttribute(java.lang.String,%20org.apache.av
alon.framework.configuration.Configuration,%20java.util.Map) says, that null
is returned if the value doesn't exist. Huh?

The value is there - I can get it by calling
sessionManager.getContextFragment("authentication", "/authentication/ID")).

Where's my daily mistake this time? :)

Cheers,
Stefan




| -----Ursprüngliche Nachricht-----
| Von: Jean-Baptiste Quenot [mailto:jbq@anyware-tech.com]
| Gesendet: Montag, 9. Januar 2006 13:15
| An: dev@cocoon.apache.org
| Betreff: Re: Access to the AuthenticationContext
| 
| * Stefan Pietschmann:
| > | You can also use the « session-context » input module to retrieve
| > | the piece of information more nicely:
| >
| > I would actually call this like
| > getInputModuleAttribute(manager,"session-context","/authentication/id")?
| And
| > it would return the id?
| 
| Yes, quite: getInputModuleAttribute(manager,"session-
| context","authentication/authentication/id")
| 
| > Even if so, I don't always have the full xpath and have to search for
| the
| > node, which would be kinda hard this way.
| 
| You can use an XPath expression like:
| 
| getInputModuleAttribute(manager,"session-context","//id")
| 
| Best regards,
| --
| Jean-Baptiste Quenot
| Systèmes d'Information
| ANYWARE TECHNOLOGIES
| Tel : +33 (0)5 61 00 52 90
| Fax : +33 (0)5 61 00 51 46
| http://www.anyware-tech.com/


Re: Access to the AuthenticationContext

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Stefan Pietschmann:
> | You can also use the « session-context » input module to retrieve
> | the piece of information more nicely:
> 
> I would actually call this like
> getInputModuleAttribute(manager,"session-context","/authentication/id")? And
> it would return the id?

Yes, quite: getInputModuleAttribute(manager,"session-context","authentication/authentication/id")

> Even if so, I don't always have the full xpath and have to search for the
> node, which would be kinda hard this way.

You can use an XPath expression like:

getInputModuleAttribute(manager,"session-context","//id")

Best regards,
-- 
Jean-Baptiste Quenot
Systèmes d'Information
ANYWARE TECHNOLOGIES
Tel : +33 (0)5 61 00 52 90
Fax : +33 (0)5 61 00 51 46
http://www.anyware-tech.com/

AW: Access to the AuthenticationContext

Posted by Stefan Pietschmann <de...@gmail.com>.
| Jean-Baptiste Quenot [mailto:jbq@anyware-tech.com]
| 
| * Stefan Pietschmann:
| 
| > So, am I  supposed to use this  or not? If not, what  would be a
| > better way?
| 
| Don't  getSitemapComponentManager().   Why   don't  you  use  your
| ServiceManager to lookup the component?

Well, how? I don't know what I'm supposed to lookup! As I said... strangely
enough when I lookup the SessionManager from my ServiceManager I get null.
Once I look it up from the ComponentManager I get from
getSitemapComponentManager() it works.

ComponentManager componentManager =  
    CocoonComponentManager.getSitemapComponentManager();
sessionManager = (SessionManager) 
    componentManager.lookup(SessionManager.ROLE);

works, but 

sessionManager = (SessionManager) manager.lookup(SessionManager.ROLE);

doesn't.

| 
| You can also use the « session-context » input module to retrieve
| the piece of information more nicely:
| 
|     public static String getInputModuleAttribute(ServiceManager manager,
| String module, String value) {
|                 InputModule inputModule;
|                 try {
|                         ServiceSelector selector = (ServiceSelector)
| manager.lookup(InputModule.ROLE + "Selector");
|                         inputModule =
| (InputModule)selector.select(module);
|                         return (String) inputModule.getAttribute(value,
| null, null);
|                 } catch (ServiceException e1) {
|                         throw new CascadingRuntimeException("Could not
| select " + module + " input module", e1);
|                 } catch (ConfigurationException e1) {
|                         throw new CascadingRuntimeException("Could not get
| input module value for " + module + ":" + value, e1);
|                 }
|     }

No idea about this. I haven't worked with InputModules yet, so feel free to
correct me.
I would actually call this like
getInputModuleAttribute(manager,"session-context","/authentication/id")? And
it would return the id?
Even if so, I don't always have the full xpath and have to search for the
node, which would be kinda hard this way.

Thank you,
Stefan
 
| Cheers,
| --
| Jean-Baptiste Quenot
| Systèmes d'Information
| ANYWARE TECHNOLOGIES
| Tel : +33 (0)5 61 00 52 90
| Fax : +33 (0)5 61 00 51 46
| http://www.anyware-tech.com/


Re: Access to the AuthenticationContext

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Stefan Pietschmann:

> So, am I  supposed to use this  or not? If not, what  would be a
> better way?

Don't  getSitemapComponentManager().   Why   don't  you  use  your
ServiceManager to lookup the component?

You can also use the « session-context » input module to retrieve
the piece of information more nicely:

    public static String getInputModuleAttribute(ServiceManager manager, String module, String value) {
                InputModule inputModule;
                try {
                        ServiceSelector selector = (ServiceSelector) manager.lookup(InputModule.ROLE + "Selector");
                        inputModule = (InputModule)selector.select(module);
                        return (String) inputModule.getAttribute(value, null, null);
                } catch (ServiceException e1) {
                        throw new CascadingRuntimeException("Could not select " + module + " input module", e1);
                } catch (ConfigurationException e1) {
                        throw new CascadingRuntimeException("Could not get input module value for " + module + ":" + value, e1);
                }
    }

Cheers,
-- 
Jean-Baptiste Quenot
Systèmes d'Information
ANYWARE TECHNOLOGIES
Tel : +33 (0)5 61 00 52 90
Fax : +33 (0)5 61 00 51 46
http://www.anyware-tech.com/