You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.2 KiB

package common.ecommerce.shared.raw;
/**
* 原始报文采集策略。
*
* <p>只在显式调用 {@code pullWithRaw} 时生效;{@code pull()} 路径不采集,
* 也不分配任何缓冲。</p>
*
* <p><b>默认值取舍</b>:单页原文上限 4 MiB(领星多平台单页 500 条、单条 2~4 KB,
* 正常一页 1~2 MB);请求体<b>默认不采集</b>,因为领星请求体含 {@code sign};
* 逐条切片默认开启。</p>
*/
public final class RawCapturePolicy {
/** 单份原文的默认字节上限:4 MiB。 */
public static final int DEFAULT_MAX_BYTES = 4 * 1024 * 1024;
private static final RawCapturePolicy DEFAULTS =
new RawCapturePolicy(DEFAULT_MAX_BYTES, false, true);
/** 单份原文的字节上限;0 表示不限制。 */
private final int maxBytes;
/** 是否采集请求体。 */
private final boolean includeRequest;
/** 是否把列表响应对应成逐条 JSON。 */
private final boolean includeItems;
private RawCapturePolicy(int maxBytes, boolean includeRequest, boolean includeItems) {
this.maxBytes = maxBytes;
this.includeRequest = includeRequest;
this.includeItems = includeItems;
}
/** 默认策略:4 MiB、不带请求体、逐条切片开启。 */
public static RawCapturePolicy defaults() {
return DEFAULTS;
}
public static Builder builder() {
return new Builder();
}
/** 单份原文的字节上限;0 表示不限制。 */
public int maxBytes() {
return maxBytes;
}
/** 是否采集请求体;默认 false。 */
public boolean includeRequest() {
return includeRequest;
}
/** 是否把列表响应对应成逐条 JSON;默认 true。 */
public boolean includeItems() {
return includeItems;
}
@Override
public String toString() {
return "RawCapturePolicy{maxBytes=" + maxBytes
+ ", includeRequest=" + includeRequest
+ ", includeItems=" + includeItems + '}';
}
/** 可变构建器;默认值与 {@link #defaults()} 一致。 */
public static final class Builder {
private int maxBytes = DEFAULT_MAX_BYTES;
private boolean includeRequest;
private boolean includeItems = true;
/** 单份原文上限;0 表示不限制,负数非法。 */
public Builder maxBytes(int maxBytes) {
if (maxBytes < 0) {
throw new IllegalArgumentException("maxBytes must not be negative");
}
this.maxBytes = maxBytes;
return this;
}
/** 是否采集请求体;开启后原始请求体会原样返回,需自行承担签名落库风险。 */
public Builder includeRequest(boolean includeRequest) {
this.includeRequest = includeRequest;
return this;
}
/** 是否把列表响应对应成逐条 JSON。 */
public Builder includeItems(boolean includeItems) {
this.includeItems = includeItems;
return this;
}
public RawCapturePolicy build() {
return new RawCapturePolicy(maxBytes, includeRequest, includeItems);
}
}
}