Parse_url equivalent in grails / groovy?
Asked Answered
R

2

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

Rahel answered 13/7, 2011 at 21:52 Comment(0)
L
10

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

new URI('http://google.com/?q=URL').query
Latona answered 13/7, 2011 at 22:11 Comment(0)
B
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
Bowrah answered 12/3, 2015 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.