|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectus.monoid.web.Resty
public class Resty
Main class. Use me! Use me! Resty is a small, convenient interface to talk to RESTful services. Basic usage is very simple: Create a Resty instance, use authenticate methode to add credentials (optional), then call one of the content type specific methods, like json(...), xml(...), text(...) or bytes(...). The idea is that the method name will convey the expected content type you can then operate on. Most static methods help you build content objects or queries with a compact syntax. Static methods like put(...) and delete() are used to implement the respective HTTP methods. A neat trick to save you typing is to use
import static us.monoid.web.Resty.*;
GETting an URL (as JSON):
new Resty().json(url);
POSTing to an URL (using multipart/form-data) and expecting JSON back:
new Resty().json(url, form(data("name", "Don Draper"), data("occupation", "Ad Man")));
PUTting content and expecting JSON back:
new Resty().json(url, put(content(someJSON)));
DELETE a resource via URL expecting JSON back:
new Resty().json(url, delete());
Here is an example on how to use the geonames web service. It retrieves the json object (see json.org for details) and gets the name of a place from the zip code:
Resty r = new Resty();
Object name = r.json("http://ws.geonames.org/postalCodeLookupJSON?postalcode=66780&country=DE").get("postalcodes[0].placeName");
assertEquals(name, "Rehlingen-Siersburg");
The return value is a resource with the data you requested AND a new Resty instance with the same set of options you initialized Resty with.
Resty supports complex path queries to navigate into a json object. This is mainly used for extracting URIs to surf along a series of REST resources for web services following the HATEOS paradigm.
Resty objects are not re-entrant.
You can also specify options when creating a Resty instance. Well, currently there is one option to set the timeout for connections.
But you can also create your own options! See Resty.Option for more info.
Nested Class Summary | |
---|---|
static class |
Resty.Option
Base class for Resty options. |
static class |
Resty.Proxy
Option to set the proxy for a Resty instance. |
static class |
Resty.Timeout
Option to set a timeout. |
Constructor Summary | |
---|---|
Resty(Resty.Option... someOptions)
Create an instance of Resty with the following list of options. |
Method Summary | |
---|---|
void |
alwaysSend(String aHeader,
String aValue)
Deprecated. |
void |
authenticate(String string,
String aLogin,
char[] charArray)
|
void |
authenticate(URI aSite,
String aLogin,
char[] aPwd)
Register this root URI for authentication. |
void |
authenticateForRealm(String realm,
String aLogin,
char[] charArray)
Register a login password for the realm returned by the authorization challenge. |
BinaryResource |
bytes(String anUri)
Get the resource specified by the uri and return a binary resource for it. |
BinaryResource |
bytes(String anUri,
AbstractContent someContent)
POST to the URI and get the resource as binary resource. |
BinaryResource |
bytes(URI anUri)
Get the resource specified by the uri and return a binary resource for it. |
BinaryResource |
bytes(URI anUri,
AbstractContent someContent)
POST to the URI and get the resource as binary resource. |
static Content |
content(byte[] bytes)
Create a content object from a byte array. |
static Content |
content(JSONObject someJson)
Create a content object from JSON. |
static Content |
content(String somePlainText)
Create a content object from plain text. |
static FormData |
data(String name,
AbstractContent content)
Create a form data entry for a multipart form with any kind of content type. |
static FormData |
data(String name,
String plainTextValue)
Create a plain/text form data entry for a multipart form. |
static AbstractContent |
delete()
Tell Resty to delete the URL content on the server, resulting in a DELETE. |
void |
dontSend(String aHeader)
Don't send a header that was formely added in the alwaysSend method. |
static String |
enc(String unencodedString)
Shortcut to URLEncoder.encode with UTF-8. |
static us.monoid.web.mime.MultipartContent |
form(FormData... formData)
Create form content to be sent as multipart/form-data. |
static FormContent |
form(String query)
Create form content as application/x-www-form-urlencoded (i.e. |
Resty |
identifyAsMozilla()
Sets the User-Agent to identify as Mozilla/Firefox. |
Resty |
identifyAsResty()
Sets the User-Agent to Resty. |
static void |
ignoreAllCerts()
Install a SSL socket factory which will trust all certificates. |
JSONResource |
json(String string)
GET a URI given by string and parse the result as JSON. |
JSONResource |
json(String anUri,
AbstractContent content)
|
JSONResource |
json(URI anUri)
GET a URI and parse the result as JSON. |
JSONResource |
json(URI anUri,
AbstractContent requestContent)
POST to a URI and parse the result as JSON |
static JSONPathQuery |
path(String string)
Create a JSONPathQuery to extract data from a JSON object. |
static AbstractContent |
put(Content someContent)
Tell Resty to replace the specified content on the server, resulting in a PUT operation instead of a POST operation. |
Resty |
setOptions(Resty.Option... someOptions)
Set options if you missed your opportunity in the c'tor or if you want to change the options. |
void |
setProxy(String proxyhost,
int proxyport)
Set the proxy for this instance of Resty. |
TextResource |
text(String anUri)
Get a plain text resource for the specified URI. |
TextResource |
text(String anUri,
AbstractContent content)
Get a plain text resource for the specified URI by POSTing to it. |
TextResource |
text(URI anUri)
Get a plain text resource for the specified URI. |
TextResource |
text(URI anUri,
AbstractContent content)
Get a plain text resource for the specified URI by POSTing to it. |
void |
withHeader(String aHeader,
String aValue)
Tell Resty to send the specified header with each request done on this instance. |
XMLResource |
xml(String string)
GET a URI given by string and parse the result as XML. |
XMLResource |
xml(String anUri,
AbstractContent content)
|
XMLResource |
xml(URI anUri)
GET a URI and parse the result as XML. |
XMLResource |
xml(URI anUri,
AbstractContent requestContent)
POST to a URI and parse the result as XML |
static XPathQuery |
xpath(String anXPathExpression)
Create an XPathQuery to extract data from an XML document. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Resty(Resty.Option... someOptions)
setOptions(Option...)
to change options afterwards.
Method Detail |
---|
public Resty setOptions(Resty.Option... someOptions)
someOptions
- new set of options
public void authenticate(URI aSite, String aLogin, char[] aPwd)
aSite
- the root URI of the siteaLogin
- the login nameaPwd
- the password. The array will not be internally copied. Whenever you null it, the password is gone within Restypublic void authenticate(String string, String aLogin, char[] charArray)
string
- aLogin
- charArray
- authenticate(URI, String, char[])
public void authenticateForRealm(String realm, String aLogin, char[] charArray)
realm
- the realm (see rfc2617, section 1.2)aLogin
- charArray
- public Resty identifyAsMozilla()
public Resty identifyAsResty()
public JSONResource json(String string) throws IOException
string
- - the string to use as URI
IOException
for more docs
public JSONResource json(URI anUri) throws IOException
anUri
- the URI to request
IOException
public JSONResource json(URI anUri, AbstractContent requestContent) throws IOException
anUri
- the URI to visitrequestContent
- the content to POST to the URI
IOException
- if uri is wrong or no connection could be made or for 10 zillion other reasonspublic JSONResource json(String anUri, AbstractContent content) throws IOException
IOException
Resty#json(URI, Content)
public TextResource text(URI anUri) throws IOException
anUri
- the URI to follow
IOException
- if content type sent is not a plain text, if the connection could not be made and gazillion other reasonspublic TextResource text(URI anUri, AbstractContent content) throws IOException
anUri
- the URI to follow
IOException
- if content type sent is not a plain text, if the connection could not be made and gazillion other reasonspublic TextResource text(String anUri) throws IOException
anUri
- the URI to follow
IOException
- if content type sent is not a plain text, if the connection could not be made and gazillion other reasonspublic TextResource text(String anUri, AbstractContent content) throws IOException
anUri
- the URI to follow
IOException
- if content type sent is not a plain text, if the connection could not be made and gazillion other reasonspublic XMLResource xml(String string) throws IOException
string
- - the string to use as URI
IOException
for more docs
public XMLResource xml(URI anUri) throws IOException
anUri
- the URI to request
IOException
public XMLResource xml(URI anUri, AbstractContent requestContent) throws IOException
anUri
- the URI to visitrequestContent
- the content to POST to the URI
IOException
- if uri is wrong or no connection could be made or for 10 zillion other reasonspublic XMLResource xml(String anUri, AbstractContent content) throws IOException
IOException
Resty#xml(URI, Content)
public BinaryResource bytes(String anUri) throws IOException
anUri
- the uri to follow
IOException
public BinaryResource bytes(URI anUri) throws IOException
uri
- the uri to follow
IOException
public BinaryResource bytes(String anUri, AbstractContent someContent) throws IOException
anUri
- the uri to follow
IOException
public BinaryResource bytes(URI anUri, AbstractContent someContent) throws IOException
uri
- the uri to follow
IOException
public static JSONPathQuery path(String string)
Resty r = new Resty();
r.json(someUrl).json(path("path.to.url.in.json"));
string
-
public static XPathQuery xpath(String anXPathExpression) throws XPathException
anXPathExpression
- an XPath expression with result type String
XPathException
public static Content content(JSONObject someJson)
someJson
- the JSON to use
public static Content content(String somePlainText)
somePlainText
- the plain text to send
public static Content content(byte[] bytes)
bytes
- the bytes to send
public static FormContent form(String query)
query
- the preformatted, properly encoded form data
public static us.monoid.web.mime.MultipartContent form(FormData... formData)
public static FormData data(String name, String plainTextValue)
name
- the name of the control of the formplainTextValue
- the plain text value
public static FormData data(String name, AbstractContent content)
name
- the name of the control or variable in a formcontent
- the content to send
public static String enc(String unencodedString)
unencodedString
- the string to encode
public static AbstractContent put(Content someContent)
public static AbstractContent delete()
public void alwaysSend(String aHeader, String aValue)
aHeader
- the header to sendaValue
- the valuepublic void withHeader(String aHeader, String aValue)
aHeader
- the header to sendaValue
- the valuepublic void dontSend(String aHeader)
aHeader
- the header to removepublic static void ignoreAllCerts()
public void setProxy(String proxyhost, int proxyport)
proxyhost
- name of the hostproxyport
- port to be used
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |