You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Josh2007 <jo...@gmail.com> on 2007/10/09 23:13:37 UTC

How to access cocoon object from javascript scriptAction?

Hi all,

I need to access cocoon object from a javascript scriptAction to make use of
session, request... functions.
It seems the only way to access cocoon object is to create a flowscript.
Is there another solution as I don't want to redirect to another pipeline
after the script is performed unlike flowscript does?

Thanks,

Josh
-- 
View this message in context: http://www.nabble.com/How-to-access-cocoon-object-from-javascript-scriptAction--tf4596915.html#a13124834
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Joerg Heinicke <jo...@gmx.de>.
On 10.10.2007 13:51 Uhr, Josh2007 wrote:

> I use request.getSession() instead of objectModel.session and replace
> objectModel.session.setAttribute("stream", stream) with
> session.setAttribute("stream", stream) and it works.

Just to complete the picture ... The manager is only for retrieving 
Cocoon components, not for the environment stuff. The latter you have to 
get from the object model. For the session it would have been something 
like 
Packages.org.apache.cocoon.environment.ObjectModelHelper.getSession(objectModel). 
But getting the session from the request is good as well.

Joerg

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Josh2007 <jo...@gmail.com>.
Sorry, everything is fine.
I use request.getSession() instead of objectModel.session and replace
objectModel.session.setAttribute("stream", stream) with
session.setAttribute("stream", stream) and it works.

Thanks again,

Josh



Josh2007 wrote:
> 
> Thanks Joerg, I'm almost done.
> 
> Not familiar with Java, I still need to get access to the session to store
> my xml.
> Using session =
> manager.lookup(Packages.org.apache.cocoon.environmentSession.ROLE); I get
> an error "no public instance called "ROLE".
> Furthermore I use objectModel to get sitemap instance and do
> objectModel.session.setAttribute(...) but I'm not sure about it's the good
> way.
> 
> Here's my scriptAction, if you have time to take a look.
> Thanks,
> 
> Josh
> 
> 
> // streamToSession
> /*
> get xforms post from stream and store the result 
> in session attribute "stream".
> */
> 
> // Step 1 — Retrieve helper "beans" from the BSF framework
> request = bsf.lookupBean( "request" )
> logger = bsf.lookupBean( "logger" )
> actionMap = bsf.lookupBean( "actionMap" )
> 
> objectModel = bsf.lookupBean( "objectModel" )
> manager = bsf.lookupBean( "manager" )
> 
> // Step 2 — Perform the action
> logger.debug( "START streamToSession.js" )
> 
> // loadDocument() reads in an XML file and returns a DOM Document. 
> function loadDocument(uri) {
> var parser = null;
> var source = null;
> var resolver = null;
> try {
> parser =
> manager.lookup(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
> resolver =
> manager.lookup(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
> source = resolver.resolveURI(uri);
> var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
> is.setSystemId(source.getURI());
> return parser.parseDocument(is);
> } finally {
> if (source != null) resolver.release(source);
> }
> }
> 
> // ------------------------- getStreamToSession
> -----------------------------------------------------------------------------------------------------------------------------
> 
> // retrieve the XML
> var stream = loadDocument("cocoon:/stream");
> 
> // save stream in session
> var session = objectModel.session;
> objectModel.session.setAttribute("stream", stream);
> 
> //
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> 
> // Hello action always succeeds, and returns message
> actionMap.put( "scriptaction-continue", "" )
> actionMap.put( "message", "success!" )
> logger.debug( "END streamToSession.js" )
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-access-cocoon-object-from-javascript-scriptAction--tf4596915.html#a13140932
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Josh2007 <jo...@gmail.com>.
Thanks Joerg, I'm almost done.

Not familiar with Java, I still need to get access to the session to store
my xml.
Using session =
manager.lookup(Packages.org.apache.cocoon.environmentSession.ROLE); I get an
error "no public instance called "ROLE".
Furthermore I use objectModel to get sitemap instance and do
objectModel.session.setAttribute(...) but I'm not sure about it's the good
way.

Here's my scriptAction, if you have time to take a look.
Thanks,

Josh


// streamToSession
/*
get xforms post from stream and store the result 
in session attribute "stream".
*/

// Step 1 — Retrieve helper "beans" from the BSF framework
request = bsf.lookupBean( "request" )
logger = bsf.lookupBean( "logger" )
actionMap = bsf.lookupBean( "actionMap" )

objectModel = bsf.lookupBean( "objectModel" )
manager = bsf.lookupBean( "manager" )

// Step 2 — Perform the action
logger.debug( "START streamToSession.js" )

// loadDocument() reads in an XML file and returns a DOM Document. 
function loadDocument(uri) {
var parser = null;
var source = null;
var resolver = null;
try {
parser =
manager.lookup(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
resolver =
manager.lookup(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
source = resolver.resolveURI(uri);
var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
is.setSystemId(source.getURI());
return parser.parseDocument(is);
} finally {
if (source != null) resolver.release(source);
}
}

// ------------------------- getStreamToSession
-----------------------------------------------------------------------------------------------------------------------------

// retrieve the XML
var stream = loadDocument("cocoon:/stream");

// save stream in session
var session = objectModel.session;
objectModel.session.setAttribute("stream", stream);

//
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Hello action always succeeds, and returns message
actionMap.put( "scriptaction-continue", "" )
actionMap.put( "message", "success!" )
logger.debug( "END streamToSession.js" )

-- 
View this message in context: http://www.nabble.com/How-to-access-cocoon-object-from-javascript-scriptAction--tf4596915.html#a13137398
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Joerg Heinicke <jo...@gmx.de>.
Instead of cocoon.getComponent() you have to use manager.lookup(). Look 
at ServiceManager [1] for the details. It's available in your JavaScript 
as you can see in ScriptAction's code.

Joerg

[1] 
http://excalibur.apache.org/apidocs/org/apache/avalon/framework/service/ServiceManager.html

On 10.10.2007 7:44 Uhr, Josh2007 wrote:
> Thanks for the information,
> 
> I took a look at ScriptAction's source code.
> I have a better understanding of how it works but it seems I need to import
> in my scriptAction other components and functions (DOMParser,
> session.setAttribute...).
> 
> Actually I'm trying to adapt my flowscript code to a scriptAction as I need
> to stay in the same sitemap after the execution of the javascript.
> 
> Here's the flowscript I'm trying to adapt.
> 
> // loadDocument() reads in an XML file and returns a DOM Document. 
> function loadDocument(uri) {
> var parser = null;
> var source = null;
> var resolver = null;
> try {
> parser =
> cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
> resolver =
> cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
> source = resolver.resolveURI(uri);
> var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
> is.setSystemId(source.getURI());
> return parser.parseDocument(is);
> } finally {
> if (source != null) resolver.release(source);
> if (parser != null) cocoon.releaseComponent(parser);
> if (resolver != null) cocoon.releaseComponent(resolver);
> }
> }
> 
> // ------------------------- getStreamToSession
> -------------------------------------------
> 
> // retrieve the XML
> var stream = loadDocument("cocoon:/stream");
> 
> // save stream in session
> var session = cocoon.session;
> session.setAttribute("stream", stream);
> 
> //
> ---------------------------------------------------------------------------------------------
> 
> Of course, getComponent to load java components is undefined as I don't have
> access to the class, the same with the parser.
> 
> I noticed that getComponent is part of org.apache.components.flow and
> therefore not accessible from scriptAction.
> 
> I should be able to import java comonents with BSF. 
> 
> Do you have any idea how to do it?
> 
> Thanks,
> 
> Josh
> 
> 
> Joerg Heinicke wrote:
>> On 09.10.2007 17:13 Uhr, Josh2007 wrote:
>>
>>> I need to access cocoon object from a javascript scriptAction to make use
>>> of
>>> session, request... functions.
>> The ScriptAction works more like a usual Cocoon Java component than like 
>> flowscript. Therefore you have the ObjectModel available which is 
>> actually only a Map. To access the stuff you need you use the 
>> ObjectModelHelper and call the corresponding static methods like 
>> getRequest() or getSession() [1]. If you have a look into ScriptAction's 
>> source code (which is simple) you will see the other objects it 
>> registers for usage in the JavaScript:
>>
>> mgr.registerBean("resolver", resolver);
>> mgr.registerBean("objectModel", objectModel);
>> mgr.registerBean("parameters", par);
>>
>> // ScriptAction housekeeping
>> mgr.registerBean("actionMap", actionMap);
>>
>> // helpers
>> mgr.registerBean("logger", getLogger());
>> mgr.registerBean("request", ObjectModelHelper.getRequest(objectModel) );
>> mgr.registerBean("scriptaction", this );
>> mgr.registerBean("manager", this.manager );
>>
>>
>>> It seems the only way to access cocoon object is to create a flowscript.
>>> Is there another solution as I don't want to redirect to another pipeline
>>> after the script is performed unlike flowscript does?
>> The cocoon object is only a simplified version to access all the stuff 
>> in the ObjectModel. It's indeed only available in flow script.
>>
>> Joerg
>>
>> [1] 
>> http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/environment/ObjectModelHelper.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Josh2007 <jo...@gmail.com>.
Thanks for the information,

I took a look at ScriptAction's source code.
I have a better understanding of how it works but it seems I need to import
in my scriptAction other components and functions (DOMParser,
session.setAttribute...).

Actually I'm trying to adapt my flowscript code to a scriptAction as I need
to stay in the same sitemap after the execution of the javascript.

Here's the flowscript I'm trying to adapt.

// loadDocument() reads in an XML file and returns a DOM Document. 
function loadDocument(uri) {
var parser = null;
var source = null;
var resolver = null;
try {
parser =
cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
resolver =
cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
source = resolver.resolveURI(uri);
var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
is.setSystemId(source.getURI());
return parser.parseDocument(is);
} finally {
if (source != null) resolver.release(source);
if (parser != null) cocoon.releaseComponent(parser);
if (resolver != null) cocoon.releaseComponent(resolver);
}
}

// ------------------------- getStreamToSession
-------------------------------------------

// retrieve the XML
var stream = loadDocument("cocoon:/stream");

// save stream in session
var session = cocoon.session;
session.setAttribute("stream", stream);

//
---------------------------------------------------------------------------------------------

Of course, getComponent to load java components is undefined as I don't have
access to the class, the same with the parser.

I noticed that getComponent is part of org.apache.components.flow and
therefore not accessible from scriptAction.

I should be able to import java comonents with BSF. 

Do you have any idea how to do it?

Thanks,

Josh


Joerg Heinicke wrote:
> 
> On 09.10.2007 17:13 Uhr, Josh2007 wrote:
> 
>> I need to access cocoon object from a javascript scriptAction to make use
>> of
>> session, request... functions.
> 
> The ScriptAction works more like a usual Cocoon Java component than like 
> flowscript. Therefore you have the ObjectModel available which is 
> actually only a Map. To access the stuff you need you use the 
> ObjectModelHelper and call the corresponding static methods like 
> getRequest() or getSession() [1]. If you have a look into ScriptAction's 
> source code (which is simple) you will see the other objects it 
> registers for usage in the JavaScript:
> 
> mgr.registerBean("resolver", resolver);
> mgr.registerBean("objectModel", objectModel);
> mgr.registerBean("parameters", par);
> 
> // ScriptAction housekeeping
> mgr.registerBean("actionMap", actionMap);
> 
> // helpers
> mgr.registerBean("logger", getLogger());
> mgr.registerBean("request", ObjectModelHelper.getRequest(objectModel) );
> mgr.registerBean("scriptaction", this );
> mgr.registerBean("manager", this.manager );
> 
> 
>> It seems the only way to access cocoon object is to create a flowscript.
>> Is there another solution as I don't want to redirect to another pipeline
>> after the script is performed unlike flowscript does?
> 
> The cocoon object is only a simplified version to access all the stuff 
> in the ObjectModel. It's indeed only available in flow script.
> 
> Joerg
> 
> [1] 
> http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/environment/ObjectModelHelper.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-access-cocoon-object-from-javascript-scriptAction--tf4596915.html#a13134122
Sent from the Cocoon - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to access cocoon object from javascript scriptAction?

Posted by Joerg Heinicke <jo...@gmx.de>.
On 09.10.2007 17:13 Uhr, Josh2007 wrote:

> I need to access cocoon object from a javascript scriptAction to make use of
> session, request... functions.

The ScriptAction works more like a usual Cocoon Java component than like 
flowscript. Therefore you have the ObjectModel available which is 
actually only a Map. To access the stuff you need you use the 
ObjectModelHelper and call the corresponding static methods like 
getRequest() or getSession() [1]. If you have a look into ScriptAction's 
source code (which is simple) you will see the other objects it 
registers for usage in the JavaScript:

mgr.registerBean("resolver", resolver);
mgr.registerBean("objectModel", objectModel);
mgr.registerBean("parameters", par);

// ScriptAction housekeeping
mgr.registerBean("actionMap", actionMap);

// helpers
mgr.registerBean("logger", getLogger());
mgr.registerBean("request", ObjectModelHelper.getRequest(objectModel) );
mgr.registerBean("scriptaction", this );
mgr.registerBean("manager", this.manager );


> It seems the only way to access cocoon object is to create a flowscript.
> Is there another solution as I don't want to redirect to another pipeline
> after the script is performed unlike flowscript does?

The cocoon object is only a simplified version to access all the stuff 
in the ObjectModel. It's indeed only available in flow script.

Joerg

[1] 
http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/environment/ObjectModelHelper.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org