/** * 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()); } }
private Object convertToTextPlain(Object content, MediaType contentType, MediaType destinationType) { if (contentType.match(APPLICATION_UNKNOWN)) { try { return StandardConversions.convertJavaToOctetStream(content, APPLICATION_OBJECT, javaMarshaller); } catch (IOException | InterruptedException e) { throw log.unsupportedContent(content); } } if (contentType.match(APPLICATION_OCTET_STREAM)) { byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType); return StandardConversions.convertOctetStreamToText(decoded, destinationType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertJavaToText(content, contentType, destinationType); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToText(content, contentType, destinationType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToText(content, destinationType); } throw log.unsupportedContent(content); }
/** * Handle x-www-form-urlencoded as single values for now. * Ideally it should generate a Map<String, String> */ public static Object convertUrlEncodedToObject(Object content) { Object decoded = urlDecode(content); return convertTextToObject(decoded, TEXT_PLAIN); }
public static Object convertUrlEncodedToText(Object content, MediaType destinationType) { return convertTextToText(urlDecode(content), TEXT_PLAIN, destinationType); }
public static Object convertUrlEncodedToOctetStream(Object content) { return convertTextToOctetStream(urlDecode(content), TEXT_PLAIN); }
private Object convertToByteArray(Object content, MediaType contentType) { try { if (contentType.match(APPLICATION_OCTET_STREAM)) { return StandardConversions.decodeOctetStream(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToOctetStream(content); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } return StandardConversions.convertJavaToOctetStream(content, contentType, jbossMarshaller); } catch (EncodingException | InterruptedException | IOException e) { throw log.unsupportedContent(content); } }
private Object convertToObject(Object content, MediaType contentType, MediaType destinationType) { if (contentType.match(APPLICATION_OCTET_STREAM)) { byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType); return StandardConversions.convertOctetStreamToJava(decoded, destinationType, javaMarshaller); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.decodeObjectContent(content, contentType); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToObject(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToObject(content); } if (contentType.equals(MediaType.APPLICATION_UNKNOWN)) { if (content instanceof byte[]) { return tryDeserialize((byte[]) content); } if (content instanceof WrappedByteArray) { return tryDeserialize(((WrappedByteArray) content).getBytes()); } return content; } throw log.unsupportedContent(content); }
@Test public void testHexConversion() throws Exception { assertNull(bytesToHex(null)); assertNull(hexToBytes(null)); assertEquals("", bytesToHex(new byte[]{})); assertArrayEquals(new byte[]{}, hexToBytes("")); JavaSerializationMarshaller marshaller = new JavaSerializationMarshaller(); byte[] before = marshaller.objectToByteBuffer(Calendar.getInstance()); byte[] after = hexToBytes(bytesToHex(before)); assertArrayEquals(before, after); assertEquals("0x010203", bytesToHex(new byte[]{1, 2, 3})); assertEquals("0x808080", bytesToHex(new byte[]{-128, -128, -128})); } }
private Object convertToUrlEncoded(Object content, MediaType contentType) { if (contentType.match(APPLICATION_OCTET_STREAM)) { return StandardConversions.convertOctetStreamToUrlEncoded(content, contentType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertUrlEncodedToObject(content); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToUrlEncoded(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return content; } throw log.unsupportedContent(content); }
@Test public void shouldGetAllEntriesConvertedToBytes() throws Exception { //given putStringValueInCache("default", "key1", "test1"); putStringValueInCache("default", "key2", "test2"); //when ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s", restServer().getPort(), "default")) .header(HttpHeader.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_TYPE) .send(); //then ResponseAssertion.assertThat(response).isOk(); ResponseAssertion.assertThat(response).hasContentType(MediaType.APPLICATION_OCTET_STREAM_TYPE); // keys can be returned in any order ResponseAssertion.assertThat(response).hasReturnedText( String.format("%s\n%s", bytesToHex("key1".getBytes()), bytesToHex("key2".getBytes())), String.format("%s\n%s", bytesToHex("key2".getBytes()), bytesToHex("key1".getBytes()))); }
/** * 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); }
/** * 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 testOctetStreamToTextConversion() { String text = "Have you ever retired a human by mistake?"; byte[] bytes1 = text.getBytes(); byte[] bytes2 = new byte[]{1, 2, 3}; byte[] result1 = StandardConversions.convertOctetStreamToText(bytes1, TEXT_PLAIN.withCharset(US_ASCII)); byte[] result2 = StandardConversions.convertOctetStreamToText(bytes2, TEXT_PLAIN); assertArrayEquals(text.getBytes(US_ASCII), result1); assertArrayEquals(new String(bytes2).getBytes(UTF_8), result2); }
@Test public void testTextToOctetStreamConversion() { String source = "Like our owl?"; byte[] result = StandardConversions.convertTextToOctetStream(source, TEXT_PLAIN); assertArrayEquals(source.getBytes(UTF_8), result); }
@Test public void testTextToObjectConversion() { String source = "Can the maker repair what he makes?"; String source2 = "I had your job once. I was good at it."; byte[] sourceBytes = source2.getBytes(US_ASCII); Object result = StandardConversions.convertTextToObject(source, APPLICATION_OBJECT); Object result2 = StandardConversions.convertTextToObject(sourceBytes, TEXT_PLAIN.withCharset(US_ASCII)); assertEquals(source, result); assertEquals(source2, result2); }
@Test public void testTextToURLEncodedConversion() throws UnsupportedEncodingException { String source = "They're either a benefit or a hazard. If they're a benefit, it's not my problem."; String result = StandardConversions.convertTextToUrlEncoded(source, TEXT_PLAIN.withCharset(UTF_16)); assertEquals(URLEncoder.encode(source, "UTF-16"), result); }
@Test public void textToTextConversion() { String source = "All those moments will be lost in time, like tears in rain."; byte[] sourceAs8859 = source.getBytes(ISO_8859_1); byte[] sourceAsASCII = source.getBytes(US_ASCII); Object result = StandardConversions.convertTextToText(sourceAs8859, TEXT_PLAIN.withCharset(ISO_8859_1), TEXT_PLAIN.withCharset(US_ASCII)); assertArrayEquals(sourceAsASCII, (byte[]) result); }
@Test public void testOctetStreamToJavaConversion() { String value = "It's not an easy thing to meet your maker."; byte[] textStream = value.getBytes(UTF_8); byte[] randomBytes = new byte[]{23, 23, 34, 1, -1, -123}; JavaSerializationMarshaller marshaller = new JavaSerializationMarshaller(); MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); Object result = StandardConversions.convertOctetStreamToJava(textStream, stringType, marshaller); assertEquals(value, result); MediaType byteArrayType = APPLICATION_OBJECT.withParameter("type", "ByteArray"); Object result2 = StandardConversions.convertOctetStreamToJava(textStream, byteArrayType, marshaller); assertArrayEquals(textStream, (byte[]) result2); Object result3 = StandardConversions.convertOctetStreamToJava(randomBytes, byteArrayType, marshaller); assertArrayEquals(randomBytes, (byte[]) result3); thrown.expect(EncodingException.class); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); StandardConversions.convertOctetStreamToJava(randomBytes, doubleType, marshaller); System.out.println(thrown); }
@Test public void testJavaToTextConversion() { String string = "I've seen things you people wouldn't believe."; Double number = 12.1d; Calendar complex = Calendar.getInstance(); MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); byte[] result1 = StandardConversions.convertJavaToText(string, stringType, TEXT_PLAIN.withCharset(UTF_16BE)); assertArrayEquals(string.getBytes(UTF_16BE), result1); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); byte[] result2 = StandardConversions.convertJavaToText(number, doubleType, TEXT_PLAIN.withCharset(US_ASCII)); assertArrayEquals("12.1".getBytes(US_ASCII), result2); MediaType customType = APPLICATION_OBJECT.withParameter("type", complex.getClass().getName()); byte[] bytes = StandardConversions.convertJavaToText(complex, customType, TEXT_PLAIN.withCharset(US_ASCII)); assertEquals(complex.toString(), new String(bytes)); }
@Test public void testJavaToOctetStreamConversion() throws IOException, InterruptedException { JavaSerializationMarshaller marshaller = new JavaSerializationMarshaller(); String string = "I've seen things you people wouldn't believe."; Double number = 12.1d; Calendar complex = Calendar.getInstance(); byte[] binary = new byte[]{1, 2, 3}; MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); byte[] result = StandardConversions.convertJavaToOctetStream(string, stringType, marshaller); assertArrayEquals(string.getBytes(UTF_8), result); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); result = StandardConversions.convertJavaToOctetStream(number, doubleType, marshaller); assertArrayEquals(marshaller.objectToByteBuffer(number), result); MediaType customType = APPLICATION_OBJECT.withParameter("type", complex.getClass().getName()); result = StandardConversions.convertJavaToOctetStream(complex, customType, marshaller); assertArrayEquals(marshaller.objectToByteBuffer(complex), result); MediaType byteArrayType = APPLICATION_OBJECT.withParameter("type", "ByteArray"); result = StandardConversions.convertJavaToOctetStream(binary, byteArrayType, marshaller); assertArrayEquals(result, binary); }