use of com.dtflys.forest.converter.ForestEncoder in project forest by dromara.
the class BodyTypeLifeCycle method onMethodInitialized.
@Override
public void onMethodInitialized(ForestMethod method, BodyType annotation) {
Map<String, Object> attrs = ReflectUtils.getAttributesFromAnnotation(annotation);
String type = (String) attrs.get("type");
Object encodeClass = attrs.get("encoder");
MetaRequest metaRequest = method.getMetaRequest();
if (metaRequest == null) {
return;
}
metaRequest.setBodyType(type);
if (encodeClass != null && encodeClass instanceof Class && !((Class<?>) encodeClass).isInterface() && ForestEncoder.class.isAssignableFrom((Class<?>) encodeClass)) {
metaRequest.setEncoder((Class<? extends ForestEncoder>) encodeClass);
}
}
use of com.dtflys.forest.converter.ForestEncoder in project forest by dromara.
the class AbstractBodyBuilder method buildBody.
/**
* 构建请求体
* @param httpRequest 后端http请求对象
* @param request Forest请求对象
* @param lifeCycleHandler 生命周期处理器
*/
@Override
public void buildBody(T httpRequest, ForestRequest request, LifeCycleHandler lifeCycleHandler) {
String contentType = request.getContentType();
if (StringUtils.isEmpty(contentType)) {
contentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
}
String[] typeGroup = contentType.split(";[ ]*charset=");
String mineType = typeGroup[0];
String strCharset = request.getCharset();
Charset charset = null;
boolean mergeCharset = typeGroup.length > 1;
if (StringUtils.isEmpty(strCharset)) {
if (typeGroup.length > 1) {
strCharset = typeGroup[1];
charset = Charset.forName(strCharset);
} else {
charset = StandardCharsets.UTF_8;
}
} else {
charset = Charset.forName(strCharset);
}
if (StringUtils.isEmpty(mineType)) {
mineType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
}
ContentType mineContentType = new ContentType(mineType);
String ctypeWithoutParams = mineContentType.toStringWithoutParameters();
ForestEncoder encoder = request.getEncoder();
if (encoder != null) {
byte[] bodyBytes = encoder.encodeRequestBody(request, charset);
setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
return;
}
ForestBody reqBody = request.getBody();
boolean needRequestBody = request.getType().isNeedBody() || !reqBody.isEmpty() || !request.getMultiparts().isEmpty();
if (needRequestBody) {
ForestDataType bodyType = request.bodyType();
if (bodyType == null || bodyType == ForestDataType.AUTO) {
bodyType = mineContentType.bodyType();
}
if (bodyType == ForestDataType.MULTIPART) {
setFileBody(httpRequest, request, charset, ctypeWithoutParams, lifeCycleHandler);
return;
}
ForestEncoder bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(bodyType);
if (bodyEncoder == null) {
bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(ForestDataType.TEXT);
}
byte[] bodyBytes = bodyEncoder.encodeRequestBody(request, charset);
setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
}
}
use of com.dtflys.forest.converter.ForestEncoder in project forest by dromara.
the class ForestMethod method processMetaRequest.
private void processMetaRequest(MetaRequest metaRequest) {
Class[] paramTypes = method.getParameterTypes();
Type[] genericParamTypes = method.getGenericParameterTypes();
Annotation[][] paramAnns = method.getParameterAnnotations();
Parameter[] parameters = method.getParameters();
Class<? extends Annotation> reqAnnType = metaRequest.getRequestAnnotation().annotationType();
parameterTemplateArray = new MappingParameter[paramTypes.length];
processParameters(parameters, genericParamTypes, paramAnns);
bodyTypeTemplate = makeTemplate(reqAnnType, "type", metaRequest.getBodyType());
urlTemplate = makeURLTemplate(reqAnnType, "url", metaRequest.getUrl());
typeTemplate = makeTemplate(reqAnnType, "type", metaRequest.getType());
dataTypeTemplate = makeTemplate(reqAnnType, "dataType", metaRequest.getDataType());
if (StringUtils.isNotEmpty(metaRequest.getContentType())) {
contentTypeTemplate = makeTemplate(reqAnnType, "contentType", metaRequest.getContentType());
}
if (StringUtils.isNotEmpty(metaRequest.getUserAgent())) {
userAgentTemplate = makeTemplate(reqAnnType, "userAgent", metaRequest.getUserAgent());
}
sslKeyStoreId = makeTemplate(reqAnnType, "keyStore", metaRequest.getKeyStore());
if (StringUtils.isNotEmpty(metaRequest.getContentEncoding())) {
encodeTemplate = makeTemplate(reqAnnType, "contentEncoding", metaRequest.getContentEncoding());
}
charsetTemplate = makeTemplate(reqAnnType, "charset", metaRequest.getCharset());
if (metaRequest.getResponseEncoding() != null) {
responseEncodingTemplate = makeTemplate(reqAnnType, "responseEncoding", metaRequest.getResponseEncoding());
}
sslProtocolTemplate = makeTemplate(reqAnnType, "sslProtocol", metaRequest.getSslProtocol());
progressStep = metaRequest.getProgressStep();
async = metaRequest.isAsync();
retryerClass = metaRequest.getRetryer();
Class<? extends ForestEncoder> encoderClass = metaRequest.getEncoder();
Class<? extends ForestConverter> decoderClass = metaRequest.getDecoder();
String[] dataArray = metaRequest.getData();
String[] headerArray = metaRequest.getHeaders();
Integer tout = metaRequest.getTimeout();
if (tout != null && tout >= 0) {
timeout = tout;
}
Integer ctout = metaRequest.getConnectTimeout();
if (ctout != null && ctout >= 0) {
connectTimeout = ctout;
}
Integer rtout = metaRequest.getReadTimeout();
if (rtout != null && rtout >= 0) {
readTimeout = rtout;
}
Integer rtnum = metaRequest.getRetryCount();
if (rtnum != null && rtnum >= 0) {
retryCount = rtnum;
}
maxRetryInterval = metaRequest.getMaxRetryInterval();
logEnabled = configuration.isLogEnabled();
if (!logEnabled) {
logEnabled = metaRequest.isLogEnabled();
}
logRequest = configuration.isLogRequest();
logResponseStatus = configuration.isLogResponseStatus();
logResponseContent = configuration.isLogResponseContent();
LogConfiguration metaLogConfiguration = metaRequest.getLogConfiguration();
if (metaLogConfiguration == null && baseLogConfiguration != null) {
metaLogConfiguration = baseLogConfiguration;
}
if (metaLogConfiguration != null) {
logEnabled = metaLogConfiguration.isLogEnabled();
logRequest = metaLogConfiguration.isLogRequest();
logResponseStatus = metaLogConfiguration.isLogResponseStatus();
logResponseContent = metaLogConfiguration.isLogResponseContent();
logHandler = metaLogConfiguration.getLogHandler();
if (logHandler == null && baseLogConfiguration != null) {
logHandler = baseLogConfiguration.getLogHandler();
}
}
if (logHandler == null && configuration.getLogHandler() != null) {
logHandler = configuration.getLogHandler();
}
if (logHandler == null) {
logHandler = new DefaultLogHandler();
}
logConfiguration = new LogConfiguration();
logConfiguration.setLogEnabled(logEnabled);
logConfiguration.setLogRequest(logRequest);
logConfiguration.setLogResponseStatus(logResponseStatus);
logConfiguration.setLogResponseContent(logResponseContent);
logConfiguration.setLogHandler(logHandler);
dataTemplateArray = new MappingTemplate[dataArray.length];
for (int j = 0; j < dataArray.length; j++) {
String data = dataArray[j];
MappingTemplate dataTemplate = makeTemplate(reqAnnType, "data", data);
dataTemplateArray[j] = dataTemplate;
}
headerTemplateArray = new MappingTemplate[headerArray.length];
for (int j = 0; j < headerArray.length; j++) {
String header = headerArray[j];
MappingTemplate headerTemplate = makeTemplate(reqAnnType, "header", header);
headerTemplateArray[j] = headerTemplate;
}
Class[] interceptorClasses = metaRequest.getInterceptor();
if (interceptorClasses != null && interceptorClasses.length > 0) {
for (int cidx = 0, len = interceptorClasses.length; cidx < len; cidx++) {
Class interceptorClass = interceptorClasses[cidx];
addInterceptor(interceptorClass);
}
}
if (encoderClass != null && !encoderClass.isInterface() && ForestEncoder.class.isAssignableFrom(encoderClass)) {
this.encoder = configuration.getForestObjectFactory().getObject(encoderClass);
}
if (decoderClass != null && !encoderClass.isInterface() && ForestConverter.class.isAssignableFrom(decoderClass)) {
this.decoder = configuration.getForestObjectFactory().getObject(decoderClass);
}
}
Aggregations