LIFT improvement

159 views
Skip to first unread message

Viktor Klang

unread,
Sep 28, 2007, 10:00:58 AM9/28/07
to lif...@googlegroups.com
Greets,
 
I was looking through some of the lift source and noticed the strategy to use a servlet-mapping and then pipe out existing resources if there are resources matching the request URI.
 
Why not use a Filter instead, and use a default-filtermapping?
This would remove the somewhat ugly readAndWrite-hack and the arbitrary mime-type lookup.
in doFilter (note: pseudo-code)
 
if(isRealResource(requestURI))
    filterChain.doFilter(request,response);
else
{
   //do the lift stuff
}
 
 
Wouldn't this work much better and be cleaner?
 
Best regards
Viktor Klang

David Pollak

unread,
Sep 28, 2007, 12:48:35 PM9/28/07
to lif...@googlegroups.com
Viktor,

You are way more up on how to do Servlets than I am.  This looks like a great change.

Feel free to either open a ticket on the Google Code project or submit a patch (if you submit a patch, it would be ideal if you sign the IP Assignment agreement to keep the lift IP clean.)

If I make the change, it'll be a week to 2 weeks before the code gets checked in.

Thanks,

David
--
lift, the secure, simple, powerful web framework
http://liftweb.net

Viktor Klang

unread,
Sep 28, 2007, 12:57:44 PM9/28/07
to lif...@googlegroups.com
Yo man, I'll code it up, test it out and submit it as a patch.
 
Best regards
Viktor
--
Software Architect
Yves Rocher Nordique
 

Viktor Klang

unread,
Sep 28, 2007, 5:14:44 PM9/28/07
to lif...@googlegroups.com
Yo people,

Ok, I've done some twiddling and some testing, I'll paste in some sample-code (I didn't have time to play with the lift source direcly, but I have good hopes that you are competent enough to put it in :)

Here you go :)

----begin LiftFilter.scala---
[code]
package lift.http

import javax.servlet._
import javax.servlet.http._
import java.io._
import java.net._

class LiftFilter extends Filter
{
  //The variable holds the current ServletContext (we need it for request URI - handling
   var context : ServletContext = null;
  
    @throws(classOf[IOException])
    @throws(classOf[ServletException])
    override def doFilter(req: ServletRequest, res: ServletResponse,chain: FilterChain) =
    {     
   //If it's a lift-request, do the lift magic
       if(req.isInstanceOf[HttpServletRequest] && isLiftRequest_?( req.asInstanceOf[HttpServletRequest]))
         lift(req.asInstanceOf[HttpServletRequest],res.asInstanceOf[HttpServletResponse])
       else //Else just pretend like we don't exist and serve those static files baby!
         chain.doFilter(req,res)
    }
 
    //We need to capture the ServletContext on init
    @throws(classOf[ServletException])
    override def init(config:FilterConfig) = { context = config.getServletContext }
   
 //And throw it away on destruction
    override def destroy = { context = null }
 
    private def lift(req: HttpServletRequest, res: HttpServletResponse) : Unit =
    {
       //Do your lift stuff here
    }
 
    //This function tells you wether a resource exists or not, could probably be better
    private def isLiftRequest_?(req: HttpServletRequest) : boolean = {
      context.getResource(req.getRequestURI.substring (req.getContextPath.length)) == null
    }
}

---- end LiftFilter.scala ----

-----begin web.xml ----

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <filter>
        <filter-name>Lift</filter-name>
        <filter-class>lift.http.LiftFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Lift</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

---- End web.xml ----

Burak Emir

unread,
Sep 28, 2007, 5:29:26 PM9/28/07
to lif...@googlegroups.com
Yo Viktor,

just a suggestion: how about replacing

if(req.isInstanceOf[HttpServletRequest] && isLiftRequest_?( req.asInstanceOf[HttpServletRequest]))
         lift(req.asInstanceOf[HttpServletRequest],res.asInstanceOf[HttpServletResponse])
       else //Else just pretend like we don't exist and serve those static files baby!
         chain.doFilter(req,res)

with

req match {
  case reqh: HttpServletRequest if isLiftRequest_?(reqh) => lift(reqh, res.asInstanceOf[HttpServletResponse])
  case _  => chain.doFilter(req,res)
}

it looks a tiny bit cleaner to me and you save the second cast. if you can live with an additional instanceof test (it might not hurt performance, the JVM has become quite clever for all that is type test and cast) , then

(req,res) match {
  case (reqh: HttpServletRequest, resh: HttpServletResponse) if isLiftRequest_?(reqh)=> lift(reqh, resh)
  case _  => chain.doFilter(req,res)
}

cheers,
Burak
--
Burak Emir
Research Assistant / PhD Candidate
Programming Methods Group
EPFL, 1015 Lausanne, Switzerland
http://lamp.epfl.ch/~emir

Viktor Klang

unread,
Sep 28, 2007, 5:33:57 PM9/28/07
to lif...@googlegroups.com
Yo Burak,

thanks for the hints man!

I'm an imperative man and new to this flashy shit :)

Burak Emir

unread,
Sep 28, 2007, 5:42:40 PM9/28/07
to lif...@googlegroups.com
On 9/28/07, Viktor Klang <viktor...@gmail.com> wrote:
Yo Burak,

thanks for the hints man!

you're welcome ;-) see it as my attempt at fighting the immense lack of Scala documentation.

I'm an imperative man and new to this flashy shit :)

believe me bro, there's much more where this came from |-)

Burak

Viktor Klang

unread,
Sep 28, 2007, 5:54:32 PM9/28/07
to lif...@googlegroups.com
On 9/28/07, Burak Emir <burak...@gmail.com> wrote:


On 9/28/07, Viktor Klang < viktor...@gmail.com> wrote:
Yo Burak,

thanks for the hints man!

you're welcome ;-) see it as my attempt at fighting the immense lack of Scala documentation.


Well, I had an idea today, and discussed with John Nilsson (from the Scala list and also from my workplace),
the thing is that I think currently functional programmers have a much easier time getting into the Scala groove.
And I htink the win-strategy is to have a good way to embrace the imperative Java-monkeys.

So, how about this: Every week someone posts a simple, relevant (no frickin' fibonacci crap) and small code problem written in plain Java to the Scala-list.
And then all the different subscribers with different backgrounds come up with a Scala solution with comments and explanation on why they have solved the problem the way they did.
 
This way we can have code-examples ranging from Java -> Javaesque Scala to Java -> Pure bred sleek Scala

This gives us the possibility to teach the different types of approches and model a new way of thinking and structuring code for adopters coming from an imperative background.


I'm an imperative man and new to this flashy shit :)

believe me bro, there's much more where this came from |-)

dat be some fly shit :D
 
Viktor

Ben Lee

unread,
Sep 28, 2007, 6:58:30 PM9/28/07
to liftweb
> Well, I had an idea today, and discussed with John Nilsson (from the Scala
> list and also from my workplace),
> the thing is that I think currently functional programmers have a much
> easier time getting into the Scala groove.
> And I htink the win-strategy is to have a good way to embrace the imperative
> Java-monkeys.
>
> So, how about this: Every week someone posts a simple, relevant (no frickin'
> fibonacci crap) and small code problem written in plain Java to the
> Scala-list.
> And then all the different subscribers with different backgrounds come up
> with a Scala solution with comments and explanation on why they have solved
> the problem the way they did.
>
> This way we can have code-examples ranging from Java -> Javaesque Scala to
> Java -> Pure bred sleek Scala
>
> This gives us the possibility to teach the different types of approches and
> model a new way of thinking and structuring code for adopters coming from an
> imperative background.
>
> I'm an imperative man and new to this flashy shit :)

what about PLEAC?

http://pleac.sourceforge.net/

B

> > > > > On 9/28/07, Viktor Klang <viktor.kl...@gmail.com> wrote:
>
> > > > > > Yo man, I'll code it up, test it out and submit it as a patch.
>
> > > > > > Best regards
> > > > > > Viktor
> > > > > > --
> > > > > > Software Architect
> > > > > > Yves Rocher Nordique
>

> > > > > > On 9/28/07, David Pollak <feeder.of.the.be...@gmail.com > wrote:
>
> > > > > > > Viktor,
>
> > > > > > > You are way more up on how to do Servlets than I am. This looks
> > > > > > > like a great change.
>
> > > > > > > Feel free to either open a ticket on the Google Code project or
> > > > > > > submit a patch (if you submit a patch, it would be ideal if you sign the IP
> > > > > > > Assignment agreement to keep the lift IP clean.)
>
> > > > > > > If I make the change, it'll be a week to 2 weeks before the code
> > > > > > > gets checked in.
>
> > > > > > > Thanks,
>
> > > > > > > David
>

> > > > > > > On 9/28/07, Viktor Klang < viktor.kl...@gmail.com > wrote:
>
> > > > > > > > Greets,
>

> > > > > > > > I was looking through some of the *lift* source and noticed


> > > > > > > > the strategy to use a servlet-mapping and then pipe out existing resources
> > > > > > > > if there are resources matching the request URI.
>
> > > > > > > > Why not use a Filter instead, and use a default-filtermapping?
> > > > > > > > This would remove the somewhat ugly readAndWrite-hack and the
> > > > > > > > arbitrary mime-type lookup.
> > > > > > > > in doFilter (note: pseudo-code)
>
> > > > > > > > if(isRealResource(requestURI))
> > > > > > > > filterChain.doFilter(request,response);
> > > > > > > > else
> > > > > > > > {

> > > > > > > > //do the *lift* stuff


> > > > > > > > }
>
> > > > > > > > Wouldn't this work much better and be cleaner?
>
> > > > > > > > Best regards
> > > > > > > > Viktor Klang
>
> > > > > > > --
> > > > > > > lift, the secure, simple, powerful web framework
> > > > > > >http://liftweb.net
>
> > > > > --
> > > > > Software Architect
> > > > > Yves Rocher Nordique
>
> > > > --
> > > > Burak Emir
> > > > Research Assistant / PhD Candidate
> > > > Programming Methods Group
> > > > EPFL, 1015 Lausanne, Switzerland

> > > >http://lamp.epfl.ch/~emir<http://lamp.epfl.ch/%7Eemir>


>
> > > --
> > > Software Architect
> > > Yves Rocher Nordique
>
> > --
> > Burak Emir
> > Research Assistant / PhD Candidate
> > Programming Methods Group
> > EPFL, 1015 Lausanne, Switzerland

> >http://lamp.epfl.ch/~emir<http://lamp.epfl.ch/%7Eemir>

Viktor Klang

unread,
Sep 28, 2007, 7:01:13 PM9/28/07
to lif...@googlegroups.com
On 9/29/07, Ben Lee <midf...@gmail.com> wrote:

> Well, I had an idea today, and discussed with John Nilsson (from the Scala
> list and also from my workplace),
> the thing is that I think currently functional programmers have a much
> easier time getting into the Scala groove.
> And I htink the win-strategy is to have a good way to embrace the imperative
> Java-monkeys.
>
> So, how about this: Every week someone posts a simple, relevant (no frickin'
> fibonacci crap) and small code problem written in plain Java to the
> Scala-list.
> And then all the different subscribers with different backgrounds come up
> with a Scala solution with comments and explanation on why they have solved
> the problem the way they did.
>
> This way we can have code-examples ranging from Java -> Javaesque Scala to
> Java -> Pure bred sleek Scala
>
> This gives us the possibility to teach the different types of approches and
> model a new way of thinking and structuring code for adopters coming from an
> imperative background.
>
> I'm an imperative man and new to this flashy shit :)

what about PLEAC?

http://pleac.sourceforge.net/

B


Would probably work, We'd just need someone/someones to do minor "editing" (in the proper sense  on the word) of the collected samples.

Viktor


Daniel Green

unread,
Sep 29, 2007, 3:49:11 PM9/29/07
to lif...@googlegroups.com
We could have a "port web application" a month :-). That would be a
great way of developing tutorials.

On 9/28/07, Viktor Klang <viktor...@gmail.com> wrote:
>
>

Viktor Klang

unread,
Sep 29, 2007, 3:52:14 PM9/29/07
to lif...@googlegroups.com
On 9/29/07, Daniel Green <octob...@gmail.com> wrote:

We could have a "port web application" a month :-). That would be a
great way of developing tutorials.

And of angst.

;)

Viktor

 

Daniel Green

unread,
Sep 29, 2007, 3:53:59 PM9/29/07
to lif...@googlegroups.com
Perhaps "port a tutorial?" That way they are simple projects and some
of the design is already done for the user

Jorge Ortiz

unread,
Sep 30, 2007, 12:07:17 AM9/30/07
to liftweb
This is a great idea. I'd really enjoy something along the lines of
ruby quiz or the perl quiz of the week.

On Sep 28, 2:54 pm, "Viktor Klang" <viktor.kl...@gmail.com> wrote:
> On 9/28/07, Burak Emir <burak.e...@gmail.com> wrote:

> > > > > On 9/28/07, Viktor Klang <viktor.kl...@gmail.com> wrote:
>
> > > > > > Yo man, I'll code it up, test it out and submit it as a patch.
>
> > > > > > Best regards
> > > > > > Viktor
> > > > > > --
> > > > > > Software Architect
> > > > > > Yves Rocher Nordique
>

> > > > > > On 9/28/07, David Pollak <feeder.of.the.be...@gmail.com > wrote:
>
> > > > > > > Viktor,
>
> > > > > > > You are way more up on how to do Servlets than I am. This looks
> > > > > > > like a great change.
>
> > > > > > > Feel free to either open a ticket on the Google Code project or
> > > > > > > submit a patch (if you submit a patch, it would be ideal if you sign the IP
> > > > > > > Assignment agreement to keep the lift IP clean.)
>
> > > > > > > If I make the change, it'll be a week to 2 weeks before the code
> > > > > > > gets checked in.
>
> > > > > > > Thanks,
>
> > > > > > > David
>

> > > > > > > On 9/28/07, Viktor Klang < viktor.kl...@gmail.com > wrote:
>
> > > > > > > > Greets,
>

> > > > > > > > I was looking through some of the *lift* source and noticed


> > > > > > > > the strategy to use a servlet-mapping and then pipe out existing resources
> > > > > > > > if there are resources matching the request URI.
>
> > > > > > > > Why not use a Filter instead, and use a default-filtermapping?
> > > > > > > > This would remove the somewhat ugly readAndWrite-hack and the
> > > > > > > > arbitrary mime-type lookup.
> > > > > > > > in doFilter (note: pseudo-code)
>
> > > > > > > > if(isRealResource(requestURI))
> > > > > > > > filterChain.doFilter(request,response);
> > > > > > > > else
> > > > > > > > {

> > > > > > > > //do the *lift* stuff


> > > > > > > > }
>
> > > > > > > > Wouldn't this work much better and be cleaner?
>
> > > > > > > > Best regards
> > > > > > > > Viktor Klang
>
> > > > > > > --
> > > > > > > lift, the secure, simple, powerful web framework
> > > > > > >http://liftweb.net
>
> > > > > --
> > > > > Software Architect
> > > > > Yves Rocher Nordique
>
> > > > --
> > > > Burak Emir
> > > > Research Assistant / PhD Candidate
> > > > Programming Methods Group
> > > > EPFL, 1015 Lausanne, Switzerland

> > > >http://lamp.epfl.ch/~emir<http://lamp.epfl.ch/%7Eemir>


>
> > > --
> > > Software Architect
> > > Yves Rocher Nordique
>
> > --
> > Burak Emir
> > Research Assistant / PhD Candidate
> > Programming Methods Group
> > EPFL, 1015 Lausanne, Switzerland

> >http://lamp.epfl.ch/~emir<http://lamp.epfl.ch/%7Eemir>

Michael Campbell

unread,
Sep 30, 2007, 5:23:05 PM9/30/07
to lif...@googlegroups.com

Viktor Klang

unread,
Sep 30, 2007, 5:29:48 PM9/30/07
to lif...@googlegroups.com
Guys, guys, guys, let's not get flooded with alternatives. do we really need a framework for this one? ;)

For all I care we could put it up as a blog and then anyone can comment with an answer...What do you think about that?

Regards
/Viktor

Michael Campbell

unread,
Sep 30, 2007, 5:49:01 PM9/30/07
to lif...@googlegroups.com
On 9/30/07, Viktor Klang <viktor...@gmail.com> wrote:
> Guys, guys, guys, let's not get flooded with alternatives. do we really need
> a framework for this one? ;)

I wasn't suggesting such, just giving more ideas for ideas to solve.

Viktor Klang

unread,
Sep 30, 2007, 6:10:30 PM9/30/07
to lif...@googlegroups.com
On 9/30/07, Michael Campbell <michael....@gmail.com> wrote:


I didn't mean it like that, I was just getting afraid of the long discussion about which medium is best.
The focal point is that we get some good tutorials and examples, and not as I've seen millions of times, good ideas that never become realized because people can't agree on the method ;)

I just want Scala and lift to achieve world domination. :)

Viktor
 

David Pollak

unread,
Sep 30, 2007, 11:21:54 PM9/30/07
to lif...@googlegroups.com
Do I hear anyone volunteering to be the manager/editor of LiftCode, the lift code exchange and maybe quiz?

Viktor Klang

unread,
Oct 1, 2007, 1:59:58 PM10/1/07
to lif...@googlegroups.com
By the way, David, I made an attempt to apply the Filter-code into the existing liftweb.http.Servlet, but Subclipse died on me when trying to get it to cooperate with Europa, and adding the code with just a download of the Servlet.scala-file on the 2.6.0 releace with the BETA3-plugin is killing me here.

I was kinda hoping you'd paste it in, and I'll help out if you need me to.

Best regards
Viktor

David Pollak

unread,
Oct 1, 2007, 2:09:03 PM10/1/07
to lif...@googlegroups.com
Viktor,

I'll put it in tonight during the lift documentation fest.

Thanks!

David

Viktor Klang

unread,
Oct 1, 2007, 2:17:28 PM10/1/07
to lif...@googlegroups.com
Sweet.

I'll hit the sack now, I've only had 3 hours of sleep last night and I've got tons of meetings, code reviews and tests to do tomorrow.

Also, just to warn you guys, I've been tinkering a bit with a good way to organize and plug dynamic content into lift.
I'll tell you guys more about it tomorrow night, (got tickets to the preview of Stardust for tomorrow night ;) )

Viktor

David Pollak

unread,
Oct 4, 2007, 8:14:43 PM10/4/07
to lif...@googlegroups.com
Yo Victor,

lift is a filter now instead of a servlet.  Thanks!

David
--
David Pollak




Viktor Klang

unread,
Oct 5, 2007, 5:08:36 AM10/5/07
to lif...@googlegroups.com
On 10/5/07, David Pollak <d...@athena.com> wrote:
Yo Victor,

 
lift is a filter now instead of a servlet.  Thanks!

 
David
 
David,
 
just glad I could contribute. :)
 
Viktor
Reply all
Reply to author
Forward
0 new messages