- •Credits
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introduction
- •Installing Groovy on Windows
- •Installing Groovy on Linux and OS X
- •Executing Groovy code from the command line
- •Using Groovy as a command-line text file editor
- •Running Groovy with invokedynamic support
- •Building Groovy from source
- •Managing multiple Groovy installations on Linux
- •Using groovysh to try out Groovy commands
- •Starting groovyConsole to execute Groovy snippets
- •Configuring Groovy in Eclipse
- •Configuring Groovy in IntelliJ IDEA
- •Introduction
- •Using Java classes from Groovy
- •Embedding Groovy into Java
- •Compiling Groovy code
- •Generating documentation for Groovy code
- •Introduction
- •Searching strings with regular expressions
- •Writing less verbose Java Beans with Groovy Beans
- •Inheriting constructors in Groovy classes
- •Defining code as data in Groovy
- •Defining data structures as code in Groovy
- •Implementing multiple inheritance in Groovy
- •Defining type-checking rules for dynamic code
- •Adding automatic logging to Groovy classes
- •Introduction
- •Reading from a file
- •Reading a text file line by line
- •Processing every word in a text file
- •Writing to a file
- •Replacing tabs with spaces in a text file
- •Deleting a file or directory
- •Walking through a directory recursively
- •Searching for files
- •Changing file attributes on Windows
- •Reading data from a ZIP file
- •Reading an Excel file
- •Extracting data from a PDF
- •Introduction
- •Reading XML using XmlSlurper
- •Reading XML using XmlParser
- •Reading XML content with namespaces
- •Searching in XML with GPath
- •Searching in XML with XPath
- •Constructing XML content
- •Modifying XML content
- •Sorting XML nodes
- •Serializing Groovy Beans to XML
- •Introduction
- •Parsing JSON messages with JsonSlurper
- •Constructing JSON messages with JsonBuilder
- •Modifying JSON messages
- •Validating JSON messages
- •Converting JSON message to XML
- •Converting JSON message to Groovy Bean
- •Using JSON to configure your scripts
- •Introduction
- •Creating a database table
- •Connecting to an SQL database
- •Modifying data in an SQL database
- •Calling a stored procedure
- •Reading BLOB/CLOB from a database
- •Building a simple ORM framework
- •Using Groovy to access Redis
- •Using Groovy to access MongoDB
- •Using Groovy to access Apache Cassandra
- •Introduction
- •Downloading content from the Internet
- •Executing an HTTP GET request
- •Executing an HTTP POST request
- •Constructing and modifying complex URLs
- •Issuing a REST request and parsing a response
- •Issuing a SOAP request and parsing a response
- •Consuming RSS and Atom feeds
- •Using basic authentication for web service security
- •Using OAuth for web service security
- •Introduction
- •Querying methods and properties
- •Dynamically extending classes with new methods
- •Overriding methods dynamically
- •Adding performance logging to methods
- •Adding transparent imports to a script
- •DSL for executing commands over SSH
- •DSL for generating reports from logfiles
- •Introduction
- •Processing collections concurrently
- •Downloading files concurrently
- •Splitting a large task into smaller parallel jobs
- •Running tasks in parallel and asynchronously
- •Using actors to build message-based concurrency
- •Using STM to atomically update fields
- •Using dataflow variables for lazy evaluation
- •Index
8
Working with Web Services in Groovy
In this chapter, we will cover:
ff
ff
ff
ff
ff
ff
ff
ff
ff
Downloading content from the Internet Executing an HTTP GET request Executing an HTTP POST request Constructing and modifying complex URLs
Issuing a REST request and parsing a response Issuing a SOAP request and parsing a response Consuming RSS and Atom feeds
Using basic authentication for web service security Using OAuth for web service security
Introduction
The recipes collected in this chapter deal with the so called programmable web.
Today's applications consume data from a multitude of external sources. These sources are often accessed through HTTP and one of the two main methods for exchanging data over the web, SOAP and REST.
www.it-ebooks.info
Working with Web Services in Groovy
The first recipes describe how to execute simple calls to external web services through HTTP
POST and GET request methods, as well as construct complex URLs. The next recipes of the chapter delve into SOAP and REST: how to make a request and parse the result using Groovy. We continue with the RSS and Atom formats and how to consume them. The last recipe covers the basic authentication mechanism and how to interact with the OAuth protocol - the dominant open authorization standard.
Downloading content from the Internet
The title of this recipe may not seem related to web services at a first glance, but since most of the available services are actually based on Hyper Text Transfer Protocol (HTTP) as well as most of the content on the Internet, it is worth starting with getting a basic understanding of simple HTTP operations before diving into the more complex world of web services.
How to do it...
For downloading HTTP-based content, we don't need any special libraries or stratagem. All that is needed are the standard Java classes—File and URL—and their Groovy extensions:
1.We first start with defining our target and source files:
def outputFile = new File('image.png') def baseUrl = 'http://groovy.codehaus.org'
def imagePath = '/images/groovy-logo-medium.png' def url = new URL("${baseUrl}${imagePath}")
2.Then, just in case, the outputFile already exists, we need to delete it to avoid appending content:
outputFile.delete()
3.The last step is to stream the URL's content into the outputFile:
url.withInputStream { inputStream -> outputFile << inputStream
}
How it works...
The withInputStream method is a Groovy extension added to the URL class. We already presented many extension methods that Groovy appends to the standard JDK classes in previous chapters (for example, the Using Java Classes from Groovy recipe in Chapter 2, Using Groovy Ecosystem), and we showed the way to write your own extensions in the Adding a functionality to the existing Java/Groovy classes recipe in Chapter 3, Using Groovy Language Features. More information on extended functionality, which exists in Groovy for the java.
net.URL class, can be found at http://groovy.codehaus.org/groovy-jdk/java/ net/URL.html.
264
www.it-ebooks.info
Chapter 8
Basically, the withInputStream method takes care of flushing and closing the stream automatically, and gives access to the java.io.InputStream instance, which contains the binary data retrieved from a remote resource over the HTTP protocol. More information on manipulating files and input streams can be found in Chapter 4, Working with Files in Groovy.
Under the hood, the URL class performs an HTTP GET request for the resource specified by the URL. A lengthier description on how to construct complex GET requests can be found in the Executing an HTTP GET request recipe.
With this new knowledge about the basics of executing an HTTP GET request described in this recipe, you can use many REST-based APIs in a read-only mode as well as download WSDL and XSD for the SOAP-based web services.
There's more...
If the content to download is textual, we can use a more concise way to fetch it with the help of the getText method available on the URL class:
def outputFile = new File('groovy.html') def baseUrl = 'http://groovy.codehaus.org' def url = new URL(baseUrl)
// Saving textual content. outputFile.text = url.text
See also
ff http://groovy.codehaus.org/groovy-jdk/java/net/URL.html ff Executing an HTTP GET request
ff Chapter 4, Working with Files in Groovy
Executing an HTTP GET request
In the previous recipe, Downloading content from the Internet, we described a simple way of getting binary/textual content from a URL. In this recipe, we will present a method to execute HTTP GET requests with more control over the returned data.
265
www.it-ebooks.info
Working with Web Services in Groovy
How to do it...
As a starting point, we will use again the same URL class encountered in the previous recipe. This time we'll explore the openConnection method of the URL class in order to have more options for understanding the intricacies of the response returned by the remote server:
1.We can make the code which retrieves the remote content more reliable by checking the response code as follows:
def url = new URL('http://groovy.codehaus.org/') def connection = url.openConnection() connection.requestMethod = 'GET'
if (connection.responseCode == 200) { println connection.content.text println connection.contentType println connection.lastModified
connection.headerFields.each { println "> ${it}"}
}else {
println 'An error occurred: ' + connection.responseCode + ' ' + connection.responseMessage
}
2.The code will print something similar to the following output:
<!DOCTYPE html> <html>
...
</html>
text/html; charset=UTF-8 0
>null=[HTTP/1.1 200 OK]
>Date=[Tue, 03 Sep 2013 13:03:13 GMT]
>Content-Length=[39028]
>Connection=[close]
>Content-Type=[text/html; charset=UTF-8]
>Server=[Resin/3.0.14]
266
www.it-ebooks.info
Chapter 8
How it works...
The java.net.URLConnection object, returned by the openConnection method, is convenient for getting hold of the additional response data such as, responseCode,
contentType, or response headers.
As you can notice, we set the requestMethod property on the connection object to GET value; but, in fact, it's not needed, because it is a default value anyway.
responseCode is the Status Code Definition returned from the web server. For instance, a 404 status code, informs the client that the requested resource doesn't exist.
The connection object has the getContent method, which is declared to return java.lang.Object by the JDK API. The mechanism used by JDK to handle specific content types is out of scope of this recipe. However, the getContent method typically returns an implementation of the InputStream interface. That's why it is safe to retrieve the text property, which is made available by Groovy on all classes that implement the InputStream functionality.
There's more...
An HTTP GET request often requires additional parameters needed by the server or remote application. These parameters are propagated inside the URL through a query string:
http://groovy.codehaus.org/Project+Information?print=1
The previous URL passes to the server the following key/value information: print=1, which is used to retrieve the Groovy project information page in printer-friendly format. In Groovy, the plain procedure to add a query string to a URL is just to use a string object. For example:
println ('http://groovy.codehaus.org/' + 'Project+Information?print=1').toURL().text
Query strings can be way lengthier than a single key/value entry. A Map is the perfect data structure for representing the query string data. Let us see how a Map can be converted into a query string:
def baseUrl = 'http://docs.codehaus.org/pages/editpage.action?'
def params = [spaceKey: 'GROOVY',
title: 'Project+Information']
def query = params.collect { k,v -> "$k=${URLEncoder.encode(v)}"
}.join('&')
println "${baseUrl}${query}"
267
www.it-ebooks.info