6
6
import org .json .JSONException ;
7
7
import org .json .JSONObject ;
8
8
9
+ import android .util .Log ;
10
+
9
11
import com .smartdevicelink .marshal .JsonRPCMarshaller ;
10
12
import com .smartdevicelink .protocol .enums .FunctionID ;
11
13
import com .smartdevicelink .proxy .RPCNotification ;
@@ -26,46 +28,91 @@ public class OnSystemRequest extends RPCNotification {
26
28
public static final String KEY_DATA = "data" ;
27
29
public static final String KEY_OFFSET = "offset" ;
28
30
public static final String KEY_LENGTH = "length" ;
29
-
30
31
31
- Hashtable < String , Object > httpreqparams = null ;
32
- JSONObject myJSONObj = null ;
32
+ private String body ;
33
+ private Headers headers ;
33
34
34
35
public OnSystemRequest () {
35
36
super (FunctionID .ON_SYSTEM_REQUEST );
36
37
}
37
38
38
- @ SuppressWarnings ("unchecked" )
39
39
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 ){
40
44
super (hash );
45
+ setBulkData (bulkData );
46
+ }
47
+
48
+ private void handleBulkData (byte [] bulkData ){
49
+ if (bulkData == null ){
50
+ return ;
51
+ }
41
52
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 ;
58
72
}
59
73
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 ();
65
82
}
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 );
66
105
}
106
+
107
+ @ Deprecated
67
108
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 );
69
116
}
70
117
71
118
@@ -84,72 +131,21 @@ public List<String> getLegacyData() {
84
131
}
85
132
86
133
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 ;
114
135
}
115
136
116
137
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 ;
122
139
}
123
140
124
-
125
141
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 ;
131
143
}
132
-
133
- @ SuppressWarnings ("unchecked" )
144
+
134
145
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 ;
149
147
}
150
148
151
-
152
-
153
149
public RequestType getRequestType () {
154
150
Object obj = parameters .get (KEY_REQUEST_TYPE );
155
151
if (obj == null ) return null ;
0 commit comments