Class DataSerializer

java.lang.Object
org.codelibs.fess.crawler.serializer.DataSerializer

public class DataSerializer extends Object
A serializer class for handling object serialization and deserialization.

This class provides serialization capabilities using different serializers, currently supporting Kryo and JavaBin serialization formats. The serializer type is determined by the crawler data serializer configuration.

The class is thread-safe and uses ThreadLocal to maintain Kryo instances per thread to avoid synchronization overhead.

  • Field Details

    • JAVABIN

      protected static final String JAVABIN
      Constant for JavaBin serializer type.
      See Also:
    • KRYO

      protected static final String KRYO
      Constant for Kryo serializer type.
      See Also:
    • kryoThreadLocal

      protected final ThreadLocal<com.esotericsoftware.kryo.Kryo> kryoThreadLocal
      ThreadLocal container for Kryo instances to ensure thread safety.
  • Constructor Details

    • DataSerializer

      public DataSerializer()
      Constructs a new DataSerializer.

      Initializes the ThreadLocal Kryo instances with appropriate configuration. The Kryo instances are configured to require class registration for security, preventing deserialization of arbitrary classes that could lead to RCE vulnerabilities. Only explicitly registered classes can be serialized/deserialized.

  • Method Details

    • registerClasses

      protected void registerClasses(com.esotericsoftware.kryo.Kryo kryo)
      Registers all classes that are allowed for Kryo serialization/deserialization.

      This method registers only the classes that are needed by the crawler data serialization. By explicitly registering classes, we prevent deserialization of arbitrary classes which could lead to remote code execution vulnerabilities through gadget chains.

      Parameters:
      kryo - the Kryo instance to register classes with
    • getSerializerType

      protected String getSerializerType()
      Gets the configured serializer type from the Fess configuration.
      Returns:
      the serializer type (either "kryo" or "javabin")
    • fromObjectToBinary

      public byte[] fromObjectToBinary(Object obj)
      Serializes an object to a byte array.

      The serialization method used depends on the configured serializer type. Supported types are Kryo and JavaBin serialization.

      Parameters:
      obj - the object to serialize
      Returns:
      the serialized object as a byte array
      Throws:
      IllegalArgumentException - if an unsupported serializer type is configured
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during serialization
    • fromBinaryToObject

      public Object fromBinaryToObject(byte[] bytes)
      Deserializes a byte array back to an object.

      The deserialization method used depends on the configured serializer type. Supported types are Kryo and JavaBin deserialization.

      Parameters:
      bytes - the byte array to deserialize
      Returns:
      the deserialized object
      Throws:
      IllegalArgumentException - if an unsupported serializer type is configured
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during deserialization
    • serializeWithKryo

      protected byte[] serializeWithKryo(Object obj)
      Serializes an object using Kryo serialization.

      Uses the thread-local Kryo instance to serialize the object along with its class information. The serialized data is written to a byte array output stream.

      Parameters:
      obj - the object to serialize
      Returns:
      the serialized object as a byte array
      Throws:
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during serialization
    • deserializeWithKryo

      protected Object deserializeWithKryo(byte[] bytes)
      Deserializes a byte array using Kryo deserialization.

      Uses the thread-local Kryo instance to read both the class information and object data from the byte array input stream.

      Parameters:
      bytes - the byte array to deserialize
      Returns:
      the deserialized object