use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.
the class DefaultIterationManager method start.
@Override
public IterationState start(AdvancedCache cache, BitSet segments, String filterConverterFactory, List<byte[]> filterConverterParams, MediaType requestValueType, int batch, boolean metadata) {
String iterationId = Util.threadLocalRandomUUID().toString();
EmbeddedCacheManager cacheManager = SecurityActions.getEmbeddedCacheManager(cache);
EncoderRegistry encoderRegistry = SecurityActions.getGlobalComponentRegistry(cacheManager).getComponent(EncoderRegistry.class);
DataConversion valueDataConversion = cache.getValueDataConversion();
Function<Object, Object> unmarshaller = p -> encoderRegistry.convert(p, requestValueType, APPLICATION_OBJECT);
MediaType storageMediaType = cache.getValueDataConversion().getStorageMediaType();
IterationSegmentsListener segmentListener = new IterationSegmentsListener();
CacheStream<CacheEntry<Object, Object>> stream;
Stream<CacheEntry<Object, Object>> filteredStream;
Function<Object, Object> resultTransformer = Function.identity();
AdvancedCache iterationCache = cache;
if (filterConverterFactory == null) {
stream = cache.cacheEntrySet().stream();
if (segments != null) {
stream.filterKeySegments(IntSets.from(segments.stream().iterator()));
}
filteredStream = stream.segmentCompletionListener(segmentListener);
} else {
KeyValueFilterConverterFactory factory = getFactory(filterConverterFactory);
KeyValuePair<KeyValueFilterConverter, Boolean> filter = buildFilter(factory, filterConverterParams.toArray(Util.EMPTY_BYTE_ARRAY_ARRAY), unmarshaller);
KeyValueFilterConverter customFilter = filter.getKey();
MediaType filterMediaType = customFilter.format();
if (filterMediaType != null && filterMediaType.equals(storageMediaType)) {
iterationCache = cache.withMediaType(filterMediaType, filterMediaType);
}
stream = iterationCache.cacheEntrySet().stream();
if (segments != null) {
stream.filterKeySegments(IntSets.from(segments.stream().iterator()));
}
IterationFilter iterationFilter = new IterationFilter(storageMediaType, requestValueType, Optional.of(filter.getKey()));
filteredStream = filterAndConvert(stream.segmentCompletionListener(segmentListener), iterationFilter);
if (filterMediaType != null && !storageMediaType.equals(requestValueType)) {
resultTransformer = valueDataConversion::fromStorage;
}
}
Iterator<CacheEntry<Object, Object>> iterator = filteredStream.iterator();
DefaultIterationState iterationState = new DefaultIterationState(iterationId, segmentListener, iterator, stream, batch, metadata, resultTransformer, new IterationReaper(this, iterationId));
iterationStateMap.put(iterationId, iterationState);
if (log.isTraceEnabled())
log.tracef("Started iteration %s", iterationId);
return iterationState;
}
use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.
the class MediaTypeUtils method negotiateMediaType.
/**
* Negotiates the {@link MediaType} to be used during the request execution
*
* @param cache the {@link AdvancedCache} associated with the request
* @param restRequest the {@link RestRequest} with the headers
* @return The negotiated MediaType
* @throws UnacceptableDataFormatException if no suitable {@link MediaType} could be found.
*/
static MediaType negotiateMediaType(AdvancedCache<?, ?> cache, EncoderRegistry registry, RestRequest restRequest) throws UnacceptableDataFormatException {
try {
String accept = restRequest.getAcceptHeader();
MediaType storageMedia = cache.getValueDataConversion().getStorageMediaType();
Optional<MediaType> negotiated = MediaType.parseList(accept).filter(media -> registry.isConversionSupported(storageMedia, media)).findFirst();
return negotiated.map(m -> {
if (!m.matchesAll())
return m;
MediaType storageMediaType = cache.getValueDataConversion().getStorageMediaType();
if (storageMediaType == null)
return m;
if (storageMediaType.equals(MediaType.APPLICATION_OBJECT))
return TEXT_PLAIN;
if (storageMediaType.match(MediaType.APPLICATION_PROTOSTREAM))
return APPLICATION_JSON;
return m;
}).orElseThrow(() -> Log.REST.unsupportedDataFormat(accept));
} catch (EncodingException e) {
throw new UnacceptableDataFormatException();
}
}
use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.
the class TranscoderRegistrationTest method testTranscoderLookup.
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 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);
}
use of org.infinispan.marshall.core.EncoderRegistry 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));
}
}
use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.
the class LifecycleManager method initProtobufMetadataManager.
private void initProtobufMetadataManager(BasicComponentRegistry bcr) {
ProtobufMetadataManagerImpl protobufMetadataManager = new ProtobufMetadataManagerImpl();
bcr.registerComponent(ProtobufMetadataManager.class, protobufMetadataManager, true).running();
EncoderRegistry encoderRegistry = bcr.getComponent(EncoderRegistry.class).wired();
encoderRegistry.registerWrapper(ProtobufWrapper.INSTANCE);
}
Aggregations