On Dec 30, 5:05 pm, Alex Black <[email protected]> wrote:
> I'll try out that solution Russ, thanks.  Although as the solution
> gets a bit more complex, and taking into account Marius's advice, it
> sounds like this might not be the right direction to go in.
>
> Stepping back a moment, conceptually what I'd like to do is: create a
> nice 404.html template (using lift features like head merge and
> surround tags and sitemap), and then tell Lift to show this page to my
> users when it can't find a handler for the url requested.
>
> Conceptually a nice solution might be:
>
> LiftRules.uriNotFound.prepend {
>             case _ => RewriteResponse("404.html")
>         }
>
> Any other opinions on Russ's solution? Is it the right way to go? Or
> is there another way? Or perhaps could we propose an enhancement to
> Lift to handle this well?
>
> To Marius's question about why I (and perhaps others) don't want to
> use a redirect:
>
> It breaks the internet.  The way its meant to work is: a user-agent
> (either browser, or crawler etc) asks a web server for a resource by
> url, the web server should respond with code 404 if it can't find the
> resource.  Your proposal would have it respond with code 301 which
> tells the user-agent that the resource was located but its moved.

While I totally agree that a plain 404 + markup is much more
straightforward,
"breaks internet" are too big words :) .. sending back a
302 or 301 tells the UA "you asked me for a resource that I know I
don't have but I wont tell
you explicitely, instead I want you to go to this location as an
alternative resource". Somehow it's like scratching with the wrong
hand and purely from HTTP protocol perspective this is not quite
straightforward as 404 + template, but I don't necessarily see it as a
so bad thing.

Specifically for 404 (when a template is not found we could do
something like:

1. In LiftRules instead of:

type URINotFoundPF = PartialFunction[(Req, Box[Failure]),
LiftResponse]

use

type URINotFoundPF = PartialFunction[(Req, Box[Failure]), Either[List
[String], LiftResponse]]

so that function can return a template path instead of response.

2. In LiftSession#processRequest instead of applying the normal
request pipeline only if the addressed template is found, we can use
the path obtained from LiftRules.uriNotFound if Lift fails to find the
normal tempalte. Hence apply the normal rendering pipeline to the
template referenced by uriNotFound.

This approach allows your 404 case to be handled by the normal
rendering pipeline without other hacks.

Unless someone thinks this is a bad solution, Alex you could open an
issue and I'll work on it.

Br's,
Marius

Br's,
Marius
>
> LIft is generally very good I've found at making it easy to build good
> internet applications the *right* way.  Examples include: its easy to
> build rest-ful APIs and return the correct response codes, its easy to
> re-write urls to have friendly urls for users, its easy to build XHTML
> compliant pages etc.
>
> This is one of those areas where we (the lift community) should make
> sure that Lift makes it easy to do things the right way.  Unlike say
> Microsoft of the past where they continually broke internet standards
> and made it easy to do things the wrong way!
>
> - Alex
>
> On Dec 30, 4:32 am, Marius <[email protected]> wrote:
>
> > Re init-ing S is a very risky thing to do. You loose all your context
> > information. uriNotFound is called by Lift from several key points.
> > From those key points, Lift uses the S context and updates the
> > function Map. With your example if your template is binding Scala
> > functions, those function will be written to a function map that will
> > not be seen by Lift as you're writing to a different S context.
>
> > I would advice no doing such things or if you do, then expect side
> > effects. In general avoid bypassing Lift's rendering pipeline. Lift
> > does a lot of things some of them internal and missing those you can
> > get into nasty hidden problems.
>
> > For head merge you could use HeadHelper.scala but note that internally
> > Lift doesn't use this ... it uses a more sophisticated head merge ...
> > see LiftMerge.scala. HeadHelper holds the old merge. IMO it should be
> > removed.
>
> > I'm not really sure why you guys don't like redirects, probably to
> > avoid traffic, but IMO this is not very relevant. But regardless, you
> > guys have your own reasons.
>
> > Br's,
> > Marius
>
> > On Dec 30, 9:39 am, Ross Mellgren <[email protected]> wrote:
>
> > > I think the problem with the site map is that the request you have is for 
> > > a page that doesn't exist so it can't build the current-page-relative 
> > > sitemap.
>
> > > Here's a version that works:
>
> > > In Boot:
>
> > >         val notFoundNode = <lift:embed what="404" />
> > >         LiftRules.uriNotFound.prepend {
> > >             case _ => XhtmlTemplateResponse(notFoundNode, 404)
> > >         }
>
> > > And the other part:
>
> > > object XhtmlTemplateResponse extends HeaderStuff {
> > >     def apply(nodeToRender: NodeSeq, statusCode: Int): LiftResponse = {
> > >         val renderedNode = {
> > >             val forceToRoot: LiftRules.RewritePF = { case _ => 
> > > RewriteResponse(Req.parsePath("/"), TreeMap.empty, true) }
> > >             for (req <- S.request; session <- S.session) yield
> > >                 S.init(Req(req, forceToRoot::Nil), session) {
> > >                     session.processSurroundAndInclude("/", nodeToRender)
> > >                 }
> > >         } openOr {
> > >             error("No session")
> > >         }
>
> > >         new XhtmlResponse(Group(renderedNode), Empty, headers, cookies, 
> > > statusCode, false)
> > >     }            
>
> > > }
>
> > > The secret sauce is reiniting S with a new request that has been 
> > > rewritten to seem as if it were a hit to /
>
> > > I'm not sure if this is a good idea, but it seems to work for the site 
> > > map thing.
>
> > > However, I can't find a way to make the head merge work properly. It 
> > > looks like the head merging lies in the code path inside LiftSession that 
> > > is private to Lift. You could rearrange your template to make only one 
> > > head I guess, or wait until someone more wise than I can direct you as 
> > > far as that goes.
>
> > > -Ross
>
> > > On Dec 30, 2009, at 12:32 AM, Alex Black wrote:
>
> > > > I noticed another problem: the head merge is not working in this
> > > > scenario, the resulting page is not valid XHTML because it has two
> > > > head tags :)  So, this makes me think I'm going about things the wrong
> > > > way.
>
> > > > I'm hoping you can suggest the right way, to create a custom 404 page,
> > > > correctly (without a redirect, with 404 status code), and processed by
> > > > the template engine so it uses our default template with sitemap etc.
>
> > > > - Alex
>
> > > > On Dec 30, 12:22 am, Alex Black <[email protected]> wrote:
> > > >> One more detail:
>
> > > >> notFoundNode is defined like this:
>
> > > >>   val notFoundNode = <lift:embed what="404" />
>
> > > >> and 404.html like this:
>
> > > >> <lift:surround with="default" at="content">
> > > >>     <head>
> > > >>         <title>Page Not Found</title>
> > > >>     </head>
> > > >>         <h1>Page Not Found</h1>
> > > >>         <p>We couldn't find the page you were looking for.</p>
> > > >> </lift:surround>
>
> > > >> And my default.html hidden template has a site map tag in it, and the
> > > >> sitemap contains a number of entries.
>
> > > >> On Dec 30, 12:14 am, Alex Black <[email protected]> wrote:
>
> > > >>> I emailed Ross directly and said I did run with -Drun.mode=production.
>
> > > >>> Here's what I'm seeing:
> > > >>> 1. If I run with -Drun.mode=production, and I access a not found url,
> > > >>> say mysite.com/foobar, then, I *do* see the custom 404 page as
> > > >>> expected, but, where I normally see my sitemap I see "No Navigation
> > > >>> Defined".
> > > >>> 2. If I run *without* that production flag, and I access a not found
> > > >>> url, I get a blank white screen that says "The requested page was not
> > > >>> defined in your SiteMap, so access was blocked.  (This message is
> > > >>> displayed in development mode only)".
>
> > > >>> So #2 is expected, #1 is not.
>
> > > >>> Ross, when you emailed me you said you tried to reproduce this, but I
> > > >>> think one difference between your scenario and mine is that on my 404
> > > >>> page I have a sitemap rendered.
>
> > > >>> So instead of what dpp had:
>
> > > >>> LiftRules.uriNotFound.prepend {
> > > >>>       case _ =>  XhtmlResponse((<html> <body>Silly goose</body> </
> > > >>> html>),
> > > >>>                                Empty, List("Content-Type" -> "text/
> > > >>> html;
> > > >>> charset=utf-8"), Nil, 404, S.ieMode)
> > > >>>     }
>
> > > >>> I have:
>
> > > >>>     // Catch 404s
> > > >>>     LiftRules.uriNotFound.prepend {
> > > >>>       case (req, _) => XhtmlTemplateResponse(notFoundNode, 404)
> > > >>>     }
>
> > > >>> with
>
> > > >>> object XhtmlTemplateResponse extends HeaderStuff {
> > > >>>   def apply(nodeToRender: Node, statusCode: Int): LiftResponse = {
> > > >>>     val renderedNode = S.render(nodeToRender,
> > > >>> S.request.get.request).first
> > > >>>     new XhtmlResponse(renderedNode, Empty, headers, cookies,
> > > >>> statusCode, false)
> > > >>>   }
>
> > > >>> }
>
> > > >>> - Alex
>
> > > >>> On Dec 29, 7:49 pm, Ross Mellgren <[email protected]> wrote:
>
> > > >>>> This is the production run mode thing dpp talked about. Try your app 
> > > >>>> with -Drun.mode=production
>
> > > >>>> -Ross
>
> > > >>>> On Dec 29, 2009, at 7:39 PM, Alex Black wrote:
>
> > > >>>>> Thanks for pointing that out, the catch-all sounds dangerous.
>
> > > >>>>> I tried your suggestion, and it works well except I'm encountering 
> > > >>>>> one
> > > >>>>> issue, my sitemap renders as "No Navigation Defined." for urls that
> > > >>>>> are not found.   I'm using 1.1-M8, I launched jetty in production 
> > > >>>>> mode
> > > >>>>> as you indicated, and added the passNotFoundToChain = false.
>
> > > >>>>> Did you expect that - or is the passNotFoundToChain meant to solve 
> > > >>>>> the
> > > >>>>> sitemap issue?
>
> > > >>>>> - Alex
>
> > > >>>>> On Dec 29, 6:10 pm, David Pollak <[email protected]>
> > > >>>>> wrote:
> > > >>>>>> Keep in mind that Lift's behavior for 404s differs between 
> > > >>>>>> development and
> > > >>>>>> production mode in Lift 1.1-xxx
>
> > > >>>>>> To have your app/Lift handle a 404 rather than passing it to your 
> > > >>>>>> web
> > > >>>>>> container, in Boot.scala:
>
> > > >>>>>>     LiftRules.passNotFoundToChain = false
>
> > > >>>>>>     LiftRules.uriNotFound.prepend {
> > > >>>>>>       case _ =>  XhtmlResponse((<html> <body>Silly goose</body> 
> > > >>>>>> </html>),
> > > >>>>>>                                Empty, List("Content-Type" -> 
> > > >>>>>> "text/html;
> > > >>>>>> charset=utf-8"), Nil, 404, S.ieMode)
> > > >>>>>>     }
>
> > > >>>>>> In development mode, Lift will tell you about SiteMap as this was 
> > > >>>>>> a common
> > > >>>>>> confusion point among new developers.  In production mode, Lift 
> > > >>>>>> will use
> > > >>>>>> that code.  To put Lift in production mode:
>
> > > >>>>>> mvn -Drun.mode=production jetty:run
>
> > > >>>>>> I would strongly recommend against a "catch-all" entry in SiteMap 
> > > >>>>>> as it will
> > > >>>>>> expose every page on your site to access.
>
> > > >>>>>> On Tue, Dec 29, 2009 at 11:17 AM, Alex Black <[email protected]> 
> > > >>>>>> wrote:
> > > >>>>>>> Ok, I got this working, with 1.1-M8 using S.render.
>
> > > >>>>>>>  // A node which embeds our 404 template (e.g. 404.html)
> > > >>>>>>>  val notFoundNode =
> > > >>>>>>>    <lift:embed what="404" />
>
> > > >>>>>>>    // Catch 404s
> > > >>>>>>>  
>
> ...
>
> read more »

--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.


Reply via email to