]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use UidlValue for sending legacy maps and arrays (#8878)
authorLeif Åstrand <leif@vaadin.com>
Wed, 30 May 2012 13:23:49 +0000 (16:23 +0300)
committerLeif Åstrand <leif@vaadin.com>
Wed, 6 Jun 2012 10:33:38 +0000 (13:33 +0300)
src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java
src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java
src/com/vaadin/terminal/gwt/server/JsonCodec.java

index edd900174a8866165d102b0cbc32f1f532fe2ebf..20ed319253aa68a40ab0a8cf2050ee83857e0174 100644 (file)
@@ -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);
index 5ead96c42e9ba8526110db11c9d4c22852716849..11e86b99fcc68f9eb782ad673a8fd6c450f0fa0a 100644 (file)
@@ -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();
 
index 9c7abc53b466d5b17029a3283d39cebbdbd45f82..a7515c7135c1c02cc1688601c3d294e1f7ea8603 100644 (file)
@@ -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();
         }
     }