Skip to content
Permalink
Browse files

work in progress

  • Loading branch information...
turbomanage committed Mar 27, 2014
1 parent c6e4642 commit 34c895a58eeb49dba2ae100b347a10acd0e531ec
@@ -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>
@@ -0,0 +1 @@
/bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -25,6 +25,7 @@

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 = "";

@@ -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);
}

}
@@ -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);
}

}
@@ -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);
}

}
@@ -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);
}

}
@@ -0,0 +1,7 @@
package com.turbomanage.httpclient.json;

import com.turbomanage.httpclient.rest.MediaType;

public class JsonType extends MediaType {

}
@@ -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);
}

}
@@ -0,0 +1,5 @@
package com.turbomanage.httpclient.rest;

public abstract class MediaType {

}
@@ -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);

}
@@ -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;
}

}
@@ -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.
You can’t perform that action at this time.