@Override
public boolean uploadShareable(Shareable shareable) {
boolean retval = false;
try {
HttpClient client = new HttpClient();
// post to the server SERVER_NAME =
// "http://android-geocast.com/GeocastServer/ShareablePostingServlet";
PostMethod method = new PostMethod(SERVER_NAME);
// add the content(the shareable as a byte array) to the postmethod
method.setRequestEntity(new ByteArrayRequestEntity(shareable
.getBytes()));
// do the actual http post
client.executeMethod(method);
// the servlet will return an error code if something goes wrong
String result = String.valueOf(method.getResponseBody());
if (result.contains(COMMON_ERROR_RESPONSE)) {
retval = false;
} else {
retval = true;
}
} catch (Exception e) {
Log.e("ShareableDaoDbImpl.uploadShareable", e.getMessage());
// TODO XXX in the final version network failure should not be fatal
throw new FatalException("ShareableDaoDbImpl.uploadShareable", e);
}
return retval;
}
/*
* (non-Javadoc)
*
* @see org.technocore.integration.dao.ShareableDao#getShareableContent(long)
* shareable content has to go through separately as it is large and
* may contain the terminating sequence
*/
@Override
public byte[] getShareableContent(long shareableId) {
byte[] retval = null;
try {
HttpClient client = new HttpClient();
// do a get from the server SERVER_NAME =
// "http://android-geocast.com/GeocastServer/ShareablePostingServlet";
GetMethod getMethod = new GetMethod(SERVER_NAME);
// add a parameter shareable id to the get, namevaluepair makes
// url encoding a non issue!
getMethod.setQueryString(new NameValuePair[] { new NameValuePair(
"shareableId", String.valueOf(shareableId)) });
// execute the actual http get
client.executeMethod(getMethod);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// pull out what the server sent back
InputStream inputStream = getMethod.getResponseBodyAsStream();
int temp;
while ((temp = inputStream.read()) != -1) {
bos.write(temp);
}
// the resulting byte array is the content of the shareable
retval = bos.toByteArray();
} catch (Exception e) {
Log.e("ShareableDaoDbImpl.getShareableContent", e.getMessage());
// TODO XXX in the final version network failure should not be fatal
throw new FatalException("ShareableDaoDbImpl.getShareableContent",
e);
}
return retval;
}