Search in sources :

Example 6 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project wildfly by wildfly.

the class ProtoStreamOperation method findMarshaller.

/**
 * Returns a marshaller suitable of marshalling an object of the specified type.
 * @param <T> the type of the associated marshaller
 * @param <V> the type of the object to be marshalled
 * @param javaClass the type of the value to be written.
 * @return a marshaller suitable for the specified type
 * @throws IllegalArgumentException if no suitable marshaller exists
 */
@SuppressWarnings("unchecked")
default <T, V extends T> ProtoStreamMarshaller<T> findMarshaller(Class<V> javaClass) {
    ImmutableSerializationContext context = this.getSerializationContext();
    Class<?> targetClass = javaClass;
    IllegalArgumentException exception = null;
    while (targetClass != null) {
        try {
            return (ProtoStreamMarshaller<T>) context.getMarshaller((Class<T>) targetClass);
        } catch (IllegalArgumentException e) {
            // If no marshaller was found, check super class
            if (exception == null) {
                exception = e;
            }
            targetClass = targetClass.getSuperclass();
        }
    }
    throw exception;
}
Also used : ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext)

Example 7 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project infinispan by infinispan.

the class ProtostreamTranscoder method doTranscode.

@Override
public Object doTranscode(Object content, MediaType contentType, MediaType destinationType) {
    try {
        if (destinationType.match(MediaType.APPLICATION_PROTOSTREAM)) {
            if (contentType.match(APPLICATION_JSON)) {
                content = addTypeIfNeeded(content);
                return fromJsonCascading(content);
            }
            if (contentType.match(APPLICATION_UNKNOWN) || contentType.match(APPLICATION_PROTOSTREAM)) {
                return content;
            }
            if (contentType.match(TEXT_PLAIN)) {
                content = StandardConversions.convertTextToObject(content, contentType);
            }
            return marshall(content, destinationType);
        }
        if (destinationType.match(MediaType.APPLICATION_OCTET_STREAM)) {
            Object unmarshalled = content instanceof byte[] ? unmarshall((byte[]) content, contentType, destinationType) : content;
            if (unmarshalled instanceof byte[]) {
                return unmarshalled;
            }
            ImmutableSerializationContext ctx = getCtxForMarshalling(unmarshalled);
            return StandardConversions.convertJavaToProtoStream(unmarshalled, MediaType.APPLICATION_OBJECT, ctx);
        }
        if (destinationType.match(MediaType.TEXT_PLAIN)) {
            Object decoded = unmarshallCascading((byte[]) content);
            if (decoded == null)
                return null;
            return decoded.toString().getBytes(destinationType.getCharset());
        }
        if (destinationType.match(MediaType.APPLICATION_OBJECT)) {
            return unmarshall((byte[]) content, contentType, destinationType);
        }
        if (destinationType.match(MediaType.APPLICATION_JSON)) {
            String converted = toJsonCascading((byte[]) content);
            String convertType = destinationType.getClassType();
            return convertType == null ? StandardConversions.convertCharset(converted, contentType.getCharset(), destinationType.getCharset()) : converted;
        }
        if (destinationType.equals(APPLICATION_UNKNOWN)) {
            // TODO: Remove wrapping of byte[] into WrappedByteArray from the Hot Rod Multimap operations.
            if (content instanceof WrappedByteArray)
                return content;
            ImmutableSerializationContext ctx = getCtxForMarshalling(content);
            return StandardConversions.convertJavaToProtoStream(content, MediaType.APPLICATION_OBJECT, ctx);
        }
        throw logger.unsupportedContent(ProtostreamTranscoder.class.getSimpleName(), content);
    } catch (InterruptedException | IOException e) {
        throw logger.errorTranscoding(ProtostreamTranscoder.class.getSimpleName(), e);
    }
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) IOException(java.io.IOException)

Example 8 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project infinispan by infinispan.

the class ProtostreamTranscoder method unmarshall.

private Object unmarshall(byte[] bytes, MediaType contentType, MediaType destinationType) throws IOException {
    if (isWrapped(contentType))
        return unmarshallCascading(bytes);
    String type = destinationType.getClassType();
    if (type == null)
        throw logger.missingTypeForUnwrappedPayload();
    Class<?> destination = Util.loadClass(type, classLoader);
    ImmutableSerializationContext ctx = getCtxForMarshalling(destination);
    return ProtobufUtil.fromByteArray(ctx, bytes, destination);
}
Also used : ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext)

Aggregations

ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)8 SerializationContextRegistry (org.infinispan.marshall.protostream.impl.SerializationContextRegistry)3 MediaType (org.infinispan.commons.dataconversion.MediaType)2 DataConversion (org.infinispan.encoding.DataConversion)2 ComponentRegistry (org.infinispan.factories.ComponentRegistry)2 Descriptor (org.infinispan.protostream.descriptors.Descriptor)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Types (java.sql.Types)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1