/** * 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()); }
@Test public void testToString() { assertEquals("application/xml", new MediaType("application", "xml", createMap(new MapEntry("q", "0.9"))).toString()); assertEquals("text/csv", new MediaType("text", "csv").toString()); assertEquals("foo/bar; a=2", new MediaType("foo", "bar", createMap(new MapEntry("a", "2"))).toString()); assertEquals("foo/bar; a=2; b=1; c=2", new MediaType("foo", "bar", createMap(new MapEntry("a", "2"), new MapEntry("b", "1"), new MapEntry("c", "2"))).toString()); assertEquals("a/b; p=1", MediaType.fromString("a/b; p=1; q=2;").toStringExcludingParam("q")); }
public RestRequest value(Object value, MediaType contentType) { this.value = value; this.contentType = contentType.toString(); return this; }
@Test public void testQuotedParams() { MediaType mediaType = MediaType.fromString("application/json; charset=\"UTF-8\""); assertMediaTypeWithParam(mediaType, "application", "json", "charset", "UTF-8"); }
@Test public void testParsingTypeSubType() { MediaType appJson = MediaType.fromString("application/json"); assertMediaTypeNoParams(appJson, "application", "json"); }
@Override public MediaType getStorageFormat() { return MediaType.parse("application/gzip"); }
@Test public void testMediaTypeExternalizerId() throws Exception { ObjectInOut inOutOrig = new ObjectInOut(); MediaType.MediaTypeExternalizer mediaTypeExternalizer = new MediaType.MediaTypeExternalizer(); mediaTypeExternalizer.writeObject(inOutOrig, MediaType.TEXT_PLAIN); MediaType mediaType = mediaTypeExternalizer.readObject(inOutOrig); assertMediaTypeNoParams(mediaType, "text", "plain"); }
@Test public void testMultipleParameters() { MediaType mediaType = MediaType.fromString("application/json ; charset=UTF-8; param1=value1; param2 = value2"); assertMediaTypeWithParams(mediaType, "application", "json", new String[]{"charset", "param1", "param2"}, new String[]{"UTF-8", "value1", "value2"}); }
private static int forDigit(char digit) { if (digit >= '0' && digit <= '9') return digit - 48; if (digit == 'a') return 10; if (digit == 'b') return 11; if (digit == 'c') return 12; if (digit == 'd') return 13; if (digit == 'e') return 14; if (digit == 'f') return 15; throw new EncodingException("Invalid digit found in hex format!"); }
public static Object convertOctetStreamToUrlEncoded(Object content, MediaType contentType) { byte[] decoded = decodeOctetStream(content, contentType); return urlEncode(decoded, MediaType.TEXT_PLAIN); } }
/** * Convert text format to a URL safe format. * * @param source the source text/plain content. * @param sourceType the MediaType of the source content. * @return a String with the content URLEncoded. * @throws EncodingException if the source format cannot be interpreted as plain/text. */ public static String convertTextToUrlEncoded(Object source, MediaType sourceType) { return urlEncode(source, sourceType); }
@Test public void testQuotedParams2() { MediaType mediaType = MediaType.fromString("application/json; charset='UTF-8'"); assertMediaTypeWithParam(mediaType, "application", "json", "charset", "UTF-8"); }
@Test public void testParsingEmptySpaces2() { MediaType appJson = MediaType.fromString("application/ json"); assertMediaTypeNoParams(appJson, "application", "json"); }
@Test public void testMediaTypeExternalizerNoId() throws Exception { ObjectInOut inOutOrig = new ObjectInOut(); MediaType.MediaTypeExternalizer mediaTypeExternalizer = new MediaType.MediaTypeExternalizer(); mediaTypeExternalizer.writeObject(inOutOrig, MediaType.APPLICATION_XML); MediaType mediaType = mediaTypeExternalizer.readObject(inOutOrig); assertMediaTypeNoParams(mediaType, "application", "xml"); }
@Test public void testUnQuotedParamWithSpaces() { MediaType mediaType = MediaType.fromString("application/json ; charset= UTF-8"); assertMediaTypeWithParam(mediaType, "application", "json", "charset", "UTF-8"); }
@Test public void testParsingEmptySpaces3() { MediaType appJson = MediaType.fromString("application / json"); assertMediaTypeNoParams(appJson, "application", "json"); }
@Test public void testUnQuotedParam() { MediaType mediaType = MediaType.fromString("application/json; charset=UTF-8"); assertMediaTypeWithParam(mediaType, "application", "json", "charset", "UTF-8"); }