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 38KB

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