Search in sources :

Example 1 with CacheConnectorInterceptor

use of io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor in project apiman by apiman.

the class CachingPolicy method doApply.

/**
 * If the request is cached an {@link IConnectorInterceptor} is set in order to prevent the back-end connection to be established.
 * Otherwise an empty {@link CachedResponse} will be added to the context, this will be used to cache the response once it has been
 * received from the back-end API
 *
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Deprecated
@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final CachingConfig config, final IPolicyChain<ApiRequest> chain) {
    if (config.getTtl() > 0) {
        // Check to see if there is a cache entry for this request.  If so, we need to
        // short-circuit the connector factory by providing a connector interceptor
        String cacheId = buildCacheID(request, config);
        context.setAttribute(CACHE_ID_ATTR, cacheId);
        ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
        cache.getBinary(cacheId, ApiResponse.class, new IAsyncResultHandler<ISignalReadStream<ApiResponse>>() {

            @Override
            public void handle(IAsyncResult<ISignalReadStream<ApiResponse>> result) {
                if (result.isError()) {
                    chain.throwError(result.getError());
                } else {
                    ISignalReadStream<ApiResponse> cacheEntry = result.getResult();
                    if (cacheEntry != null) {
                        context.setConnectorInterceptor(new CacheConnectorInterceptor(cacheEntry));
                        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE);
                        context.setAttribute(CACHED_RESPONSE, cacheEntry.getHead());
                    } else {
                        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.TRUE);
                    }
                    chain.doApply(request);
                }
            }
        });
    } else {
        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE);
        chain.doApply(request);
    }
}
Also used : CacheConnectorInterceptor(io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor) ISignalReadStream(io.apiman.gateway.engine.io.ISignalReadStream) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent)

Example 2 with CacheConnectorInterceptor

use of io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor in project apiman by apiman.

the class CachingResourcesPolicy method doApply.

/**
 * If the request is cached an {@link IConnectorInterceptor} is set in order to prevent the back-end connection to be established.
 * Otherwise an empty {@link CachedResponse} will be added to the context, this will be used to cache the response once it has been
 * received from the back-end API
 *
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final CachingResourcesConfig config, final IPolicyChain<ApiRequest> chain) {
    List<CachingResourcesSettingsEntry> possibleMatchingEntries = new ArrayList<CachingResourcesSettingsEntry>();
    if (config.getTtl() > 0) {
        for (CachingResourcesSettingsEntry entry : config.getCachingResourcesSettingsEntries()) {
            // check if caching policy allows wildcards for http method or path pattern or check if the corresponding policy entry matches the request http method or path pattern.
            if (matchesHttpMethod(entry.getHttpMethod(), request.getType()) && matchesPolicyEntryVsActualValue(entry.getPathPattern(), request.getDestination())) {
                possibleMatchingEntries.add(entry);
            }
        }
        context.setAttribute(CACHE_POSSIBLE_MATCHING_ENTRIES, possibleMatchingEntries);
    }
    if (possibleMatchingEntries.size() > 0) {
        // Check to see if there is a cache entry for this request.
        // If so, we deliver the cached result by CacheConnectorInterceptor
        String cacheId = buildCacheID(request, context);
        context.setAttribute(CACHE_ID_ATTR, cacheId);
        ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
        cache.getBinary(cacheId, ApiResponse.class, new IAsyncResultHandler<ISignalReadStream<ApiResponse>>() {

            @Override
            public void handle(IAsyncResult<ISignalReadStream<ApiResponse>> result) {
                if (result.isError()) {
                    chain.throwError(result.getError());
                } else {
                    ISignalReadStream<ApiResponse> cacheEntry = result.getResult();
                    if (cacheEntry != null) {
                        markCacheEntryAsCached(cacheEntry, config);
                        context.setConnectorInterceptor(new CacheConnectorInterceptor(cacheEntry));
                        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE);
                        context.setAttribute(CACHED_RESPONSE, cacheEntry.getHead());
                    } else {
                        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.TRUE);
                    }
                    chain.doApply(request);
                }
            }
        });
    } else {
        context.setAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE);
        chain.doApply(request);
    }
}
Also used : CachingResourcesSettingsEntry(io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry) CacheConnectorInterceptor(io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor) ArrayList(java.util.ArrayList) ISignalReadStream(io.apiman.gateway.engine.io.ISignalReadStream) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent)

Aggregations

ICacheStoreComponent (io.apiman.gateway.engine.components.ICacheStoreComponent)2 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)2 CacheConnectorInterceptor (io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor)2 CachingResourcesSettingsEntry (io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry)1 ArrayList (java.util.ArrayList)1