Class EntityEncodingManager
- java.lang.Object
-
- org.sakaiproject.entitybroker.rest.EntityEncodingManager
-
public class EntityEncodingManager extends Object
This handles the internal encoding (translation and formatting) of entity data, this can be used by various parts of the EB system
this is for internal use only currently but may be exposed later- Author:
- Aaron Zeckoski (azeckoski @ gmail.com)
-
-
Field Summary
Fields Modifier and Type Field Description static StringBATCH_PREFIXstatic StringCOLLECTIONprotected static StringDATA_KEYstatic StringENTITY_IDstatic StringENTITY_PREFIXstatic StringENTITY_REFERENCEstatic StringENTITY_TITLEstatic StringENTITY_URLstatic String[]HANDLED_INPUT_FORMATSstatic String[]HANDLED_OUTPUT_FORMATSstatic StringJSON_CALLBACK_PARAMstatic StringJSON_DEFAULT_CALLBACKprotected static StringXHTML_FOOTERprotected static StringXHTML_HEADERprotected static StringXML_HEADERprotected static StringXML_HEADER_PREFIXprotected static StringXML_HEADER_SUFFIX
-
Constructor Summary
Constructors Modifier Constructor Description protectedEntityEncodingManager()EntityEncodingManager(EntityProviderManager entityProviderManager, EntityBrokerManager entityBrokerManager)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,Object>decodeData(String data, String format)Decode a string of a specified format into a java map
Returned map can be fed into theReflectUtils.populate(Object, Map)if you want to convert it into a known object type
Types are likely to require conversion as guesses are made about the right formats, use of theReflectUtils.convert(Object, Class)method is recommendedStringencodeData(Object data, String format, String name, Map<String,Object> properties)Encode data into a given format, can handle any java object, note that unsupported formats will result in an exceptionStringencodeEntity(String prefix, String format, EntityData entityData, EntityView view)Encodes entity datavoidformatAndOutputEntity(EntityReference ref, String format, List<EntityData> entities, OutputStream outputStream, Map<String,Object> params)Format and output an entity or collection included or referred to by this entity ref object into output according to the format string provided, Should take into account the reference when determining what the entities are and how to encode them (This is basically a copy of the code in EntityHandlerImpl with stuff removed)org.azeckoski.reflectutils.transcoders.TranscodergetTranscoder(String format)ObjectinternalInputTranslator(EntityReference ref, String format, InputStream input, HttpServletRequest req)Handled the internal encoding of data into an entity objectvoidinternalOutputFormatter(EntityReference ref, String format, List<EntityData> entities, Map<String,Object> params, OutputStream output, EntityView view)Format entities for output based on the reference into a format, use the provided list or get the entitiesprotected StringmakeFormViewUrl(String contextUrl, String viewKey, EntityView view)protected StringsanitizeJsonCallback(Object param)Clean the JSONP callback parameter to make sure it is sensiblevoidsetEntityBrokerManager(EntityBrokerManager entityBrokerManager)voidsetEntityProviderManager(EntityProviderManager entityProviderManager)voidsetTranscoder(org.azeckoski.reflectutils.transcoders.Transcoder transcoder)Override the transcoder used for a specific formatvoidsetTranscoders(Map<String,org.azeckoski.reflectutils.transcoders.Transcoder> transcoders)ObjecttranslateInputToEntity(EntityReference ref, String format, InputStream inputStream, Map<String,Object> params)Translates the input data stream in the supplied format into an entity object for this reference (This is basically a copy of the code in EntityHandlerImpl with stuff removed)booleanvalidateFormat(String data, String format)Will attempt to validate that string data is of a specific format
-
-
-
Field Detail
-
ENTITY_REFERENCE
public static final String ENTITY_REFERENCE
- See Also:
- Constant Field Values
-
ENTITY_ID
public static final String ENTITY_ID
- See Also:
- Constant Field Values
-
ENTITY_URL
public static final String ENTITY_URL
- See Also:
- Constant Field Values
-
ENTITY_TITLE
public static final String ENTITY_TITLE
- See Also:
- Constant Field Values
-
ENTITY_PREFIX
public static final String ENTITY_PREFIX
- See Also:
- Constant Field Values
-
COLLECTION
public static final String COLLECTION
- See Also:
- Constant Field Values
-
BATCH_PREFIX
public static final String BATCH_PREFIX
- See Also:
- Constant Field Values
-
HANDLED_INPUT_FORMATS
public static final String[] HANDLED_INPUT_FORMATS
-
HANDLED_OUTPUT_FORMATS
public static final String[] HANDLED_OUTPUT_FORMATS
-
JSON_CALLBACK_PARAM
public static final String JSON_CALLBACK_PARAM
- See Also:
- Constant Field Values
-
JSON_DEFAULT_CALLBACK
public static final String JSON_DEFAULT_CALLBACK
- See Also:
- Constant Field Values
-
XML_HEADER_PREFIX
protected static final String XML_HEADER_PREFIX
- See Also:
- Constant Field Values
-
XML_HEADER_SUFFIX
protected static final String XML_HEADER_SUFFIX
- See Also:
- Constant Field Values
-
XML_HEADER
protected static final String XML_HEADER
- See Also:
- Constant Field Values
-
XHTML_HEADER
protected static final String XHTML_HEADER
- See Also:
- Constant Field Values
-
XHTML_FOOTER
protected static final String XHTML_FOOTER
- See Also:
- Constant Field Values
-
DATA_KEY
protected static final String DATA_KEY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
EntityEncodingManager
protected EntityEncodingManager()
-
EntityEncodingManager
public EntityEncodingManager(EntityProviderManager entityProviderManager, EntityBrokerManager entityBrokerManager)
-
-
Method Detail
-
setEntityProviderManager
public void setEntityProviderManager(EntityProviderManager entityProviderManager)
-
setEntityBrokerManager
public void setEntityBrokerManager(EntityBrokerManager entityBrokerManager)
-
formatAndOutputEntity
public void formatAndOutputEntity(EntityReference ref, String format, List<EntityData> entities, OutputStream outputStream, Map<String,Object> params)
Format and output an entity or collection included or referred to by this entity ref object into output according to the format string provided, Should take into account the reference when determining what the entities are and how to encode them (This is basically a copy of the code in EntityHandlerImpl with stuff removed)- Parameters:
ref- a globally unique reference to an entity, consists of the entity prefix and optional segmentsformat- a string constant indicating the format (fromFormats) for output, (example:#XML)entities- (optional) a list of entities to create formatted output for, if this is null then the entities should be retrieved based on the reference, if this contains only a single item AND the ref refers to a single entity then the entity should be extracted from the list and encoded without the indication that it is a collection, for all other cases the encoding should include an indication that this is a list of entitiesoutputStream- the output stream to place the formatted data in, should be UTF-8 encoded if there is char data- Throws:
FormatUnsupportedException- if you do not handle this format type (passes control to the internal handlers)EntityEncodingException- if you cannot encode the received data into an entityIllegalArgumentException- if any of the arguments are invalidIllegalStateException- for all other failures
-
translateInputToEntity
public Object translateInputToEntity(EntityReference ref, String format, InputStream inputStream, Map<String,Object> params)
Translates the input data stream in the supplied format into an entity object for this reference (This is basically a copy of the code in EntityHandlerImpl with stuff removed)- Parameters:
ref- a globally unique reference to an entity, consists of the entity prefix and optional segmentsformat- a string constant indicating the format (fromFormats) of the input, (example:#XML)input- a stream which contains the data to make up this entity, you may assume this is UTF-8 encoded if you don't know anything else about it- Returns:
- an entity object of the type used for the given reference
- Throws:
FormatUnsupportedException- if you do not handle this format type (passes control to the internal handlers)EntityEncodingException- if you cannot encode the received data into an entityIllegalArgumentException- if any of the arguments are invalidIllegalStateException- for all other failures
-
validateFormat
public boolean validateFormat(String data, String format)
Will attempt to validate that string data is of a specific format- Parameters:
data- a chunk of data to validateformat- the format which the data is supposed encoded in- Returns:
- true if the data appears valid for the given format, false otherwise
-
internalInputTranslator
public Object internalInputTranslator(EntityReference ref, String format, InputStream input, HttpServletRequest req)
Handled the internal encoding of data into an entity object- Parameters:
ref- the entity referenceformat- the format which the input is encoded ininput- the data being input- Returns:
- the entity object based on the data
- Throws:
FormatUnsupportedException- if you do not handle this format type (passes control to the internal handlers)EntityEncodingException- if you cannot encode the received data into an entityIllegalArgumentException- if any of the arguments are invalidIllegalStateException- for all other failures
-
internalOutputFormatter
public void internalOutputFormatter(EntityReference ref, String format, List<EntityData> entities, Map<String,Object> params, OutputStream output, EntityView view)
Format entities for output based on the reference into a format, use the provided list or get the entities- Parameters:
ref- the entity reference for this, if this is a reference to a collection then this will be rendered as a collection of entities, if a reference to a single entity then only the matching one from the collection will be usedformat- the format to use for the output dataentities- (optional) if this is null then the entities will be fetchedoutput- the outputstream to place the encoded data intoview- (optional)- Throws:
FormatUnsupportedException- if you do not handle this format type (passes control to the internal handlers)EntityEncodingException- if you cannot encode the received data into an entityIllegalArgumentException- if any of the arguments are invalidIllegalStateException- for all other failures
-
encodeEntity
public String encodeEntity(String prefix, String format, EntityData entityData, EntityView view)
Encodes entity data- Parameters:
prefix- the entity prefix related to this dataformat- the format to encode the data intoentityData- (optional) entity data to encodeview- (optional) used to generate links and determine the correct incoming view- Returns:
- the encoded entity or "" if encoding fails
-
makeFormViewUrl
protected String makeFormViewUrl(String contextUrl, String viewKey, EntityView view)
- Returns:
- the form view URLs which should be used with the forms
-
setTranscoders
public void setTranscoders(Map<String,org.azeckoski.reflectutils.transcoders.Transcoder> transcoders)
-
setTranscoder
public void setTranscoder(org.azeckoski.reflectutils.transcoders.Transcoder transcoder)
Override the transcoder used for a specific format- Parameters:
transcoder- a transcoder implementation
-
getTranscoder
public org.azeckoski.reflectutils.transcoders.Transcoder getTranscoder(String format)
-
encodeData
public String encodeData(Object data, String format, String name, Map<String,Object> properties)
Encode data into a given format, can handle any java object, note that unsupported formats will result in an exception- Parameters:
data- the data to encode (can be a POJO or Map or pretty much any java object)format- the format to use for output (fromFormats)name- (optional) the name to use for the encoded data (e.g. root node for XML)properties- (optional) extra properties to add into the encoding, ignored if encoded object is not a map or bean- Returns:
- the encoded string
- Throws:
UnsupportedOperationException- if the data cannot be encoded
-
sanitizeJsonCallback
protected String sanitizeJsonCallback(Object param)
Clean the JSONP callback parameter to make sure it is sensible- Parameters:
param- The parameter for the callback, should be a String- Returns:
- The string version of the param or the default callback name
-
decodeData
public Map<String,Object> decodeData(String data, String format)
Decode a string of a specified format into a java map
Returned map can be fed into theReflectUtils.populate(Object, Map)if you want to convert it into a known object type
Types are likely to require conversion as guesses are made about the right formats, use of theReflectUtils.convert(Object, Class)method is recommended- Parameters:
data- encoded dataformat- the format of the encoded data (fromFormats)- Returns:
- a map containing all the data derived from the encoded data
- Throws:
UnsupportedOperationException- if the data cannot be decoded
-
-