Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
turbomanage committed Mar 27, 2014
1 parent c6e4642 commit 34c895a
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/Users/drfibonacci/android-sdk-macosx/platforms/android-8/android.jar"/>
<classpathentry kind="lib" path="/Users/david/adt-bundle-mac-x86_64-20130729/sdk/platforms/android-8/android.jar"/>
<classpathentry exported="true" kind="lib" path="lib/servlet-api-2.5.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry exported="true" kind="lib" path="lib/jackson-annotations-2.3.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jackson-core-2.3.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jackson-databind-2.3.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
Binary file added lib/jackson-annotations-2.3.0.jar
Binary file not shown.
Binary file added lib/jackson-core-2.3.2.jar
Binary file not shown.
Binary file added lib/jackson-databind-2.3.2.jar
Binary file not shown.
1 change: 1 addition & 0 deletions src/com/turbomanage/httpclient/AbstractHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class AbstractHttpClient {

public static final String URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8";
public static final String MULTIPART = "multipart/form-data";
public static final String JSON = "application/json;charset=UTF-8";

protected String baseUrl = "";

Expand Down
23 changes: 23 additions & 0 deletions src/com/turbomanage/httpclient/android/AndroidJsonRestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.turbomanage.httpclient.android;

import com.turbomanage.httpclient.AsyncHttpClient;
import com.turbomanage.httpclient.json.DefaultJsonMapper;
import com.turbomanage.httpclient.json.JsonRestClient;
import com.turbomanage.httpclient.json.JsonType;
import com.turbomanage.httpclient.rest.ObjectFactory;

public class AndroidJsonRestClient extends JsonRestClient {

public AndroidJsonRestClient() {
this("");
}

public AndroidJsonRestClient(String baseUrl) {
this(new AndroidHttpClient(baseUrl), new DefaultJsonMapper());
}

public AndroidJsonRestClient(AsyncHttpClient httpClient, ObjectFactory<JsonType> factory) {
super(httpClient, factory);
}

}
46 changes: 46 additions & 0 deletions src/com/turbomanage/httpclient/json/DefaultJsonMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.turbomanage.httpclient.json;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.turbomanage.httpclient.HttpResponse;
import com.turbomanage.httpclient.rest.ObjectFactory;

public class DefaultJsonMapper extends ObjectFactory<JsonType> {

private ObjectMapper om = new ObjectMapper();

@Override
public <T> byte[] toBytes(T obj) {
try {
return om.writeValueAsBytes(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

@Override
public <T> T toObj(byte[] bytes, Class<T> responseType) {
try {
T obj = om.readValue(bytes, responseType);
return obj;
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public JsonResponse wrapResponse(HttpResponse res) {
return new JsonResponse(res, this);
}

}
13 changes: 13 additions & 0 deletions src/com/turbomanage/httpclient/json/JsonResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.turbomanage.httpclient.json;

import com.turbomanage.httpclient.HttpResponse;
import com.turbomanage.httpclient.rest.ObjectFactory;
import com.turbomanage.httpclient.rest.ObjectResponse;

public class JsonResponse extends ObjectResponse<JsonType> {

public JsonResponse(HttpResponse res, ObjectFactory<JsonType> factory) {
super(res, factory);
}

}
13 changes: 13 additions & 0 deletions src/com/turbomanage/httpclient/json/JsonRestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.turbomanage.httpclient.json;

import com.turbomanage.httpclient.AsyncHttpClient;
import com.turbomanage.httpclient.rest.AsyncRestClient;
import com.turbomanage.httpclient.rest.ObjectFactory;

public class JsonRestClient extends AsyncRestClient<JsonType> {

public JsonRestClient(AsyncHttpClient httpClient, ObjectFactory<JsonType> factory) {
super(httpClient, factory);
}

}
7 changes: 7 additions & 0 deletions src/com/turbomanage/httpclient/json/JsonType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.turbomanage.httpclient.json;

import com.turbomanage.httpclient.rest.MediaType;

public class JsonType extends MediaType {

}
50 changes: 50 additions & 0 deletions src/com/turbomanage/httpclient/rest/AsyncRestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.turbomanage.httpclient.rest;

import com.turbomanage.httpclient.AbstractHttpClient;
import com.turbomanage.httpclient.AsyncHttpClient;
import com.turbomanage.httpclient.HttpResponse;
import com.turbomanage.httpclient.ParameterMap;

public abstract class AsyncRestClient<M extends MediaType> {

protected AsyncHttpClient httpClient;
protected ObjectFactory<M> objFactory;

public AsyncRestClient(AsyncHttpClient httpClient, ObjectFactory<M> factory) {
this.httpClient = httpClient;
this.objFactory = factory;
}

// public abstract R get(String path, ParameterMap params);
//
// public abstract <T> R post(String path, T obj);
//
// public abstract <T> R put(String path, T obj);
//
// public abstract R delete(String path, ParameterMap params);

public <T> byte[] toBytes(T obj) {
return objFactory.toBytes(obj);
}

public ObjectResponse<M> get(String path, ParameterMap params) {
HttpResponse httpResponse = httpClient.get(path, params);
return objFactory.wrapResponse(httpResponse);
}

public <T> ObjectResponse<M> post(String path, T obj) {
HttpResponse httpResponse = httpClient.post(path, AbstractHttpClient.JSON, toBytes(obj));
return objFactory.wrapResponse(httpResponse);
}

public <T> ObjectResponse<M> put(String path, T obj) {
HttpResponse httpResponse = httpClient.put(path, AbstractHttpClient.JSON, toBytes(obj));
return objFactory.wrapResponse(httpResponse);
}

public ObjectResponse<M> delete(String path, ParameterMap params) {
HttpResponse httpResponse = httpClient.delete(path, params);
return objFactory.wrapResponse(httpResponse);
}

}
5 changes: 5 additions & 0 deletions src/com/turbomanage/httpclient/rest/MediaType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.turbomanage.httpclient.rest;

public abstract class MediaType {

}
14 changes: 14 additions & 0 deletions src/com/turbomanage/httpclient/rest/ObjectFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.turbomanage.httpclient.rest;

import com.turbomanage.httpclient.HttpResponse;


public abstract class ObjectFactory<M extends MediaType> {

public abstract <T> byte[] toBytes(T obj);

public abstract <T> T toObj(byte[] bytes, Class<T> responseType);

public abstract ObjectResponse<M> wrapResponse(HttpResponse res);

}
23 changes: 23 additions & 0 deletions src/com/turbomanage/httpclient/rest/ObjectResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.turbomanage.httpclient.rest;

import com.turbomanage.httpclient.HttpResponse;

public abstract class ObjectResponse<M extends MediaType> {

protected HttpResponse httpResponse;
protected ObjectFactory<M> objFactory;

public ObjectResponse(HttpResponse res, ObjectFactory<M> factory) {
this.httpResponse = res;
this.objFactory = factory;
}

public <T> T toObj(Class<T> responseType) {
return (T) objFactory.toObj(httpResponse.getBody(), responseType);
}

public HttpResponse getHttpResponse() {
return httpResponse;
}

}
33 changes: 33 additions & 0 deletions src/com/turbomanage/httpclient/rest/RestRequestHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.turbomanage.httpclient.rest;

import com.turbomanage.httpclient.HttpResponse;

/**
* Handler passed to an {@link AsyncRestClient} so that the
* calling code can be notified when a request is complete or
* has thrown an exception. Make your own subclass to handle
* things like login on authentication failure.
*
* @author David M. Chandler
*/
public abstract class RestRequestHandler {

/**
* Called when response is available
*
* @param httpResponse may be null!
*/
public abstract void onSuccess(HttpResponse httpResponse);

/**
* Called when a non-recoverable exception has occurred.
* Timeout exceptions are considered recoverable and won't
* trigger this call.
*
* @param e
*/
public void onError(Exception e) {
e.printStackTrace();
}

}

0 comments on commit 34c895a

Please sign in to comment.