tags:

views:

137

answers:

1

Hi all,
How can we check whether urls are working or not in groovy?
when we click a button, i will get all the urls from existing db from 'urls' table and need to check which url is working
Ex: http://baldwinfilter.com/products/start.html - not working
http://www.subaru.com/ - working
and so many urls from db.

My aim is to get all urls and check which one is working and which is not .
do we need to check on the status it returns ??
Can any one help me giving idea ...

thanks in advance sri...

+1  A: 

You could use HttpBuilder like so:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.HTTPBuilder

def urls = [
  "http://baldwinfilter.com/products/start.html",
  "http://www.subaru.com/" 
]

def up = urls.collect { url ->
  try {
    new HTTPBuilder( url ).get( path:'' ) { response ->
      response.statusLine.statusCode == 200
    }
  }
  catch( e ) { false }
}
println up
tim_yates
Hi tim yates,I was getting erors with first line says "unexpected token:import @line 2, column 1. i tried removing first line and deployed but it shows "org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: groovyx.net.http.HTTPBuilder".Can you please tell me to include any jars ??
srinath
Sounds like the Grab issue in Groovy version 1.6. Can you try with 1.7.1? Looking up the download URLs as I type this, but the site's down :-(
tim_yates
Oh god, checked in ubuntu synaptic shows version groovy 1.5.7.1, I should update to latest version 1.7.1 ?
srinath
1.5 is kinda old. Incase you need it though, the http-builder can be downloaded form here: http://repository.codehaus.org/org/codehaus/groovy/modules/http-builder/http-builder/
tim_yates
thanks tim, i will try with this, if not anyways will upgrade to 1.7.1 .so that Grab will come default with 1.7.1. I will be in touch with you .
srinath
Still i was getting same issue with 1.7.1 .srinath@srinath-laptop:~$ groovy -vGroovy Version: 1.7.1 JVM: 1.6.0_17when added - @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )netbeans giving "unexpected token:import @line 2, column 1"Do i need to add any external jars ??I just upgraded to 1.7.1 and added lines on top of file . nothing did next, immediately shows error warnings .Please help me tim .thanks in advance
srinath
How are you trying to run this? Does it work from the command line?
tim_yates
yes,this is working from command line , i created a file "test.groovy" and pasted your code and it works when run "groovy test". But not working from Netbeans, shows error message when include "@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' ) . Do we need to import or include any thing ?
srinath
What version of Groovy is NetBeans using? I have NetBeans 6.8 and it seems to be set to use 1.6.4
tim_yates
Groovy 1.6.4.Netbeans 6.8. It showed under Tools -> Libraries.hmmm but showing srinath@srinath-laptop:~$ groovy -vGroovy Version: 1.7.1 JVM: 1.6.0_17 from terminal
srinath
Yeah, NetBeans uses groovy-all that is bundled with NetBeans. Not sure if you can just change this to point to groovy-all in a more recent download of groovy or not (Not sure if it would break the grails integration -- it shouldn't do I guess as grails has it's own groovy-all inside grails/lib). I am rapidly coming to the conclusion that NetBeans support for Groovy is falling behind, and it might be time to investigate IntelliJ idea (the free version does groovy, but you have to pay for the grails support)
tim_yates
So then it should work from outside Netbeans. right?. I will try to find a work around meanwhile.Thank you tim for giving valuable comments.
srinath
Yeah, it should work outside of netbeans so long as you either have a recent enough version of Groovy to use the @Grab annotation, or you download the http-builder and all of it's dependencies from the link in the 4th comment on this thread, and include them in your classpath :-) Good luck!
tim_yates
Hi Tim, sorry to interrupt you again, downloaded latest groovy-all-1.7.1.jar and added in tools -> libraries -> Groovy-1.6.4 and removed previous jar groovy-all.jar, but still the issue persists . I'm unable to understand why this is working from command line for .groovy file and not working for IDE, also tried creating app from outside but no luck. Could you please give me suggestion for working under Netbeans , I'm running behind schedule these days with this .I can send you screenshots what i did. Thanks in advance
srinath
As I said, I don't know if netbeans supports Groovy 1.7. Have you tried the free version of IntelliJ IDEA? That supports it
tim_yates
Or, as I also said, you could try downloading the JARs for Http-Builder and just add them to your classpath (and get rid of the Grab line)
tim_yates
I installed IntelliJ commercial 30 days trial package and the same issue repeats here too . I have grails-1.1.2/lib/groovy-all-1.6.3.jar . Is it the problem with this ??
srinath