Example usage for com.google.gwt.user.server.rpc.impl StandardSerializationPolicy StandardSerializationPolicy

List of usage examples for com.google.gwt.user.server.rpc.impl StandardSerializationPolicy StandardSerializationPolicy

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc.impl StandardSerializationPolicy StandardSerializationPolicy.

Prototype

public StandardSerializationPolicy(Map<Class<?>, Boolean> serializationWhitelist,
        Map<Class<?>, Boolean> deserializationWhitelist, Map<Class<?>, String> obfuscatedTypeIds,
        Map<Class<?>, Set<String>> clientFields) 

Source Link

Document

Constructs a SerializationPolicy from several Map s.

Usage

From source file:com.gdevelop.gwt.syncrpc.RemoteServiceSyncProxy.java

License:Apache License

private void unionizeWhitelists() {
    if (serializationPolicy instanceof StandardSerializationPolicy) {
        try {/*from  w  ww  .ja va 2 s.c  o  m*/
            Field f = StandardSerializationPolicy.class.getDeclaredField("serializationWhitelist");
            f.setAccessible(true);
            Map<Class<?>, Boolean> serializationWhitelist = (Map<Class<?>, Boolean>) f.get(serializationPolicy);
            f = StandardSerializationPolicy.class.getDeclaredField("deserializationWhitelist");
            f.setAccessible(true);
            Map<Class<?>, Boolean> deserializationWhitelist = (Map<Class<?>, Boolean>) f
                    .get(serializationPolicy);
            f = StandardSerializationPolicy.class.getDeclaredField("typeIds");
            f.setAccessible(true);
            Map<Class<?>, String> obfuscatedTypeIds = (Map<Class<?>, String>) f.get(serializationPolicy);
            f = StandardSerializationPolicy.class.getDeclaredField("clientFields");
            f.setAccessible(true);
            Map<Class<?>, Set<String>> clientFields = (Map<Class<?>, Set<String>>) f.get(serializationPolicy);
            serializationWhitelist.putAll(deserializationWhitelist);
            deserializationWhitelist = serializationWhitelist;
            serializationPolicy = new StandardSerializationPolicy(serializationWhitelist,
                    deserializationWhitelist, obfuscatedTypeIds, clientFields);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.gdevelop.gwt.syncrpc.SerializationPolicyLoader.java

License:Apache License

/**
 * Loads a SerializationPolicy from an input stream and optionally record
 * any {@link ClassNotFoundException}s.//w w  w . j ava 2s  .c om
 * 
 * @param inputStream
 *            stream to load the SerializationPolicy from.
 * @param classNotFoundExceptions
 *            if not <code>null</code>, all of the
 *            {@link ClassNotFoundException}s thrown while loading this
 *            serialization policy will be added to this list
 * @return a {@link SerializationPolicy} loaded from the input stream.
 * 
 * @throws IOException
 *             if an error occurs while reading the stream
 * @throws ParseException
 *             if the input stream is not properly formatted
 */
public static SerializationPolicy loadFromStream(InputStream inputStream,
        List<ClassNotFoundException> classNotFoundExceptions) throws IOException, ParseException {

    if (inputStream == null) {
        throw new NullPointerException("inputStream");
    }

    Map<Class<?>, Boolean> whitelistSer = new HashMap<Class<?>, Boolean>();
    Map<Class<?>, Boolean> whitelistDeser = new HashMap<Class<?>, Boolean>();
    Map<Class<?>, String> typeIds = new HashMap<Class<?>, String>();
    Map<Class<?>, Set<String>> clientFields = new HashMap<Class<?>, Set<String>>();

    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

    InputStreamReader isr = new InputStreamReader(inputStream, SERIALIZATION_POLICY_FILE_ENCODING);
    BufferedReader br = new BufferedReader(isr);

    String line = br.readLine();
    int lineNum = 1;
    while (line != null) {
        line = line.trim();
        if (line.length() > 0) {
            String[] components = line.split(",");

            if (components[0].equals(CLIENT_FIELDS_KEYWORD)) {
                /*
                 * Lines starting with '@ClientFields' list potentially
                 * serializable fields known to client code for classes that
                 * may be enhanced with additional fields on the server. If
                 * additional server fields are found, they will be
                 * serizalized separately from the normal RPC process and
                 * transmitted to the client as an opaque blob of data
                 * stored in a WeakMapping associated with the object
                 * instance.
                 */
                String binaryTypeName = components[1].trim();
                Class<?> clazz;
                try {
                    clazz = Class.forName(binaryTypeName, false, contextClassLoader);
                    HashSet<String> fieldNames = new HashSet<String>();
                    for (int i = 2; i < components.length; i++) {
                        fieldNames.add(components[i]);
                    }
                    clientFields.put(clazz, fieldNames);
                } catch (ClassNotFoundException ex) {
                    // Ignore the error, but add it to the list of errors if
                    // one was
                    // provided.
                    if (classNotFoundExceptions != null) {
                        classNotFoundExceptions.add(ex);
                    }
                }
            } else {
                if (components.length != 2 && components.length != 7) {
                    throw new ParseException(FORMAT_ERROR_MESSAGE, lineNum);
                }

                for (int i = 0; i < components.length; i++) {
                    components[i] = components[i].trim();
                    if (components[i].length() == 0) {
                        throw new ParseException(FORMAT_ERROR_MESSAGE, lineNum);
                    }
                }

                String binaryTypeName = components[0].trim();
                boolean fieldSer;
                boolean instantSer;
                boolean fieldDeser;
                boolean instantDeser;
                String typeId;

                if (components.length == 2) {
                    fieldSer = fieldDeser = true;
                    instantSer = instantDeser = Boolean.valueOf(components[1]);
                    typeId = binaryTypeName;
                } else {
                    int idx = 1;
                    // TODO: Validate the instantiable string better.
                    fieldSer = Boolean.valueOf(components[idx++]);
                    instantSer = Boolean.valueOf(components[idx++]);
                    fieldDeser = Boolean.valueOf(components[idx++]);
                    instantDeser = Boolean.valueOf(components[idx++]);
                    typeId = components[idx++];

                    if (!fieldSer && !fieldDeser && !TypeNameObfuscator.SERVICE_INTERFACE_ID.equals(typeId)) {
                        throw new ParseException("Type " + binaryTypeName
                                + " is neither field serializable, field deserializable "
                                + "nor the service interface", lineNum);
                    }
                }

                try {
                    Class<?> clazz = Class.forName(binaryTypeName, false, contextClassLoader);
                    if (fieldSer) {
                        whitelistSer.put(clazz, instantSer);
                    }
                    if (fieldDeser) {
                        whitelistDeser.put(clazz, instantDeser);
                    }
                    typeIds.put(clazz, typeId);
                } catch (ClassNotFoundException ex) {
                    // Ignore the error, but add it to the list of errors if
                    // one was
                    // provided.
                    if (classNotFoundExceptions != null) {
                        classNotFoundExceptions.add(ex);
                    }
                }
            }
        }

        line = br.readLine();
        lineNum++;
    }
    /****************************************
     * HERE'S THE CHANGE FROM THE ORIGINAL Deser and Ser are swapped because
     * we are the client side
     */
    return new StandardSerializationPolicy(whitelistDeser, whitelistSer, typeIds, clientFields);
}