Package org.restlet.ext.jackson
Class JacksonRepresentation<T>
- java.lang.Object
-
- 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.
-
-
Field Summary
Fields Modifier and Type Field Description private com.fasterxml.jackson.dataformat.csv.CsvSchemacsvSchemaThe modifiable Jackson CSV schema.private booleanexpandingEntityRefsSpecifies that the parser will expand entity reference nodes.private TobjectThe (parsed) object to format.private java.lang.Class<T>objectClassThe object class to instantiate.private com.fasterxml.jackson.databind.ObjectMapperobjectMapperThe modifiable Jackson object mapper.private com.fasterxml.jackson.databind.ObjectReaderobjectReaderThe modifiable Jackson object reader.private com.fasterxml.jackson.databind.ObjectWriterobjectWriterThe modifiable Jackson object writer.private RepresentationrepresentationThe representation to parse.private booleanvalidatingDtdIndicates the desire for validating this type of XML representations against a DTD.static booleanXML_EXPANDING_ENTITY_REFSTrue for expanding entity references when parsing XML representations.static booleanXML_VALIDATING_DTDTrue for validating DTD documents when parsing XML representations.-
Fields inherited from class org.restlet.representation.Representation
UNKNOWN_SIZE
-
-
Constructor Summary
Constructors Constructor Description JacksonRepresentation(MediaType mediaType, T object)Constructor.JacksonRepresentation(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.CsvSchemacreateCsvSchema(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.ObjectMappercreateObjectMapper()Creates a Jackson object mapper based on a media type.protected com.fasterxml.jackson.databind.ObjectReadercreateObjectReader()Creates a Jackson object reader based on a mapper.protected com.fasterxml.jackson.databind.ObjectWritercreateObjectWriter()Creates a Jackson object writer based on a mapper.com.fasterxml.jackson.dataformat.csv.CsvSchemagetCsvSchema()Returns the modifiable Jackson CSV schema.TgetObject()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.ObjectMappergetObjectMapper()Returns the modifiable Jackson object mapper.com.fasterxml.jackson.databind.ObjectReadergetObjectReader()Returns the modifiable Jackson object reader.com.fasterxml.jackson.databind.ObjectWritergetObjectWriter()Returns the modifiable Jackson object writer.booleanisExpandingEntityRefs()Indicates if the parser will expand entity reference nodes.booleanisValidatingDtd()Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.voidsetCsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema csvSchema)Sets the Jackson CSV schema.voidsetExpandingEntityRefs(boolean expandEntityRefs)Indicates if the parser will expand entity reference nodes.voidsetObject(T object)Sets the object to format.voidsetObjectClass(java.lang.Class<T> objectClass)Sets the object class to instantiate.voidsetObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Sets the Jackson object mapper.voidsetObjectReader(com.fasterxml.jackson.databind.ObjectReader objectReader)Sets the Jackson object reader.voidsetObjectWriter(com.fasterxml.jackson.databind.ObjectWriter objectWriter)Sets the Jackson object writer.voidsetValidatingDtd(boolean validating)Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.voidwrite(java.io.OutputStream outputStream)Writes the representation to a byte stream.-
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
-
-
-
-
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.IOExceptionDescription copied from class:RepresentationWrites 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 givenOutputStreamafter writing to it as this will be handled by the Restlet connectors automatically.- Specified by:
writein classRepresentation- Parameters:
outputStream- The output stream.- Throws:
java.io.IOException
-
-