7

Is there a groovy/grails equivalent to PHP's parse_url ( http://php.net/manual/en/function.parse-url.php ) or python's urlparse ( http://docs.python.org/library/urlparse.html ) that turns a URL string into a struct containing host,protocol, querystring, fragment, URI, etc?

I thought it might be in the grails.org/doc/latest/api/org/codehaus/groovy/grails/web/util/WebUtils.html , but didnt see anything. I dont think HTTPBuilder or assorted URLMapping utilities are what I need.

I really just want to pull a map out of the path and queryString and handle the edge cases (array of params /blah/fuzz?foo=bar&foo=baz, fragments /blah/fuzz?foo=bar#baz, encoded URLs for redirects) correctly.

I know I can handle the PATH component via clever use of URLMapping eg: /blah/$code, but im left with decoding the param block...

Thanks

Tak
  • 232
  • 3
  • 10

2 Answers2

10

If I understand correctly, what you really need is plain old URI class:

new URI('http://google.com/?q=URL').query
Artur Nowak
  • 5,254
  • 3
  • 22
  • 32
3

Extending @Artur Nowak answer, maybe you will need some more effort to get what you want. Here is an example:

URI dbUri = new URI('http://google.com/?q=URL')
def username = dbUri?.getUserInfo()?.split(":")?.getAt(0)
def password = dbUri?.getUserInfo()?.split(":")?.getAt(1)
def host = dbUri?.getHost()
def databaseInstance = dbUri?.getPath()
def url = "jdbc:mysql://" + host + databaseInstance
Cléssio Mendes
  • 996
  • 1
  • 9
  • 25