use of io.apiman.gateway.engine.policies.config.CachingConfig 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;
}
}
use of io.apiman.gateway.engine.policies.config.CachingConfig in project apiman by apiman.
the class CachingPolicyConfigTest method testParseConfiguration.
/**
* Test method for {@link io.apiman.gateway.engine.policies.CachingPolicy#parseConfiguration(java.lang.String)}.
*/
@Test
public void testParseConfiguration() {
CachingPolicy policy = new CachingPolicy();
// Empty config test
String config = "{}";
Object parsed = policy.parseConfiguration(config);
Assert.assertNotNull(parsed);
Assert.assertEquals(CachingConfig.class, parsed.getClass());
CachingConfig parsedConfig = (CachingConfig) parsed;
Assert.assertEquals(0, parsedConfig.getTtl());
// Sample real config
config = "{\n" + " \"ttl\" : 12345\n" + "}";
parsed = policy.parseConfiguration(config);
parsedConfig = (CachingConfig) parsed;
Assert.assertEquals(12345L, parsedConfig.getTtl());
}
Aggregations