Class JacksonRepresentation<T>

  • Type Parameters:
    T - The type to wrap.

    public class JacksonRepresentation<T>
    extends org.restlet.representation.OutputRepresentation
    Representation based on the Jackson library. It can serialize and deserialize automatically in JSON, JSON binary (Smile), XML, YAML and CSV.

    SECURITY WARNING: Using XML parsers configured to not prevent nor limit document type definition (DTD) entity resolution can expose the parser to an XML Entity Expansion injection attack.
    Author:
    Jerome Louvel
    See Also:
    Jackson project, XML Entity Expansion injection attack
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean XML_EXPANDING_ENTITY_REFS
      True for expanding entity references when parsing XML representations.
      static boolean XML_VALIDATING_DTD
      True for validating DTD documents when parsing XML representations.
      • Fields inherited from class org.restlet.representation.Representation

        UNKNOWN_SIZE
    • Constructor Summary

      Constructors 
      Constructor Description
      JacksonRepresentation​(org.restlet.data.MediaType mediaType, T object)
      Constructor.
      JacksonRepresentation​(org.restlet.representation.Representation representation, java.lang.Class<T> objectClass)
      Constructor.
      JacksonRepresentation​(T object)
      Constructor for the JSON media type.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected com.fasterxml.jackson.dataformat.csv.CsvSchema createCsvSchema​(com.fasterxml.jackson.dataformat.csv.CsvMapper csvMapper)
      Creates a Jackson CSV schema based on a mapper and the current object class.
      protected com.fasterxml.jackson.databind.ObjectMapper createObjectMapper()
      Creates a Jackson object mapper based on a media type.
      protected com.fasterxml.jackson.databind.ObjectReader createObjectReader()
      Creates a Jackson object reader based on a mapper.
      protected com.fasterxml.jackson.databind.ObjectWriter createObjectWriter()
      Creates a Jackson object writer based on a mapper.
      com.fasterxml.jackson.dataformat.csv.CsvSchema getCsvSchema()
      Returns the modifiable Jackson CSV schema.
      T getObject()
      Returns the wrapped object, deserializing the representation with Jackson if necessary.
      java.lang.Class<T> getObjectClass()
      Returns the object class to instantiate.
      com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Returns the modifiable Jackson object mapper.
      com.fasterxml.jackson.databind.ObjectReader getObjectReader()
      Returns the modifiable Jackson object reader.
      com.fasterxml.jackson.databind.ObjectWriter getObjectWriter()
      Returns the modifiable Jackson object writer.
      boolean isExpandingEntityRefs()
      Indicates if the parser will expand entity reference nodes.
      boolean isValidatingDtd()
      Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.
      void setCsvSchema​(com.fasterxml.jackson.dataformat.csv.CsvSchema csvSchema)
      Sets the Jackson CSV schema.
      void setExpandingEntityRefs​(boolean expandEntityRefs)
      Indicates if the parser will expand entity reference nodes.
      void setObject​(T object)
      Sets the object to format.
      void setObjectClass​(java.lang.Class<T> objectClass)
      Sets the object class to instantiate.
      void setObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Sets the Jackson object mapper.
      void setObjectReader​(com.fasterxml.jackson.databind.ObjectReader objectReader)
      Sets the Jackson object reader.
      void setObjectWriter​(com.fasterxml.jackson.databind.ObjectWriter objectWriter)
      Sets the Jackson object writer.
      void setValidatingDtd​(boolean validating)
      Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.
      void write​(java.io.OutputStream outputStream)  
      • Methods inherited from class org.restlet.representation.OutputRepresentation

        getChannel, getStream
      • Methods inherited from class org.restlet.representation.StreamRepresentation

        getReader, write, write
      • Methods inherited from class org.restlet.representation.Representation

        append, exhaust, getAvailableSize, getDigest, getDisposition, getExpirationDate, getRange, getRegistration, getSize, getText, hasKnownSize, isAvailable, isEmpty, isSelectable, isTransient, release, setAvailable, setDigest, setDisposition, setExpirationDate, setListener, setRange, setSize, setTransient
      • Methods inherited from class org.restlet.representation.RepresentationInfo

        getModificationDate, getTag, setModificationDate, setTag
      • Methods inherited from class org.restlet.representation.Variant

        createClientInfo, equals, getCharacterSet, getEncodings, getLanguages, getLocationRef, getMediaType, hashCode, includes, isCompatible, setCharacterSet, setEncodings, setLanguages, setLocationRef, setLocationRef, setMediaType, toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • XML_EXPANDING_ENTITY_REFS

        public static final boolean XML_EXPANDING_ENTITY_REFS
        True for expanding entity references when parsing XML representations. Default value provided by system property "org.restlet.ext.xml.expandingEntityRefs", false by default.
      • XML_VALIDATING_DTD

        public static final boolean XML_VALIDATING_DTD
        True for validating DTD documents when parsing XML representations. Default value provided by system property "org.restlet.ext.xml.validatingDtd", false by default.
    • Constructor Detail

      • JacksonRepresentation

        public JacksonRepresentation​(org.restlet.data.MediaType mediaType,
                                     T object)
        Constructor.
        Parameters:
        mediaType - The target media type.
        object - The object to format.
      • JacksonRepresentation

        public JacksonRepresentation​(org.restlet.representation.Representation representation,
                                     java.lang.Class<T> objectClass)
        Constructor.
        Parameters:
        representation - The representation to parse.
        objectClass - The object class to instantiate.
      • JacksonRepresentation

        public JacksonRepresentation​(T object)
        Constructor for the JSON media type.
        Parameters:
        object - The object to format.
    • Method Detail

      • createCsvSchema

        protected com.fasterxml.jackson.dataformat.csv.CsvSchema createCsvSchema​(com.fasterxml.jackson.dataformat.csv.CsvMapper csvMapper)
        Creates a Jackson CSV schema based on a mapper and the current object class.
        Parameters:
        csvMapper - The source CSV mapper.
        Returns:
        A Jackson CSV schema
      • createObjectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper createObjectMapper()
        Creates a Jackson object mapper based on a media type. It supports JSON, JSON Smile, XML, YAML and CSV.
        Returns:
        The Jackson object mapper.
      • createObjectReader

        protected com.fasterxml.jackson.databind.ObjectReader createObjectReader()
        Creates a Jackson object reader based on a mapper. Has a special handling for CSV media types.
        Returns:
        The Jackson object reader.
      • createObjectWriter

        protected com.fasterxml.jackson.databind.ObjectWriter createObjectWriter()
        Creates a Jackson object writer based on a mapper. Has a special handling for CSV media types.
        Returns:
        The Jackson object writer.
      • getCsvSchema

        public com.fasterxml.jackson.dataformat.csv.CsvSchema getCsvSchema()
        Returns the modifiable Jackson CSV schema.
        Returns:
        The modifiable Jackson CSV schema.
      • getObject

        public T getObject()
                    throws java.io.IOException
        Returns the wrapped object, deserializing the representation with Jackson if necessary.
        Returns:
        The wrapped object.
        Throws:
        java.io.IOException
      • getObjectClass

        public java.lang.Class<T> getObjectClass()
        Returns the object class to instantiate.
        Returns:
        The object class to instantiate.
      • getObjectMapper

        public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
        Returns the modifiable Jackson object mapper. Useful to customize mappings.
        Returns:
        The modifiable Jackson object mapper.
      • getObjectReader

        public com.fasterxml.jackson.databind.ObjectReader getObjectReader()
        Returns the modifiable Jackson object reader. Useful to customize deserialization.
        Returns:
        The modifiable Jackson object reader.
      • getObjectWriter

        public com.fasterxml.jackson.databind.ObjectWriter getObjectWriter()
        Returns the modifiable Jackson object writer. Useful to customize serialization.
        Returns:
        The modifiable Jackson object writer.
      • isExpandingEntityRefs

        public boolean isExpandingEntityRefs()
        Indicates if the parser will expand entity reference nodes. By default the value of this is set to true.
        Returns:
        True if the parser will expand entity reference nodes.
      • isValidatingDtd

        public boolean isValidatingDtd()
        Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.
        Returns:
        True if the schema-based validation is enabled.
      • setCsvSchema

        public void setCsvSchema​(com.fasterxml.jackson.dataformat.csv.CsvSchema csvSchema)
        Sets the Jackson CSV schema.
        Parameters:
        csvSchema - The Jackson CSV schema.
      • setExpandingEntityRefs

        public void setExpandingEntityRefs​(boolean expandEntityRefs)
        Indicates if the parser will expand entity reference nodes. By default the value of this is set to true.
        Parameters:
        expandEntityRefs - True if the parser will expand entity reference nodes.
      • setObject

        public void setObject​(T object)
        Sets the object to format.
        Parameters:
        object - The object to format.
      • setObjectClass

        public void setObjectClass​(java.lang.Class<T> objectClass)
        Sets the object class to instantiate.
        Parameters:
        objectClass - The object class to instantiate.
      • setObjectMapper

        public void setObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
        Sets the Jackson object mapper.
        Parameters:
        objectMapper - The Jackson object mapper.
      • setObjectReader

        public void setObjectReader​(com.fasterxml.jackson.databind.ObjectReader objectReader)
        Sets the Jackson object reader.
        Parameters:
        objectReader - The Jackson object reader.
      • setObjectWriter

        public void setObjectWriter​(com.fasterxml.jackson.databind.ObjectWriter objectWriter)
        Sets the Jackson object writer.
        Parameters:
        objectWriter - The Jackson object writer.
      • setValidatingDtd

        public void setValidatingDtd​(boolean validating)
        Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.
        Parameters:
        validating - The new validation flag to set.
      • write

        public void write​(java.io.OutputStream outputStream)
                   throws java.io.IOException
        Specified by:
        write in class org.restlet.representation.Representation
        Throws:
        java.io.IOException