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();
}
};
}
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;
}
Aggregations