Search in sources :

Example 1 with DataFormat

use of io.apiman.plugins.transformation_policy.beans.DataFormat 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 DataFormat

use of io.apiman.plugins.transformation_policy.beans.DataFormat 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)

Example 3 with DataFormat

use of io.apiman.plugins.transformation_policy.beans.DataFormat in project apiman-plugins by apiman.

the class TransformationPolicy method doApply.

@Override
protected void doApply(ApiRequest request, IPolicyContext context, TransformationConfigBean config, IPolicyChain<ApiRequest> chain) {
    DataFormat clientFormat = config.getClientFormat();
    DataFormat serverFormat = config.getServerFormat();
    if (isValidTransformation(clientFormat, serverFormat)) {
        context.setAttribute(CLIENT_FORMAT, clientFormat);
        context.setAttribute(SERVER_FORMAT, serverFormat);
        request.getHeaders().put(CONTENT_TYPE, serverFormat.getContentType());
        request.getHeaders().remove(CONTENT_LENGTH);
        request.getHeaders().put(ACCEPT, serverFormat.getContentType());
    }
    super.doApply(request, context, config, chain);
}
Also used : DataFormat(io.apiman.plugins.transformation_policy.beans.DataFormat)

Example 4 with DataFormat

use of io.apiman.plugins.transformation_policy.beans.DataFormat in project apiman-plugins by apiman.

the class TransformationPolicy method doApply.

@Override
protected void doApply(ApiResponse response, IPolicyContext context, TransformationConfigBean config, IPolicyChain<ApiResponse> chain) {
    final DataFormat clientFormat = (DataFormat) context.getAttribute(CLIENT_FORMAT, null);
    final DataFormat serverFormat = (DataFormat) context.getAttribute(SERVER_FORMAT, null);
    if (isValidTransformation(clientFormat, serverFormat)) {
        response.getHeaders().put(CONTENT_TYPE, clientFormat.getContentType());
        response.getHeaders().remove(CONTENT_LENGTH);
    }
    super.doApply(response, context, config, chain);
}
Also used : DataFormat(io.apiman.plugins.transformation_policy.beans.DataFormat)

Aggregations

DataFormat (io.apiman.plugins.transformation_policy.beans.DataFormat)4 IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)2 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)2 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)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