use of io.apiman.gateway.engine.components.IBufferFactoryComponent in project apiman-plugins by apiman.
the class JsonpPolicy 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 String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null);
if (callbackFunctionName != null) {
HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders());
final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name());
final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length();
// JSONP responses should have the Content-Type header set to "application/javascript"
httpHeaders.setContentType(APPLICATION_JAVASCRIPT);
// the Content-Length will need to be longer
httpHeaders.incrementContentLength(additionalContentLength);
final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class);
return new AbstractStream<ApiResponse>() {
private boolean firstChunk = true;
@Override
public ApiResponse getHead() {
return response;
}
@Override
protected void handleHead(ApiResponse head) {
}
@Override
public void write(IApimanBuffer chunk) {
if (firstChunk) {
IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length());
try {
buffer.append(callbackFunctionName, encoding);
buffer.append(OPEN_PARENTHESES, encoding);
} catch (UnsupportedEncodingException e) {
// TODO Review the exception handling. A better approach might be throwing an IOException.
throw new RuntimeException(e);
}
// Write callbackFunctionName(
super.write(buffer);
firstChunk = false;
}
super.write(chunk);
}
@Override
public void end() {
// Write close parenth ) on end if something has been written
if (!firstChunk) {
IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length());
try {
buffer.append(CLOSE_PARENTHESES, encoding);
} catch (UnsupportedEncodingException e) {
// TODO Review the exception handling. A better approach might be throwing an IOException.
throw new RuntimeException(e);
}
super.write(buffer);
}
super.end();
}
};
}
return null;
}
use of io.apiman.gateway.engine.components.IBufferFactoryComponent 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.gateway.engine.components.IBufferFactoryComponent 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;
}
use of io.apiman.gateway.engine.components.IBufferFactoryComponent 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);
}
}
};
}
use of io.apiman.gateway.engine.components.IBufferFactoryComponent in project apiman by apiman.
the class URLRewritingStreamTest method doTest.
/**
* @param fromRegexp
* @param toReplacement
* @param chunks
* @param expectedResult
*/
private void doTest(String fromRegexp, String toReplacement, String[] chunks, String expectedResult) {
IBufferFactoryComponent bufferFactory = new ByteBufferFactoryComponent();
URLRewritingStream stream = new URLRewritingStream(bufferFactory, (ApiResponse) null, fromRegexp, toReplacement);
RecordingHandler handler = new RecordingHandler(bufferFactory.createBuffer());
stream.bodyHandler(handler);
stream.endHandler(new IAsyncHandler<Void>() {
@Override
public void handle(Void result) {
}
});
for (String chunk : chunks) {
stream.write(bufferFactory.createBuffer(chunk));
}
stream.end();
String result = handler.getBuffer().toString();
Assert.assertEquals(expectedResult, result);
}
Aggregations