Search in sources :

Example 1 with DataTransformer

use of io.apiman.plugins.transformation_policy.transformer.DataTransformer in project apiman-plugins by apiman.

the class TransformationPolicy 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, IPolicyContext context, Object policyConfiguration) {
    final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class);
    final int contentLength = request.getHeaders().containsKey(CONTENT_LENGTH) ? Integer.parseInt(request.getHeaders().get(CONTENT_LENGTH)) : 0;
    return new AbstractStream<ApiRequest>() {

        private IApimanBuffer readBuffer = bufferFactory.createBuffer(contentLength);

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

        @Override
        protected void handleHead(ApiRequest head) {
        }

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

        @Override
        public void end() {
            final DataFormat clientFormat = (DataFormat) context.getAttribute(CLIENT_FORMAT, null);
            final DataFormat serverFormat = (DataFormat) context.getAttribute(SERVER_FORMAT, null);
            if (readBuffer.length() > 0) {
                if (isValidTransformation(clientFormat, serverFormat)) {
                    DataTransformer dataTransformer = DataTransformerFactory.getDataTransformer(clientFormat, serverFormat);
                    IApimanBuffer writeBuffer = bufferFactory.createBuffer(readBuffer.length());
                    String data = dataTransformer.transform(new String(readBuffer.getBytes()));
                    writeBuffer.append(data);
                    super.write(writeBuffer);
                } else {
                    super.write(readBuffer);
                }
            }
            super.end();
        }
    };
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IBufferFactoryComponent(io.apiman.gateway.engine.components.IBufferFactoryComponent) DataTransformer(io.apiman.plugins.transformation_policy.transformer.DataTransformer) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) DataFormat(io.apiman.plugins.transformation_policy.beans.DataFormat) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest)

Example 2 with DataTransformer

use of io.apiman.plugins.transformation_policy.transformer.DataTransformer in project apiman-plugins by apiman.

the class TransformationPolicy method getResponseDataHandler.

/**
 * @see io.apiman.gateway.engine.policy.IDataPolicy#getResponseDataHandler(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object)
 */
@Override
public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) {
    final DataFormat clientFormat = (DataFormat) context.getAttribute(CLIENT_FORMAT, null);
    final DataFormat serverFormat = (DataFormat) context.getAttribute(SERVER_FORMAT, null);
    if (isValidTransformation(clientFormat, serverFormat)) {
        final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class);
        final int contentLength = response.getHeaders().containsKey(CONTENT_LENGTH) ? Integer.parseInt(response.getHeaders().get(CONTENT_LENGTH)) : 0;
        return new AbstractStream<ApiResponse>() {

            private IApimanBuffer readBuffer = bufferFactory.createBuffer(contentLength);

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

            @Override
            protected void handleHead(ApiResponse head) {
            }

            @Override
            public void write(IApimanBuffer chunk) {
                byte[] bytes = chunk.getBytes();
                readBuffer.append(bytes);
            }

            @Override
            public void end() {
                if (readBuffer.length() > 0) {
                    DataTransformer dataTransformer = DataTransformerFactory.getDataTransformer(serverFormat, clientFormat);
                    IApimanBuffer writeBuffer = bufferFactory.createBuffer(readBuffer.length());
                    String data = dataTransformer.transform(new String(readBuffer.getBytes()));
                    writeBuffer.append(data);
                    super.write(writeBuffer);
                }
                super.end();
            }
        };
    }
    return null;
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IBufferFactoryComponent(io.apiman.gateway.engine.components.IBufferFactoryComponent) DataTransformer(io.apiman.plugins.transformation_policy.transformer.DataTransformer) DataFormat(io.apiman.plugins.transformation_policy.beans.DataFormat) AbstractStream(io.apiman.gateway.engine.io.AbstractStream) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse)

Aggregations

IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)2 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)2 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)2 DataFormat (io.apiman.plugins.transformation_policy.beans.DataFormat)2 DataTransformer (io.apiman.plugins.transformation_policy.transformer.DataTransformer)2 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)1 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)1