Class StandardConversions


  • public final class StandardConversions
    extends java.lang.Object
    Utilities to convert between text/plain, octet-stream, java-objects and url-encoded contents.
    Since:
    9.2
    • Constructor Detail

      • StandardConversions

        public StandardConversions()
    • Method Detail

      • convertTextToText

        public static java.lang.Object convertTextToText​(java.lang.Object source,
                                                         MediaType sourceType,
                                                         MediaType destinationType)
        Convert text content to a different encoding.
        Parameters:
        source - The source content.
        sourceType - MediaType for the source content.
        destinationType - the MediaType of the converted content.
        Returns:
        content conforming to the destination MediaType.
      • convertTextToOctetStream

        public static byte[] convertTextToOctetStream​(java.lang.Object source,
                                                      MediaType sourceType)
        Converts text content to binary.
        Parameters:
        source - The source content.
        sourceType - MediaType for the source content.
        Returns:
        content converted as octet-stream represented as byte[].
        Throws:
        EncodingException - if the source cannot be interpreted as plain text.
      • convertTextToObject

        public static java.lang.String convertTextToObject​(java.lang.Object source,
                                                           MediaType sourceType)
        Converts text content to the Java representation (String).
        Parameters:
        source - The source content
        sourceType - the MediaType of the source content.
        Returns:
        String representation of the text content.
        Throws:
        EncodingException - if the source cannot be interpreted as plain text.
      • convertTextToUrlEncoded

        public static java.lang.String convertTextToUrlEncoded​(java.lang.Object source,
                                                               MediaType sourceType)
        Convert text format to a URL safe format.
        Parameters:
        source - the source text/plain content.
        sourceType - the MediaType of the source content.
        Returns:
        a String with the content URLEncoded.
        Throws:
        EncodingException - if the source format cannot be interpreted as plain/text.
      • convertOctetStreamToText

        public static byte[] convertOctetStreamToText​(byte[] source,
                                                      MediaType destination)
        Converts generic byte[] to text.
        Parameters:
        source - byte[] to convert.
        destination - MediaType of the desired text conversion.
        Returns:
        byte[] content interpreted as text, in the encoding specified by the destination MediaType.
      • convertOctetStreamToJava

        public static java.lang.Object convertOctetStreamToJava​(byte[] source,
                                                                MediaType destination,
                                                                Marshaller marshaller)
        Converts an octet stream to a Java object
        Parameters:
        source - The source to convert
        destination - The type of the converted object.
        Returns:
        an instance of a java object compatible with the supplied destination type.
      • convertJavaToOctetStream

        public static byte[] convertJavaToOctetStream​(java.lang.Object source,
                                                      MediaType sourceMediaType,
                                                      Marshaller marshaller)
                                               throws java.io.IOException,
                                                      java.lang.InterruptedException
        Converts a java object to a sequence of bytes applying standard java serialization.
        Parameters:
        source - source the java object to convert.
        sourceMediaType - the MediaType matching application/x-application-object describing the source.
        Returns:
        byte[] representation of the java object.
        Throws:
        EncodingException - if the sourceMediaType is not a application/x-java-object or if the conversion is not supported.
        java.io.IOException
        java.lang.InterruptedException
      • convertJavaToText

        public static byte[] convertJavaToText​(java.lang.Object source,
                                               MediaType sourceMediaType,
                                               MediaType destinationMediaType)
        Converts a java object to a text/plain representation.
        Parameters:
        source - Object to convert.
        sourceMediaType - The MediaType for the source object.
        destinationMediaType - The required text/plain specification.
        Returns:
        byte[] with the text/plain representation of the object with the requested charset.
      • decodeObjectContent

        public static java.lang.Object decodeObjectContent​(java.lang.Object content,
                                                           MediaType contentMediaType)
        Decode UTF-8 as a java object. For this conversion, the "type" parameter is used in the supplied MediaType. Currently supported types are primitives and String, plus a special "ByteArray" to describe a sequence of bytes.
        Parameters:
        content - The content to decode.
        contentMediaType - the MediaType describing the content.
        Returns:
        instance of Object according to the supplied MediaType "type" parameter, or if no type is present, the object itself.
        Throws:
        EncodingException - if the provided type is not supported.
      • convertCharset

        public static byte[] convertCharset​(java.lang.Object content,
                                            java.nio.charset.Charset fromCharset,
                                            java.nio.charset.Charset toCharset)
        Convert text content.
        Parameters:
        content - Object to convert.
        fromCharset - Charset of the provided content.
        toCharset - Charset to convert to.
        Returns:
        byte[] with the content in the desired charset.
      • decodeOctetStream

        public static byte[] decodeOctetStream​(java.lang.Object input,
                                               MediaType octetStream)
        Decode a octet-stream content that is not a byte[]. For this, it uses a special param called "encoding" in the "application/octet-stream" MediaType. The "encoding" param supports only the "hex" value that represents an octet-stream as a hexadecimal representation, for example "0xdeadbeef". In the absence of the "encoding" param, it will assume base64 encoding.
        Parameters:
        input - Object representing the binary content.
        octetStream - The MediaType describing the input.
        Returns:
        a byte[] with the decoded content.
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
      • hexToBytes

        public static byte[] hexToBytes​(java.lang.String hex)
      • convertUrlEncodedToObject

        public static java.lang.Object convertUrlEncodedToObject​(java.lang.Object content)
        Handle x-www-form-urlencoded as single values for now. Ideally it should generate a Map<String, String>
      • convertUrlEncodedToText

        public static java.lang.Object convertUrlEncodedToText​(java.lang.Object content,
                                                               MediaType destinationType)
      • convertUrlEncodedToOctetStream

        public static java.lang.Object convertUrlEncodedToOctetStream​(java.lang.Object content)
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.Object content,
                                                 MediaType mediaType)
      • urlDecode

        public static java.lang.Object urlDecode​(java.lang.Object content)
      • convertOctetStreamToUrlEncoded

        public static java.lang.Object convertOctetStreamToUrlEncoded​(java.lang.Object content,
                                                                      MediaType contentType)