play.libs
Class WS

java.lang.Object
  extended by play.PlayPlugin
      extended by play.libs.WS
All Implemented Interfaces:
java.lang.Comparable<PlayPlugin>

public class WS
extends PlayPlugin

Simple HTTP client to make webservices requests.

Get latest BBC World news as a RSS content

    response = WS.GET("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml");
    Document xmldoc = response.getXml();
    // the real pain begins here...
 

Search what Yahoo! thinks of google (starting from the 30th result).

    response = WS.GET("http://search.yahoo.com/search?p=%s&pstart=1&b=%d", "Google killed me", 30 );
    if( response.getStatus() == 200 ) {
       html = response.getString();
    }
 


Nested Class Summary
static class WS.FileParam
           
static class WS.HttpResponse
          An HTTP response wrapper
static class WS.WSRequest
           
 
Field Summary
 
Fields inherited from class play.PlayPlugin
index
 
Constructor Summary
WS()
           
 
Method Summary
static void authenticate(org.apache.commons.httpclient.Credentials credentials, org.apache.commons.httpclient.auth.AuthScope authScope)
           
static void authenticate(java.lang.String username, java.lang.String password)
           
static void authenticate(java.lang.String username, java.lang.String password, java.lang.String url)
          define client authentication for a server host provided credentials will be used during the request
static WS.HttpResponse DELETE(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String url)
          Deprecated. Use WS.url().headers().delete() instead.
static WS.HttpResponse DELETE(java.lang.String url)
          Deprecated. Use WS.url().delete() instead.
static java.lang.String encode(java.lang.String part)
          URL-encode an UTF-8 string to be used as a query string parameter.
static WS.HttpResponse GET(java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url(url).params().header().get() instead.
static WS.HttpResponse GET(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> params)
          Deprecated. Use WS.url(url).params(params).get() instead.
static WS.HttpResponse GET(java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url(url, params...).get() instead.
static WS.HttpResponse HEAD(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().headers().params().head() instead.
static WS.HttpResponse HEAD(java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().head() instead.
 void invocationFinally()
          Called at the end of the invocation.
static WS.HttpResponse OPTIONS(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().headers().params().options() instead.
static WS.HttpResponse OPTIONS(java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().options() instead.
static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.String url, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Deprecated. Use WS.url(url).params().header().post() instead.
static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.Object> headers, java.lang.String url, java.lang.String body)
          Deprecated. Use WS.url().headers().body().post() instead
static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String url, java.io.File body)
          Deprecated. Use WS.url(url).header().file().post() instead.
static WS.HttpResponse POST(java.lang.String url, java.io.File body)
          Deprecated. Use WS.url(url).file().post() instead.
static WS.HttpResponse POST(java.lang.String url, java.io.File body, java.lang.String mimeType)
          Deprecated. Use WS.url(url).file().mimetype().post() instead.
static WS.HttpResponse POST(java.lang.String url, java.util.Map<java.lang.String,java.lang.Object> parameters)
          Deprecated. Use WS.url(url).params().post() instead.
static WS.HttpResponse POST(java.lang.String url, java.lang.String body, java.lang.String mimeType)
          Deprecated. Use WS.url(url).params().body().mimetype().post() instead.
static WS.HttpResponse TRACE(java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().headers().params().delete() instead.
static WS.HttpResponse TRACE(java.lang.String url, java.lang.Object... params)
          Deprecated. Use WS.url().trace() instead.
static WS.WSRequest url(java.lang.String url)
          Build a WebService Request with the given URL.
static WS.WSRequest url(java.lang.String url, java.lang.String... params)
          Build a WebService Request with the given URL.
 
Methods inherited from class play.PlayPlugin
afterActionInvocation, afterApplicationStart, afterInvocation, beforeActionInvocation, beforeInvocation, bind, compareTo, compileAll, detectChange, enhance, getStatus, onActionInvocationResult, onApplicationStart, onApplicationStop, onConfigurationRead, onEvent, onInvocationException, onLoad, onRoutesLoaded, postEvent, rawInvocation, routeRequest, serveStatic
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WS

public WS()
Method Detail

invocationFinally

public void invocationFinally()
Description copied from class: PlayPlugin
Called at the end of the invocation. (even if an exception occured). Time to close request specific things.

Overrides:
invocationFinally in class PlayPlugin

authenticate

public static void authenticate(java.lang.String username,
                                java.lang.String password,
                                java.lang.String url)
define client authentication for a server host provided credentials will be used during the request

Parameters:
username -
password -
url - hostname url or null to authenticate on any hosts

authenticate

public static void authenticate(java.lang.String username,
                                java.lang.String password)

authenticate

public static void authenticate(org.apache.commons.httpclient.Credentials credentials,
                                org.apache.commons.httpclient.auth.AuthScope authScope)

GET

public static WS.HttpResponse GET(java.lang.String url,
                                  java.lang.Object... params)
Deprecated. Use WS.url(url, params...).get() instead.

Make a GET request using an url template. If you provide no parameters, the url is left unchanged (no replacements)

Parameters:
url - an URL template with placeholders for parameters
params - a variable list of parameters to be replaced in the template using the String.format(...) syntax
Returns:
server response WS.HttpResponse

GET

public static WS.HttpResponse GET(java.lang.String url,
                                  java.util.Map<java.lang.String,java.lang.String> params)
Deprecated. Use WS.url(url).params(params).get() instead.

Make a GET request using an url template. If you provide no parameters, the url is left unchanged (no replacements)

Parameters:
url - an URL template with placeholders for parameters
params - a variable Map
Returns:
server response WS.HttpResponse

GET

public static WS.HttpResponse GET(java.util.Map<java.lang.String,java.lang.Object> headers,
                                  java.lang.String url,
                                  java.lang.Object... params)
Deprecated. Use WS.url(url).params().header().get() instead.

Make a GET request using an url template. If you provide no parameters, the url is left unchanged (no replacements)

Parameters:
headers - a map of headers to be appended to the request.
url - an template with %s,%d placeholders for parameters.
params - parameters to be replaced in the url template using the String.format(...) syntax
Returns:
HTTP response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.lang.String url,
                                   java.io.File body)
Deprecated. Use WS.url(url).file().post() instead.

Make a POST request.

Parameters:
url - resource URL
body - content to be posted. content-type is guessed from the filename extension.
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.lang.String url,
                                   java.io.File body,
                                   java.lang.String mimeType)
Deprecated. Use WS.url(url).file().mimetype().post() instead.

Make a POST request

Parameters:
url - resource URL
body - content to be posted
mimeType - the content type as a mimetype
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.String> headers,
                                   java.lang.String url,
                                   java.io.File body)
Deprecated. Use WS.url(url).header().file().post() instead.

Make a POST request

Parameters:
headers - a map of headers to be appended to the request.
url - resource URL
body - content to be posted. content-type is guessed from the filename extension.
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.lang.String url,
                                   java.util.Map<java.lang.String,java.lang.Object> parameters)
Deprecated. Use WS.url(url).params().post() instead.

Make a POST request

Parameters are encoded using a application/x-www-form-urlencoded scheme. You cannot send multipart/form-data for now.

Parameters:
url - the request URL
parameters - the parameters to be posted
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.Object> headers,
                                   java.lang.String url,
                                   java.util.Map<java.lang.String,java.lang.Object> parameters)
Deprecated. Use WS.url(url).params().header().post() instead.

Make a POST request

Parameters are encoded using a application/x-www-form-urlencoded scheme. You cannot send multipart/form-data for now.

Parameters:
headers - the request HTTP headers
url - the request URL
parameters - the parameters to be posted
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.lang.String url,
                                   java.lang.String body,
                                   java.lang.String mimeType)
Deprecated. Use WS.url(url).params().body().mimetype().post() instead.

POST some text data

Parameters:
url - resource URL
body - text content to be posted "as is", with not encoding
mimeType - the request content-type
Returns:
server response WS.HttpResponse

POST

public static WS.HttpResponse POST(java.util.Map<java.lang.String,java.lang.Object> headers,
                                   java.lang.String url,
                                   java.lang.String body)
Deprecated. Use WS.url().headers().body().post() instead

POST some text data

Parameters:
headers - extra request headers
url - resource URL
body - text content to be posted "as is"
Returns:
server response WS.HttpResponse

DELETE

public static WS.HttpResponse DELETE(java.lang.String url)
Deprecated. Use WS.url().delete() instead.

Make a DELETE request

Parameters:
url - resource URL
Returns:
server response WS.HttpResponse

DELETE

public static WS.HttpResponse DELETE(java.util.Map<java.lang.String,java.lang.String> headers,
                                     java.lang.String url)
Deprecated. Use WS.url().headers().delete() instead.

Make a DELETE request

Parameters:
headers - extra request headers
url - resource URL
Returns:
server response WS.HttpResponse

HEAD

public static WS.HttpResponse HEAD(java.lang.String url,
                                   java.lang.Object... params)
Deprecated. Use WS.url().head() instead.

Make a HEAD request

Parameters:
url - resource URL
params - query parameters
Returns:
server response WS.HttpResponse

HEAD

public static WS.HttpResponse HEAD(java.util.Map<java.lang.String,java.lang.String> headers,
                                   java.lang.String url,
                                   java.lang.Object... params)
Deprecated. Use WS.url().headers().params().head() instead.

Make a HEAD request

Parameters:
headers - extra request headers
url - resource URL
params - query parameters
Returns:
server response WS.HttpResponse

TRACE

public static WS.HttpResponse TRACE(java.lang.String url,
                                    java.lang.Object... params)
Deprecated. Use WS.url().trace() instead.

Make a TRACE request

Parameters:
url - resource URL
params - query parameters
Returns:
server response WS.HttpResponse

TRACE

public static WS.HttpResponse TRACE(java.util.Map<java.lang.String,java.lang.String> headers,
                                    java.lang.String url,
                                    java.lang.Object... params)
Deprecated. Use WS.url().headers().params().delete() instead.

Make a TRACE request

Parameters:
headers - extra request headers
url - resource URL
params - query parameters
Returns:
the response

OPTIONS

public static WS.HttpResponse OPTIONS(java.lang.String url,
                                      java.lang.Object... params)
Deprecated. Use WS.url().options() instead.

Make a OPTIONS request

Parameters:
url - resource URL
params - query parameters
Returns:
the OPTIONS response

OPTIONS

public static WS.HttpResponse OPTIONS(java.util.Map<java.lang.String,java.lang.String> headers,
                                      java.lang.String url,
                                      java.lang.Object... params)
Deprecated. Use WS.url().headers().params().options() instead.

Make a OPTIONS request

Parameters:
headers - extra request headers
url - resource URL
params - query parameters
Returns:
the OPTIONS response

encode

public static java.lang.String encode(java.lang.String part)
URL-encode an UTF-8 string to be used as a query string parameter.

Parameters:
part - string to encode
Returns:
url-encoded string

url

public static WS.WSRequest url(java.lang.String url)
Build a WebService Request with the given URL. This object support chaining style programming for adding params, file, headers to requests.

Parameters:
url - of the request
Returns:
a WSRequest on which you can add params, file headers using a chaining style programming.

url

public static WS.WSRequest url(java.lang.String url,
                               java.lang.String... params)
Build a WebService Request with the given URL. This constructor will format url using params passed in arguments. This object support chaining style programming for adding params, file, headers to requests.

Parameters:
url - to format using the given params.
params - the params passed to format the URL.
Returns:
a WSRequest on which you can add params, file headers using a chaining style programming.


Guillaume Bort & zenexity - Distributed under Apache 2 licence, without any warrantly