Search in sources :

Example 16 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer 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 17 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer 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 18 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer in project apiman by apiman.

the class InfinispanCacheStoreComponent method getBinary.

/**
 * @see io.apiman.gateway.engine.components.ICacheStoreComponent#getBinary(java.lang.String, java.lang.Class, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@SuppressWarnings("unchecked")
@Override
public <T> void getBinary(String cacheKey, Class<T> type, IAsyncResultHandler<ISignalReadStream<T>> handler) {
    ISignalReadStream<T> rval;
    synchronized (mutex) {
        InfinispanCacheEntry entry = (InfinispanCacheEntry) getCache().get(cacheKey);
        if (entry != null) {
            if (entry.getExpiresOn() <= System.currentTimeMillis()) {
                getCache().remove(cacheKey);
                rval = null;
            } else {
                final T head = (T) entry.getHead();
                final IApimanBuffer dataBuffer = bufferFactory.createBuffer(entry.getData());
                rval = new ISignalReadStream<T>() {

                    boolean finished = false;

                    IAsyncHandler<IApimanBuffer> bodyHandler;

                    IAsyncHandler<Void> endHandler;

                    @Override
                    public void bodyHandler(IAsyncHandler<IApimanBuffer> bodyHandler) {
                        this.bodyHandler = bodyHandler;
                    }

                    @Override
                    public void endHandler(IAsyncHandler<Void> endHandler) {
                        this.endHandler = endHandler;
                    }

                    @Override
                    public T getHead() {
                        return head;
                    }

                    @Override
                    public boolean isFinished() {
                        return finished;
                    }

                    @Override
                    public void abort(Throwable t) {
                        finished = true;
                    }

                    @Override
                    public void transmit() {
                        bodyHandler.handle(dataBuffer);
                        endHandler.handle(null);
                    }
                };
            }
        } else {
            rval = null;
        }
    }
    handler.handle(AsyncResultImpl.create(rval));
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer)

Example 19 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer in project apiman by apiman.

the class AbstractCacheStoreComponent method putBinary.

/**
 * @see ICacheStoreComponent#putBinary(String, Object, long)
 */
@Override
public <T> ISignalWriteStream putBinary(final String cacheKey, final T jsonObject, final long timeToLive) throws IOException {
    final CacheEntry entry = new CacheEntry();
    entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000));
    entry.setHead(JSON_MAPPER.writeValueAsString(jsonObject));
    final IApimanBuffer data = bufferFactory.createBuffer();
    return new ISignalWriteStream() {

        boolean finished = false;

        boolean aborted = false;

        @Override
        public void abort(Throwable t) {
            finished = true;
            aborted = false;
        }

        @Override
        public boolean isFinished() {
            return finished;
        }

        @Override
        public void write(IApimanBuffer chunk) {
            data.append(chunk);
        }

        @Override
        public void end() {
            if (!aborted) {
                entry.setData(Base64.encodeBase64String(data.getBytes()));
                try {
                    getStore().put(cacheKey, entry, timeToLive);
                } catch (Throwable e) {
                    LOGGER.error("Error writing binary cache entry with key: {}", cacheKey, e);
                }
            }
            finished = true;
        }
    };
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream)

Example 20 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer in project apiman by apiman.

the class SimpleDataPolicy method getRequestDataHandler.

/**
 * @see io.apiman.gateway.engine.policy.IDataPolicy#getRequestDataHandler(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
 */
@Override
public IReadWriteStream<ApiRequest> getRequestDataHandler(final ApiRequest request, final IPolicyContext context, final Object policyConfiguration) {
    return new AbstractStream<ApiRequest>() {

        @Override
        public ApiRequest getHead() {
            return request;
        }

        @Override
        protected void handleHead(ApiRequest head) {
        }

        @Override
        public void write(IApimanBuffer chunk) {
            try {
                String chunkstr = chunk.toString("UTF-8");
                if (chunkstr.contains("$NAME")) {
                    chunkstr = chunkstr.replaceAll("\\$NAME", "Barry Allen");
                    IBufferFactoryComponent bufferFactory = context.<IBufferFactoryComponent>getComponent(IBufferFactoryComponent.class);
                    super.write(bufferFactory.createBuffer(chunkstr));
                } else {
                    super.write(chunk);
                }
            } catch (UnsupportedEncodingException e) {
                super.write(chunk);
            }
        }
    };
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IBufferFactoryComponent(io.apiman.gateway.engine.components.IBufferFactoryComponent) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest)

Aggregations

IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)31 ByteBuffer (io.apiman.gateway.engine.io.ByteBuffer)12 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)11 Test (org.junit.Test)11 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)7 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)7 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)6 IAsyncHandler (io.apiman.gateway.engine.async.IAsyncHandler)5 IEngineResult (io.apiman.gateway.engine.IEngineResult)4 IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)4 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)4 IApiRequestExecutor (io.apiman.gateway.engine.IApiRequestExecutor)3 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)3 CacheEntry (io.apiman.gateway.engine.storage.model.CacheEntry)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 IApiConnector (io.apiman.gateway.engine.IApiConnector)2 IConnectorConfig (io.apiman.gateway.engine.IConnectorConfig)2 IEngine (io.apiman.gateway.engine.IEngine)2 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)2