@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()); } }
default boolean supports(MediaType mediaType) { return getSupportedMediaTypes().stream().anyMatch(m -> m.match(mediaType)); }
@Override public C filterAndConvert(K key, V value, Metadata metadata) { if (providedFilter.isPresent()) { KeyValueFilterConverter<K, V, C> f = providedFilter.get(); Object keyTranscoded = key; Object valueTranscoded = value; if (applyBefore != null) { keyTranscoded = applyBefore.transcode(key, storageMediaType, f.format()); valueTranscoded = applyBefore.transcode(value, storageMediaType, f.format()); } C result = f.filterAndConvert((K) keyTranscoded, (V) valueTranscoded, metadata); if (result == null) return null; if (applyAfter == null) return result; return (C) applyAfter.transcode(result, f.format(), requestType); } else { return (C) value; } }
@BeforeClass(alwaysRun = true) public void setUp() { dataSrc = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; transcoder = new DefaultTranscoder(); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Override public void testTranscoderTranscode() { Object transcoded = transcoder.transcode(dataSrc, MediaType.TEXT_PLAIN, MediaType.APPLICATION_OCTET_STREAM); assertTrue(transcoded instanceof byte[], "Must be byte[]"); Object transcodedBack = transcoder.transcode(transcoded, MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_PLAIN); assertEquals(transcodedBack, dataSrc.getBytes(), "Must be an equal objects"); } }
@BeforeClass(alwaysRun = true) public void setUp() throws IOException { dataSrc = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; transcoder = new ProtostreamTextTranscoder(ProtobufUtil.newSerializationContext()); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Test @Override public void testTranscoderTranscode() throws Exception { Object transcoded = transcoder.transcode(dataSrc, MediaType.TEXT_PLAIN, MediaType.APPLICATION_PROTOSTREAM); assertTrue(transcoded instanceof byte[], "Must be byte[]"); Object transcodedBack = transcoder.transcode(transcoded, MediaType.APPLICATION_PROTOSTREAM, MediaType.TEXT_PLAIN); assertTrue(transcodedBack instanceof String, "Must be instance of String"); assertEquals(dataSrc, transcodedBack, "Must be equal strings"); } }
@BeforeClass(alwaysRun = true) public void setUp() throws IOException { dataSrc = "{\"_type\":\"Person\", \"name\":\"joe\", \"address\":{\"_type\":\"Address\", \"street\":\"\", \"city\":\"London\", \"zip\":\"0\"}}"; SerializationContext serCtx = ProtobufUtil.newSerializationContext(); serCtx.registerProtoFiles(FileDescriptorSource.fromString("person_definition.proto", PROTO_DEFINITIONS)); transcoder = new ProtostreamJsonTranscoder(serCtx); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Override public void testTranscoderTranscode() { MediaType personMediaType = MediaType.fromString("application/x-java-object;type=org.infinispan.test.data.Person"); Object result = transcoder.transcode(dataSrc, personMediaType, MediaType.APPLICATION_SERIALIZED_OBJECT); assertTrue(result instanceof byte[], "Must be byte[]"); Object transcodedBack = transcoder.transcode(result, MediaType.APPLICATION_SERIALIZED_OBJECT, personMediaType); assertEquals(transcodedBack, dataSrc, "Must be an equal objects"); } }
@BeforeClass(alwaysRun = true) public void setUp() { dataSrc = new Person("joe"); Address address = new Address(); address.setCity("London"); dataSrc.setAddress(address); transcoder = new JsonTranscoder(new ClassWhiteList(Collections.singletonList(".*"))); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Test @Override public void testTranscoderTranscode() throws Exception { Object transcoded = transcoder.transcode(dataSrc.getBytes(StandardCharsets.UTF_8), MediaType.APPLICATION_JSON, MediaType.APPLICATION_PROTOSTREAM); assertTrue(transcoded instanceof byte[], "Must be byte[]"); Object transcodedBack = transcoder.transcode(transcoded, MediaType.APPLICATION_PROTOSTREAM, MediaType.APPLICATION_JSON); assertEquals( dataSrc.replace(" ", ""), (new String((byte[]) transcodedBack)).replace(" ", "").replace("\n", ""), "Must be the same JSON string" ); } }
@BeforeClass(alwaysRun = true) public void setUp() { dataSrc = new Person("Joe"); Address address = new Address(); address.setCity("London"); dataSrc.setAddress(address); transcoder = new DefaultTranscoder(); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Override public void testTranscoderTranscode() { Object transcoded = transcoder.transcode(dataSrc, MediaType.APPLICATION_OBJECT, MediaType.TEXT_PLAIN); assertEquals(new String((byte[]) transcoded), dataSrc.toString()); transcoded = transcoder.transcode(transcoded, MediaType.APPLICATION_OBJECT, MediaType.TEXT_PLAIN); assertTrue(transcoded instanceof byte[], "Must be byte[]"); } }
@BeforeClass(alwaysRun = true) public void setUp() { dataSrc = new Person("Joe"); Address address = new Address(); address.setCity("London"); dataSrc.setAddress(address); transcoder = new JavaSerializationTranscoder(new ClassWhiteList(Collections.singletonList(".*"))); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
@Override public void testTranscoderTranscode() throws Exception { MediaType jsonMediaType = APPLICATION_JSON; MediaType personMediaType = MediaType.fromString("application/x-java-object;type=org.infinispan.test.data.Person"); Object result = transcoder.transcode(dataSrc, personMediaType, jsonMediaType); assertEquals(new String((byte[]) result), String.format("{\"" + TYPE_PROPERTY + "\":\"%s\",\"name\":\"%s\",\"address\":{\"" + TYPE_PROPERTY + "\":\"%s\",\"street\":null,\"city\":\"%s\",\"zip\":0}}", Person.class.getName(), "joe", Address.class.getName(), "London") ); Object fromJson = transcoder.transcode(result, jsonMediaType, personMediaType); assertEquals(fromJson, dataSrc); }
@Test public void testEmptyContent() { byte[] empty = Util.EMPTY_BYTE_ARRAY; assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_JSON, APPLICATION_JSON.withCharset(US_ASCII))); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_UNKNOWN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OCTET_STREAM, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, TEXT_PLAIN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OBJECT, APPLICATION_JSON)); }
public Object convertToRequestType(Object obj, MediaType objType, MediaType requestType) { if (obj == null) return null; if (requestType.equals(MediaType.MATCH_ALL)) return obj; // Older HR clients do not send request type and assume the script metadata type is the output type MediaType outputFormat = requestType.match(MediaType.APPLICATION_UNKNOWN) ? objType : requestType; OutputFormatter outputFormatter = formatterByMediaType.get(outputFormat.getTypeSubtype()); if (obj instanceof Collection) { if (outputFormatter != null) { return outputFormatter.formatCollection((Collection<?>) obj, objType, requestType); } } Transcoder transcoder = encoderRegistry.getTranscoder(objType, requestType); return transcoder.transcode(obj, objType, requestType); }
byte[] result = (byte[]) transcoder.transcode(contentKorean, jsonKorean, TEXT_PLAIN); assertArrayEquals(result, contentUTF); String strResult = (String) transcoder.transcode(contentKorean, jsonKorean, textPlainAsString); assertEquals(strResult, content); result = (byte[]) transcoder.transcode(contentKorean, jsonKorean, textPlainKorean); assertArrayEquals(result, contentKorean); result = (byte[]) transcoder.transcode(contentKorean, textPlainKorean, APPLICATION_JSON); assertArrayEquals(result, contentUTF); strResult = (String) transcoder.transcode(contentKorean, jsonKorean, jsonAsString); assertEquals(strResult, content); result = (byte[]) transcoder.transcode(contentKorean, textPlainKorean, jsonKorean); assertArrayEquals(result, contentKorean); result = (byte[]) transcoder.transcode(contentUTF, TEXT_PLAIN, jsonKorean); assertArrayEquals(result, contentKorean); result = (byte[]) transcoder.transcode(content, textPlainAsString, jsonKorean); assertArrayEquals(result, contentKorean); result = (byte[]) transcoder.transcode(contentUTF, APPLICATION_JSON, TEXT_PLAIN); assertArrayEquals(result, contentUTF); result = (byte[]) transcoder.transcode(contentUTF, APPLICATION_JSON, textPlainKorean); assertArrayEquals(result, contentKorean);