Search in sources :

Example 1 with JsonTranscoder

use of org.infinispan.server.core.dataconversion.JsonTranscoder in project infinispan by infinispan.

the class CacheResourceTest method testServerDeserialization.

@Test
public void testServerDeserialization() throws Exception {
    Object value = new Person();
    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);
    String expectError = "Class '" + value.getClass().getName() + "' blocked by deserialization allow list";
    RestEntity jsonEntity = RestEntity.create(APPLICATION_JSON, jsonMarshalled);
    RestEntity xmlEntity = RestEntity.create(APPLICATION_XML, xmlMarshalled);
    RestEntity javaEntity = RestEntity.create(APPLICATION_SERIALIZED_OBJECT, javaMarshalled);
    CompletionStage<RestResponse> jsonResponse = client.cache("objectCache").put("addr2", jsonEntity);
    assertThat(jsonResponse).isError();
    assertThat(jsonResponse).containsReturnedText(expectError);
    CompletionStage<RestResponse> xmlResponse = client.cache("objectCache").put("addr3", xmlEntity);
    assertThat(xmlResponse).isError();
    assertThat(xmlResponse).containsReturnedText(expectError);
    CompletionStage<RestResponse> serializationResponse = client.cache("objectCache").put("addr4", javaEntity);
    assertThat(serializationResponse).isError();
    assertThat(serializationResponse).containsReturnedText(expectError);
}
Also used : XMLTranscoder(org.infinispan.server.core.dataconversion.XMLTranscoder) RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Person(org.infinispan.rest.search.entity.Person) JsonTranscoder(org.infinispan.server.core.dataconversion.JsonTranscoder) JavaSerializationMarshaller(org.infinispan.commons.marshall.JavaSerializationMarshaller) Test(org.testng.annotations.Test)

Example 2 with JsonTranscoder

use of org.infinispan.server.core.dataconversion.JsonTranscoder in project infinispan by infinispan.

the class LifecycleCallbacks method cacheManagerStarting.

@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalConfiguration) {
    SerializationContextRegistry ctxRegistry = gcr.getComponent(SerializationContextRegistry.class);
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.PERSISTENCE, new PersistenceContextInitializerImpl());
    ClassAllowList classAllowList = gcr.getComponent(EmbeddedCacheManager.class).getClassAllowList();
    ClassLoader classLoader = globalConfiguration.classLoader();
    EncoderRegistry encoderRegistry = gcr.getComponent(EncoderRegistry.class);
    JsonTranscoder jsonTranscoder = new JsonTranscoder(classLoader, classAllowList);
    encoderRegistry.registerTranscoder(jsonTranscoder);
    registerXmlTranscoder(encoderRegistry, classLoader, classAllowList);
    // Allow transcoding between JBoss Marshalling and JSON
    if (encoderRegistry.isConversionSupported(MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_JBOSS_MARSHALLING)) {
        Transcoder jbossMarshallingTranscoder = encoderRegistry.getTranscoder(MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_JBOSS_MARSHALLING);
        encoderRegistry.registerTranscoder(new TwoStepTranscoder(jbossMarshallingTranscoder, jsonTranscoder));
    }
}
Also used : SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) TwoStepTranscoder(org.infinispan.encoding.impl.TwoStepTranscoder) ClassAllowList(org.infinispan.commons.configuration.ClassAllowList) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) Transcoder(org.infinispan.commons.dataconversion.Transcoder) TwoStepTranscoder(org.infinispan.encoding.impl.TwoStepTranscoder) JsonTranscoder(org.infinispan.server.core.dataconversion.JsonTranscoder) XMLTranscoder(org.infinispan.server.core.dataconversion.XMLTranscoder) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) JsonTranscoder(org.infinispan.server.core.dataconversion.JsonTranscoder)

Aggregations

JsonTranscoder (org.infinispan.server.core.dataconversion.JsonTranscoder)2 XMLTranscoder (org.infinispan.server.core.dataconversion.XMLTranscoder)2 RestEntity (org.infinispan.client.rest.RestEntity)1 RestResponse (org.infinispan.client.rest.RestResponse)1 ClassAllowList (org.infinispan.commons.configuration.ClassAllowList)1 Transcoder (org.infinispan.commons.dataconversion.Transcoder)1 JavaSerializationMarshaller (org.infinispan.commons.marshall.JavaSerializationMarshaller)1 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)1 TwoStepTranscoder (org.infinispan.encoding.impl.TwoStepTranscoder)1 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)1 EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)1 SerializationContextRegistry (org.infinispan.marshall.protostream.impl.SerializationContextRegistry)1 Person (org.infinispan.rest.search.entity.Person)1 Test (org.testng.annotations.Test)1