Search in sources :

Example 1 with PostFilterHandler

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);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) PostFilterHandler(com.mendmix.gateway.filter.PostFilterHandler) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) ByteArrayInputStream(java.io.ByteArrayInputStream) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) GatewayConfigs(com.mendmix.gateway.GatewayConfigs) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Flux(reactor.core.publisher.Flux) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ApiInfo(com.mendmix.common.model.ApiInfo) CustomRequestHeaders(com.mendmix.common.CustomRequestHeaders) GZIPOutputStream(java.util.zip.GZIPOutputStream) ResourceUtils(com.mendmix.common.util.ResourceUtils) Comparator(java.util.Comparator) FileCopyUtils(org.springframework.util.FileCopyUtils) HttpHeaders(org.springframework.http.HttpHeaders) PostFilterHandler(com.mendmix.gateway.filter.PostFilterHandler) ApiInfo(com.mendmix.common.model.ApiInfo) Flux(reactor.core.publisher.Flux) MediaType(org.springframework.http.MediaType) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with PostFilterHandler

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) PostFilterHandler(com.mendmix.gateway.filter.PostFilterHandler)

Aggregations

PostFilterHandler (com.mendmix.gateway.filter.PostFilterHandler)2 HttpHeaders (org.springframework.http.HttpHeaders)2 CustomRequestHeaders (com.mendmix.common.CustomRequestHeaders)1 ApiInfo (com.mendmix.common.model.ApiInfo)1 ResourceUtils (com.mendmix.common.util.ResourceUtils)1 GatewayConfigs (com.mendmix.gateway.GatewayConfigs)1 BizSystemModule (com.mendmix.gateway.model.BizSystemModule)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Publisher (org.reactivestreams.Publisher)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1