Support request body access in plugin #261
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>fizz-gateway-community</artifactId>
|
||||
<groupId>com.fizzgate</groupId>
|
||||
<version>2.2.3</version>
|
||||
<version>2.2.4-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
87
fizz-common/src/main/java/we/constants/CommonConstants.java
Normal file
87
fizz-common/src/main/java/we/constants/CommonConstants.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.constants;
|
||||
|
||||
/**
|
||||
* @author unknown
|
||||
*/
|
||||
public class CommonConstants {
|
||||
|
||||
/**
|
||||
* traceId for log
|
||||
*/
|
||||
public static final String TRACE_ID = "traceId";
|
||||
|
||||
/**
|
||||
* Header key to transfer traceId
|
||||
*/
|
||||
public static final String HEADER_TRACE_ID = "X-TRACE-ID";
|
||||
|
||||
|
||||
/**
|
||||
* Prefix of traceId
|
||||
*/
|
||||
public static final String TRACE_ID_PREFIX = "fizz-";
|
||||
|
||||
|
||||
/**
|
||||
* Star WildCard for PathMapping
|
||||
*/
|
||||
public static final String WILDCARD_STAR = "*";
|
||||
|
||||
|
||||
/**
|
||||
* Tilde WildCard for PathMapping
|
||||
*/
|
||||
public static final String WILDCARD_TILDE = "~";
|
||||
|
||||
|
||||
/**
|
||||
* Stop the underlying processes and response immediately, using in scripts
|
||||
*/
|
||||
public static final String STOP_AND_RESPONSE_KEY = "_stopAndResponse";
|
||||
|
||||
/**
|
||||
* Stop the underlying processes and redirect to the specified URL immediately, work with STOP_AND_RESPONSE_KEY using in scripts
|
||||
*/
|
||||
public static final String REDIRECT_URL_KEY = "_redirectUrl";
|
||||
|
||||
|
||||
/**
|
||||
* Content-Length Header
|
||||
*/
|
||||
public static final String HEADER_CONTENT_LENGTH = "Content-Length";
|
||||
|
||||
/**
|
||||
* Content-Type Header
|
||||
*/
|
||||
public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
||||
|
||||
/**
|
||||
* JSON Content-Type
|
||||
*/
|
||||
public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
|
||||
|
||||
|
||||
/**
|
||||
* File key prefix to identify upload file
|
||||
*/
|
||||
public static final String FILE_KEY_PREFIX = "__fizz_file__";
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.flume.clients.log4j2appender;
|
||||
|
||||
import we.constants.CommonConstants;
|
||||
|
||||
public enum LogService {
|
||||
|
||||
BIZ_ID, HANDLE_STGY, APP;
|
||||
|
||||
public static void cleanBizId() {
|
||||
setBizId(null);
|
||||
}
|
||||
|
||||
public static Object getBizId() {
|
||||
return ThreadContext.get(Constants.BIZ_ID);
|
||||
}
|
||||
|
||||
public static void setBizId(Object bizId) {
|
||||
ThreadContext.set(Constants.BIZ_ID, bizId);
|
||||
if (bizId != null) {
|
||||
org.apache.logging.log4j.ThreadContext.put(CommonConstants.TRACE_ID, String.valueOf(bizId));
|
||||
}
|
||||
}
|
||||
|
||||
public static String toKF(String topic) {
|
||||
return topic;
|
||||
}
|
||||
|
||||
public static String toESaKF(String topic) {
|
||||
return Constants.AND + topic;
|
||||
}
|
||||
|
||||
public static class Constants {
|
||||
static final String BIZ_ID = "bizId";
|
||||
static final char AND = '&';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.flume.clients.log4j2appender;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** for internal use */
|
||||
public abstract class ThreadContext {
|
||||
|
||||
private static ThreadLocal<Map<String, Object>> tl = new ThreadLocal<>();
|
||||
private static final int mapCap = 16;
|
||||
private static final float mapLoadFactor = 1.0f;
|
||||
|
||||
private static final String sb = "sb";
|
||||
private static final int sbCap = 256;
|
||||
|
||||
public static StringBuilder getStringBuilder() {
|
||||
return getStringBuilder(true);
|
||||
}
|
||||
|
||||
public static StringBuilder getStringBuilder(boolean clean) {
|
||||
Map<String, Object> m = getMap();
|
||||
StringBuilder b = (StringBuilder) m.get(sb);
|
||||
if (b == null) {
|
||||
b = new StringBuilder(sbCap);
|
||||
m.put(sb, b);
|
||||
} else {
|
||||
if (clean) {
|
||||
b.delete(0, b.length());
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getSimpleDateFormat(String pattern) {
|
||||
Map<String, Object> m = getMap();
|
||||
SimpleDateFormat sdf = (SimpleDateFormat) m.get(pattern);
|
||||
if (sdf == null) {
|
||||
sdf = new SimpleDateFormat(pattern);
|
||||
m.put(pattern, sdf);
|
||||
}
|
||||
return sdf;
|
||||
}
|
||||
|
||||
public static Object get(String key, Class<?> clz) {
|
||||
Object obj = get(key);
|
||||
if (obj == null) {
|
||||
try {
|
||||
obj = clz.newInstance();
|
||||
set(key, obj);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static Map<String, Object> getMap() {
|
||||
Map<String, Object> m = tl.get();
|
||||
if (m == null) {
|
||||
m = new HashMap<>(mapCap, mapLoadFactor);
|
||||
tl.set(m);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static Object get(String key) {
|
||||
return getMap().get(key);
|
||||
}
|
||||
|
||||
public static void set(String key, Object obj) {
|
||||
getMap().put(key, obj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.util;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class ConvertedRequestBodyDataBufferWrapper {
|
||||
|
||||
public DataBuffer body;
|
||||
|
||||
public ConvertedRequestBodyDataBufferWrapper(DataBuffer body) {
|
||||
this.body = body;
|
||||
}
|
||||
}
|
||||
80
fizz-common/src/main/java/we/util/NettyDataBufferUtils.java
Normal file
80
fizz-common/src/main/java/we/util/NettyDataBufferUtils.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2020 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.util;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.NettyDataBuffer;
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.core.io.buffer.PooledDataBuffer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public abstract class NettyDataBufferUtils extends org.springframework.core.io.buffer.DataBufferUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NettyDataBufferUtils.class);
|
||||
|
||||
private static NettyDataBufferFactory dataBufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
|
||||
|
||||
public static NettyDataBuffer from(String s) {
|
||||
return from(s.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static NettyDataBuffer from(byte[] bytes) {
|
||||
return (NettyDataBuffer) dataBufferFactory.wrap(bytes);
|
||||
}
|
||||
|
||||
public static NettyDataBuffer from(ByteBuffer byteBuffer) {
|
||||
return dataBufferFactory.wrap(byteBuffer);
|
||||
}
|
||||
|
||||
public static NettyDataBuffer from(ByteBuf byteBuf) {
|
||||
return dataBufferFactory.wrap(byteBuf);
|
||||
}
|
||||
|
||||
public static boolean release(@Nullable String reqId, @Nullable DataBuffer dataBuffer) {
|
||||
if (dataBuffer instanceof PooledDataBuffer) {
|
||||
PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer;
|
||||
if (pooledDataBuffer.isAllocated()) {
|
||||
if (pooledDataBuffer instanceof NettyDataBuffer) {
|
||||
NettyDataBuffer ndb = (NettyDataBuffer) pooledDataBuffer;
|
||||
ByteBuf nativeBuffer = ndb.getNativeBuffer();
|
||||
int refCnt = nativeBuffer.refCnt();
|
||||
if (refCnt < 1) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(nativeBuffer + " ref cnt is " + refCnt, LogService.BIZ_ID, reqId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return pooledDataBuffer.release();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user