summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/proxy/RPCStruct.java
blob: 40286629169c9f44607aa7d70db4280b193cede4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (c) 2017 - 2019, SmartDeviceLink Consortium, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the SmartDeviceLink Consortium, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this 
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.smartdevicelink.proxy;

import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.util.SdlDataTypeConverter;
import com.smartdevicelink.util.Version;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

public class RPCStruct {
    public static final String KEY_BULK_DATA = "bulkData";
    public static final String KEY_PROTECTED = "protected";
    
	private byte[] _bulkData = null;
	private Boolean protectedPayload = false;

	private boolean formatRequested = false;
	private Version rpcSpecVersion = null;


	protected Hashtable<String, Object> store = null;
	
	public boolean getStoreValue(String key) { // for unit testing
		return store.contains(key);
	}
	
	public Hashtable<String,Object> getStore () { // for unit testing
		return store;
	}
	
	public RPCStruct() {
		store = new Hashtable<String, Object>();
	}
	
	protected RPCStruct(RPCStruct rpcs) {
		this.store = cloneStore(rpcs);
	}
	
	public RPCStruct(Hashtable<String, Object> hashtable) {
		store = hashtable;
		//store = (Hashtable<String, Object>) ObjectCopier.copy(hashtable);
	}

	static Hashtable<String, Object> cloneStore(RPCStruct rpcStruct){
		if(rpcStruct!= null && rpcStruct.store !=null){
			return (Hashtable)rpcStruct.store.clone();
		}
		return null;
	}

	public void deserializeJSON(JSONObject jsonObject) throws JSONException {
		store = JsonRPCMarshaller.deserializeJSONObject(jsonObject);

	}
	
	// deserializeJSONObject method moved to JsonRPCMarshaller for consistency
	// Keep reference here for backwards compatibility
	@Deprecated
	public static Hashtable<String, Object> deserializeJSONObject(JSONObject jsonObject) 
			throws JSONException {
		return JsonRPCMarshaller.deserializeJSONObject(jsonObject);
	}

	public JSONObject serializeJSON() throws JSONException {
		return JsonRPCMarshaller.serializeHashtable(store);
	}
	
	@SuppressWarnings("unchecked")
    public JSONObject serializeJSON(byte protocolVersion) throws JSONException {
		if (protocolVersion > 1) {
			String messageType = getMessageTypeName(store.keySet());
			Hashtable<String, Object> function = (Hashtable<String, Object>) store.get(messageType);
			if(function != null){
				Hashtable<String, Object> parameters = (Hashtable<String, Object>) function.get(RPCMessage.KEY_PARAMETERS);
				return JsonRPCMarshaller.serializeHashtable(parameters);
			}else{
				return null;
			}

		} else return JsonRPCMarshaller.serializeHashtable(store);
	}

	/**
	 * This method should clean the the RPC to make sure it is compliant with the spec.
	 * <br><br><b> NOTE:</b> Super needs to be called at the END of the method
	 *
	 * @param rpcVersion the rpc spec version that has been negotiated. If value is null the
	 *                   the max value of RPC spec version this library supports should be used.
	 *  @param formatParams if true, the format method will be called on subsequent params
	 */
	public void format(Version rpcVersion, boolean formatParams){
		formatRequested = true;
		rpcSpecVersion = rpcVersion;
		//Should override this method when breaking changes are made to the RPC spec
		if(formatParams && store != null){
			Hashtable<String, Object> parameters;

			if(this instanceof RPCMessage) {
				//If this is a message (request, response, notification) the parameters have to be
				//retrieved from the store object.
				String messageType = getMessageTypeName(store.keySet());
				Hashtable<String, Object> function = (Hashtable<String, Object>) store.get(messageType);
				if(function != null){
					parameters = (Hashtable<String, Object>) function.get(RPCMessage.KEY_PARAMETERS);
				}else {
					parameters = null;
				}
			} else {
				//If this is just an RPC struct the store itself should be used
				parameters = store;
			}

			if (parameters != null) {
				for(Object value:parameters.values()){
					internalFormat(rpcVersion, value);
				}
			}
		}
	}

	/**
	 * Cycles through parameters in this RPC to ensure they all get formated
	 * @param rpcVersion version of the rpc spec that should be used to format this rpc
	 * @param value the object to investigate if it needs to be formated
	 */
	private void internalFormat(Version rpcVersion, Object value) {
		if(value instanceof RPCStruct) {
			((RPCStruct)value).format(rpcVersion,true);
		} else if(value instanceof List<?>) {
			List<?> list = (List<?>)value;
			if(list != null && list.size() > 0) {
				for(Object listItem: list){
					internalFormat(rpcVersion, listItem);
				}
			}
		}
	}


	public byte[] getBulkData() {
		return this._bulkData;
	}

	public void setBulkData(byte[] bulkData) {
		if (bulkData != null) {
			this._bulkData = new byte[bulkData.length];
			System.arraycopy(bulkData, 0, _bulkData, 0, bulkData.length);
		}
		else{
		    this._bulkData = null;
		}
	}
	
	public void setPayloadProtected(Boolean bVal) {
		protectedPayload = bVal;
	}
	
	public Boolean isPayloadProtected() {
		return protectedPayload;
	}
	
	protected String getMessageTypeName(Set<String> keys) {
	      for (String key : keys) {
	          if (key == null) {
	              continue;
	          }
	          if (key.equals(RPCMessage.KEY_REQUEST) || key.equals(RPCMessage.KEY_RESPONSE) ||
	                  key.equals(RPCMessage.KEY_NOTIFICATION)) {
	              return key;
	          }
	      }
	      return null;
	}
	  
	protected boolean hasKey(Set<String> keys, String keyName) {
	      for (String key : keys) {
	    	  if (key == null) {
	    		  continue;
	          }
	    	  if (key.equals(keyName)) {
	    		  return true;
	    	  }
	      }
	      return false;
	}

	// Generalized Getters and Setters

	public void setValue(String key, Object value){
		if (value != null) {
			store.put(key, value);
		} else {
			store.remove(key);
		}
	}

	public Object getValue(String key) {
		return store.get(key);
	}

	public Object getObject(Class tClass, String key) {
		Object obj = store.get(key);
		return formatObject(tClass, obj);
	}

	// Helper methods

	/**
	 * @param tClass a Class to cast Objects to
	 * @param obj Object returned from a stored hashtable
	 * @return A null object if obj is null or if none of the following is true:
	 * a) obj is an instance of tClass
	 * b) obj is an instance of String and it tClass has a valid `valueForString` method
	 * c) obj is an instance of a Hashtable
	 * d) obj is an instance of a List
	 */
	protected Object formatObject(Class tClass, Object obj){
		if(obj == null){
			return null;
		} else if (tClass.isInstance(obj)) {
			return obj;
		} else if (obj instanceof String) {
			return getValueForString(tClass, (String) obj);
		} else if (obj instanceof Hashtable) {
			try {
				Constructor constructor = tClass.getConstructor(Hashtable.class);
				Object customObject = constructor.newInstance((Hashtable<String, Object>) obj);
				if(formatRequested && customObject instanceof RPCStruct){
					((RPCStruct)customObject).format(rpcSpecVersion,true);
				}

				return customObject;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (obj instanceof List<?>) {
			List<?> list = (List<?>) obj;
			if (list != null && list.size() > 0) {
				Object item = null;
				//Iterate through list to find first non-null object
				for(Object object: list){
					if(object != null) {
						item = object;
						break;
					}
				}

				if (item == null) {
					return list;
				} else if (tClass.isInstance(item)) {
					return list;
				} else if (item instanceof Hashtable) {
					List<Object> newList = new ArrayList<Object>();
					Object customObject;
					for (Object hashObj : list) {
						try {
							Constructor constructor = tClass.getConstructor(Hashtable.class);
							customObject = constructor.newInstance((Hashtable<String, Object>) hashObj);
							if(formatRequested
									&& customObject != null
									&& customObject instanceof RPCStruct){
								((RPCStruct)customObject).format(rpcSpecVersion,true);
							}
							newList.add(customObject);
						} catch (Exception e) {
							e.printStackTrace();
							return null;
						}
					}
					return newList;
				} else if (item instanceof String){
					List<Object> newList = new ArrayList<Object>();
					for (Object hashObj : list) {
						Object toAdd = getValueForString(tClass, (String) hashObj);
						if (toAdd != null) {
							newList.add(toAdd);
						}
					}
					return newList;
				}
			} else {
				//If the list is either null or empty it should be returned. It will keep the same
				//behavior as it does today with null lists, but empty ones will now also be returned.
				return list;
			}
		}
		return null;
	}

	/**
	 * @param tClass - a Class with a `valueForString(String s)` method that returns an Object for a given String
	 * @param s - a String to be converted to an Object using a `valueForString(String s)` method
	 * @return An Object converted using a `valueForString(String s)` method in the Class passed in, or a null object if such method does not exist
	 */
	protected Object getValueForString(Class tClass, String s){
		Method valueForString = null;
		try {
			valueForString = tClass.getDeclaredMethod("valueForString", String.class);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		if(valueForString != null){
			try {
				return valueForString.invoke(null, (String) s);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	// Common Object Getters
	public String getString(String key) {
		return (String) store.get(key);
	}

	public Integer getInteger(String key) {
		return (Integer) store.get(key);
	}

	public Double getDouble(String key) {
		return SdlDataTypeConverter.objectToDouble(store.get(key));
	}

	public Float getFloat(String key) {
		return SdlDataTypeConverter.objectToFloat(store.get(key));
	}

	public Boolean getBoolean(String key) { return (Boolean) store.get(key); }

	public Long getLong(String key){
		Object result = store.get(key);
		if (result instanceof Integer) {
			return ((Integer) result).longValue();
		}else if(result instanceof Long){
			return (Long) result;
		}
		return null;
	}
}