Downloading URL or copying Files using Categories in Groovy

Over the weekend I needed to write a script in groovy which first downloaded a file from a remote webserver then, if that file was retrieved & different copy it to a final location.

Now as Groovy is based on Java you could have done this in the usual manner but I wanted to find a groovy way & found this method which is way more elegant.

The way I found to do this is by using a category to extend the << operator. To do this we create a new class at the end of the script and add a method implementing the operator with the appropriate types.

First we want to implement File << Url which will write the content of a url to a file:

    def static leftShift(File file, URL url) {
       url.withInputStream { is->
            file.withOutputStream { os->
                def bs = new BufferedOutputStream( os )
                bs << is
            }
        }
    }

Now we have that we can simply download the remote file with a few lines. Here we set a URL of a remote rss feed, the file we want to download it to then, using our new category we do the actual copy.

def source = new URL( 'http://trainwatch.co.uk/forums/feed.php?mode=topics' )
def target = new File( 'trainwatch.rss' )

use( FileBinaryCategory ) {
  target << source} 

Extending the category to copy local files is just as simple, just overload leftShift with a File as the source:

    def static leftShift(File dst, File src) {
         src.withInputStream {
           is -> dst.withOutputStream {
          os -> def bs = new BufferedOutputStream( os )
          bs << is
        }
      }
    }

Now we can extend our example above to copy the file elsewhere:

def source = new URL( 'http://trainwatch.co.uk/forums/feed.php?mode=topics' )
def temp = new File( '/tmp/trainwatch.rss' )
def target = new File( 'trainwatch.rss' )

use( FileBinaryCategory ) {
  // Download the rss from the remote site to the temp file
  temp << source

  // Imagine we do some tests here & then copy the file
  target << temp
  } 

Here’s the full category class, all I do in standalone scripts is put this at the end:

class FileBinaryCategory {
  def static leftShift(File file, URL url) {
    url.withInputStream { is->
      file.withOutputStream { os->
        def bs = new BufferedOutputStream( os )
        bs << is
      }
    }
  }
  def static leftShift(File dst, File src) {
    src.withInputStream {
      is -> dst.withOutputStream {
        os -> def bs = new BufferedOutputStream( os )
        bs << is
      }
    }
  }
 }

Now this doesn’t just have to be used for file io either. One usecase I had was to take a list of command line arguments to pass on to a command. Simple except if the list contained another list then I had to flatten the two lists before passing them to the command. Now usually you’d flatten them but what if you had a null or even an empty string? In this case this was possible but I had to strip them out first.

So how to do it? well here’s the category:

class ArgumentsCategory {
 def static leftShift( List list, String arg ) {
   if( arg!=null && !arg.empty ) {
     list.add(arg)
   }
 }

 def static leftShift( List list, List args ) {
   args.each{
     leftShift( list, it )
   }
 }

 def static leftShift( List list, File file ) {
    list.add( file.canonicalPath )
 }
}

Now here we’ve defined three extensions to the << operator. For all three the left and side is the list were appending to while the right is either a String, List or File. For a string it simply appends it. For the file it’s the canonical Path thats appended otherwise we run through each element of the list as if it’s the same command.

Now we can just use this in a similar manner as before:

def src = new File( 'a.tiff' )
def dst = new File( 'a.png' )
def parms = [ '-rotate', '-90', '-resize', '200x200' ]

def args = []
use( ArgumentsCategory ) {
  args << src
  args << params
  args << dst
}

The above code would generate a single list of command arguments for the ImageMagick convert command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s