Browse files

Ability to parse the JSONResource into a JSONArray

- revert to Maven. Gradle came in through a pull request. Not intending to move to gradle just yet
- fix for open input streams
- support for arrays being returned as JSON
- support for security realms (Resty.authenticateForRealm()) in case the regular authentication is not working because java is not able to determine the URL
- deprecated alwaysSend in favor of withHeader
- Resty will not set a CookieHandler if one is already set.
  • Loading branch information...
1 parent cfb6175 commit 524179b5c6d4525a5f6553bd874fc73b0b0b4eb1 @RobertFischer RobertFischer committed with Apr 19, 2012
View
3 .buildpath
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<buildpath>
+</buildpath>
View
2 .gitignore
@@ -4,3 +4,5 @@ target/*
*.iws
.classpath
.project
+bin/*
+*~
View
8 CHANGES.rst
@@ -1,10 +1,13 @@
Changes
-------
-In Development:
+New in 0.3.2:
+- fix for open input streams
+- support for arrays being returned as JSON
- support for security realms (Resty.authenticateForRealm()) in case the regular authentication is not working because java is not able to determine the URL
-
+- deprecated alwaysSend in favor of withHeader
+- Resty will not set a CookieHandler if one is already set.
Since 0.3.0:
@@ -20,3 +23,4 @@ Since 0.3.0:
Since 0.2.0:
- Support for PUT, DELETE, Support for application/multipart-formdata
+
View
20 README.rst
@@ -46,27 +46,14 @@ Features
- Complex path queries for JSON (simple tests on fields with operators >,=,< and full boolean expressions (&&,||,!))
- Support for XPath expressions
- Authentication with login/passwd
-- Automatic Cookie management
+- Automatic Cookie management: IMPORTANT: Currently Resty will store cookies in a single instance of CookieHandler. System-wide!
- Full support for multipart/form-data
- GAE compatible (no cookie support though)
Changes
-------
-Since 0.2.0:
-
-- Support for PUT, DELETE, Support for application/multipart-formdata
-
-Since 0.3.0:
-
-- Option to ignore SSL certificate errors: Resty.ignoreAllCerts (global switch for now)
-- New constructor to specify options: new Resty(Option.timeout(3000)); (sets the socket connect timeout)
-- Create your own Options (see Resty.Option.Timeout or Resty.Option.Proxy for example)
-- Fixed scala naming issue
-- enhanced syntax for JSON queries
-- bugfixes from my contributors. Thank you!
-- Proxy support. Thank you, Gabriel. r.setProxy(...) for object r or new Resty(Option.proxy(...)) to carry proxy settings over when traversing paths
-- convenient location header: new Resty().bytes(url, put(someContent)).location(); // gets Location header as URI
+see CHANGES.rst
Status
-------
@@ -83,7 +70,7 @@ Either create the JAR yourself (see target directory or grab the rest-*.jar file
Or grab it from Maven central::
groupId: us.monoid.web
artifactId: resty
- version: 0.2.0
+ version: 0.3.2
Compile it yourself
-------------------
@@ -155,6 +142,7 @@ Contributors
============
Gabriel Falkenberg <gabriel.falkenberg@gmail.com>
Remi Alvergnat <remi.alvergnat@gmail.com>
+Robert Fischer <robert.fischer@smokejumperit.com>
View
2 src/main/java/us/monoid/web/AbstractContent.java
@@ -12,7 +12,7 @@
/**
* Abstract base class of the content being sent to a server.
- *
+ * Check Resty.content(...) methods to create objects
* @author beders
*
*/
View
35 src/main/java/us/monoid/web/AbstractResource.java
@@ -10,7 +10,7 @@
* Abstract base class for all resource handlers you want to use with Resty.
*
* It gives access to the underlying URLConnection and the current inputStream
- *
+ *
* @author beders
*
*/
@@ -23,35 +23,33 @@ public AbstractResource(Option... options) {
}
abstract String getAcceptedTypes();
-
+
void fill(URLConnection anUrlConnection) throws IOException {
urlConnection = anUrlConnection;
- try{
+ try {
inputStream = anUrlConnection.getInputStream();
- } catch (IOException e){
+ } catch (IOException e) {
// Per http://docs.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html
// (comparable documentation exists for later java versions)
// if an HttpURLConnection was used clear the errorStream and close it
// so that keep alive can keep doing its work
- if(anUrlConnection instanceof HttpURLConnection){
- HttpURLConnection conn = (HttpURLConnection)anUrlConnection;
+ if (anUrlConnection instanceof HttpURLConnection) {
+ HttpURLConnection conn = (HttpURLConnection) anUrlConnection;
InputStream es = new BufferedInputStream(conn.getErrorStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
// read the response body
- int read;
- while ((read = es.read()) != -1) {
- baos.write(read);
+ byte[] buf = new byte[1024];
+ int read = -1;
+ while ((read = es.read(buf)) > 0) {
+ baos.write(buf, 0, read);
}
// close the errorstream
es.close();
- throw new IOException(
- "Error while reading from " + conn.getRequestMethod() + ": [" + conn.getResponseCode() + "] " +
- conn.getResponseMessage() + "\n" + new String(baos.toByteArray(), "UTF-8"), e
- );
+ throw new IOException("Error while reading from " + conn.getRequestMethod() + ": [" + conn.getResponseCode() + "] "
+ + conn.getResponseMessage() + "\n" + new String(baos.toByteArray(), "UTF-8"), e);
} else {
throw e;
}
@@ -63,13 +61,13 @@ public URLConnection getUrlConnection() {
}
public HttpURLConnection http() {
- return (HttpURLConnection)urlConnection;
+ return (HttpURLConnection) urlConnection;
}
public InputStream stream() {
return inputStream;
}
-
+
/**
* Check if the URLConnection has returned the specified responseCode
*
@@ -89,13 +87,14 @@ public boolean status(int responseCode) {
return false;
}
- /** Get the location header as URI. Returns null if there is no location header.
+ /**
+ * Get the location header as URI. Returns null if there is no location header.
*
*/
public URI location() {
String loc = http().getHeaderField("Location");
if (loc != null) {
- return URI.create(loc);
+ return URI.create(loc);
}
return null;
}
View
2 src/main/java/us/monoid/web/Content.java
@@ -8,7 +8,7 @@
import java.net.URLConnection;
/**
- * Class to encapsulate content being sent as payload of a POST request.
+ * Class to encapsulate content being sent as payload of a POST or PUT request.
* You can use Resty.content(...) to create content objects.
*
*
View
3 src/main/java/us/monoid/web/FormData.java
@@ -7,9 +7,6 @@
import java.io.OutputStream;
import java.net.URLConnection;
-import us.monoid.util.EncoderUtil;
-import us.monoid.util.EncoderUtil.Usage;
-
/**
* Wrapper for content to be sent as form-data.
* It adds a specific content-disposition header.
View
4 src/main/java/us/monoid/web/JSONPathQuery.java
@@ -2,6 +2,7 @@
import java.io.IOException;
import java.io.StringReader;
+import java.util.logging.Logger;
import us.monoid.json.JSONException;
import us.monoid.json.JSONObject;
@@ -28,6 +29,7 @@
public class JSONPathQuery extends PathQuery<JSONResource, Object> {
private JSONPathCompiler compiler;
private String expr;
+ static private Logger log = Logger.getLogger(JSONPathQuery.class.getName());
public JSONPathQuery(String anExpression) {
expr = anExpression;
@@ -36,7 +38,7 @@ public JSONPathQuery(String anExpression) {
@Override
Object eval(JSONResource resource) throws JSONException, ParseException, IOException {
JSONObject json = resource.object();
- System.out.println("GOT:" + json);
+ log.fine("JSON Received:" + json);
Object result = getCompiler().json().eval(json);
return result;
}
View
30 src/main/java/us/monoid/web/Resty.java
@@ -16,6 +16,7 @@
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
+import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
@@ -65,6 +66,9 @@
* </code>
* </pre>
*
+ * 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.
@@ -82,12 +86,10 @@
protected static String DEFAULT_USER_AGENT = "Resty/0.1 (Java)";
static RestyAuthenticator rath = new RestyAuthenticator();
static {
- // set up content handlers. note: this is not ideal as it might conflict
- // with existing content factories
- // got rid of it: System.setProperty("java.content.handler.pkgs",
- // "us.monoid.web.content.handler");
try {
- CookieHandler.setDefault(new CookieManager());
+ if (CookieHandler.getDefault() == null) {
+ CookieHandler.setDefault(new CookieManager()); // TODO: get rid of this for 0.4 release
+ }
} catch (NoClassDefFoundError oops) {
System.err.println("No CookieHandler. Running on GAE? Fine. No cookie support for you!");
}
@@ -101,7 +103,7 @@
/**
* Create an instance of Resty with the following list of options.
- * Also, options are carried over to resources created by calls to json/text/binary etc.
+ * <b>Also, options are carried over to resources created by calls to json/text/binary etc.</b>
* Use {@link #setOptions(Option...)} to change options afterwards.
*
*/
@@ -502,7 +504,7 @@ public static Content content(String somePlainText) {
public static Content content(byte[] bytes) {
return new Content("application/octet-stream", bytes);
}
-
+
/**
* Create form content as application/x-www-form-urlencoded (i.e. a=b&c=d&...)
*
@@ -586,6 +588,7 @@ public static AbstractContent delete() {
* Tell Resty to send the specified header with each request done on this instance. These headers will also be sent from any resource object returned by this instance. I.e. chained calls will carry
* over the headers r.json(url).json(get("some.path.to.a.url")); Multiple headers of the same type are not supported (yet).
*
+ * @deprecated
* @param aHeader
* the header to send
* @param aValue
@@ -596,6 +599,19 @@ public void alwaysSend(String aHeader, String aValue) {
}
/**
+ * Tell Resty to send the specified header with each request done on this instance. These headers will also be sent from any resource object returned by this instance. I.e. chained calls will carry
+ * over the headers r.json(url).json(get("some.path.to.a.url")); Multiple headers of the same type are not supported (yet).
+ *
+ * @param aHeader
+ * the header to send
+ * @param aValue
+ * the value
+ */
+ public void withHeader(String aHeader, String aValue) {
+ getAdditionalHeaders().put(aHeader, aValue);
+ }
+
+ /**
* Don't send a header that was formely added in the alwaysSend method.
*
* @param aHeader

0 comments on commit 524179b

Please sign in to comment.