@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); }
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); } }
public Object convertToOctetStream(Object content, MediaType contentType, MediaType destinationType) throws IOException, InterruptedException { if (contentType.match(APPLICATION_OCTET_STREAM) || contentType.match(APPLICATION_UNKNOWN)) { return StandardConversions.decodeOctetStream(content, contentType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertJavaToOctetStream(content, contentType, javaMarshaller); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToOctetStream(content, destinationType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToOctetStream(content); } throw log.unsupportedContent(content); }
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); }