Optimize WebUtils.java
This commit is contained in:
@@ -51,7 +51,7 @@ spring:
|
||||
aggregate:
|
||||
redis:
|
||||
# need replace
|
||||
host: 6.6.6.6 #please input the redis host (default:localhost)
|
||||
host: 172.25.62.191 #please input the redis host (default:localhost)
|
||||
# need replace
|
||||
port: 6379 #please input the redis port (default:6379)
|
||||
# need replace
|
||||
|
||||
@@ -24,7 +24,6 @@ import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import we.util.Consts.DP;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -89,6 +88,14 @@ public abstract class JacksonUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T readValue(byte[] bytes, Class<T> clz) {
|
||||
try {
|
||||
return m.readValue(bytes, clz);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String writeValueAsString(Object value) {
|
||||
try {
|
||||
return m.writeValueAsString(value);
|
||||
@@ -96,6 +103,14 @@ public abstract class JacksonUtils {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] writeValueAsBytes(Object value) {
|
||||
try {
|
||||
return m.writeValueAsBytes(value);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DateDeseralizer extends JsonDeserializer<Date> {
|
||||
|
||||
@@ -127,7 +127,7 @@ public class ApiPairingController {
|
||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
return response.writeWith(Mono.just(response.bufferFactory().wrap(docsJson.getBytes())));
|
||||
} else {
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.FORBIDDEN, null, auth.msg);
|
||||
return WebUtils.response(response, HttpStatus.FORBIDDEN, null, auth.msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ public class ApiPairingController {
|
||||
if (auth.code == Result.SUCC) {
|
||||
String managerUrl = fizzMangerConfig.managerUrl;
|
||||
if (managerUrl == null) {
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, null, "no fizz manager url config");
|
||||
return WebUtils.response(response, HttpStatus.INTERNAL_SERVER_ERROR, null, "no fizz manager url config");
|
||||
}
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String uri = request.getURI().toString();
|
||||
@@ -185,7 +185,7 @@ public class ApiPairingController {
|
||||
);
|
||||
|
||||
} else {
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.FORBIDDEN, null, auth.msg);
|
||||
return WebUtils.response(response, HttpStatus.FORBIDDEN, null, auth.msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ class FizzApiPairingHttpHandler implements HttpHandler {
|
||||
ApiPairingInfo apiPairingInfo = apiPairingInfoService.get(service);
|
||||
if (apiPairingInfo == null) {
|
||||
log.warn("{}{} service no api pairing info", logPrefix, service);
|
||||
return WebUtils.buildDirectResponse(response, HttpStatus.FORBIDDEN, null, service + " service no api pairing info").then(response.setComplete());
|
||||
return WebUtils.response(response, HttpStatus.FORBIDDEN, null, service + " service no api pairing info").then(response.setComplete());
|
||||
}
|
||||
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
|
||||
@@ -136,7 +136,7 @@ public class CallbackFilter extends FizzWebFilter {
|
||||
httpHeaders.addAll(h, v);
|
||||
}
|
||||
);
|
||||
return WebUtils.buildDirectResponse(exchange.getResponse(), HttpStatus.OK, httpHeaders, cc.respBody);
|
||||
return WebUtils.response(exchange.getResponse(), HttpStatus.OK, httpHeaders, cc.respBody);
|
||||
}
|
||||
|
||||
private HashMap<String, ServiceInstance> getService2instMap(ApiConfig ac) {
|
||||
|
||||
@@ -116,7 +116,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
||||
LogService.setBizId(traceId);
|
||||
if (!apiConfigService.serviceConfigMap.containsKey(service)) {
|
||||
String json = WebUtils.jsonRespBody(HttpStatus.FORBIDDEN.value(), "no service " + service + " in flow config", traceId);
|
||||
return WebUtils.buildJsonDirectResponse(exchange, HttpStatus.FORBIDDEN, null, json);
|
||||
return WebUtils.responseJson(exchange, HttpStatus.FORBIDDEN, null, json);
|
||||
}
|
||||
String app = WebUtils.getAppId(exchange);
|
||||
path = WebUtils.getClientReqPath(exchange);
|
||||
|
||||
@@ -241,7 +241,7 @@ public class RouteFilter extends FizzWebFilter {
|
||||
)
|
||||
.flatMap(
|
||||
dubboRpcResponseBody -> {
|
||||
Mono<Void> m = WebUtils.buildJsonDirectResponse(exchange, HttpStatus.OK, null, JacksonUtils.writeValueAsString(dubboRpcResponseBody));
|
||||
Mono<Void> m = WebUtils.responseJson(exchange, HttpStatus.OK, null, JacksonUtils.writeValueAsString(dubboRpcResponseBody));
|
||||
return m;
|
||||
}
|
||||
)
|
||||
|
||||
@@ -20,6 +20,7 @@ package we.util;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -41,6 +42,8 @@ import we.proxy.Route;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -88,8 +91,6 @@ public abstract class WebUtils {
|
||||
|
||||
private static List<String> timestampHeaders = Stream.of(SystemConfig.FIZZ_TIMESTAMP).collect(Collectors.toList());
|
||||
|
||||
private static final String app = "app";
|
||||
|
||||
public static final String TRACE_ID = "traid@";
|
||||
|
||||
public static final String BACKEND_SERVICE = "bs@";
|
||||
@@ -231,102 +232,59 @@ public abstract class WebUtils {
|
||||
return exchange.getAttribute(ROUTE);
|
||||
}
|
||||
|
||||
public static Mono<Void> getDirectResponse(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(WebUtils.directResponse);
|
||||
public static Mono<Void> response(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String body) {
|
||||
return response(exchange.getResponse(), status, headers, body);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Map<String, FilterResult> getFilterContext(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(FILTER_CONTEXT);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static FilterResult getFilterResult(ServerWebExchange exchange, String filter) {
|
||||
return getFilterContext(exchange).get(filter);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Map<String, Object> getFilterResultData(ServerWebExchange exchange, String filter) {
|
||||
return getFilterResult(exchange, filter).data;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getFilterResultDataItem(ServerWebExchange exchange, String filter, String key) {
|
||||
return getFilterResultData(exchange, filter).get(key);
|
||||
}
|
||||
|
||||
public static Mono<Void> buildDirectResponse(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String bodyContent) {
|
||||
return buildDirectResponse(exchange.getResponse(), status, headers, bodyContent);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono buildDirectResponseAndBindContext(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String bodyContent) {
|
||||
Mono<Void> mv = buildDirectResponse(exchange, status, headers, bodyContent);
|
||||
exchange.getAttributes().put(WebUtils.directResponse, mv);
|
||||
return mv;
|
||||
}
|
||||
|
||||
public static Mono buildJsonDirectResponse(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
return buildDirectResponse(exchange, status, headers, json);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono buildJsonDirectResponseAndBindContext(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
return buildDirectResponseAndBindContext(exchange, status, headers, json);
|
||||
}
|
||||
|
||||
public static Mono<Void> buildDirectResponse(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, String bodyContent) {
|
||||
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, DataBuffer body) {
|
||||
if (clientResp.isCommitted()) {
|
||||
log.warn(bodyContent + ", but client resp is committed, " + clientResp.getStatusCode());
|
||||
return Mono.error(new RuntimeException(bodyContent, null, false, false) {});
|
||||
String s = body.toString(StandardCharsets.UTF_8);
|
||||
String msg = "try to response: " + s + ", but server http response is committed and it's status: " + clientResp.getStatusCode();
|
||||
log.warn(msg);
|
||||
return Mono.error(Utils.runtimeExceptionWithoutStack(msg));
|
||||
}
|
||||
if (status != null) {
|
||||
clientResp.setStatusCode(status);
|
||||
}
|
||||
if (headers != null) {
|
||||
headers.forEach(
|
||||
(h, vs) -> {
|
||||
clientResp.getHeaders().addAll(h, vs);
|
||||
}
|
||||
);
|
||||
headers.forEach( (h, vs) -> {clientResp.getHeaders().addAll(h, vs);} );
|
||||
}
|
||||
if (bodyContent == null) {
|
||||
bodyContent = Consts.S.EMPTY;
|
||||
if (body == null) {
|
||||
body = NettyDataBufferUtils.EMPTY_DATA_BUFFER;
|
||||
}
|
||||
return clientResp
|
||||
.writeWith(Mono.just(clientResp.bufferFactory().wrap(bodyContent.getBytes())));
|
||||
return clientResp.writeWith(Mono.just(body));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitSuccessFilterResult(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
||||
FilterResult fr = FilterResult.SUCCESS_WITH(filter, data);
|
||||
bind(exchange, filter, fr);
|
||||
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, ByteBuffer body) {
|
||||
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body);
|
||||
return response(clientResp, status, headers, dataBuffer);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono transmitSuccessFilterResultAndEmptyMono(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
||||
transmitSuccessFilterResult(exchange, filter, data);
|
||||
return Mono.empty();
|
||||
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, byte[] body) {
|
||||
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body);
|
||||
return response(clientResp, status, headers, dataBuffer);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter) {
|
||||
FilterResult fr = FilterResult.FAIL(filter);
|
||||
bind(exchange, filter, fr);
|
||||
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, String body) {
|
||||
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
|
||||
return response(clientResp, status, headers, dataBuffer);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter, Throwable cause) {
|
||||
FilterResult fr = FilterResult.FAIL_WITH(filter, cause);
|
||||
bind(exchange, filter, fr);
|
||||
public static Mono responseJson(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, Object object) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
byte[] bytes = JacksonUtils.writeValueAsBytes(object);
|
||||
return response(exchange.getResponse(), status, headers, bytes);
|
||||
}
|
||||
|
||||
public static Mono responseJson(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return response(exchange, status, headers, json);
|
||||
}
|
||||
|
||||
private static void bind(ServerWebExchange exchange, String filter, FilterResult fr) {
|
||||
@@ -335,11 +293,6 @@ public abstract class WebUtils {
|
||||
fc.put(PREV_FILTER_RESULT, fr);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static FilterResult getPrevFilterResult(ServerWebExchange exchange) {
|
||||
return getFilterContext(exchange).get(PREV_FILTER_RESULT);
|
||||
}
|
||||
|
||||
public static String getClientReqPath(ServerWebExchange exchange) {
|
||||
String p = exchange.getAttribute(clientRequestPath);
|
||||
if (p == null) {
|
||||
@@ -412,7 +365,6 @@ public abstract class WebUtils {
|
||||
|
||||
public static String getClientReqPathQuery(ServerWebExchange exchange) {
|
||||
String pathQry = getClientReqPath(exchange);
|
||||
// String qry = getClientReqQuery(exchange);
|
||||
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
||||
if (!queryParams.isEmpty()) {
|
||||
String qry = toQueryString(queryParams);
|
||||
@@ -557,16 +509,6 @@ public abstract class WebUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, int code, String msg) {
|
||||
return responseError(exchange, filter, code, msg, null, true);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, int code, String msg, Throwable t) {
|
||||
return responseError(exchange, filter, code, msg, t, true);
|
||||
}
|
||||
|
||||
public static Mono<Void> responseError(ServerWebExchange exchange, int code, String msg) {
|
||||
return responseError(exchange, null, code, msg, null, false);
|
||||
}
|
||||
@@ -575,35 +517,6 @@ public abstract class WebUtils {
|
||||
return responseError(exchange, reporter, code, msg, t, false);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, HttpStatus httpStatus) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
String rid = getTraceId(exchange);
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
request2stringBuilder(exchange, b);
|
||||
b.append(Consts.S.LINE_SEPARATOR);
|
||||
b.append(filter).append(Consts.S.SPACE).append(httpStatus);
|
||||
log.error(b.toString(), LogService.BIZ_ID, rid);
|
||||
transmitFailFilterResult(exchange, filter);
|
||||
return buildDirectResponseAndBindContext(exchange, httpStatus, new HttpHeaders(), Consts.S.EMPTY);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, HttpStatus httpStatus,
|
||||
HttpHeaders headers, String content) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
String rid = getTraceId(exchange);
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
request2stringBuilder(exchange, b);
|
||||
b.append(Consts.S.LINE_SEPARATOR);
|
||||
b.append(filter).append(Consts.S.SPACE).append(httpStatus);
|
||||
log.error(b.toString(), LogService.BIZ_ID, rid);
|
||||
transmitFailFilterResult(exchange, filter);
|
||||
headers = headers == null ? new HttpHeaders() : headers;
|
||||
content = StringUtils.isBlank(content) ? Consts.S.EMPTY : content;
|
||||
return buildDirectResponseAndBindContext(exchange, httpStatus, headers, content);
|
||||
}
|
||||
|
||||
public static String getOriginIp(ServerWebExchange exchange) {
|
||||
String ip = exchange.getAttribute(originIp);
|
||||
if (ip == null) {
|
||||
@@ -686,12 +599,10 @@ public abstract class WebUtils {
|
||||
String name = param.getKey();
|
||||
List<String> values = param.getValue();
|
||||
if (values.isEmpty()) {
|
||||
// b.append(URLEncoder.encode(name, Consts.C.UTF8));
|
||||
b.append(name);
|
||||
} else {
|
||||
int vs = values.size();
|
||||
for (int i = 0; i < vs; ) {
|
||||
// b.append(URLEncoder.encode(name, Consts.C.UTF8));
|
||||
b.append(name);
|
||||
String v = values.get(i);
|
||||
if (v != null) {
|
||||
@@ -719,11 +630,159 @@ public abstract class WebUtils {
|
||||
}
|
||||
}
|
||||
|
||||
// private static String encodeQueryParamComp(String source) {
|
||||
// try {
|
||||
// return URLEncoder.encode(source, Consts.C.UTF8);
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// return URLEncoder.encode(source);
|
||||
// }
|
||||
// }
|
||||
// the method below will be deprecated.
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> getDirectResponse(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(WebUtils.directResponse);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Map<String, FilterResult> getFilterContext(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(FILTER_CONTEXT);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static FilterResult getFilterResult(ServerWebExchange exchange, String filter) {
|
||||
return getFilterContext(exchange).get(filter);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Map<String, Object> getFilterResultData(ServerWebExchange exchange, String filter) {
|
||||
return getFilterResult(exchange, filter).data;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getFilterResultDataItem(ServerWebExchange exchange, String filter, String key) {
|
||||
return getFilterResultData(exchange, filter).get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* can replace with response(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String body) method.
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public static Mono<Void> buildDirectResponse(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String body) {
|
||||
return buildDirectResponse(exchange.getResponse(), status, headers, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* can replace with response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, String body) method.
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public static Mono<Void> buildDirectResponse(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, String body) {
|
||||
if (clientResp.isCommitted()) {
|
||||
String msg = "try to response: " + body + ", but server http response is committed and it's status: " + clientResp.getStatusCode();
|
||||
log.warn(msg);
|
||||
return Mono.error(Utils.runtimeExceptionWithoutStack(msg));
|
||||
}
|
||||
if (status != null) {
|
||||
clientResp.setStatusCode(status);
|
||||
}
|
||||
if (headers != null) {
|
||||
headers.forEach( (h, vs) -> {clientResp.getHeaders().addAll(h, vs);} );
|
||||
}
|
||||
if (body == null) {
|
||||
body = Consts.S.EMPTY;
|
||||
}
|
||||
return clientResp.writeWith(Mono.just(clientResp.bufferFactory().wrap(body.getBytes())));
|
||||
}
|
||||
|
||||
/**
|
||||
* can replace with responseJson(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) method.
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public static Mono buildJsonDirectResponse(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return buildDirectResponse(exchange, status, headers, json);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono buildDirectResponseAndBindContext(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String bodyContent) {
|
||||
Mono<Void> mv = buildDirectResponse(exchange, status, headers, bodyContent);
|
||||
exchange.getAttributes().put(WebUtils.directResponse, mv);
|
||||
return mv;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono buildJsonDirectResponseAndBindContext(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String json) {
|
||||
if (headers == null) {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
return buildDirectResponseAndBindContext(exchange, status, headers, json);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitSuccessFilterResult(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
||||
FilterResult fr = FilterResult.SUCCESS_WITH(filter, data);
|
||||
bind(exchange, filter, fr);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono transmitSuccessFilterResultAndEmptyMono(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
||||
transmitSuccessFilterResult(exchange, filter, data);
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter) {
|
||||
FilterResult fr = FilterResult.FAIL(filter);
|
||||
bind(exchange, filter, fr);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter, Throwable cause) {
|
||||
FilterResult fr = FilterResult.FAIL_WITH(filter, cause);
|
||||
bind(exchange, filter, fr);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static FilterResult getPrevFilterResult(ServerWebExchange exchange) {
|
||||
return getFilterContext(exchange).get(PREV_FILTER_RESULT);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, int code, String msg) {
|
||||
return responseError(exchange, filter, code, msg, null, true);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, int code, String msg, Throwable t) {
|
||||
return responseError(exchange, filter, code, msg, t, true);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, HttpStatus httpStatus) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
String rid = getTraceId(exchange);
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
request2stringBuilder(exchange, b);
|
||||
b.append(Consts.S.LINE_SEPARATOR);
|
||||
b.append(filter).append(Consts.S.SPACE).append(httpStatus);
|
||||
log.error(b.toString(), LogService.BIZ_ID, rid);
|
||||
transmitFailFilterResult(exchange, filter);
|
||||
return buildDirectResponseAndBindContext(exchange, httpStatus, new HttpHeaders(), Consts.S.EMPTY);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Mono<Void> responseErrorAndBindContext(ServerWebExchange exchange, String filter, HttpStatus httpStatus,
|
||||
HttpHeaders headers, String content) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
String rid = getTraceId(exchange);
|
||||
StringBuilder b = ThreadContext.getStringBuilder();
|
||||
request2stringBuilder(exchange, b);
|
||||
b.append(Consts.S.LINE_SEPARATOR);
|
||||
b.append(filter).append(Consts.S.SPACE).append(httpStatus);
|
||||
log.error(b.toString(), LogService.BIZ_ID, rid);
|
||||
transmitFailFilterResult(exchange, filter);
|
||||
headers = headers == null ? new HttpHeaders() : headers;
|
||||
content = StringUtils.isBlank(content) ? Consts.S.EMPTY : content;
|
||||
return buildDirectResponseAndBindContext(exchange, httpStatus, headers, content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,13 +75,13 @@ public class ApiDocAuthPluginFilter implements FizzPluginFilter {
|
||||
response.getHeaders().setExpires(0);
|
||||
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
||||
HttpStatus.UNAUTHORIZED.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
||||
return WebUtils.response(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("{} exception", API_DOC_AUTH_PLUGIN_FILTER, e);
|
||||
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
||||
return WebUtils.response(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -103,11 +103,11 @@ public class BasicAuthPluginFilter implements FizzPluginFilter {
|
||||
response.getHeaders().setCacheControl("no-store");
|
||||
response.getHeaders().setExpires(0);
|
||||
response.getHeaders().add("WWW-authenticate", "Basic Realm=\"input username and password\"");
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.UNAUTHORIZED, null, null);
|
||||
return WebUtils.response(exchange, HttpStatus.UNAUTHORIZED, null, null);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Basic Auth plugin Exception", e);
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, null);
|
||||
return WebUtils.response(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,13 +80,13 @@ public class ApiPairingPluginFilter implements FizzPluginFilter {
|
||||
response.getHeaders().setExpires(0);
|
||||
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
||||
HttpStatus.UNAUTHORIZED.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
||||
return WebUtils.response(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("{} Exception", API_PAIRING_PLUGIN_FILTER, e);
|
||||
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
||||
return WebUtils.buildDirectResponse(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
||||
return WebUtils.response(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user