Class 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)
    • Constructor Detail

      • EntityEncodingManager

        protected EntityEncodingManager()
    • 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 segments
        format - a string constant indicating the format (from Formats) 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 entities
        outputStream - 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 entity
        IllegalArgumentException - if any of the arguments are invalid
        IllegalStateException - 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 segments
        format - a string constant indicating the format (from Formats) 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 entity
        IllegalArgumentException - if any of the arguments are invalid
        IllegalStateException - 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 validate
        format - the format which the data is supposed encoded in
        Returns:
        true if the data appears valid for the given format, false otherwise
      • 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 used
        format - the format to use for the output data
        entities - (optional) if this is null then the entities will be fetched
        output - the outputstream to place the encoded data into
        view - (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 entity
        IllegalArgumentException - if any of the arguments are invalid
        IllegalStateException - 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 data
        format - the format to encode the data into
        entityData - (optional) entity data to encode
        view - (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 (from Formats)
        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 the ReflectUtils.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 the ReflectUtils.convert(Object, Class) method is recommended
        Parameters:
        data - encoded data
        format - the format of the encoded data (from Formats)
        Returns:
        a map containing all the data derived from the encoded data
        Throws:
        UnsupportedOperationException - if the data cannot be decoded