Search in sources :

Example 1 with ICacheStoreComponent

use of io.apiman.gateway.engine.components.ICacheStoreComponent in project apiman by apiman.

the class CachingPolicy method responseDataHandler.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedDataPolicy#responseDataHandler(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
 */
@Deprecated
@Override
protected IReadWriteStream<ApiResponse> responseDataHandler(final ApiResponse response, IPolicyContext context, CachingConfig policyConfiguration) {
    // Possibly cache the response for future posterity.
    // Check the response code against list in config (empty/null list means cache all).
    final boolean shouldCache = (context.getAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE) && ofNullable(policyConfiguration.getStatusCodes()).map(statusCodes -> statusCodes.isEmpty() || statusCodes.contains(String.valueOf(response.getCode()))).orElse(true));
    if (shouldCache) {
        try {
            String cacheId = context.getAttribute(CACHE_ID_ATTR, null);
            ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
            final ISignalWriteStream writeStream = cache.putBinary(cacheId, response, policyConfiguration.getTtl());
            return new AbstractStream<ApiResponse>() {

                @Override
                public ApiResponse getHead() {
                    return response;
                }

                @Override
                protected void handleHead(ApiResponse head) {
                }

                @Override
                public void write(IApimanBuffer chunk) {
                    writeStream.write(chunk);
                    super.write(chunk);
                }

                @Override
                public void end() {
                    writeStream.end();
                    super.end();
                }
            };
        } catch (ComponentNotFoundException | IOException e) {
            // TODO log error
            return null;
        }
    } else {
        return null;
    }
}
Also used : IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) ISignalReadStream(io.apiman.gateway.engine.io.ISignalReadStream) Optional.ofNullable(java.util.Optional.ofNullable) CacheConnectorInterceptor(io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor) IOException(java.io.IOException) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) CachingConfig(io.apiman.gateway.engine.policies.config.CachingConfig) IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IConnectorInterceptor(io.apiman.gateway.engine.policy.IConnectorInterceptor) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) IReadWriteStream(io.apiman.gateway.engine.io.IReadWriteStream) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) CachedResponse(io.apiman.gateway.engine.impl.CachedResponse) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IOException(java.io.IOException) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse)

Example 2 with ICacheStoreComponent

use of io.apiman.gateway.engine.components.ICacheStoreComponent 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 3 with ICacheStoreComponent

use of io.apiman.gateway.engine.components.ICacheStoreComponent in project apiman by apiman.

the class CachingResourcesPolicy method responseDataHandler.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedDataPolicy#responseDataHandler(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
 */
@Override
protected IReadWriteStream<ApiResponse> responseDataHandler(final ApiResponse response, IPolicyContext context, CachingResourcesConfig policyConfiguration) {
    if (response == null) {
        // if the response is empty because of a policy failure before we end here and return null
        return null;
    }
    List<CachingResourcesSettingsEntry> possibleMatchingCachingEntries = context.getAttribute(CACHE_POSSIBLE_MATCHING_ENTRIES, new ArrayList<CachingResourcesSettingsEntry>());
    boolean isAMatch = false;
    for (CachingResourcesSettingsEntry entry : possibleMatchingCachingEntries) {
        isAMatch = isAMatch || matchesPolicyEntryVsActualValue(entry.getStatusCode(), String.valueOf(response.getCode()));
    }
    // Possibly cache the response for future posterity.
    final boolean shouldCache = context.getAttribute(SHOULD_CACHE_ATTR, Boolean.FALSE) && isAMatch;
    if (shouldCache) {
        try {
            String cacheId = context.getAttribute(CACHE_ID_ATTR, null);
            ICacheStoreComponent cache = context.getComponent(ICacheStoreComponent.class);
            final ISignalWriteStream writeStream = cache.putBinary(cacheId, response, policyConfiguration.getTtl());
            return new AbstractStream<ApiResponse>() {

                @Override
                public ApiResponse getHead() {
                    return response;
                }

                @Override
                protected void handleHead(ApiResponse head) {
                }

                @Override
                public void write(IApimanBuffer chunk) {
                    writeStream.write(chunk);
                    super.write(chunk);
                }

                @Override
                public void end() {
                    writeStream.end();
                    super.end();
                }
            };
        } catch (ComponentNotFoundException | IOException e) {
            throw new RuntimeException(e);
        }
    }
    return null;
}
Also used : CachingResourcesSettingsEntry(io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) IOException(java.io.IOException) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) ComponentNotFoundException(io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException) ICacheStoreComponent(io.apiman.gateway.engine.components.ICacheStoreComponent)

Example 4 with ICacheStoreComponent

use of io.apiman.gateway.engine.components.ICacheStoreComponent 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)4 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)3 CacheConnectorInterceptor (io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor)3 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)2 ComponentNotFoundException (io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException)2 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)2 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)2 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)2 CachingResourcesSettingsEntry (io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry)2 IOException (java.io.IOException)2 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)1 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)1 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)1 CachedResponse (io.apiman.gateway.engine.impl.CachedResponse)1 IReadWriteStream (io.apiman.gateway.engine.io.IReadWriteStream)1 CachingConfig (io.apiman.gateway.engine.policies.config.CachingConfig)1 IConnectorInterceptor (io.apiman.gateway.engine.policy.IConnectorInterceptor)1 IDataPolicy (io.apiman.gateway.engine.policy.IDataPolicy)1 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)1 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)1