You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonCodec.java 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.beans.IntrospectionException;
  18. import java.beans.Introspector;
  19. import java.beans.PropertyDescriptor;
  20. import java.io.Serializable;
  21. import java.lang.reflect.Array;
  22. import java.lang.reflect.Field;
  23. import java.lang.reflect.GenericArrayType;
  24. import java.lang.reflect.Method;
  25. import java.lang.reflect.Modifier;
  26. import java.lang.reflect.ParameterizedType;
  27. import java.lang.reflect.Type;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.HashSet;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Map.Entry;
  36. import java.util.Set;
  37. import java.util.concurrent.ConcurrentHashMap;
  38. import java.util.concurrent.ConcurrentMap;
  39. import com.vaadin.server.communication.DateSerializer;
  40. import com.vaadin.server.communication.JsonSerializer;
  41. import com.vaadin.shared.Connector;
  42. import com.vaadin.shared.JsonConstants;
  43. import com.vaadin.shared.communication.UidlValue;
  44. import com.vaadin.ui.Component;
  45. import com.vaadin.ui.ConnectorTracker;
  46. import elemental.json.Json;
  47. import elemental.json.JsonArray;
  48. import elemental.json.JsonException;
  49. import elemental.json.JsonNull;
  50. import elemental.json.JsonObject;
  51. import elemental.json.JsonString;
  52. import elemental.json.JsonType;
  53. import elemental.json.JsonValue;
  54. import elemental.json.impl.JreJsonArray;
  55. /**
  56. * Decoder for converting RPC parameters and other values from JSON in transfer
  57. * between the client and the server and vice versa.
  58. *
  59. * @since 7.0
  60. */
  61. public class JsonCodec implements Serializable {
  62. /* Immutable Encode Result representing null */
  63. private static final EncodeResult ENCODE_RESULT_NULL = new EncodeResult(Json.createNull());
  64. /* Immutable empty JSONArray */
  65. private static final JsonArray EMPTY_JSON_ARRAY = new JreJsonArray(Json.instance()) {
  66. @Override
  67. public void set(int index, JsonValue value) {
  68. throw new UnsupportedOperationException(
  69. "Immutable empty JsonArray.");
  70. }
  71. @Override
  72. public void set(int index, String string) {
  73. throw new UnsupportedOperationException(
  74. "Immutable empty JsonArray.");
  75. }
  76. @Override
  77. public void set(int index, double number) {
  78. throw new UnsupportedOperationException(
  79. "Immutable empty JsonArray.");
  80. }
  81. @Override
  82. public void set(int index, boolean bool) {
  83. throw new UnsupportedOperationException(
  84. "Immutable empty JsonArray.");
  85. }
  86. };
  87. public static interface BeanProperty extends Serializable {
  88. public Object getValue(Object bean) throws Exception;
  89. public void setValue(Object bean, Object value) throws Exception;
  90. public String getName();
  91. public Type getType();
  92. }
  93. private static class FieldProperty implements BeanProperty {
  94. private final Field field;
  95. public FieldProperty(Field field) {
  96. this.field = field;
  97. }
  98. @Override
  99. public Object getValue(Object bean) throws Exception {
  100. return field.get(bean);
  101. }
  102. @Override
  103. public void setValue(Object bean, Object value) throws Exception {
  104. field.set(bean, value);
  105. }
  106. @Override
  107. public String getName() {
  108. return field.getName();
  109. }
  110. @Override
  111. public Type getType() {
  112. return field.getGenericType();
  113. }
  114. public static Collection<FieldProperty> find(Class<?> type)
  115. throws IntrospectionException {
  116. Field[] fields = type.getFields();
  117. Collection<FieldProperty> properties = new ArrayList<FieldProperty>(
  118. fields.length);
  119. for (Field field : fields) {
  120. if (!Modifier.isStatic(field.getModifiers())) {
  121. properties.add(new FieldProperty(field));
  122. }
  123. }
  124. return properties;
  125. }
  126. }
  127. private static class MethodProperty implements BeanProperty {
  128. private final PropertyDescriptor pd;
  129. public MethodProperty(PropertyDescriptor pd) {
  130. this.pd = pd;
  131. }
  132. @Override
  133. public Object getValue(Object bean) throws Exception {
  134. Method readMethod = pd.getReadMethod();
  135. return readMethod.invoke(bean);
  136. }
  137. @Override
  138. public void setValue(Object bean, Object value) throws Exception {
  139. pd.getWriteMethod().invoke(bean, value);
  140. }
  141. @Override
  142. public String getName() {
  143. String fieldName = pd.getWriteMethod().getName().substring(3);
  144. fieldName = Character.toLowerCase(fieldName.charAt(0))
  145. + fieldName.substring(1);
  146. return fieldName;
  147. }
  148. public static Collection<MethodProperty> find(Class<?> type)
  149. throws IntrospectionException {
  150. Collection<MethodProperty> properties = new ArrayList<MethodProperty>();
  151. for (PropertyDescriptor pd : Introspector.getBeanInfo(type)
  152. .getPropertyDescriptors()) {
  153. if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
  154. continue;
  155. }
  156. properties.add(new MethodProperty(pd));
  157. }
  158. return properties;
  159. }
  160. @Override
  161. public Type getType() {
  162. return pd.getReadMethod().getGenericReturnType();
  163. }
  164. }
  165. /**
  166. * Cache the collection of bean properties for a given type to avoid doing a
  167. * quite expensive lookup multiple times. Will be used from any thread that
  168. * happens to process Vaadin requests, so it must be protected from
  169. * corruption caused by concurrent access.
  170. */
  171. private static ConcurrentMap<Class<?>, Collection<BeanProperty>> typePropertyCache = new ConcurrentHashMap<Class<?>, Collection<BeanProperty>>();
  172. private static Map<Class<?>, String> typeToTransportType = new HashMap<Class<?>, String>();
  173. /**
  174. * Note! This does not contain primitives.
  175. * <p>
  176. */
  177. private static Map<String, Class<?>> transportTypeToType = new HashMap<String, Class<?>>();
  178. private static Map<Class<?>, JsonSerializer<?>> customSerializers = new HashMap<Class<?>, JsonSerializer<?>>();
  179. static {
  180. customSerializers.put(Date.class, new DateSerializer());
  181. }
  182. static {
  183. registerType(String.class, JsonConstants.VTYPE_STRING);
  184. registerType(Connector.class, JsonConstants.VTYPE_CONNECTOR);
  185. registerType(Boolean.class, JsonConstants.VTYPE_BOOLEAN);
  186. registerType(boolean.class, JsonConstants.VTYPE_BOOLEAN);
  187. registerType(Integer.class, JsonConstants.VTYPE_INTEGER);
  188. registerType(int.class, JsonConstants.VTYPE_INTEGER);
  189. registerType(Float.class, JsonConstants.VTYPE_FLOAT);
  190. registerType(float.class, JsonConstants.VTYPE_FLOAT);
  191. registerType(Double.class, JsonConstants.VTYPE_DOUBLE);
  192. registerType(double.class, JsonConstants.VTYPE_DOUBLE);
  193. registerType(Long.class, JsonConstants.VTYPE_LONG);
  194. registerType(long.class, JsonConstants.VTYPE_LONG);
  195. registerType(String[].class, JsonConstants.VTYPE_STRINGARRAY);
  196. registerType(Object[].class, JsonConstants.VTYPE_ARRAY);
  197. registerType(Map.class, JsonConstants.VTYPE_MAP);
  198. registerType(HashMap.class, JsonConstants.VTYPE_MAP);
  199. registerType(List.class, JsonConstants.VTYPE_LIST);
  200. registerType(Set.class, JsonConstants.VTYPE_SET);
  201. registerType(Void.class, JsonConstants.VTYPE_NULL);
  202. }
  203. private static void registerType(Class<?> type, String transportType) {
  204. typeToTransportType.put(type, transportType);
  205. if (!type.isPrimitive()) {
  206. transportTypeToType.put(transportType, type);
  207. }
  208. }
  209. public static boolean isInternalTransportType(String transportType) {
  210. return transportTypeToType.containsKey(transportType);
  211. }
  212. public static boolean isInternalType(Type type) {
  213. if (type instanceof Class && ((Class<?>) type).isPrimitive()) {
  214. if (type == byte.class || type == char.class) {
  215. // Almost all primitive types are handled internally
  216. return false;
  217. }
  218. // All primitive types are handled internally
  219. return true;
  220. } else if (type == UidlValue.class) {
  221. // UidlValue is a special internal type wrapping type info and a
  222. // value
  223. return true;
  224. }
  225. return typeToTransportType.containsKey(getClassForType(type));
  226. }
  227. private static Class<?> getClassForType(Type type) {
  228. if (type instanceof ParameterizedType) {
  229. return (Class<?>) (((ParameterizedType) type).getRawType());
  230. } else if (type instanceof Class<?>) {
  231. return (Class<?>) type;
  232. } else {
  233. return null;
  234. }
  235. }
  236. private static Class<?> getType(String transportType) {
  237. return transportTypeToType.get(transportType);
  238. }
  239. public static Object decodeInternalOrCustomType(Type targetType,
  240. JsonValue value, ConnectorTracker connectorTracker) {
  241. if (isInternalType(targetType)) {
  242. return decodeInternalType(targetType, false, value,
  243. connectorTracker);
  244. } else {
  245. return decodeCustomType(targetType, value, connectorTracker);
  246. }
  247. }
  248. public static Object decodeCustomType(Type targetType, JsonValue value,
  249. ConnectorTracker connectorTracker) {
  250. if (isInternalType(targetType)) {
  251. throw new JsonException("decodeCustomType cannot be used for "
  252. + targetType + ", which is an internal type");
  253. }
  254. // Try to decode object using fields
  255. if (value.getType() == JsonType.NULL) {
  256. return null;
  257. } else if (targetType == byte.class || targetType == Byte.class) {
  258. return Byte.valueOf((byte) value.asNumber());
  259. } else if (targetType == char.class || targetType == Character.class) {
  260. return Character.valueOf(value.asString().charAt(0));
  261. } else if (targetType instanceof Class<?>
  262. && ((Class<?>) targetType).isArray()) {
  263. // Legacy Object[] and String[] handled elsewhere, this takes care
  264. // of generic arrays
  265. Class<?> componentType = ((Class<?>) targetType).getComponentType();
  266. return decodeArray(componentType, (JsonArray) value,
  267. connectorTracker);
  268. } else if (targetType instanceof GenericArrayType) {
  269. Type componentType = ((GenericArrayType) targetType)
  270. .getGenericComponentType();
  271. return decodeArray(componentType, (JsonArray) value,
  272. connectorTracker);
  273. } else if (JsonValue.class.isAssignableFrom(getClassForType(targetType))) {
  274. return value;
  275. } else if (Enum.class.isAssignableFrom(getClassForType(targetType))) {
  276. Class<?> classForType = getClassForType(targetType);
  277. return decodeEnum(classForType.asSubclass(Enum.class),
  278. (JsonString) value);
  279. } else if (customSerializers.containsKey(getClassForType(targetType))) {
  280. return customSerializers.get(getClassForType(targetType))
  281. .deserialize(targetType, value, connectorTracker);
  282. } else {
  283. return decodeObject(targetType, (JsonObject) value,
  284. connectorTracker);
  285. }
  286. }
  287. private static Object decodeArray(Type componentType, JsonArray value,
  288. ConnectorTracker connectorTracker) {
  289. Class<?> componentClass = getClassForType(componentType);
  290. Object array = Array.newInstance(componentClass, value.length());
  291. for (int i = 0; i < value.length(); i++) {
  292. Object decodedValue = decodeInternalOrCustomType(componentType,
  293. value.get(i), connectorTracker);
  294. Array.set(array, i, decodedValue);
  295. }
  296. return array;
  297. }
  298. /**
  299. * Decodes a value that is of an internal type.
  300. * <p>
  301. * Ensures the encoded value is of the same type as target type.
  302. * </p>
  303. * <p>
  304. * Allows restricting collections so that they must be declared using
  305. * generics. If this is used then all objects in the collection are encoded
  306. * using the declared type. Otherwise only internal types are allowed in
  307. * collections.
  308. * </p>
  309. *
  310. * @param targetType
  311. * The type that should be returned by this method
  312. * @param valueAndType
  313. * The encoded value and type array
  314. * @param application
  315. * A reference to the application
  316. * @param enforceGenericsInCollections
  317. * true if generics should be enforce, false to only allow
  318. * internal types in collections
  319. * @return
  320. */
  321. public static Object decodeInternalType(Type targetType,
  322. boolean restrictToInternalTypes, JsonValue encodedJsonValue,
  323. ConnectorTracker connectorTracker) {
  324. if (!isInternalType(targetType)) {
  325. throw new JsonException("Type " + targetType
  326. + " is not a supported internal type.");
  327. }
  328. String transportType = getInternalTransportType(targetType);
  329. if (encodedJsonValue.getType() == JsonType.NULL) {
  330. return null;
  331. } else if (targetType == Void.class) {
  332. throw new JsonException(
  333. "Something other than null was encoded for a null type");
  334. }
  335. // UidlValue
  336. if (targetType == UidlValue.class) {
  337. return decodeUidlValue((JsonArray) encodedJsonValue,
  338. connectorTracker);
  339. }
  340. // Collections
  341. if (JsonConstants.VTYPE_LIST.equals(transportType)) {
  342. return decodeList(targetType, restrictToInternalTypes,
  343. (JsonArray) encodedJsonValue, connectorTracker);
  344. } else if (JsonConstants.VTYPE_SET.equals(transportType)) {
  345. return decodeSet(targetType, restrictToInternalTypes,
  346. (JsonArray) encodedJsonValue, connectorTracker);
  347. } else if (JsonConstants.VTYPE_MAP.equals(transportType)) {
  348. return decodeMap(targetType, restrictToInternalTypes,
  349. encodedJsonValue, connectorTracker);
  350. }
  351. // Arrays
  352. if (JsonConstants.VTYPE_ARRAY.equals(transportType)) {
  353. return decodeObjectArray(targetType, (JsonArray) encodedJsonValue,
  354. connectorTracker);
  355. } else if (JsonConstants.VTYPE_STRINGARRAY.equals(transportType)) {
  356. return decodeStringArray((JsonArray) encodedJsonValue);
  357. }
  358. // Special Vaadin types
  359. if (JsonConstants.VTYPE_CONNECTOR.equals(transportType)) {
  360. return connectorTracker.getConnector(encodedJsonValue.asString());
  361. }
  362. // Legacy types
  363. if (JsonConstants.VTYPE_STRING.equals(transportType)) {
  364. return encodedJsonValue.asString();
  365. } else if (JsonConstants.VTYPE_INTEGER.equals(transportType)) {
  366. return (int) encodedJsonValue.asNumber();
  367. } else if (JsonConstants.VTYPE_LONG.equals(transportType)) {
  368. return (long) encodedJsonValue.asNumber();
  369. } else if (JsonConstants.VTYPE_FLOAT.equals(transportType)) {
  370. return (float) encodedJsonValue.asNumber();
  371. } else if (JsonConstants.VTYPE_DOUBLE.equals(transportType)) {
  372. return encodedJsonValue.asNumber();
  373. } else if (JsonConstants.VTYPE_BOOLEAN.equals(transportType)) {
  374. return encodedJsonValue.asBoolean();
  375. }
  376. throw new JsonException("Unknown type " + transportType);
  377. }
  378. private static UidlValue decodeUidlValue(JsonArray encodedJsonValue,
  379. ConnectorTracker connectorTracker) {
  380. String type = encodedJsonValue.getString(0);
  381. Object decodedValue = decodeInternalType(getType(type), true,
  382. encodedJsonValue.get(1), connectorTracker);
  383. return new UidlValue(decodedValue);
  384. }
  385. private static Map<Object, Object> decodeMap(Type targetType,
  386. boolean restrictToInternalTypes, JsonValue jsonMap,
  387. ConnectorTracker connectorTracker) {
  388. if (jsonMap.getType() == JsonType.ARRAY) {
  389. // Client-side has no declared type information to determine
  390. // encoding method for empty maps, so these are handled separately.
  391. // See #8906.
  392. JsonArray jsonArray = (JsonArray) jsonMap;
  393. if (jsonArray.length() == 0) {
  394. return new HashMap<Object, Object>();
  395. }
  396. }
  397. if (!restrictToInternalTypes && targetType instanceof ParameterizedType) {
  398. Type keyType = ((ParameterizedType) targetType)
  399. .getActualTypeArguments()[0];
  400. Type valueType = ((ParameterizedType) targetType)
  401. .getActualTypeArguments()[1];
  402. if (keyType == String.class) {
  403. return decodeStringMap(valueType, (JsonObject) jsonMap,
  404. connectorTracker);
  405. } else if (keyType == Connector.class) {
  406. return decodeConnectorMap(valueType, (JsonObject) jsonMap,
  407. connectorTracker);
  408. } else {
  409. return decodeObjectMap(keyType, valueType, (JsonArray) jsonMap,
  410. connectorTracker);
  411. }
  412. } else {
  413. return decodeStringMap(UidlValue.class, (JsonObject) jsonMap,
  414. connectorTracker);
  415. }
  416. }
  417. private static Map<Object, Object> decodeObjectMap(Type keyType,
  418. Type valueType, JsonArray jsonMap, ConnectorTracker connectorTracker)
  419. {
  420. JsonArray keys = jsonMap.getArray(0);
  421. JsonArray values = jsonMap.getArray(1);
  422. assert (keys.length() == values.length());
  423. Map<Object, Object> map = new HashMap<Object, Object>(keys.length() * 2);
  424. for (int i = 0; i < keys.length(); i++) {
  425. Object key = decodeInternalOrCustomType(keyType, keys.get(i),
  426. connectorTracker);
  427. Object value = decodeInternalOrCustomType(valueType, values.get(i),
  428. connectorTracker);
  429. map.put(key, value);
  430. }
  431. return map;
  432. }
  433. private static Map<Object, Object> decodeConnectorMap(Type valueType,
  434. JsonObject jsonMap, ConnectorTracker connectorTracker) {
  435. Map<Object, Object> map = new HashMap<Object, Object>();
  436. for (String key : jsonMap.keys()) {
  437. Object value = decodeInternalOrCustomType(valueType,
  438. jsonMap.get(key), connectorTracker);
  439. if (valueType == UidlValue.class) {
  440. value = ((UidlValue) value).getValue();
  441. }
  442. map.put(connectorTracker.getConnector(key), value);
  443. }
  444. return map;
  445. }
  446. private static Map<Object, Object> decodeStringMap(Type valueType,
  447. JsonObject jsonMap, ConnectorTracker connectorTracker) {
  448. Map<Object, Object> map = new HashMap<Object, Object>();
  449. for (String key : jsonMap.keys()) {
  450. Object value = decodeInternalOrCustomType(valueType,
  451. jsonMap.get(key), connectorTracker);
  452. if (valueType == UidlValue.class) {
  453. value = ((UidlValue) value).getValue();
  454. }
  455. map.put(key, value);
  456. }
  457. return map;
  458. }
  459. /**
  460. * @param targetType
  461. * @param restrictToInternalTypes
  462. * @param typeIndex
  463. * The index of a generic type to use to define the child type
  464. * that should be decoded
  465. * @param encodedValueAndType
  466. * @param application
  467. * @return
  468. */
  469. private static Object decodeParametrizedType(Type targetType,
  470. boolean restrictToInternalTypes, int typeIndex, JsonValue value,
  471. ConnectorTracker connectorTracker) {
  472. if (!restrictToInternalTypes && targetType instanceof ParameterizedType) {
  473. Type childType = ((ParameterizedType) targetType)
  474. .getActualTypeArguments()[typeIndex];
  475. // Only decode the given type
  476. return decodeInternalOrCustomType(childType, value,
  477. connectorTracker);
  478. } else {
  479. // Only UidlValue when not enforcing a given type to avoid security
  480. // issues
  481. UidlValue decodeInternalType = (UidlValue) decodeInternalType(
  482. UidlValue.class, true, value, connectorTracker);
  483. return decodeInternalType.getValue();
  484. }
  485. }
  486. private static Object decodeEnum(Class<? extends Enum> cls, JsonString value) {
  487. return Enum.valueOf(cls, value.getString());
  488. }
  489. private static String[] decodeStringArray(JsonArray jsonArray) {
  490. int length = jsonArray.length();
  491. List<String> tokens = new ArrayList<String>(length);
  492. for (int i = 0; i < length; ++i) {
  493. tokens.add(jsonArray.getString(i));
  494. }
  495. return tokens.toArray(new String[tokens.size()]);
  496. }
  497. private static Object[] decodeObjectArray(Type targetType,
  498. JsonArray jsonArray, ConnectorTracker connectorTracker) {
  499. List<Object> list = decodeList(List.class, true, jsonArray,
  500. connectorTracker);
  501. return list.toArray(new Object[list.size()]);
  502. }
  503. private static List<Object> decodeList(Type targetType,
  504. boolean restrictToInternalTypes, JsonArray jsonArray,
  505. ConnectorTracker connectorTracker) {
  506. int arrayLength = jsonArray.length();
  507. List<Object> list = new ArrayList<Object>(arrayLength);
  508. for (int i = 0; i < arrayLength; ++i) {
  509. // each entry always has two elements: type and value
  510. JsonValue encodedValue = jsonArray.get(i);
  511. Object decodedChild = decodeParametrizedType(targetType,
  512. restrictToInternalTypes, 0, encodedValue, connectorTracker);
  513. list.add(decodedChild);
  514. }
  515. return list;
  516. }
  517. private static Set<Object> decodeSet(Type targetType,
  518. boolean restrictToInternalTypes, JsonArray jsonArray,
  519. ConnectorTracker connectorTracker) {
  520. HashSet<Object> set = new HashSet<Object>();
  521. set.addAll(decodeList(targetType, restrictToInternalTypes, jsonArray,
  522. connectorTracker));
  523. return set;
  524. }
  525. private static Object decodeObject(Type targetType,
  526. JsonObject serializedObject, ConnectorTracker connectorTracker) {
  527. Class<?> targetClass = getClassForType(targetType);
  528. try {
  529. Object decodedObject = targetClass.newInstance();
  530. for (BeanProperty property : getProperties(targetClass)) {
  531. String fieldName = property.getName();
  532. JsonValue encodedFieldValue = serializedObject.get(fieldName);
  533. Type fieldType = property.getType();
  534. Object decodedFieldValue = decodeInternalOrCustomType(
  535. fieldType, encodedFieldValue, connectorTracker);
  536. property.setValue(decodedObject, decodedFieldValue);
  537. }
  538. return decodedObject;
  539. } catch (Exception e) {
  540. throw new RuntimeException(e);
  541. }
  542. }
  543. public static EncodeResult encode(Object value, JsonValue diffState,
  544. Type valueType, ConnectorTracker connectorTracker) {
  545. if (null == value) {
  546. return ENCODE_RESULT_NULL;
  547. }
  548. // Storing a single reference and only returning the EncodeResult at the
  549. // end the method is much shorter in bytecode which allows inlining
  550. JsonValue toReturn;
  551. if (value instanceof JsonValue) {
  552. // all JSON compatible types are returned as is.
  553. toReturn = (JsonValue) value;
  554. } else if (value instanceof String) {
  555. toReturn = Json.create((String) value);
  556. } else if (value instanceof Boolean) {
  557. toReturn = Json.create((Boolean) value);
  558. } else if (value instanceof Number) {
  559. toReturn = Json.create(((Number) value).doubleValue());
  560. } else if (value instanceof Character) {
  561. toReturn = Json.create(Character.toString((Character) value));
  562. } else if (value instanceof String[]) {
  563. toReturn = toJsonArray((String[]) value);
  564. } else if (value instanceof Collection) {
  565. toReturn = encodeCollection(valueType, (Collection<?>) value,
  566. connectorTracker);
  567. } else if (value instanceof Map) {
  568. toReturn = encodeMap(valueType, (Map<?, ?>) value, connectorTracker);
  569. } else if (value instanceof Connector) {
  570. if (value instanceof Component
  571. && !(LegacyCommunicationManager
  572. .isComponentVisibleToClient((Component) value))) {
  573. // an encoded null is cached, return it directly.
  574. return ENCODE_RESULT_NULL;
  575. }
  576. // Connectors are simply serialized as ID.
  577. toReturn = Json.create(((Connector) value).getConnectorId());
  578. } else if (value instanceof Enum) {
  579. toReturn = Json.create(((Enum<?>) value).name());
  580. } else if (customSerializers.containsKey(value.getClass())) {
  581. toReturn = serializeJson(value, connectorTracker);
  582. } else if (valueType instanceof GenericArrayType) {
  583. toReturn = encodeArrayContents(
  584. ((GenericArrayType) valueType).getGenericComponentType(),
  585. value, connectorTracker);
  586. } else if (valueType instanceof Class<?>) {
  587. if (((Class<?>) valueType).isArray()) {
  588. toReturn = encodeArrayContents(
  589. ((Class<?>) valueType).getComponentType(), value,
  590. connectorTracker);
  591. } else {
  592. // encodeObject returns an EncodeResult with a diff, thus it
  593. // needs to return it directly rather than assigning it to
  594. // toReturn.
  595. return encodeObject(value, (Class<?>) valueType,
  596. (JsonObject) diffState, connectorTracker);
  597. }
  598. } else {
  599. throw new JsonException("Can not encode type " + valueType);
  600. }
  601. return new EncodeResult(toReturn);
  602. }
  603. public static Collection<BeanProperty> getProperties(Class<?> type)
  604. throws IntrospectionException {
  605. Collection<BeanProperty> cachedProperties = typePropertyCache.get(type);
  606. if (cachedProperties != null) {
  607. return cachedProperties;
  608. }
  609. Collection<BeanProperty> properties = new ArrayList<BeanProperty>();
  610. properties.addAll(MethodProperty.find(type));
  611. properties.addAll(FieldProperty.find(type));
  612. // Doesn't matter if the same calculation is done multiple times from
  613. // different threads, so there's no need to do e.g. putIfAbsent
  614. typePropertyCache.put(type, properties);
  615. return properties;
  616. }
  617. /*
  618. * Loops through the fields of value and encodes them.
  619. */
  620. private static EncodeResult encodeObject(Object value, Class<?> valueType,
  621. JsonObject referenceValue, ConnectorTracker connectorTracker) {
  622. JsonObject encoded = Json.createObject();
  623. JsonObject diff = Json.createObject();
  624. try {
  625. for (BeanProperty property : getProperties(valueType)) {
  626. String fieldName = property.getName();
  627. // We can't use PropertyDescriptor.getPropertyType() as it does
  628. // not support generics
  629. Type fieldType = property.getType();
  630. Object fieldValue = property.getValue(value);
  631. if (encoded.hasKey(fieldName)) {
  632. throw new RuntimeException(
  633. "Can't encode "
  634. + valueType.getName()
  635. + " as it has multiple properties with the name "
  636. + fieldName.toLowerCase()
  637. + ". This can happen if there are getters and setters for a public field (the framework can't know which to ignore) or if there are properties with only casing distinguishing between the names (e.g. getFoo() and getFOO())");
  638. }
  639. JsonValue fieldReference;
  640. if (referenceValue != null) {
  641. fieldReference = referenceValue.get(fieldName);
  642. if (fieldReference instanceof JsonNull) {
  643. fieldReference = null;
  644. }
  645. } else {
  646. fieldReference = null;
  647. }
  648. EncodeResult encodeResult = encode(fieldValue, fieldReference,
  649. fieldType, connectorTracker);
  650. encoded.put(fieldName, encodeResult.getEncodedValue());
  651. if (!jsonEquals(encodeResult.getEncodedValue(), fieldReference)) {
  652. diff.put(fieldName, encodeResult.getDiffOrValue());
  653. }
  654. }
  655. } catch (Exception e) {
  656. // TODO: Should exceptions be handled in a different way?
  657. throw new RuntimeException(e);
  658. }
  659. return new EncodeResult(encoded, diff);
  660. }
  661. /**
  662. * Compares the value with the reference. If they match, returns true.
  663. *
  664. * @param fieldValue
  665. * @param referenceValue
  666. * @return
  667. */
  668. private static boolean jsonEquals(JsonValue fieldValue, JsonValue referenceValue) {
  669. if (fieldValue instanceof JsonNull) {
  670. fieldValue = null;
  671. }
  672. if (fieldValue == referenceValue) {
  673. return true;
  674. } else if (fieldValue == null || referenceValue == null) {
  675. return false;
  676. } else {
  677. return fieldValue.jsEquals(referenceValue);
  678. }
  679. }
  680. private static JsonArray encodeArrayContents(Type componentType,
  681. Object array, ConnectorTracker connectorTracker) {
  682. JsonArray jsonArray = Json.createArray();
  683. for (int i = 0; i < Array.getLength(array); i++) {
  684. EncodeResult encodeResult = encode(Array.get(array, i), null,
  685. componentType, connectorTracker);
  686. jsonArray.set(i, encodeResult.getEncodedValue());
  687. }
  688. return jsonArray;
  689. }
  690. private static JsonArray encodeCollection(Type targetType,
  691. Collection<?> collection, ConnectorTracker connectorTracker) {
  692. JsonArray jsonArray = Json.createArray();
  693. for (Object o : collection) {
  694. jsonArray.set(jsonArray.length(), encodeChild(targetType, 0, o, connectorTracker));
  695. }
  696. return jsonArray;
  697. }
  698. private static JsonValue encodeChild(Type targetType, int typeIndex, Object o,
  699. ConnectorTracker connectorTracker) {
  700. if (targetType instanceof ParameterizedType) {
  701. Type childType = ((ParameterizedType) targetType)
  702. .getActualTypeArguments()[typeIndex];
  703. // Encode using the given type
  704. EncodeResult encodeResult = encode(o, null, childType,
  705. connectorTracker);
  706. return encodeResult.getEncodedValue();
  707. } else {
  708. throw new JsonException("Collection is missing generics");
  709. }
  710. }
  711. private static JsonValue encodeMap(Type mapType, Map<?, ?> map,
  712. ConnectorTracker connectorTracker) {
  713. Type keyType, valueType;
  714. if (mapType instanceof ParameterizedType) {
  715. keyType = ((ParameterizedType) mapType).getActualTypeArguments()[0];
  716. valueType = ((ParameterizedType) mapType).getActualTypeArguments()[1];
  717. } else {
  718. throw new JsonException("Map is missing generics");
  719. }
  720. if (map.isEmpty()) {
  721. // Client -> server encodes empty map as an empty array because of
  722. // #8906. Do the same for server -> client to maintain symmetry.
  723. return EMPTY_JSON_ARRAY;
  724. }
  725. if (keyType == String.class) {
  726. return encodeStringMap(valueType, map, connectorTracker);
  727. } else if (keyType == Connector.class) {
  728. return encodeConnectorMap(valueType, map, connectorTracker);
  729. } else {
  730. return encodeObjectMap(keyType, valueType, map, connectorTracker);
  731. }
  732. }
  733. private static JsonArray encodeObjectMap(Type keyType, Type valueType,
  734. Map<?, ?> map, ConnectorTracker connectorTracker) {
  735. JsonArray keys = Json.createArray();
  736. JsonArray values = Json.createArray();
  737. for (Entry<?, ?> entry : map.entrySet()) {
  738. EncodeResult encodedKey = encode(entry.getKey(), null, keyType,
  739. connectorTracker);
  740. EncodeResult encodedValue = encode(entry.getValue(), null,
  741. valueType, connectorTracker);
  742. keys.set(keys.length(), encodedKey.getEncodedValue());
  743. values.set(values.length(), encodedValue.getEncodedValue());
  744. }
  745. JsonArray jsonMap = Json.createArray();
  746. jsonMap.set(0, keys);
  747. jsonMap.set(1, values);
  748. return jsonMap;
  749. }
  750. /*
  751. * Encodes a connector map. Invisible connectors are skipped.
  752. */
  753. private static JsonObject encodeConnectorMap(Type valueType, Map<?, ?> map,
  754. ConnectorTracker connectorTracker) {
  755. JsonObject jsonMap = Json.createObject();
  756. for (Entry<?, ?> entry : map.entrySet()) {
  757. ClientConnector key = (ClientConnector) entry.getKey();
  758. if (LegacyCommunicationManager.isConnectorVisibleToClient(key)) {
  759. EncodeResult encodedValue = encode(entry.getValue(), null,
  760. valueType, connectorTracker);
  761. jsonMap.put(key.getConnectorId(),
  762. encodedValue.getEncodedValue());
  763. }
  764. }
  765. return jsonMap;
  766. }
  767. private static JsonObject encodeStringMap(Type valueType, Map<?, ?> map,
  768. ConnectorTracker connectorTracker) {
  769. JsonObject jsonMap = Json.createObject();
  770. for (Entry<?, ?> entry : map.entrySet()) {
  771. String key = (String) entry.getKey();
  772. EncodeResult encodedValue = encode(entry.getValue(), null,
  773. valueType, connectorTracker);
  774. jsonMap.put(key, encodedValue.getEncodedValue());
  775. }
  776. return jsonMap;
  777. }
  778. /*
  779. * These methods looks good to inline, but are on a cold path of the
  780. * otherwise hot encode method, which needed to be shorted to allow inlining
  781. * of the hot part.
  782. */
  783. private static String getInternalTransportType(Type valueType) {
  784. return typeToTransportType.get(getClassForType(valueType));
  785. }
  786. private static JsonValue serializeJson(Object value,
  787. ConnectorTracker connectorTracker) {
  788. JsonSerializer serializer = customSerializers.get(value.getClass());
  789. return serializer.serialize(value, connectorTracker);
  790. }
  791. private static JsonArray toJsonArray(String[] array) {
  792. JsonArray jsonArray = Json.createArray();
  793. for (int i = 0; i < array.length; ++i) {
  794. jsonArray.set(i, array[i]);
  795. }
  796. return jsonArray;
  797. }
  798. }