Alex Black <[email protected]> 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 [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