• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use HttpRequestWrapper?

 
Salvador Cecilio
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When would it be appropriate to use a HttpRequestWrapper?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My favorite example is in using a wrapper to trim all of the request parameter values. It's easy for a user to accidently hit the space bar at the end of an entered number. This wrapper saves a lot of frustration!
 
Salvador Cecilio
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm didn't know you could modify parameters.

I'm looking at the API of the HttpRequestWrapper and would use the getParameter() method to access the parameter.

The question I have is how do you modify the parameter values when data has been submitted via querystring or posted.
 
Paul Clapham
Sheriff
Posts: 21000
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just override the getParameter() method like this:
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a couple other methods that retrieve parameters that need to be covered as well, but that's the jist of it. I also like to hold a lazy map of the parameters in my wrapper so they only get trimmed once (and actually never get trimmed if you never ask for any parameters).
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Salvador Cecilio:
Hmmm didn't know you could modify parameters.

I'm looking at the API of the HttpRequestWrapper and would use the getParameter() method to access the parameter.

The question I have is how do you modify the parameter values when data has been submitted via querystring or posted.


As Paul shows, you don't actually change the parameters but by wrapping the request you make your application see the kind of values you want it to see. When the app asks the request for a parameter, it doesn't know that the request is wrapped and giving it a special, new-and-improved version of the original parameter.

Wrapping the request is like gold-plating costume jewelry. You're not changing the original product (only coating it), yet you fool everyone into thinking the product is better than it really is!
 
Salvador Cecilio
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:


As Paul shows, you don't actually change the parameters but by wrapping the request you make your application see the kind of values you want it to see. When the app asks the request for a parameter, it doesn't know that the request is wrapped and giving it a special, new-and-improved version of the original parameter.

Wrapping the request is like gold-plating costume jewelry. You're not changing the original product (only coating it), yet you fool everyone into thinking the product is better than it really is!



Yeah, that makes a lot more sense, and I thought I knew my decorator patterns.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic