private static Object convertTextClass(Object text, MediaType destination, boolean asString) { if (asString) { return text instanceof byte[] ? new String((byte[]) text, destination.getCharset()) : text.toString(); } return text instanceof byte[] ? text : text.toString().getBytes(destination.getCharset()); }
/** * Converts text content to binary. * * @param source The source content. * @param sourceType MediaType for the source content. * @return content converted as octet-stream represented as byte[]. * @throws EncodingException if the source cannot be interpreted as plain text. */ public static byte[] convertTextToOctetStream(Object source, MediaType sourceType) { if (source == null) return null; if (sourceType == null) { throw new NullPointerException("MediaType cannot be null!"); } if (source instanceof byte[]) return (byte[]) source; return source.toString().getBytes(sourceType.getCharset()); }
public static String urlEncode(Object content, MediaType mediaType) { if (content == null) return null; try { String asString; if (content instanceof byte[]) { asString = new String((byte[]) content, UTF_8); } else { asString = content.toString(); } return URLEncoder.encode(asString, mediaType.getCharset().toString()); } catch (UnsupportedEncodingException e) { throw log.errorEncoding(content, APPLICATION_WWW_FORM_URLENCODED); } }
/** * Converts generic byte[] to text. * * @param source byte[] to convert. * @param destination MediaType of the desired text conversion. * @return byte[] content interpreted as text, in the encoding specified by the destination MediaType. */ public static byte[] convertOctetStreamToText(byte[] source, MediaType destination) { if (source == null) return null; return convertCharset(source, UTF_8, destination.getCharset()); }
@Override public Object formatCollection(Collection<?> elements, MediaType elementType, MediaType destinationType) { Transcoder transcoder = encoderRegistry.getTranscoder(elementType, APPLICATION_TEXT_STRING); return elements.stream().map(s -> transcoder.transcode(s, elementType, APPLICATION_TEXT_STRING)) .map(this::quote).collect(Collectors.joining(", ", "[", "]")) .getBytes(destinationType.getCharset()); } }
/** * Converts text content to the Java representation (String). * * @param source The source content * @param sourceType the MediaType of the source content. * @return String representation of the text content. * @throws EncodingException if the source cannot be interpreted as plain text. */ public static String convertTextToObject(Object source, MediaType sourceType) { if (source == null) return null; if (source instanceof String) return source.toString(); if (source instanceof byte[]) { byte[] bytesSource = (byte[]) source; return new String(bytesSource, sourceType.getCharset()); } throw log.invalidTextContent(source); }
/** * Converts a java object to a text/plain representation. * @param source Object to convert. * @param sourceMediaType The MediaType for the source object. * @param destinationMediaType The required text/plain specification. * @return byte[] with the text/plain representation of the object with the requested charset. */ public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } }
/** * Convert text content to a different encoding. * * @param source The source content. * @param sourceType MediaType for the source content. * @param destinationType the MediaType of the converted content. * @return content conforming to the destination MediaType. */ public static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType) { if (source == null) return null; if (sourceType == null) throw new NullPointerException("MediaType cannot be null!"); if (!sourceType.match(MediaType.TEXT_PLAIN)) throw log.invalidMediaType(TEXT_PLAIN_TYPE, sourceType.toString()); boolean asString = destinationType.hasStringType(); Charset sourceCharset = sourceType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (sourceCharset.equals(destinationCharset)) return convertTextClass(source, destinationType, asString); byte[] byteContent = source instanceof byte[] ? (byte[]) source : source.toString().getBytes(sourceCharset); return convertTextClass(convertCharset(byteContent, sourceCharset, destinationCharset), destinationType, asString); }