summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Burke <mike@livioconnect.com>2015-03-09 11:18:36 -0400
committerMike Burke <mike@livioconnect.com>2015-03-09 11:18:36 -0400
commite2aba74c7b70695ab24b6d727762e6ce295904d1 (patch)
tree3903157765ad9f366758a21c95020d41e38c897e
parent3434fe2af0d5fdb9110d8575337234e5084cb1c5 (diff)
parent3960cd55c3346c99d9de5dfe81f0ba3d63dadf40 (diff)
downloadsdl_android-release/3.4.0.tar.gz
Merge branch 'refs/heads/hotfix/improve_on_system_request' into sdl_android_parent/developrelease/3.4.0
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java160
1 files changed, 78 insertions, 82 deletions
diff --git a/sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java b/sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java
index f62bc2479..caa99aefd 100644
--- a/sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java
+++ b/sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java
@@ -6,6 +6,8 @@ import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
+import android.util.Log;
+
import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCNotification;
@@ -26,46 +28,91 @@ public class OnSystemRequest extends RPCNotification {
public static final String KEY_DATA = "data";
public static final String KEY_OFFSET = "offset";
public static final String KEY_LENGTH = "length";
-
- Hashtable<String, Object> httpreqparams = null;
- JSONObject myJSONObj = null;
+ private String body;
+ private Headers headers;
public OnSystemRequest() {
super(FunctionID.ON_SYSTEM_REQUEST);
}
- @SuppressWarnings("unchecked")
public OnSystemRequest(Hashtable<String, Object> hash) {
+ this(hash, (byte[]) hash.get(RPCStruct.KEY_BULK_DATA));
+ }
+
+ public OnSystemRequest(Hashtable<String, Object> hash, byte[] bulkData){
super(hash);
+ setBulkData(bulkData);
+ }
+
+ private void handleBulkData(byte[] bulkData){
+ if(bulkData == null){
+ return;
+ }
- //testing
- //String sJson = "{\"HTTPRequest\":{\"headers\":{\"ContentType\":\"application/json\",\"ConnectTimeout\":60,\"DoOutput\":true,\"DoInput\":true,\"UseCaches\":false,\"RequestMethod\":\"POST\",\"ReadTimeout\":60,\"InstanceFollowRedirects\":false,\"charset\":\"utf-8\",\"Content-Length\":10743},\"body\":\"{\\\"data\\\":[\\\"HQcYAAAp+Ul19L\\\"]}\"}}";
- try {
- byte[] bulkData = (byte[]) hash.get(RPCStruct.KEY_BULK_DATA);
-
- if (bulkData == null) return;
-
- String jsonString = new String(bulkData);
-
- myJSONObj = new JSONObject(jsonString);
- Hashtable<String, Object> temp = JsonRPCMarshaller.deserializeJSONObject(myJSONObj);
- httpreqparams = (Hashtable<String, Object>) temp.get("HTTPRequest");
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ JSONObject httpJson;
+ String tempBody = null;
+ Headers tempHeaders = null;
+
+ try{
+ JSONObject bulkJson = new JSONObject(new String(bulkData));
+ httpJson = bulkJson.getJSONObject("HTTPRequest");
+ tempBody = getBody(httpJson);
+ tempHeaders = getHeaders(httpJson);
+ }catch(JSONException e){
+ Log.e("OnSystemRequest", "HTTPRequest in bulk data was malformed.");
+ e.printStackTrace();
+ }catch(NullPointerException e){
+ Log.e("OnSystemRequest", "Invalid HTTPRequest object in bulk data.");
+ e.printStackTrace();
+ }
+
+ this.body = tempBody;
+ this.headers = tempHeaders;
}
- public void setBinData(byte[] aptData) {
- if (aptData != null) {
- store.put(RPCStruct.KEY_BULK_DATA, aptData);
- } else {
- store.remove(RPCStruct.KEY_BULK_DATA);
+ private String getBody(JSONObject httpJson){
+ String result = null;
+
+ try{
+ result = httpJson.getString("body");
+ }catch(JSONException e){
+ Log.e("OnSystemRequest", "\"body\" key doesn't exist in bulk data.");
+ e.printStackTrace();
}
+
+ return result;
+ }
+
+ private Headers getHeaders(JSONObject httpJson){
+ Headers result = null;
+
+ try{
+ JSONObject httpHeadersJson = httpJson.getJSONObject("headers");
+ Hashtable<String, Object> httpHeadersHash = JsonRPCMarshaller.deserializeJSONObject(httpHeadersJson);
+ result = new Headers(httpHeadersHash);
+ }catch(JSONException e){
+ Log.e("OnSystemRequest", "\"headers\" key doesn't exist in bulk data.");
+ e.printStackTrace();
+ }
+
+ return result;
+ }
+
+ @Deprecated
+ public void setBinData(byte[] aptData) {
+ setBulkData(aptData);
}
+
+ @Deprecated
public byte[] getBinData() {
- return (byte[]) store.get(RPCStruct.KEY_BULK_DATA);
+ return getBulkData();
+ }
+
+ @Override
+ public void setBulkData(byte[] bulkData){
+ super.setBulkData(bulkData);
+ handleBulkData(bulkData);
}
@@ -84,72 +131,21 @@ public class OnSystemRequest extends RPCNotification {
}
public String getBody(){
-
- JSONObject jLayer1 = null;
- String sReturn = null;
- try
- {
- if (httpreqparams != null)
- {
- jLayer1 = myJSONObj.getJSONObject("HTTPRequest");
- sReturn = jLayer1.getString("body");
- return sReturn;
- }
- else if (myJSONObj != null)
- {
- //jLayer1 = new JSONObject();
- //jLayer1.put("data", myJSONObj);
- return myJSONObj.toString();
- }
- else
- {
- return null;
- }
-
- }
- catch (Exception e)
- {
- return null;
- }
+ return this.body;
}
public void setBody(String body) {
- if (body != null) {
- parameters.put(KEY_BODY, body);
- } else {
- parameters.remove(KEY_BODY);
- }
+ this.body = body;
}
-
public void setHeaders(Headers header) {
- if (header != null) {
- httpreqparams.put(KEY_HEADERS, header);
- } else {
- httpreqparams.remove(KEY_HEADERS);
- }
+ this.headers = header;
}
-
- @SuppressWarnings("unchecked")
+
public Headers getHeader() {
- if (httpreqparams == null) return null;
-
- Object obj = httpreqparams.get(KEY_HEADERS);
- if (obj == null) return null;
- if (obj instanceof Headers) {
- return (Headers) obj;
- } else if (obj instanceof Hashtable) {
- try {
- return new Headers((Hashtable<String, Object>) obj);
- } catch (Exception e) {
- DebugTool.logError("Failed to parse " + getClass().getSimpleName() + "." + KEY_HEADERS, e);
- }
- }
- return null;
+ return this.headers;
}
-
-
public RequestType getRequestType() {
Object obj = parameters.get(KEY_REQUEST_TYPE);
if (obj == null) return null;