/clients/android/branches/roleProviders/src/org/moca/net/MocaAPI.java

http://moca.googlecode.com/ · Java · 258 lines · 212 code · 43 blank · 3 comment · 50 complexity · 542657b1be35f1927e8e0d09ed60ebdb MD5 · raw file

  1. package org.moca.net;
  2. import java.lang.reflect.Type;
  3. import java.util.List;
  4. import org.apache.commons.codec.binary.Base64;
  5. import org.apache.commons.httpclient.NameValuePair;
  6. import org.apache.commons.httpclient.methods.GetMethod;
  7. import org.apache.commons.httpclient.methods.PostMethod;
  8. import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
  9. import org.apache.commons.httpclient.methods.multipart.FilePart;
  10. import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
  11. import org.apache.commons.httpclient.methods.multipart.Part;
  12. import org.apache.commons.httpclient.methods.multipart.StringPart;
  13. import com.google.gson.reflect.TypeToken;
  14. public class MocaAPI {
  15. public static final String TAG = "MocaAPI";
  16. private String apiUri;
  17. private String username;
  18. private String password;
  19. public MocaAPI(String apiUri, String username, String password) {
  20. this.apiUri = apiUri;
  21. this.username = username;
  22. this.password = password;
  23. }
  24. public boolean transmitCaseResponses(String savedProcedureGuid, String procedureGuid, String phoneIdentifier, String jsonResponses) throws APIException {
  25. PostMethod post = new PostMethod(APIUtil.constructProcedureSubmitURL(apiUri));
  26. post.addParameter("username", username);
  27. post.addParameter("password", password);
  28. post.addParameter("savedproc_guid", savedProcedureGuid);
  29. post.addParameter("procedure_guid", procedureGuid);
  30. post.addParameter("phone", phoneIdentifier);
  31. post.addParameter("responses", jsonResponses);
  32. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  33. APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
  34. int status = response.getStatus();
  35. MDSResponse<String> result = response.getResult();
  36. if (status == 200 && result != null) {
  37. return result.succeeded();
  38. }
  39. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  40. }
  41. public boolean transmitBinarySequence(String savedProcedureId, String elementId, String fileGuid, String element_type, String element_filename,
  42. int fileSize, int start, int end, byte byte_data[]) throws APIException {
  43. Part[] parts = {
  44. new StringPart("procedure_guid", savedProcedureId),
  45. new StringPart("element_id", elementId),
  46. new StringPart("binary_guid", fileGuid),
  47. new StringPart("element_type", element_type),
  48. new StringPart("file_size", Integer.toString(fileSize)),
  49. new StringPart("byte_start", Integer.toString(start)),
  50. new StringPart("byte_end", Integer.toString(end)),
  51. new FilePart("byte_data", new ByteArrayPartSource(element_filename, byte_data)),
  52. };
  53. PostMethod post = new PostMethod(APIUtil.constructBinaryChunkSubmitURL(apiUri));
  54. post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
  55. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  56. APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
  57. int status = response.getStatus();
  58. MDSResponse<String> result = response.getResult();
  59. if (status == 200 && result != null) {
  60. // TODO(XXX) throw failure specific error code
  61. return result.succeeded();
  62. }
  63. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  64. }
  65. public boolean transmitBinarySequenceAsBase64Text(String savedProcedureId, String elementId, String fileGuid, String element_type, String element_filename,
  66. int fileSize, int start, int end, byte byte_data[]) throws APIException {
  67. PostMethod post = new PostMethod(APIUtil.constructBinaryChunkHackSubmitURL(apiUri));
  68. post.addParameter(new NameValuePair("procedure_guid", savedProcedureId));
  69. post.addParameter(new NameValuePair("element_id", elementId));
  70. post.addParameter(new NameValuePair("binary_guid", fileGuid));
  71. post.addParameter(new NameValuePair("element_type", element_type));
  72. post.addParameter(new NameValuePair("file_size", Integer.toString(fileSize)));
  73. post.addParameter(new NameValuePair("byte_start", Integer.toString(start)));
  74. post.addParameter(new NameValuePair("byte_end", Integer.toString(end)));
  75. // Encode byte_data in Base64
  76. byte[] encoded_data = new Base64().encode(byte_data);
  77. post.addParameter(new NameValuePair("byte_data", new String(encoded_data)));
  78. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  79. APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
  80. int status = response.getStatus();
  81. MDSResponse<String> result = response.getResult();
  82. if (status == 200 && result != null) {
  83. // TODO(XXX) throw failure specific error code
  84. return result.succeeded();
  85. }
  86. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  87. }
  88. public boolean validateCredentials() throws APIException {
  89. GetMethod get = new GetMethod(APIUtil.constructValidateCredentialsURL(apiUri));
  90. NameValuePair[] arguments = new NameValuePair[2];
  91. arguments[0] = new NameValuePair("username", username);
  92. arguments[1] = new NameValuePair("password", password);
  93. get.setQueryString(arguments);
  94. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  95. APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
  96. int status = response.getStatus();
  97. MDSResponse<String> result = response.getResult();
  98. if (status == 200 && result != null) {
  99. return result.succeeded();
  100. }
  101. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  102. }
  103. public String getAllPatients() throws APIException {
  104. GetMethod get = new GetMethod(APIUtil.constructPatientDatabaseDownloadURL(apiUri));
  105. NameValuePair[] arguments = new NameValuePair[2];
  106. arguments[0] = new NameValuePair("username", username);
  107. arguments[1] = new NameValuePair("password", password);
  108. get.setQueryString(arguments);
  109. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  110. APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
  111. int status = response.getStatus();
  112. MDSResponse<String> result = response.getResult();
  113. if (status == 200 && result != null) {
  114. return result.getData();
  115. }
  116. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  117. }
  118. public String getPatientInformation(String patientIdentifier) throws APIException {
  119. GetMethod get = new GetMethod(APIUtil.constructUserInfoURL(apiUri, patientIdentifier));
  120. NameValuePair[] arguments = new NameValuePair[2];
  121. arguments[0] = new NameValuePair("username", username);
  122. arguments[1] = new NameValuePair("password", password);
  123. get.setQueryString(arguments);
  124. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  125. APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
  126. int status = response.getStatus();
  127. MDSResponse<String> result = response.getResult();
  128. if (status == 200 && result != null) {
  129. if (result.succeeded()) {
  130. return result.getData();
  131. } else {
  132. throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
  133. }
  134. }
  135. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  136. }
  137. public List<String> getRoles() throws APIException {
  138. GetMethod get = new GetMethod(APIUtil.constructRolesURL(apiUri));
  139. NameValuePair[] arguments = new NameValuePair[2];
  140. arguments[0] = new NameValuePair("username", username);
  141. arguments[1] = new NameValuePair("password", password);
  142. get.setQueryString(arguments);
  143. Type returnType = new TypeToken<MDSResponse<List<String>>>() {}.getType();
  144. APIResponse<List<String>> response = APIUtil.<List<String>>doApiRequest(get, returnType);
  145. int status = response.getStatus();
  146. MDSResponse<List<String>> result = response.getResult();
  147. if (status == 200 && result != null) {
  148. if (result.succeeded()) {
  149. return result.getData();
  150. } else {
  151. throw new APIException(APIResultCode.REQUEST_FAILED, "Unable to download available roles from server.");
  152. }
  153. }
  154. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  155. }
  156. public List<String> getLocations() throws APIException {
  157. GetMethod get = new GetMethod(APIUtil.constructLocationsURL(apiUri));
  158. NameValuePair[] arguments = new NameValuePair[2];
  159. arguments[0] = new NameValuePair("username", username);
  160. arguments[1] = new NameValuePair("password", password);
  161. get.setQueryString(arguments);
  162. Type returnType = new TypeToken<MDSResponse<List<String>>>() {}.getType();
  163. APIResponse<List<String>> response = APIUtil.<List<String>>doApiRequest(get, returnType);
  164. int status = response.getStatus();
  165. MDSResponse<List<String>> result = response.getResult();
  166. if (status == 200 && result != null) {
  167. if (result.succeeded()) {
  168. return result.getData();
  169. } else {
  170. throw new APIException(APIResultCode.REQUEST_FAILED, "Unable to download available locations from server.");
  171. }
  172. }
  173. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  174. }
  175. public List<ProcedureInfo> getAvailableProcedureList() throws APIException {
  176. GetMethod get = new GetMethod(APIUtil.constructProcedureListURL(apiUri));
  177. NameValuePair[] arguments = new NameValuePair[2];
  178. arguments[0] = new NameValuePair("username", username);
  179. arguments[1] = new NameValuePair("password", password);
  180. get.setQueryString(arguments);
  181. Type returnType = new TypeToken<MDSResponse<List<ProcedureInfo>>>() {}.getType();
  182. APIResponse<List<ProcedureInfo>> response = APIUtil.<List<ProcedureInfo>>doApiRequest(get, returnType);
  183. int status = response.getStatus();
  184. MDSResponse<List<ProcedureInfo>> result = response.getResult();
  185. if (status == 200 && result != null) {
  186. if (result.succeeded()) {
  187. List<ProcedureInfo> procedureList = result.getData();
  188. return procedureList;
  189. } else {
  190. throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
  191. }
  192. }
  193. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  194. }
  195. public String getProcedure(int procedureId) throws APIException {
  196. GetMethod get = new GetMethod(APIUtil.constructProcedureDownloadURL(apiUri, procedureId));
  197. NameValuePair[] arguments = new NameValuePair[3];
  198. arguments[0] = new NameValuePair("username", username);
  199. arguments[1] = new NameValuePair("password", password);
  200. arguments[2] = new NameValuePair("procedure_id", Integer.toString(procedureId));
  201. get.setQueryString(arguments);
  202. Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
  203. APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
  204. int status = response.getStatus();
  205. MDSResponse<String> result = response.getResult();
  206. if (status == 200 && result != null) {
  207. if (result.succeeded()) {
  208. return result.getData();
  209. } else {
  210. throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
  211. }
  212. }
  213. throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
  214. }
  215. }