summaryrefslogtreecommitdiff
path: root/sdl_android/src/main/java/com/smartdevicelink/proxy/RPCStruct.java
blob: 116c491df2687ecfb88593752965104761ea716c (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
package com.smartdevicelink.proxy;

import com.smartdevicelink.marshal.JsonRPCMarshaller;

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;

	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 = rpcs.store;
	}
	
	public RPCStruct(Hashtable<String, Object> hashtable) {
		store = hashtable;
		//store = (Hashtable<String, Object>) ObjectCopier.copy(hashtable);
	}
	
	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 version) throws JSONException {
		if (version > 1) {
			String messageType = getMessageTypeName(store.keySet());
			Hashtable<String, Object> function = (Hashtable<String, Object>) store.get(messageType);
			Hashtable<String, Object> parameters = (Hashtable<String, Object>) function.get(RPCMessage.KEY_PARAMETERS);
			return JsonRPCMarshaller.serializeHashtable(parameters);
		} else return JsonRPCMarshaller.serializeHashtable(store);
	}

	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);
				return constructor.newInstance((Hashtable<String, Object>) obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (obj instanceof List<?>) {
			List<?> list = (List<?>) obj;
			if (list != null && list.size() > 0) {
				Object item = list.get(0);
				if (tClass.isInstance(item)) {
					return list;
				} else if (item instanceof Hashtable) {
					List<Object> newList = new ArrayList<Object>();
					for (Object hashObj : list) {
						try {
							Constructor constructor = tClass.getConstructor(Hashtable.class);
							newList.add(constructor.newInstance((Hashtable<String, Object>)hashObj));
						} 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;
				}
			}
		}
		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 {
				Object value = valueForString.invoke(null, (String) s);
				return value;
			} 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 (Double) store.get(key);
	}

	public Float getFloat(String key) {
		return (Float) 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;
	}
}