com.github.lontime.extpac4j.impl.UserProfileSerializer Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extpac4j.impl;
import java.io.IOException;
import java.util.Map;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.serial.GenericObjectSerializer;
import com.github.lontime.base.serial.MapSerializers;
import com.github.lontime.base.serial.MsgpackObjectSerializer;
import com.github.lontime.shaded.com.twitter.serial.serializer.CoreSerializers;
import com.github.lontime.shaded.com.twitter.serial.serializer.SerializationContext;
import com.github.lontime.shaded.com.twitter.serial.stream.SerializerInput;
import com.github.lontime.shaded.com.twitter.serial.stream.SerializerOutput;
import org.pac4j.core.profile.BasicUserProfile;
/**
* UserProfileSerializer.
*
* @param T
* @author lontime
* @since 1.0
*/
public abstract class UserProfileSerializer extends MsgpackObjectSerializer {
/**
* createInstance.
*
* @return T
*/
public abstract T createInstance();
@Override
protected void serializeObject(SerializationContext context, SerializerOutput output, T profile) throws IOException {
output.writeString(profile.getId());
output.writeObject(context, profile.getAttributes(), MapSerializers.getMsgpackValue(
CoreSerializers.STRING, GenericObjectSerializer.INSTANCE));
output.writeObject(context, profile.getAuthenticationAttributes(),
MapSerializers.getMsgpackValue(CoreSerializers.STRING,
GenericObjectSerializer.INSTANCE));
output.writeBoolean(profile.isRemembered());
}
@Override
protected T deserializeObject(SerializationContext context, SerializerInput input, int versionNumber) throws IOException, ClassNotFoundException {
final T profile = createInstance();
profile.setId(input.readString());
final Map map = input.readObject(context, MapSerializers.getMsgpackValue(
CoreSerializers.STRING, GenericObjectSerializer.INSTANCE));
if (CollectionHelper.isNotEmpty(map)) {
profile.addAttributes(map);
}
final Map map2 = input.readObject(context, MapSerializers.getMsgpackValue(
CoreSerializers.STRING, GenericObjectSerializer.INSTANCE));
if (CollectionHelper.isNotEmpty(map2)) {
profile.addAuthenticationAttributes(map2);
}
profile.setRemembered(input.readBoolean());
return profile;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy