com.google.gwt.user.server.rpc.impl.StandardSerializationPolicy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.gwt-servlet
Show all versions of org.apache.servicemix.bundles.gwt-servlet
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.server.rpc.impl;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.SerializationPolicy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Standard implementation of a {@link SerializationPolicy}.
*/
public class StandardSerializationPolicy extends SerializationPolicy implements
TypeNameObfuscator {
/**
* Field serializable types are primitives and types on the specified
* whitelist.
*/
private static boolean isFieldSerializable(Class clazz,
Map, Boolean> whitelist) {
if (clazz.isPrimitive()) {
return true;
}
return whitelist.containsKey(clazz);
}
/**
* Instantiable types are primitives and types on the specified whitelist
* which can be instantiated.
*/
private static boolean isInstantiable(Class clazz,
Map, Boolean> whitelist) {
if (clazz.isPrimitive()) {
return true;
}
Boolean instantiable = whitelist.get(clazz);
return (instantiable != null && instantiable);
}
private final Map, Set> clientFields;
private final Map, Boolean> deserializationWhitelist;
private final Map, Boolean> serializationWhitelist;
private final boolean shouldSerializeFinalFields;
private final Map, String> typeIds;
private final Map> typeIdsToClasses = new HashMap>();
/**
* Constructs a {@link SerializationPolicy} from several {@link Map}s.
*/
public StandardSerializationPolicy(
Map, Boolean> serializationWhitelist,
Map, Boolean> deserializationWhitelist,
Map, String> obfuscatedTypeIds) {
this(serializationWhitelist, deserializationWhitelist, obfuscatedTypeIds,
null);
}
/**
* Constructs a {@link SerializationPolicy} from several {@link Map}s.
*/
public StandardSerializationPolicy(
Map, Boolean> serializationWhitelist,
Map, Boolean> deserializationWhitelist,
Map, String> obfuscatedTypeIds,
Map, Set> clientFields) {
this(serializationWhitelist, deserializationWhitelist, obfuscatedTypeIds,
clientFields, false);
}
/**
* Constructs a {@link SerializationPolicy} from several {@link Map}s.
*/
public StandardSerializationPolicy(
Map, Boolean> serializationWhitelist,
Map, Boolean> deserializationWhitelist,
Map, String> obfuscatedTypeIds,
Map, Set> clientFields,
boolean shouldSerializeFinalFields) {
if (serializationWhitelist == null || deserializationWhitelist == null) {
throw new NullPointerException("whitelist");
}
this.serializationWhitelist = serializationWhitelist;
this.deserializationWhitelist = deserializationWhitelist;
this.typeIds = obfuscatedTypeIds;
this.clientFields = clientFields;
this.shouldSerializeFinalFields = shouldSerializeFinalFields;
for (Map.Entry, String> entry : obfuscatedTypeIds.entrySet()) {
assert entry.getKey() != null : "null key";
assert entry.getValue() != null : "null value for "
+ entry.getKey().getName();
assert !typeIdsToClasses.containsKey(entry.getValue()) : "Duplicate type id "
+ entry.getValue();
typeIdsToClasses.put(entry.getValue(), entry.getKey());
}
}
public final String getClassNameForTypeId(String id)
throws SerializationException {
Class clazz = typeIdsToClasses.get(id);
if (clazz == null) {
return null;
}
return clazz.getName();
}
@Override
public Set getClientFieldNamesForEnhancedClass(Class clazz) {
if (clientFields == null) {
return null;
}
Set fieldNames = clientFields.get(clazz);
return fieldNames == null ? null : Collections.unmodifiableSet(fieldNames);
}
public final String getTypeIdForClass(Class clazz)
throws SerializationException {
return typeIds.get(clazz);
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.server.rpc.SerializationPolicy#shouldDerializeFields
* (java.lang.String)
*/
@Override
public boolean shouldDeserializeFields(Class clazz) {
return isFieldSerializable(clazz, deserializationWhitelist);
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.server.rpc.SerializationPolicy#shouldSerializeFields
* (java.lang.String)
*/
@Override
public boolean shouldSerializeFields(Class clazz) {
return isFieldSerializable(clazz, serializationWhitelist);
}
@Override
public boolean shouldSerializeFinalFields() {
return shouldSerializeFinalFields;
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.server.rpc.SerializationPolicy#validateDeserialize(
* java.lang.String)
*/
@Override
public void validateDeserialize(Class clazz) throws SerializationException {
if (!isInstantiable(clazz, deserializationWhitelist)) {
throw new SerializationException(
"Type '"
+ clazz.getName()
+ "' was not included in the set of types which can be deserialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be deserialized.");
}
}
/*
* (non-Javadoc)
*
* @see
* com.google.gwt.user.server.rpc.SerializationPolicy#validateSerialize(java
* .lang.String)
*/
@Override
public void validateSerialize(Class clazz) throws SerializationException {
if (!isInstantiable(clazz, serializationWhitelist)) {
throw new SerializationException(
"Type '"
+ clazz.getName()
+ "' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.");
}
}
}