Optimize WebUtils.java
This commit is contained in:
@@ -51,7 +51,7 @@ spring:
|
|||||||
aggregate:
|
aggregate:
|
||||||
redis:
|
redis:
|
||||||
# need replace
|
# 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
|
# need replace
|
||||||
port: 6379 #please input the redis port (default:6379)
|
port: 6379 #please input the redis port (default:6379)
|
||||||
# need replace
|
# need replace
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import com.fasterxml.jackson.core.JsonParser;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.*;
|
import com.fasterxml.jackson.databind.*;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
|
||||||
import we.util.Consts.DP;
|
import we.util.Consts.DP;
|
||||||
|
|
||||||
import java.io.IOException;
|
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) {
|
public static String writeValueAsString(Object value) {
|
||||||
try {
|
try {
|
||||||
return m.writeValueAsString(value);
|
return m.writeValueAsString(value);
|
||||||
@@ -96,6 +103,14 @@ public abstract class JacksonUtils {
|
|||||||
throw new RuntimeException(e);
|
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> {
|
class DateDeseralizer extends JsonDeserializer<Date> {
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ public class ApiPairingController {
|
|||||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||||
return response.writeWith(Mono.just(response.bufferFactory().wrap(docsJson.getBytes())));
|
return response.writeWith(Mono.just(response.bufferFactory().wrap(docsJson.getBytes())));
|
||||||
} else {
|
} 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) {
|
if (auth.code == Result.SUCC) {
|
||||||
String managerUrl = fizzMangerConfig.managerUrl;
|
String managerUrl = fizzMangerConfig.managerUrl;
|
||||||
if (managerUrl == null) {
|
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();
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
String uri = request.getURI().toString();
|
String uri = request.getURI().toString();
|
||||||
@@ -185,7 +185,7 @@ public class ApiPairingController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
} else {
|
} 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);
|
ApiPairingInfo apiPairingInfo = apiPairingInfoService.get(service);
|
||||||
if (apiPairingInfo == null) {
|
if (apiPairingInfo == null) {
|
||||||
log.warn("{}{} service no api pairing info", logPrefix, service);
|
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();
|
StringBuilder b = ThreadContext.getStringBuilder();
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ public class CallbackFilter extends FizzWebFilter {
|
|||||||
httpHeaders.addAll(h, v);
|
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) {
|
private HashMap<String, ServiceInstance> getService2instMap(ApiConfig ac) {
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ public class FlowControlFilter extends FizzWebFilter {
|
|||||||
LogService.setBizId(traceId);
|
LogService.setBizId(traceId);
|
||||||
if (!apiConfigService.serviceConfigMap.containsKey(service)) {
|
if (!apiConfigService.serviceConfigMap.containsKey(service)) {
|
||||||
String json = WebUtils.jsonRespBody(HttpStatus.FORBIDDEN.value(), "no service " + service + " in flow config", traceId);
|
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);
|
String app = WebUtils.getAppId(exchange);
|
||||||
path = WebUtils.getClientReqPath(exchange);
|
path = WebUtils.getClientReqPath(exchange);
|
||||||
|
|||||||
@@ -241,7 +241,7 @@ public class RouteFilter extends FizzWebFilter {
|
|||||||
)
|
)
|
||||||
.flatMap(
|
.flatMap(
|
||||||
dubboRpcResponseBody -> {
|
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;
|
return m;
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ package we.util;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -41,6 +42,8 @@ import we.proxy.Route;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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 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 TRACE_ID = "traid@";
|
||||||
|
|
||||||
public static final String BACKEND_SERVICE = "bs@";
|
public static final String BACKEND_SERVICE = "bs@";
|
||||||
@@ -231,102 +232,59 @@ public abstract class WebUtils {
|
|||||||
return exchange.getAttribute(ROUTE);
|
return exchange.getAttribute(ROUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Mono<Void> getDirectResponse(ServerWebExchange exchange) {
|
public static Mono<Void> response(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, String body) {
|
||||||
return exchange.getAttribute(WebUtils.directResponse);
|
return response(exchange.getResponse(), status, headers, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, DataBuffer body) {
|
||||||
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) {
|
|
||||||
if (clientResp.isCommitted()) {
|
if (clientResp.isCommitted()) {
|
||||||
log.warn(bodyContent + ", but client resp is committed, " + clientResp.getStatusCode());
|
String s = body.toString(StandardCharsets.UTF_8);
|
||||||
return Mono.error(new RuntimeException(bodyContent, null, false, false) {});
|
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) {
|
if (status != null) {
|
||||||
clientResp.setStatusCode(status);
|
clientResp.setStatusCode(status);
|
||||||
}
|
}
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
headers.forEach(
|
headers.forEach( (h, vs) -> {clientResp.getHeaders().addAll(h, vs);} );
|
||||||
(h, vs) -> {
|
|
||||||
clientResp.getHeaders().addAll(h, vs);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (bodyContent == null) {
|
if (body == null) {
|
||||||
bodyContent = Consts.S.EMPTY;
|
body = NettyDataBufferUtils.EMPTY_DATA_BUFFER;
|
||||||
}
|
}
|
||||||
return clientResp
|
return clientResp.writeWith(Mono.just(body));
|
||||||
.writeWith(Mono.just(clientResp.bufferFactory().wrap(bodyContent.getBytes())));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, ByteBuffer body) {
|
||||||
public static void transmitSuccessFilterResult(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body);
|
||||||
FilterResult fr = FilterResult.SUCCESS_WITH(filter, data);
|
return response(clientResp, status, headers, dataBuffer);
|
||||||
bind(exchange, filter, fr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, byte[] body) {
|
||||||
public static Mono transmitSuccessFilterResultAndEmptyMono(ServerWebExchange exchange, String filter, Map<String, Object> data) {
|
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body);
|
||||||
transmitSuccessFilterResult(exchange, filter, data);
|
return response(clientResp, status, headers, dataBuffer);
|
||||||
return Mono.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public static Mono<Void> response(ServerHttpResponse clientResp, HttpStatus status, HttpHeaders headers, String body) {
|
||||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter) {
|
DataBuffer dataBuffer = clientResp.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
|
||||||
FilterResult fr = FilterResult.FAIL(filter);
|
return response(clientResp, status, headers, dataBuffer);
|
||||||
bind(exchange, filter, fr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
public static Mono responseJson(ServerWebExchange exchange, HttpStatus status, HttpHeaders headers, Object object) {
|
||||||
public static void transmitFailFilterResult(ServerWebExchange exchange, String filter, Throwable cause) {
|
if (headers == null) {
|
||||||
FilterResult fr = FilterResult.FAIL_WITH(filter, cause);
|
headers = new HttpHeaders();
|
||||||
bind(exchange, filter, fr);
|
}
|
||||||
|
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) {
|
private static void bind(ServerWebExchange exchange, String filter, FilterResult fr) {
|
||||||
@@ -335,11 +293,6 @@ public abstract class WebUtils {
|
|||||||
fc.put(PREV_FILTER_RESULT, fr);
|
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) {
|
public static String getClientReqPath(ServerWebExchange exchange) {
|
||||||
String p = exchange.getAttribute(clientRequestPath);
|
String p = exchange.getAttribute(clientRequestPath);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
@@ -412,7 +365,6 @@ public abstract class WebUtils {
|
|||||||
|
|
||||||
public static String getClientReqPathQuery(ServerWebExchange exchange) {
|
public static String getClientReqPathQuery(ServerWebExchange exchange) {
|
||||||
String pathQry = getClientReqPath(exchange);
|
String pathQry = getClientReqPath(exchange);
|
||||||
// String qry = getClientReqQuery(exchange);
|
|
||||||
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
||||||
if (!queryParams.isEmpty()) {
|
if (!queryParams.isEmpty()) {
|
||||||
String qry = toQueryString(queryParams);
|
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) {
|
public static Mono<Void> responseError(ServerWebExchange exchange, int code, String msg) {
|
||||||
return responseError(exchange, null, code, msg, null, false);
|
return responseError(exchange, null, code, msg, null, false);
|
||||||
}
|
}
|
||||||
@@ -575,35 +517,6 @@ public abstract class WebUtils {
|
|||||||
return responseError(exchange, reporter, code, msg, t, false);
|
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) {
|
public static String getOriginIp(ServerWebExchange exchange) {
|
||||||
String ip = exchange.getAttribute(originIp);
|
String ip = exchange.getAttribute(originIp);
|
||||||
if (ip == null) {
|
if (ip == null) {
|
||||||
@@ -686,12 +599,10 @@ public abstract class WebUtils {
|
|||||||
String name = param.getKey();
|
String name = param.getKey();
|
||||||
List<String> values = param.getValue();
|
List<String> values = param.getValue();
|
||||||
if (values.isEmpty()) {
|
if (values.isEmpty()) {
|
||||||
// b.append(URLEncoder.encode(name, Consts.C.UTF8));
|
|
||||||
b.append(name);
|
b.append(name);
|
||||||
} else {
|
} else {
|
||||||
int vs = values.size();
|
int vs = values.size();
|
||||||
for (int i = 0; i < vs; ) {
|
for (int i = 0; i < vs; ) {
|
||||||
// b.append(URLEncoder.encode(name, Consts.C.UTF8));
|
|
||||||
b.append(name);
|
b.append(name);
|
||||||
String v = values.get(i);
|
String v = values.get(i);
|
||||||
if (v != null) {
|
if (v != null) {
|
||||||
@@ -719,11 +630,159 @@ public abstract class WebUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static String encodeQueryParamComp(String source) {
|
// the method below will be deprecated.
|
||||||
// try {
|
|
||||||
// return URLEncoder.encode(source, Consts.C.UTF8);
|
@Deprecated
|
||||||
// } catch (UnsupportedEncodingException e) {
|
public static Mono<Void> getDirectResponse(ServerWebExchange exchange) {
|
||||||
// return URLEncoder.encode(source);
|
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);
|
response.getHeaders().setExpires(0);
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
||||||
HttpStatus.UNAUTHORIZED.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
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) {
|
} catch (Exception e) {
|
||||||
log.error("{} exception", API_DOC_AUTH_PLUGIN_FILTER, e);
|
log.error("{} exception", API_DOC_AUTH_PLUGIN_FILTER, e);
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
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().setCacheControl("no-store");
|
||||||
response.getHeaders().setExpires(0);
|
response.getHeaders().setExpires(0);
|
||||||
response.getHeaders().add("WWW-authenticate", "Basic Realm=\"input username and password\"");
|
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) {
|
} catch (Exception e) {
|
||||||
log.error("Basic Auth plugin 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);
|
response.getHeaders().setExpires(0);
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
||||||
HttpStatus.UNAUTHORIZED.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
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) {
|
} catch (Exception e) {
|
||||||
log.error("{} Exception", API_PAIRING_PLUGIN_FILTER, e);
|
log.error("{} Exception", API_PAIRING_PLUGIN_FILTER, e);
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), WebUtils.getTraceId(exchange));
|
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