Forum Discussion

alibaba82's avatar
alibaba82
Super Contributor
16 years ago

Groovy Help - Validate URL

hello,
One of my services has a a list or URLs in the response. These URL point to images located on some server.

I want to validate that these URLs are valid and that there is actually an image located at this URL.

Can you help me with some groovy code that some validates that these are valid URL.

thanks

Ali

4 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Ali,

    hmm.. maybe you could try

    try
    {
        def in = new java.net.URL( urlToImage ).openStream()
        in.close()
    }
    catch( Exception e )
    {
        // not available!?
    }


    if you get an exception, that (probably) means that the URL is not available..

    let me know if this works ok!?

    regards,

    /Ole
    eviware.com
  • alibaba82's avatar
    alibaba82
    Super Contributor
    it works just had to change the code to

    def foo = new java.net.URL( urlToImage ).openStream()

    I guess 'in' is some reserved word.

    Thanks for you help

    Ali
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Ali,

    you can use the XmlHolder.getNodeValues( xpath ) method which will return an array of all values matching the xpath. Then use standard groovy iteration of this array for your validations..

    regards!

    /Ole
    eviware.com