Permalink
Browse files

touge: rename ReSTClient to RestClient.

  • Loading branch information...
1 parent d08e394 commit 046ee791b810f8ce0902d4390216593f1f4d59eb @kgilmer kgilmer committed Jan 1, 2012
@@ -33,7 +33,7 @@
* @author kgilmer
*
*/
-public class ReSTClient {
+public class RestClient {
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
@@ -421,7 +421,7 @@ public void initialize(HttpURLConnection connection) {
/**
* Default constructor.
*/
- public ReSTClient() {
+ public RestClient() {
this.connectionProvider = new DefaultConnectionProvider();
this.connectionInitializers = new ArrayList<ConnectionInitializer>();
this.errorHandler = null;
@@ -430,7 +430,7 @@ public ReSTClient() {
/**
* @param connectionProvider ConnectionProvider
*/
- public ReSTClient(ConnectionProvider connectionProvider) {
+ public RestClient(ConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
this.connectionInitializers = new ArrayList<ConnectionInitializer>();
this.errorHandler = null;
@@ -439,7 +439,7 @@ public ReSTClient(ConnectionProvider connectionProvider) {
/**
* @param initializer ConnectionInitializer
*/
- public ReSTClient(ConnectionInitializer initializer) {
+ public RestClient(ConnectionInitializer initializer) {
this();
connectionInitializers.add(initializer);
}
@@ -449,7 +449,7 @@ public ReSTClient(ConnectionInitializer initializer) {
* @param username
* @param password
*/
- public ReSTClient(String username, String password) {
+ public RestClient(String username, String password) {
this();
connectionInitializers.add(new BasicAuthConnectionInitializer(username, password));
}
@@ -458,7 +458,7 @@ public ReSTClient(String username, String password) {
* @param connectionProvider ConnectionProvider
* @param initializer ConnectionInitializer
*/
- public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer) {
+ public RestClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer) {
this(connectionProvider);
connectionInitializers.add(initializer);
}
@@ -467,7 +467,7 @@ public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer i
* @param initializer ConnectionInitializer
* @param deserializer ResponseDeserializer<T>
*/
- public ReSTClient(ConnectionInitializer initializer, ResponseDeserializer<?> deserializer) {
+ public RestClient(ConnectionInitializer initializer, ResponseDeserializer<?> deserializer) {
this(initializer);
}
@@ -476,7 +476,7 @@ public ReSTClient(ConnectionInitializer initializer, ResponseDeserializer<?> des
* @param initializer ConnectionInitializer
* @param deserializer ResponseDeserializer<T>
*/
- public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer, ResponseDeserializer<?> deserializer) {
+ public RestClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer, ResponseDeserializer<?> deserializer) {
this(connectionProvider, initializer);
}
@@ -486,7 +486,7 @@ public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer i
* @param deserializer ResponseDeserializer<T>
* @param errorHandler ErrorHandler
*/
- public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer,
+ public RestClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer,
ResponseDeserializer<?> deserializer, ErrorHandler errorHandler) {
this.connectionProvider = connectionProvider;
this.connectionInitializers = new ArrayList<ConnectionInitializer>();
@@ -501,7 +501,7 @@ public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer i
* @param errorHandler ErrorHandler
* @param debugStream OutputStream to pass debug messages to. If null, no debug output.
*/
- public ReSTClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer,
+ public RestClient(ConnectionProvider connectionProvider, ConnectionInitializer initializer,
ResponseDeserializer<?> deserializer, ErrorHandler errorHandler, PrintWriter debugStream) {
this.connectionProvider = connectionProvider;
this.connectionInitializers = new ArrayList<ConnectionInitializer>();
@@ -751,7 +751,7 @@ public T getContent() throws IOException {
if (deserializer == null) {
// If no deserializer is specified, use String.
- T response = (T) ReSTClient.STRING_DESERIALIZER.deserialize(connection.getInputStream(), 0, null);
+ T response = (T) RestClient.STRING_DESERIALIZER.deserialize(connection.getInputStream(), 0, null);
done = true;
if (responseBuffer != null) {
@@ -9,8 +9,8 @@
import junit.framework.TestCase;
-import org.touge.restclient.ReSTClient;
-import org.touge.restclient.ReSTClient.Response;
+import org.touge.restclient.RestClient;
+import org.touge.restclient.RestClient.Response;
/**
* Base tests for ReSTClient.
@@ -25,17 +25,17 @@
*/
public void testGET() throws IOException {
String testUrl = "http://kgilmer.github.com/touge/";
- ReSTClient client = new ReSTClient();
+ RestClient client = new RestClient();
//Simplest case
String res = client.callGet(testUrl);
assertNotNull(res);
- assertTrue(res.contains(ReSTClient.class.getSimpleName()));
+ assertTrue(res.contains(RestClient.class.getSimpleName()));
//Slightly more complex, specify String deserializer
- String res2 = client.callGet(testUrl, ReSTClient.STRING_DESERIALIZER).getContent();
+ String res2 = client.callGet(testUrl, RestClient.STRING_DESERIALIZER).getContent();
assertNotNull(res2);
assertTrue(res.equals(res2));
@@ -61,8 +61,8 @@ public void testGET() throws IOException {
assertTrue(res3.equals(res2));*/
//Test readStream
- InputStream is = client.callGet(testUrl, ReSTClient.INPUTSTREAM_DESERIALIZER).getContent();
- String res4 = new String(ReSTClient.readStream(is), "UTF-8");
+ InputStream is = client.callGet(testUrl, RestClient.INPUTSTREAM_DESERIALIZER).getContent();
+ String res4 = new String(RestClient.readStream(is), "UTF-8");
System.out.println("size: " + res2.length() + " " + res4.length());
assertNotNull(res4);
assertTrue(res4.equals(res2));
@@ -77,25 +77,25 @@ public void testGET() throws IOException {
}
public void testBasicAuth() throws IOException {
- ReSTClient client = new ReSTClient(new ReSTClient.BasicAuthConnectionInitializer("username", "password"));
+ RestClient client = new RestClient(new RestClient.BasicAuthConnectionInitializer("username", "password"));
client.callGet("http://basicauthserver.com");
}
public void testErrorHandling() throws IOException {
String testUrl = "http://kgilmer.github.com/touge/";
String testBadUrl = "http://shinyama.info/notfound";
- ReSTClient client = new ReSTClient();
+ RestClient client = new RestClient();
client.setErrorHandler(null);
- Response<String> resp = client.callGet(testUrl, ReSTClient.STRING_DESERIALIZER);
+ Response<String> resp = client.callGet(testUrl, RestClient.STRING_DESERIALIZER);
assertFalse(resp.isError());
//Should not throw exception.
- resp = client.callGet(testBadUrl, ReSTClient.STRING_DESERIALIZER);
+ resp = client.callGet(testBadUrl, RestClient.STRING_DESERIALIZER);
assertTrue(resp.isError());
- client.setErrorHandler(new ReSTClient.ErrorHandler() {
+ client.setErrorHandler(new RestClient.ErrorHandler() {
@Override
public void handleError(int code, String message) throws IOException {
@@ -106,27 +106,27 @@ public void handleError(int code, String message) throws IOException {
boolean exceptionThrown = false;
//good url, should not throw exception
try {
- resp = client.callGet(testUrl, ReSTClient.STRING_DESERIALIZER);
+ resp = client.callGet(testUrl, RestClient.STRING_DESERIALIZER);
} catch (IOException e) {
exceptionThrown = true;
}
assertFalse(exceptionThrown);
//bad url, should now throw exception
try {
- resp = client.callGet(testBadUrl, ReSTClient.STRING_DESERIALIZER);
+ resp = client.callGet(testBadUrl, RestClient.STRING_DESERIALIZER);
resp.getContent();
} catch (IOException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
- private class CustomObjectArrayDeserializer implements ReSTClient.ResponseDeserializer<List<Object>> {
+ private class CustomObjectArrayDeserializer implements RestClient.ResponseDeserializer<List<Object>> {
@Override
public List<Object> deserialize(InputStream input, int responseCode, Map<String, List<String>> headers) throws IOException {
- String s = new String(ReSTClient.readStream(input), "UTF-8");
+ String s = new String(RestClient.readStream(input), "UTF-8");
List<Object> tl = new ArrayList<Object>(Arrays.asList(s.split(" ")));
return tl;
@@ -15,18 +15,18 @@
import java.util.List;
import java.util.Map;
-import org.touge.restclient.ReSTClient;
-import org.touge.restclient.ReSTClient.HttpMethod;
-import org.touge.restclient.ReSTClient.Response;
-import org.touge.restclient.ReSTClient.ResponseDeserializer;
-import org.touge.restclient.ReSTClient.URLBuilder;
+import org.touge.restclient.RestClient;
+import org.touge.restclient.RestClient.HttpMethod;
+import org.touge.restclient.RestClient.Response;
+import org.touge.restclient.RestClient.ResponseDeserializer;
+import org.touge.restclient.RestClient.URLBuilder;
// ### Examples of how to use ReSTClient.
public class ReSTClient_usage {
public static void main(String[] args) throws IOException {
- ReSTClient restClient = new ReSTClient();
+ RestClient restClient = new RestClient();
//The most common simple thing to do is GET and return the body
//as a String. ReSTClient:
@@ -44,7 +44,7 @@ public static void main(String[] args) throws IOException {
//one of the few predefined deserializers.
//Call GET on localhost using long form, pass in a predefined deserializer, no body (since GET), and no custom headers.
Response<String> resp =
- restClient.call(HttpMethod.GET, "localhost", ReSTClient.STRING_DESERIALIZER, null, null);
+ restClient.call(HttpMethod.GET, "localhost", RestClient.STRING_DESERIALIZER, null, null);
pl("Response: " + resp.getContent());
//Call get and provide a custom deserializer into a custom type.
@@ -70,7 +70,7 @@ public Double deserialize(InputStream input, int responseCode, Map<String, List<
pl(resp.getContent());
// Or we can use an error handler instead.
- restClient.setErrorHandler(new ReSTClient.ErrorHandler() {
+ restClient.setErrorHandler(new RestClient.ErrorHandler() {
@Override
public void handleError(int code, String message) throws IOException {
@@ -98,34 +98,34 @@ public void handleError(int code, String message) throws IOException {
// Call GET and pass back the raw response InputStream to the client.
pl(
- restClient.callGet(localhost, ReSTClient.INPUTSTREAM_DESERIALIZER)
+ restClient.callGet(localhost, RestClient.INPUTSTREAM_DESERIALIZER)
.getContent().available());
// Create a rest client that will throw exceptions on all HTTP and I/O errors.
- restClient.setErrorHandler(ReSTClient.THROW_ALL_ERRORS);
+ restClient.setErrorHandler(RestClient.THROW_ALL_ERRORS);
//This GET will deserialize server response as a string and
//throw IOException on any error.
Response<String> rs =
- restClient.callGet(localhost, ReSTClient.STRING_DESERIALIZER);
+ restClient.callGet(localhost, RestClient.STRING_DESERIALIZER);
pl(rs.getContent());
// Subsequent calls to this rest client will not throw exceptions on HTTP errors.
restClient.setErrorHandler(null);
//Since we do not have an error handler, this call will not throw IOException.
- rs = restClient.callGet(localhost.copy("asdf"), ReSTClient.STRING_DESERIALIZER);
+ rs = restClient.callGet(localhost.copy("asdf"), RestClient.STRING_DESERIALIZER);
if (rs.isError())
pl("Error: " + rs.getCode());
//Set the error handler to throw all errors.
- restClient.setErrorHandler(ReSTClient.THROW_ALL_ERRORS);
+ restClient.setErrorHandler(RestClient.THROW_ALL_ERRORS);
//The following line will throw IOException.
try {
- rs = restClient.callGet(localhost.copy("/asdf"), ReSTClient.STRING_DESERIALIZER);
+ rs = restClient.callGet(localhost.copy("/asdf"), RestClient.STRING_DESERIALIZER);
//Error is thrown when trying to get content.
pl(rs.getContent());
} catch (IOException e) {
@@ -141,11 +141,11 @@ public void handleError(int code, String message) throws IOException {
}
// Only throw errors relating to server problems.
- restClient.setErrorHandler(ReSTClient.THROW_5XX_ERRORS);
+ restClient.setErrorHandler(RestClient.THROW_5XX_ERRORS);
//following line will throw IOException
try {
- rs = restClient.callGet("localhost/asdf", ReSTClient.STRING_DESERIALIZER);
+ rs = restClient.callGet("localhost/asdf", RestClient.STRING_DESERIALIZER);
//Error is not thrown when trying to get content because it is not a server error, but rather a 404.
pl("Should be true: " + rs.isError());
@@ -154,10 +154,10 @@ public void handleError(int code, String message) throws IOException {
}
//following line will throw IOException
- restClient.setErrorHandler(ReSTClient.THROW_ALL_ERRORS);
+ restClient.setErrorHandler(RestClient.THROW_ALL_ERRORS);
try {
//Error is thrown when trying to get content.
- String respstr = restClient.callGetContent(localhost.copy("/asdf"), ReSTClient.STRING_DESERIALIZER);
+ String respstr = restClient.callGetContent(localhost.copy("/asdf"), RestClient.STRING_DESERIALIZER);
} catch (IOException e) {
pl("Error: " + e.getMessage());
}
@@ -166,12 +166,12 @@ public void handleError(int code, String message) throws IOException {
Map<String, Object> body = new HashMap<String, Object>();
body.put("tkey", "tval");
- body.put("myfile", new ReSTClient.FormFile("/tmp/boo.txt", "text/plain"));
+ body.put("myfile", new RestClient.FormFile("/tmp/boo.txt", "text/plain"));
restClient.callPostMultipart("localhost", body);
//PUT, short form, throw exception on error
- restClient.setErrorHandler(ReSTClient.THROW_ALL_ERRORS);
+ restClient.setErrorHandler(RestClient.THROW_ALL_ERRORS);
Response<Integer> mr = restClient.callPut("localhost", new ByteArrayInputStream("boo".getBytes()));
//HTTP DELETE
@@ -2,13 +2,13 @@
import junit.framework.TestCase;
-import org.touge.restclient.ReSTClient;
-import org.touge.restclient.ReSTClient.URLBuilder;
+import org.touge.restclient.RestClient;
+import org.touge.restclient.RestClient.URLBuilder;
public class URLBuilderTests extends TestCase {
public void testURLBuilder() {
- ReSTClient restClient = new ReSTClient();
+ RestClient restClient = new RestClient();
//This URLBuilder builds https://citibank.com/secureme/halp
String url = restClient.buildURL("htTPS://citibank.com/secureme/").append("/halp").toString();

0 comments on commit 046ee79

Please sign in to comment.