Re: [Lift] URL Rewriting - 'nested' ParsePath?

2010-02-06 Thread Jeppe Nejsum Madsen
Alex Black a...@alexblack.ca writes:

 Hi, I'd like to rewrite a url like this:

 /manufacturer/product

 e.g.

 LiftRules.statelessRewrite.append( {
   case RewriteRequest(
 ParsePath( manufacturerName :: productName :: Nil, _, _,_), _,
 _)  =

 RewriteResponse(productView :: Nil)
 })

 But, I'd like to the ParsePath case to not 'catch' for non-existant
 manufacturers, and non-existant products by the manufacturer.

 So, I might do this:

 LiftRules.statelessRewrite.append( {
   case RewriteRequest(
 ParsePath( Manufacturer(manufacturer) :: Product(product) ::
 Nil, _, _,_), _, _)  =

 RewriteResponse(productView :: Nil)
 })

 Where Manufacturer and Product objects provide an extractor which does
 a lookup.

 However, the product lookup depends on the manufacturer.. is there a
 way to handle that?

 ideas:
 1. use an if guard

   case RewriteRequest(
 ParsePath( Manufacturer(manufacturer) :: productName :: Nil,
 _, _,_), _, _)  if manufacturer.contains(productName) =
   val product = manufacturer.getProduct(productName)

 sucks because it looks up the product twice.

 2. some time of nested case?

   case RewriteRequest(
 ParsePath( Manufacturer(manufacturer) :: Product(manufacturer,
 product) :: Nil, _, _,_), _, _)  =

 obviously doesn't work..

 any suggestions welcome, thanks!


You could write you own extractor. Here's an example from production
code :-)

 override val rewrite: LocRewrite = Full(NamedPF(name) {
  case RewriteRequest(ParamsExtractor(account,orgUnit) , _, _) = 
(RewriteResponse(path), Full(account, orgUnit))
  })


object ParamsExtractor {
  def unapply(pp:ParsePath): Option[(Account, OrgUnit)] = {
val result:Box[(Account, OrgUnit)] = if (pp.wholePath.startsWith(path) 
 pp.wholePath.length == (path.length + 2)) {
  val res = for {decrypted1 - 
FleetZoneRules.decrypt(pp.wholePath(path.length))
 account - 
urlAccount.is.choice(Full(_))(Account.find(decrypted1))
 decrypted2 - FleetZoneRules.decrypt(pp.wholePath.last)
 orgUnit - 
urlOrgUnit.is.choice(Full(_))(OrgUnit.find(decrypted2)) if (orgUnit.account == 
account)} yield {
   urlAccount(Full(account))
   urlOrgUnit(Full(orgUnit))
   (account, orgUnit)
 }
  log.debug(Decoded URL: %s=%s.format(pp,res))
  res
  }
else
  None
result
  }
}

/Jeppe

-- 
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.



[Lift] URL Rewriting - 'nested' ParsePath?

2010-02-05 Thread Alex Black
Hi, I'd like to rewrite a url like this:

/manufacturer/product

e.g.

LiftRules.statelessRewrite.append( {
  case RewriteRequest(
ParsePath( manufacturerName :: productName :: Nil, _, _,_), _,
_)  =

RewriteResponse(productView :: Nil)
})

But, I'd like to the ParsePath case to not 'catch' for non-existant
manufacturers, and non-existant products by the manufacturer.

So, I might do this:

LiftRules.statelessRewrite.append( {
  case RewriteRequest(
ParsePath( Manufacturer(manufacturer) :: Product(product) ::
Nil, _, _,_), _, _)  =

RewriteResponse(productView :: Nil)
})

Where Manufacturer and Product objects provide an extractor which does
a lookup.

However, the product lookup depends on the manufacturer.. is there a
way to handle that?

ideas:
1. use an if guard

  case RewriteRequest(
ParsePath( Manufacturer(manufacturer) :: productName :: Nil,
_, _,_), _, _)  if manufacturer.contains(productName) =
  val product = manufacturer.getProduct(productName)

sucks because it looks up the product twice.

2. some time of nested case?

  case RewriteRequest(
ParsePath( Manufacturer(manufacturer) :: Product(manufacturer,
product) :: Nil, _, _,_), _, _)  =

obviously doesn't work..

any suggestions welcome, thanks!

- Alex

-- 
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.