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;
}
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);
}
}
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);
}
Aggregations