Skip to content

Commit e2aba74

Browse files
author
Mike Burke
committed
Merge branch 'refs/heads/hotfix/improve_on_system_request' into sdl_android_parent/develop
2 parents 3434fe2 + 3960cd5 commit e2aba74

File tree

1 file changed

+78
-82
lines changed

1 file changed

+78
-82
lines changed

Diff for: sdl_android_lib/src/com/smartdevicelink/proxy/rpc/OnSystemRequest.java

+78-82
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.json.JSONException;
77
import org.json.JSONObject;
88

9+
import android.util.Log;
10+
911
import com.smartdevicelink.marshal.JsonRPCMarshaller;
1012
import com.smartdevicelink.protocol.enums.FunctionID;
1113
import com.smartdevicelink.proxy.RPCNotification;
@@ -26,46 +28,91 @@ public class OnSystemRequest extends RPCNotification {
2628
public static final String KEY_DATA = "data";
2729
public static final String KEY_OFFSET = "offset";
2830
public static final String KEY_LENGTH = "length";
29-
3031

31-
Hashtable<String, Object> httpreqparams = null;
32-
JSONObject myJSONObj = null;
32+
private String body;
33+
private Headers headers;
3334

3435
public OnSystemRequest() {
3536
super(FunctionID.ON_SYSTEM_REQUEST);
3637
}
3738

38-
@SuppressWarnings("unchecked")
3939
public OnSystemRequest(Hashtable<String, Object> hash) {
40+
this(hash, (byte[]) hash.get(RPCStruct.KEY_BULK_DATA));
41+
}
42+
43+
public OnSystemRequest(Hashtable<String, Object> hash, byte[] bulkData){
4044
super(hash);
45+
setBulkData(bulkData);
46+
}
47+
48+
private void handleBulkData(byte[] bulkData){
49+
if(bulkData == null){
50+
return;
51+
}
4152

42-
//testing
43-
//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\\\"]}\"}}";
44-
try {
45-
byte[] bulkData = (byte[]) hash.get(RPCStruct.KEY_BULK_DATA);
46-
47-
if (bulkData == null) return;
48-
49-
String jsonString = new String(bulkData);
50-
51-
myJSONObj = new JSONObject(jsonString);
52-
Hashtable<String, Object> temp = JsonRPCMarshaller.deserializeJSONObject(myJSONObj);
53-
httpreqparams = (Hashtable<String, Object>) temp.get("HTTPRequest");
54-
} catch (JSONException e) {
55-
// TODO Auto-generated catch block
56-
e.printStackTrace();
57-
}
53+
JSONObject httpJson;
54+
String tempBody = null;
55+
Headers tempHeaders = null;
56+
57+
try{
58+
JSONObject bulkJson = new JSONObject(new String(bulkData));
59+
httpJson = bulkJson.getJSONObject("HTTPRequest");
60+
tempBody = getBody(httpJson);
61+
tempHeaders = getHeaders(httpJson);
62+
}catch(JSONException e){
63+
Log.e("OnSystemRequest", "HTTPRequest in bulk data was malformed.");
64+
e.printStackTrace();
65+
}catch(NullPointerException e){
66+
Log.e("OnSystemRequest", "Invalid HTTPRequest object in bulk data.");
67+
e.printStackTrace();
68+
}
69+
70+
this.body = tempBody;
71+
this.headers = tempHeaders;
5872
}
5973

60-
public void setBinData(byte[] aptData) {
61-
if (aptData != null) {
62-
store.put(RPCStruct.KEY_BULK_DATA, aptData);
63-
} else {
64-
store.remove(RPCStruct.KEY_BULK_DATA);
74+
private String getBody(JSONObject httpJson){
75+
String result = null;
76+
77+
try{
78+
result = httpJson.getString("body");
79+
}catch(JSONException e){
80+
Log.e("OnSystemRequest", "\"body\" key doesn't exist in bulk data.");
81+
e.printStackTrace();
6582
}
83+
84+
return result;
85+
}
86+
87+
private Headers getHeaders(JSONObject httpJson){
88+
Headers result = null;
89+
90+
try{
91+
JSONObject httpHeadersJson = httpJson.getJSONObject("headers");
92+
Hashtable<String, Object> httpHeadersHash = JsonRPCMarshaller.deserializeJSONObject(httpHeadersJson);
93+
result = new Headers(httpHeadersHash);
94+
}catch(JSONException e){
95+
Log.e("OnSystemRequest", "\"headers\" key doesn't exist in bulk data.");
96+
e.printStackTrace();
97+
}
98+
99+
return result;
100+
}
101+
102+
@Deprecated
103+
public void setBinData(byte[] aptData) {
104+
setBulkData(aptData);
66105
}
106+
107+
@Deprecated
67108
public byte[] getBinData() {
68-
return (byte[]) store.get(RPCStruct.KEY_BULK_DATA);
109+
return getBulkData();
110+
}
111+
112+
@Override
113+
public void setBulkData(byte[] bulkData){
114+
super.setBulkData(bulkData);
115+
handleBulkData(bulkData);
69116
}
70117

71118

@@ -84,72 +131,21 @@ public List<String> getLegacyData() {
84131
}
85132

86133
public String getBody(){
87-
88-
JSONObject jLayer1 = null;
89-
String sReturn = null;
90-
try
91-
{
92-
if (httpreqparams != null)
93-
{
94-
jLayer1 = myJSONObj.getJSONObject("HTTPRequest");
95-
sReturn = jLayer1.getString("body");
96-
return sReturn;
97-
}
98-
else if (myJSONObj != null)
99-
{
100-
//jLayer1 = new JSONObject();
101-
//jLayer1.put("data", myJSONObj);
102-
return myJSONObj.toString();
103-
}
104-
else
105-
{
106-
return null;
107-
}
108-
109-
}
110-
catch (Exception e)
111-
{
112-
return null;
113-
}
134+
return this.body;
114135
}
115136

116137
public void setBody(String body) {
117-
if (body != null) {
118-
parameters.put(KEY_BODY, body);
119-
} else {
120-
parameters.remove(KEY_BODY);
121-
}
138+
this.body = body;
122139
}
123140

124-
125141
public void setHeaders(Headers header) {
126-
if (header != null) {
127-
httpreqparams.put(KEY_HEADERS, header);
128-
} else {
129-
httpreqparams.remove(KEY_HEADERS);
130-
}
142+
this.headers = header;
131143
}
132-
133-
@SuppressWarnings("unchecked")
144+
134145
public Headers getHeader() {
135-
if (httpreqparams == null) return null;
136-
137-
Object obj = httpreqparams.get(KEY_HEADERS);
138-
if (obj == null) return null;
139-
if (obj instanceof Headers) {
140-
return (Headers) obj;
141-
} else if (obj instanceof Hashtable) {
142-
try {
143-
return new Headers((Hashtable<String, Object>) obj);
144-
} catch (Exception e) {
145-
DebugTool.logError("Failed to parse " + getClass().getSimpleName() + "." + KEY_HEADERS, e);
146-
}
147-
}
148-
return null;
146+
return this.headers;
149147
}
150148

151-
152-
153149
public RequestType getRequestType() {
154150
Object obj = parameters.get(KEY_REQUEST_TYPE);
155151
if (obj == null) return null;

0 commit comments

Comments
 (0)