From: Leif Åstrand Date: Wed, 30 May 2012 13:23:49 +0000 (+0300) Subject: Use UidlValue for sending legacy maps and arrays (#8878) X-Git-Tag: 7.0.0.alpha3~207 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4d8a33b2ad876e607fb6b24b52e3d5dddc5309dd;p=vaadin-framework.git Use UidlValue for sending legacy maps and arrays (#8878) --- diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java index edd900174a..20ed319253 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java @@ -88,8 +88,8 @@ public class JsonEncoder { if (restrictToInternalTypes) { // Enums are encoded as strings in Vaadin 6 so we still do that // for backwards copmatibility. - return encode(value.toString(), restrictToInternalTypes, - connectorMap, connection); + return encode(new UidlValue(value.toString()), + restrictToInternalTypes, connectorMap, connection); } else { Enum e = (Enum) value; return encodeEnum(e, connectorMap, connection); @@ -140,6 +140,15 @@ public class JsonEncoder { JSONObject jsonMap = new JSONObject(); for (Object mapKey : map.keySet()) { Object mapValue = map.get(mapKey); + if (restrictToInternalTypes) { + if (!(mapKey instanceof String)) { + throw new IllegalStateException( + "Only string keys supported for legacy maps"); + } + // Wrap in UidlValue to send explicit type info + mapKey = new UidlValue(mapKey); + mapValue = new UidlValue(mapValue); + } JSONValue encodedKey = encode(mapKey, restrictToInternalTypes, connectorMap, connection); JSONValue encodedValue = encode(mapValue, restrictToInternalTypes, @@ -161,9 +170,13 @@ public class JsonEncoder { JSONArray jsonArray = new JSONArray(); for (int i = 0; i < array.length; ++i) { // TODO handle object graph loops? + Object value = array[i]; + if (restrictToInternalTypes) { + value = new UidlValue(value); + } jsonArray.set( i, - encode(array[i], restrictToInternalTypes, connectorMap, + encode(value, restrictToInternalTypes, connectorMap, connection)); } return combineTypeAndValue(VTYPE_ARRAY, jsonArray); diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index 5ead96c42e..11e86b99fc 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -1632,7 +1632,8 @@ public abstract class AbstractCommunicationManager implements Serializable { .decodeInternalType(String.class, true, parametersJson.getJSONArray(0), application); UidlValue uidlValue = (UidlValue) JsonCodec.decodeInternalType( - parametersJson.getJSONArray(1), application); + UidlValue.class, true, parametersJson.getJSONArray(1), + application); Object value = uidlValue.getValue(); diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index 9c7abc53b4..a7515c7135 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -96,31 +96,10 @@ public class JsonCodec implements Serializable { } } - public static String getTransportType(JSONArray encodedValue) - throws JSONException { - return encodedValue.getString(0); - } - private static Class getType(String transportType) { return transportTypeToType.get(transportType); } - /** - * Decodes the given value and type, restricted to using only internal - * types. - * - * @param valueAndType - * @param application - * @throws JSONException - */ - @Deprecated - public static Object decodeInternalType(JSONArray valueAndType, - Application application) throws JSONException { - String transportType = getTransportType(valueAndType); - return decodeInternalType(getType(transportType), true, valueAndType, - application); - } - public static Object decodeInternalOrCustomType(Type targetType, JSONArray valueAndType, Application application) throws JSONException { @@ -258,10 +237,10 @@ public class JsonCodec implements Serializable { Application application) throws JSONException { String type = encodedJsonValue.getString(0); - // Fake format used by decode method JSONArray valueAndType = new JSONArray(Arrays.asList(type, encodedJsonValue.get(1))); - Object decodedValue = decodeInternalType(valueAndType, application); + Object decodedValue = decodeInternalType(getType(type), true, + valueAndType, application); return new UidlValue(decodedValue); } @@ -322,9 +301,11 @@ public class JsonCodec implements Serializable { return decodeInternalOrCustomType(childType, encodedValueAndType, application); } else { - // Only internal types when not enforcing a given type to avoid - // security issues - return decodeInternalType(encodedValueAndType, application); + // Only UidlValue when not enforcing a given type to avoid security + // issues + UidlValue decodeInternalType = (UidlValue) decodeInternalType( + UidlValue.class, true, encodedValueAndType, application); + return decodeInternalType.getValue(); } }