Search in sources :

Example 6 with AbstractStream

use of io.apiman.gateway.engine.io.AbstractStream 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

AbstractStream (io.apiman.gateway.engine.io.AbstractStream)6 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)6 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)4 IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)4 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)3 ComponentNotFoundException (io.apiman.gateway.engine.beans.exceptions.ComponentNotFoundException)2 ICacheStoreComponent (io.apiman.gateway.engine.components.ICacheStoreComponent)2 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)2 DataFormat (io.apiman.plugins.transformation_policy.beans.DataFormat)2 DataTransformer (io.apiman.plugins.transformation_policy.transformer.DataTransformer)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)1 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)1 CachedResponse (io.apiman.gateway.engine.impl.CachedResponse)1 IReadWriteStream (io.apiman.gateway.engine.io.IReadWriteStream)1 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)1 CacheConnectorInterceptor (io.apiman.gateway.engine.policies.caching.CacheConnectorInterceptor)1 CachingConfig (io.apiman.gateway.engine.policies.config.CachingConfig)1 CachingResourcesSettingsEntry (io.apiman.gateway.engine.policies.config.CachingResourcesSettingsEntry)1