[Seaside] Accessing current session in RestfulFilter

Esteban Lorenzano estebanlm at gmail.com
Wed May 28 11:40:44 UTC 2014


AFAIK, REST purpose is to be stateless, and because of that you do not have the session available when passing through the filter. 
Nevertheless, I hacked the REST filter to use it as an bookmarked entry point for my applications (to provide readable urls). What I did is to add this:

(notice that is kind of a hack… probably there is a better way to do this).

MyRestFilter>>#continueWithRoot: aBlock
	"Continues the execution of the application. 
	 I will execute aBlock with the root component of app"
	| requestContext application session rootComponent |
	requestContext := self requestContext.	
	application := requestContext application.
	session := self obtainSessionWithApplication: application context: requestContext.

		push: session 
		during: [
			rootComponent := requestContext rootComponentIfAbsent: [ nil ].
			rootComponent ifNil: [ 
				rootComponent := (application preferenceAt: #rootClass) new.
				requestContext rootComponent: rootComponent ].
			aBlock value: rootComponent.
			self next handleFiltered: requestContext ]. 

MyRestFilter>>#obtainSessionWithApplication: application context: requestContext
	| sessionKey session |
	sessionKey  := application trackingStrategy keyFromContext: requestContext.	
	session := sessionKey ifNotNil: [  
		application cache 
			at: sessionKey  
			ifAbsent: [ nil] ].
	session  ifNil: [ 
		session := application newSession.
		application register: session ].
	^ session

with that, I can add some filters like this:

	<path: '/new'>

	self continueWithRoot: [ :root | 
		root show: EveSpyNewTrackPanel new ].

If I understand what I’m doing, this will: 

a) look for an active session
a.1) retrieve a new one if not found
b) obtain the root component for active session
c) use #show:, to display the component I want to display. 

It works… I don’t know if it can be done better… but that’s how I provide readable urls to my apps usually :)


On 28 May 2014, at 06:00, Philippe Marschall <philippe.marschall at gmail.com> wrote:

> On Wed, May 28, 2014 at 2:08 AM, Esteban A. Maringolo
> <emaringolo at gmail.com> wrote:
>> The RESTful filter is part of the Component as a decorator of it,
> Are you sure you meant to say component? Didn't you want to say application?
>> but
>> if I try to access the current session dynamic variable it is not
>> available.
>> How do I do to retrieve the current session in the filter?
>> Is it possible?
> The application looks up the session and sets up the dynamic variable.
> The filter runs earlier.
> What are you trying to achieve?
> Cheers
> Philippe
> _______________________________________________
> seaside mailing list
> seaside at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside

More information about the seaside mailing list