/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
- package org.moca.net;
- import java.lang.reflect.Type;
- import java.util.List;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
- import org.apache.commons.httpclient.methods.multipart.FilePart;
- import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
- import org.apache.commons.httpclient.methods.multipart.Part;
- import org.apache.commons.httpclient.methods.multipart.StringPart;
- import com.google.gson.reflect.TypeToken;
- public class MocaAPI {
- public static final String TAG = "MocaAPI";
-
- private String apiUri;
- private String username;
- private String password;
-
- public MocaAPI(String apiUri, String username, String password) {
- this.apiUri = apiUri;
- this.username = username;
- this.password = password;
- }
-
- public boolean transmitCaseResponses(String savedProcedureGuid, String procedureGuid, String phoneIdentifier, String jsonResponses) throws APIException {
- PostMethod post = new PostMethod(APIUtil.constructProcedureSubmitURL(apiUri));
- post.addParameter("username", username);
- post.addParameter("password", password);
- post.addParameter("savedproc_guid", savedProcedureGuid);
- post.addParameter("procedure_guid", procedureGuid);
- post.addParameter("phone", phoneIdentifier);
- post.addParameter("responses", jsonResponses);
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- return result.succeeded();
- }
-
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public boolean transmitBinarySequence(String savedProcedureId, String elementId, String fileGuid, String element_type, String element_filename,
- int fileSize, int start, int end, byte byte_data[]) throws APIException {
- Part[] parts = {
- new StringPart("procedure_guid", savedProcedureId),
- new StringPart("element_id", elementId),
- new StringPart("binary_guid", fileGuid),
- new StringPart("element_type", element_type),
- new StringPart("file_size", Integer.toString(fileSize)),
- new StringPart("byte_start", Integer.toString(start)),
- new StringPart("byte_end", Integer.toString(end)),
- new FilePart("byte_data", new ByteArrayPartSource(element_filename, byte_data)),
- };
- PostMethod post = new PostMethod(APIUtil.constructBinaryChunkSubmitURL(apiUri));
- post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- // TODO(XXX) throw failure specific error code
- return result.succeeded();
- }
-
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public boolean transmitBinarySequenceAsBase64Text(String savedProcedureId, String elementId, String fileGuid, String element_type, String element_filename,
- int fileSize, int start, int end, byte byte_data[]) throws APIException {
- PostMethod post = new PostMethod(APIUtil.constructBinaryChunkHackSubmitURL(apiUri));
- post.addParameter(new NameValuePair("procedure_guid", savedProcedureId));
- post.addParameter(new NameValuePair("element_id", elementId));
- post.addParameter(new NameValuePair("binary_guid", fileGuid));
- post.addParameter(new NameValuePair("element_type", element_type));
- post.addParameter(new NameValuePair("file_size", Integer.toString(fileSize)));
- post.addParameter(new NameValuePair("byte_start", Integer.toString(start)));
- post.addParameter(new NameValuePair("byte_end", Integer.toString(end)));
- // Encode byte_data in Base64
- byte[] encoded_data = new Base64().encode(byte_data);
- post.addParameter(new NameValuePair("byte_data", new String(encoded_data)));
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(post, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- // TODO(XXX) throw failure specific error code
- return result.succeeded();
- }
-
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public boolean validateCredentials() throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructValidateCredentialsURL(apiUri));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- return result.succeeded();
- }
-
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public String getAllPatients() throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructPatientDatabaseDownloadURL(apiUri));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- return result.getData();
- }
-
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public String getPatientInformation(String patientIdentifier) throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructUserInfoURL(apiUri, patientIdentifier));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- if (result.succeeded()) {
- return result.getData();
- } else {
- throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
- }
- }
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public List<String> getRoles() throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructRolesURL(apiUri));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<List<String>>>() {}.getType();
- APIResponse<List<String>> response = APIUtil.<List<String>>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<List<String>> result = response.getResult();
-
- if (status == 200 && result != null) {
- if (result.succeeded()) {
- return result.getData();
- } else {
- throw new APIException(APIResultCode.REQUEST_FAILED, "Unable to download available roles from server.");
- }
- }
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public List<String> getLocations() throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructLocationsURL(apiUri));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<List<String>>>() {}.getType();
- APIResponse<List<String>> response = APIUtil.<List<String>>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<List<String>> result = response.getResult();
-
- if (status == 200 && result != null) {
- if (result.succeeded()) {
- return result.getData();
- } else {
- throw new APIException(APIResultCode.REQUEST_FAILED, "Unable to download available locations from server.");
- }
- }
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public List<ProcedureInfo> getAvailableProcedureList() throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructProcedureListURL(apiUri));
- NameValuePair[] arguments = new NameValuePair[2];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<List<ProcedureInfo>>>() {}.getType();
- APIResponse<List<ProcedureInfo>> response = APIUtil.<List<ProcedureInfo>>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<List<ProcedureInfo>> result = response.getResult();
-
- if (status == 200 && result != null) {
- if (result.succeeded()) {
- List<ProcedureInfo> procedureList = result.getData();
- return procedureList;
- } else {
- throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
- }
- }
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
-
- public String getProcedure(int procedureId) throws APIException {
- GetMethod get = new GetMethod(APIUtil.constructProcedureDownloadURL(apiUri, procedureId));
- NameValuePair[] arguments = new NameValuePair[3];
- arguments[0] = new NameValuePair("username", username);
- arguments[1] = new NameValuePair("password", password);
- arguments[2] = new NameValuePair("procedure_id", Integer.toString(procedureId));
- get.setQueryString(arguments);
-
- Type returnType = new TypeToken<MDSResponse<String>>() {}.getType();
- APIResponse<String> response = APIUtil.<String>doApiRequest(get, returnType);
- int status = response.getStatus();
- MDSResponse<String> result = response.getResult();
-
- if (status == 200 && result != null) {
- if (result.succeeded()) {
- return result.getData();
- } else {
- throw new APIException(APIResultCode.REQUEST_FAILED, "Server request failed.");
- }
- }
- throw new APIException(APIResultCode.INVALID_REQUEST, "Could not connect to server.");
- }
- }