Class ConverterService


  • public class ConverterService
    extends Service
    Application service converting between representation and regular Java objects. The conversion can work in both directions. Actual converters can be plugged into the engine to support this service.

    Root object classes used for conversion shouldn't be generic classes otherwise important contextual type information will be missing at runtime due to Java type erasure mechanism. If needed, create a fully resolved subclasses and/or a container classes.
    Author:
    Jerome Louvel
    • Constructor Detail

      • ConverterService

        public ConverterService()
        Constructor.
      • ConverterService

        public ConverterService​(boolean enabled)
        Constructor.
        Parameters:
        enabled - True if the service has been enabled.
    • Method Detail

      • applyPatch

        public Representation applyPatch​(Representation initial,
                                         Representation patch)
                                  throws java.io.IOException
        Applies a patch representation to an initial representation in order to obtain a modified one. The patch must have a recognized media type in order for the ConverterService to be able to process it.
        Parameters:
        initial - The initial representation on which the patch must be applied.
        patch - The patch representation to apply.
        Returns:
        The modified representation.
        Throws:
        java.io.IOException
      • createPatch

        public Representation createPatch​(Representation initial,
                                          Representation modified)
                                   throws java.io.IOException
        Creates a patch representation by calculating a diff between initial and modified representations.
        Parameters:
        initial - The initial representation.
        modified - The modified representation.
        Returns:
        The patch representation able to convert the initial representation into the modified representation.
        Throws:
        java.io.IOException
      • getObjectClasses

        public java.util.List<java.lang.Class<?>> getObjectClasses​(Variant source)
        Returns the list of object classes that can be converted from a given variant.
        Parameters:
        source - The source variant.
        Returns:
        The list of object class that can be converted.
      • getPatchTypes

        public java.util.List<MediaType> getPatchTypes​(MediaType representationType)
        Returns the list of patch media types available for the given representation types.
        Parameters:
        representationType - The representation media type or null for all supported patch types.
        Returns:
        The list of patch media types available.
      • getVariants

        public java.util.List<? extends Variant> getVariants​(java.lang.Class<?> source,
                                                             Variant target)
                                                      throws java.io.IOException
        Returns the list of variants that can be converted from a given object class.
        Parameters:
        source - The source class.
        target - The expected representation metadata.
        Returns:
        The list of variants that can be converted.
        Throws:
        java.io.IOException
      • revertPatch

        public Representation revertPatch​(Representation modified,
                                          Representation patch)
                                   throws java.io.IOException
        Reverts a patch representation from a modified representation in order to obtain the initial one. The patch must have a recognized media type in order for the ConverterService to be able to process it.
        Parameters:
        modified - The modified representation from which the patch must be reverted.
        patch - The patch representation to revert.
        Returns:
        The initial representation.
        Throws:
        java.io.IOException
      • toObject

        public java.lang.Object toObject​(Representation source)
                                  throws java.io.IOException
        Converts a Representation into a regular Java object.
        Parameters:
        source - The source representation to convert.
        Returns:
        The converted Java object.
        Throws:
        java.io.IOException
      • toObject

        public <T> T toObject​(Representation source,
                              java.lang.Class<T> target,
                              Resource resource)
                       throws java.io.IOException
        Converts a Representation into a regular Java object.
        Type Parameters:
        T - The expected class of the Java object.
        Parameters:
        source - The source representation to convert.
        target - The target class of the Java object.
        resource - The parent resource.
        Returns:
        The converted Java object.
        Throws:
        java.io.IOException
      • toRepresentation

        public Representation toRepresentation​(java.lang.Object source)
                                        throws java.io.IOException
        Converts a regular Java object into a Representation. The converter will use the preferred variant of the selected converter.
        Parameters:
        source - The source object to convert.
        Returns:
        The converted representation.
        Throws:
        java.io.IOException
      • toRepresentation

        public Representation toRepresentation​(java.lang.Object source,
                                               MediaType target)
                                        throws java.io.IOException
        Converts a regular Java object into a Representation.
        Parameters:
        source - The source object to convert.
        target - The target representation media type.
        Returns:
        The converted representation.
        Throws:
        java.io.IOException
      • toRepresentation

        public Representation toRepresentation​(java.lang.Object source,
                                               Variant target)
                                        throws java.io.IOException
        Converts a regular Java object into a Representation.
        Parameters:
        source - The source object to convert.
        target - The target representation variant.
        Returns:
        The converted representation.
        Throws:
        java.io.IOException
      • toRepresentation

        public Representation toRepresentation​(java.lang.Object source,
                                               Variant target,
                                               Resource resource)
                                        throws java.io.IOException
        Converts a regular Java object into a Representation.
        Parameters:
        source - The source object to convert.
        target - The target representation variant.
        resource - The parent resource.
        Returns:
        The converted representation.
        Throws:
        java.io.IOException
      • updatePreferences

        public void updatePreferences​(java.util.List<Preference<MediaType>> preferences,
                                      java.lang.Class<?> entity)
        Updates the media type preferences with available conversion capabilities for the given entity class.
        Parameters:
        preferences - The media type preferences.
        entity - The entity class to convert.