Search in sources :

Example 6 with EncoderRegistry

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;
}
Also used : WithinThreadExecutor(org.infinispan.util.concurrent.WithinThreadExecutor) Arrays(java.util.Arrays) CacheStream(org.infinispan.CacheStream) TimeServiceTicker(org.infinispan.commons.time.TimeServiceTicker) IntSets(org.infinispan.commons.util.IntSets) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) APPLICATION_OBJECT(org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT) Map(java.util.Map) KeyValueFilterConverterFactory(org.infinispan.filter.KeyValueFilterConverterFactory) ParamKeyValueFilterConverterFactory(org.infinispan.filter.ParamKeyValueFilterConverterFactory) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) RemovalListener(com.github.benmanes.caffeine.cache.RemovalListener) KeyValuePair(org.infinispan.util.KeyValuePair) KeyValueFilterConverter(org.infinispan.filter.KeyValueFilterConverter) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) CacheFilters.filterAndConvert(org.infinispan.filter.CacheFilters.filterAndConvert) Iterator(java.util.Iterator) CacheEntry(org.infinispan.container.entries.CacheEntry) BaseCacheStream(org.infinispan.BaseCacheStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Util(org.infinispan.commons.util.Util) Log(org.infinispan.server.hotrod.logging.Log) Set(java.util.Set) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) OperationStatus(org.infinispan.server.hotrod.OperationStatus) Closeable(java.io.Closeable) Optional(java.util.Optional) LogFactory(org.infinispan.commons.logging.LogFactory) DataConversion(org.infinispan.encoding.DataConversion) BitSet(java.util.BitSet) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CacheEntry(org.infinispan.container.entries.CacheEntry) DataConversion(org.infinispan.encoding.DataConversion) KeyValueFilterConverterFactory(org.infinispan.filter.KeyValueFilterConverterFactory) ParamKeyValueFilterConverterFactory(org.infinispan.filter.ParamKeyValueFilterConverterFactory) KeyValueFilterConverter(org.infinispan.filter.KeyValueFilterConverter) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache)

Example 7 with EncoderRegistry

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();
    }
}
Also used : Arrays(java.util.Arrays) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) APPLICATION_JSON(org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON) EncodingException(org.infinispan.commons.dataconversion.EncodingException) Optional(java.util.Optional) UnacceptableDataFormatException(org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) Log(org.infinispan.rest.logging.Log) RestRequest(org.infinispan.rest.framework.RestRequest) TEXT_PLAIN(org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN) UnacceptableDataFormatException(org.infinispan.rest.operations.exceptions.UnacceptableDataFormatException) EncodingException(org.infinispan.commons.dataconversion.EncodingException) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 8 with EncoderRegistry

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);
}
Also used : EncoderRegistryImpl(org.infinispan.marshall.core.EncoderRegistryImpl) DefaultTranscoder(org.infinispan.commons.dataconversion.DefaultTranscoder) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) JavaSerializationMarshaller(org.infinispan.commons.marshall.JavaSerializationMarshaller)

Example 9 with EncoderRegistry

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

Example 10 with EncoderRegistry

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);
}
Also used : EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) ProtobufMetadataManager(org.infinispan.query.remote.ProtobufMetadataManager)

Aggregations

EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)12 AdvancedCache (org.infinispan.AdvancedCache)5 Cache (org.infinispan.Cache)3 MediaType (org.infinispan.commons.dataconversion.MediaType)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 CacheManagerCallable (org.infinispan.test.CacheManagerCallable)3 Test (org.testng.annotations.Test)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ClassAllowList (org.infinispan.commons.configuration.ClassAllowList)2 Transcoder (org.infinispan.commons.dataconversion.Transcoder)2 GenericJBossMarshaller (org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller)2 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)2 Person (org.infinispan.test.data.Person)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 RemovalListener (com.github.benmanes.caffeine.cache.RemovalListener)1 Closeable (java.io.Closeable)1