[Seaside] Accessing current session in RestfulFilter
Philippe Marschall
philippe.marschall at gmail.com
Sun Jun 1 15:59:31 UTC 2014
On Wed, May 28, 2014 at 1:40 PM, Esteban Lorenzano <estebanlm at gmail.com> wrote:
> Hi,
>
> 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.
>
> 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:
>
> MyRestFilter>>#newScan
> <get>
> <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 :)
Here's what you should do:
- subclass WARestfulComponentFilter
- add it to your application
- implement the methods for the URL patterns you need
- send #startSessionWithRoot: in your methods
Cheers
Philippe
More information about the seaside
mailing list