public JavaSerializationEncoder(ClassWhiteList classWhiteList) { super(new JavaSerializationMarshaller(classWhiteList)); }
private Object tryDeserialize(byte[] content) { try { return jbossMarshaller.objectFromByteBuffer(content); } catch (IOException | ClassNotFoundException e1) { try { return javaMarshaller.objectFromByteBuffer(content); } catch (IOException | ClassNotFoundException e) { return new String(content, UTF_8); } } }
@Test public void shouldNegotiateFromDefaultCacheWithBinary() throws Exception { TestClass testClass = new TestClass(); byte[] javaSerialized = new JavaSerializationMarshaller().objectToByteBuffer(testClass); putBinaryValueInCache("default", "test", javaSerialized, APPLICATION_OCTET_STREAM); ContentResponse response = get("default", "test", APPLICATION_OCTET_STREAM_TYPE); ResponseAssertion.assertThat(response).hasContentType(APPLICATION_OCTET_STREAM_TYPE); ResponseAssertion.assertThat(response).hasReturnedBytes(javaSerialized); }
@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); }
@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})); } }
public DefaultTranscoder() { this.javaMarshaller = new JavaSerializationMarshaller(); this.jbossMarshaller = new GenericJBossMarshaller(); }
byte[] jsonMarshalled = (byte[]) new JsonTranscoder().transcode(value, APPLICATION_OBJECT, APPLICATION_JSON); byte[] xmlMarshalled = (byte[]) new XMLTranscoder().transcode(value, APPLICATION_OBJECT, APPLICATION_XML); byte[] javaMarshalled = new JavaSerializationMarshaller().objectToByteBuffer(value);
public void testTranscoderLookup() { EncoderRegistry encoderRegistry = new EncoderRegistryImpl(); TestTranscoder t1 = new TestTranscoder(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OBJECT); TestTranscoder t2 = new TestTranscoder(MediaType.APPLICATION_XML, MediaType.APPLICATION_OBJECT); DefaultTranscoder t3 = new DefaultTranscoder(new GenericJBossMarshaller(), new JavaSerializationMarshaller()); encoderRegistry.registerTranscoder(t3); encoderRegistry.registerTranscoder(t2); encoderRegistry.registerTranscoder(t1); assertEquals(encoderRegistry.getTranscoder(MediaType.TEXT_PLAIN, MediaType.APPLICATION_OBJECT), t3); assertEquals(encoderRegistry.getTranscoder(MediaType.TEXT_PLAIN, MediaType.TEXT_PLAIN), t3); assertEquals(encoderRegistry.getTranscoder(MediaType.TEXT_PLAIN, MediaType.APPLICATION_OBJECT), t3); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_OCTET_STREAM), t3); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_OBJECT), t3); assertEquals(encoderRegistry.getTranscoder(MediaType.TEXT_PLAIN, MediaType.APPLICATION_OCTET_STREAM), t3); assertNotFound(encoderRegistry, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML); assertNotFound(encoderRegistry, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OBJECT), t1); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_JSON), t1); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_XML, MediaType.APPLICATION_OBJECT), t2); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_XML), t2); assertEquals(encoderRegistry.getTranscoder(MediaType.APPLICATION_WWW_FORM_URLENCODED, MediaType.APPLICATION_WWW_FORM_URLENCODED), t3); }
@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); }
@Override public void call() { ConfigurationBuilder offHeapConfig = new ConfigurationBuilder(); offHeapConfig.memory().storageType(StorageType.OFF_HEAP); offHeapConfig.customInterceptors().addInterceptor().after(EntryWrappingInterceptor.class).interceptor(new TestInterceptor(1)); ConfigurationBuilder compatConfig = new ConfigurationBuilder(); compatConfig.compatibility().enable().marshaller(new JavaSerializationMarshaller()); compatConfig.customInterceptors().addInterceptor().after(EntryWrappingInterceptor.class).interceptor(new TestInterceptor(1)); cm.defineConfiguration("offheap", offHeapConfig.build()); cm.defineConfiguration("compat", compatConfig.build()); Cache<Object, Object> cache = cm.getCache(); Cache<Object, Object> offheapCache = cm.getCache("offheap"); Cache<Object, Object> compatCache = cm.getCache("compat"); cache.put(1, 1); offheapCache.put(1, 1); compatCache.put(1, 1); assertEquals(1, cache.get(1)); assertEquals(1, offheapCache.get(1)); assertEquals(1, compatCache.get(1)); } });