Class JacksonRepresentation<T>

  • Type Parameters:
    T - The type to wrap.

    public class JacksonRepresentation<T>
    extends 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.
    See Also:
    Jackson project, XML Entity Expansion injection attack
    • 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.
      • csvSchema

        private com.fasterxml.jackson.dataformat.csv.CsvSchema csvSchema
        The modifiable Jackson CSV schema.
      • expandingEntityRefs

        private volatile boolean expandingEntityRefs
        Specifies that the parser will expand entity reference nodes. By default the value of this is set to false.
      • object

        private volatile T object
        The (parsed) object to format.
      • objectClass

        private volatile java.lang.Class<T> objectClass
        The object class to instantiate.
      • objectMapper

        private volatile com.fasterxml.jackson.databind.ObjectMapper objectMapper
        The modifiable Jackson object mapper.
      • objectReader

        private volatile com.fasterxml.jackson.databind.ObjectReader objectReader
        The modifiable Jackson object reader.
      • objectWriter

        private volatile com.fasterxml.jackson.databind.ObjectWriter objectWriter
        The modifiable Jackson object writer.
      • representation

        private volatile Representation representation
        The representation to parse.
      • validatingDtd

        private volatile boolean validatingDtd
        Indicates the desire for validating this type of XML representations against a DTD. Note that for XML schema or Relax NG validation, use the "schema" property instead.
        See Also:
        DocumentBuilderFactory.setValidating(boolean)
    • Constructor Detail

      • JacksonRepresentation

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

        public JacksonRepresentation​(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
        Description copied from class: Representation
        Writes the representation to a byte stream. This method is ensured to write the full content for each invocation unless it is a transient representation, in which case an exception is thrown.

        Note that the class implementing this method shouldn't flush or close the given OutputStream after writing to it as this will be handled by the Restlet connectors automatically.
        Specified by:
        write in class Representation
        Parameters:
        outputStream - The output stream.
        Throws:
        java.io.IOException