-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6e4642
commit 34c895a
Showing
16 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/com/turbomanage/httpclient/android/AndroidJsonRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/com/turbomanage/httpclient/json/DefaultJsonMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.turbomanage.httpclient.rest; | ||
|
||
public abstract class MediaType { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/com/turbomanage/httpclient/rest/RestRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |