use of com.mendmix.gateway.filter.PostFilterHandler in project jeesuite-libs by vakinge.
the class RewriteBodyServerHttpResponse method writeWith.
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
HttpHeaders headers = exchange.getResponse().getHeaders();
if (headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
return super.writeWith(body);
}
boolean isNullBody = !headers.containsKey(HttpHeaders.TRANSFER_ENCODING) && headers.getContentLength() == 0;
if (isNullBody) {
for (PostFilterHandler handler : handlers) {
bodyString = handler.process(exchange, module, bodyString);
}
if (bodyString != null) {
byte[] bytes = bodyString.getBytes();
DataBuffer dataBuffer = bufferFactory.wrap(bytes);
headers.setContentLength(bytes.length);
return super.writeWith(Flux.just(dataBuffer));
}
return super.writeWith(body);
}
MediaType contentType = headers.getContentType();
if (contentType == null || !contentType.getType().equals(MediaType.APPLICATION_JSON.getType())) {
return super.writeWith(body);
}
boolean buildNewResponse = rewriteEnabled && !module.isBodyRewriteIgnore();
//
if (buildNewResponse) {
buildNewResponse = !headers.containsKey(CustomRequestHeaders.HEADER_RESP_KEEP);
}
//
if (buildNewResponse) {
buildNewResponse = !exchange.getRequest().getHeaders().containsKey(CustomRequestHeaders.HEADER_RESP_KEEP);
}
if (!buildNewResponse && GatewayConfigs.actionLogEnabled) {
ApiInfo apiInfo = module.getApiInfo(exchange.getRequest().getMethodValue(), exchange.getRequest().getPath().value());
buildNewResponse = apiInfo != null && apiInfo.isActionLog() && apiInfo.isResponseLog();
}
if (!buildNewResponse) {
return super.writeWith(body);
}
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBuffer dataBuffer = bufferFactory.join(dataBuffers);
byte[] content = handleReadResponseBody(dataBuffer);
return bufferFactory.wrap(content);
}));
}
return super.writeWith(body);
}
use of com.mendmix.gateway.filter.PostFilterHandler in project jeesuite-libs by vakinge.
the class RewriteBodyServerHttpResponse method handleReadResponseBody.
private byte[] handleReadResponseBody(DataBuffer dataBuffer) {
try {
HttpHeaders headers = exchange.getResponse().getHeaders();
boolean isGzip = GZIP_ENCODE.equalsIgnoreCase(headers.getFirst(HttpHeaders.CONTENT_ENCODING));
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
if (isGzip) {
content = gzipDecode(content);
}
bodyString = new String(content, StandardCharsets.UTF_8);
//
for (PostFilterHandler handler : handlers) {
bodyString = handler.process(exchange, module, bodyString);
}
byte[] bytes = bodyString.getBytes();
if (isGzip) {
bytes = gzipEncode(bytes);
}
if (!headers.containsKey(HttpHeaders.TRANSFER_ENCODING) || headers.containsKey(HttpHeaders.CONTENT_LENGTH)) {
headers.setContentLength(bytes.length);
}
return bytes;
} finally {
DataBufferUtils.release(dataBuffer);
}
}
Aggregations