Search in sources :

Example 6 with MappingParameter

use of com.dtflys.forest.mapping.MappingParameter in project forest by dromara.

the class ProtobufBodyLifeCycle method onParameterInitialized.

@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, ProtobufBody annotation) {
    super.onParameterInitialized(method, parameter, annotation);
    MetaRequest metaRequest = method.getMetaRequest();
    String methodName = methodName(method);
    if (metaRequest == null) {
        throw new ForestRuntimeException("[Forest] method '" + methodName + "' has not bind a Forest request annotation. Hence the annotation @BinaryBody cannot be bind on a parameter in this method.");
    }
    boolean hasDataFileAnn = false;
    for (Parameter param : method.getMethod().getParameters()) {
        Annotation dataFileAnn = param.getAnnotation(DataFile.class);
        if (dataFileAnn != null) {
            hasDataFileAnn = true;
            break;
        }
    }
    String contentTypeStr = metaRequest.getContentType();
    if (StringUtils.isBlank(contentTypeStr) && !hasDataFileAnn) {
        metaRequest.setContentType(ContentType.APPLICATION_X_PROTOBUF);
    }
    if (metaRequest.getBodyType() == null) {
        metaRequest.setBodyType(ForestDataType.PROTOBUF);
    }
    parameter.setTarget(MappingParameter.TARGET_BODY);
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Parameter(java.lang.reflect.Parameter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) Annotation(java.lang.annotation.Annotation)

Example 7 with MappingParameter

use of com.dtflys.forest.mapping.MappingParameter 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);
    }
}
Also used : ForestEncoder(com.dtflys.forest.converter.ForestEncoder) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) DefaultLogHandler(com.dtflys.forest.logging.DefaultLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ForestRequestType(com.dtflys.forest.http.ForestRequestType) ContentType(com.dtflys.forest.backend.ContentType) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestConverter(com.dtflys.forest.converter.ForestConverter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) ForestQueryParameter(com.dtflys.forest.http.ForestQueryParameter)

Aggregations

MappingParameter (com.dtflys.forest.mapping.MappingParameter)7 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)5 Annotation (java.lang.annotation.Annotation)5 MetaRequest (com.dtflys.forest.reflection.MetaRequest)4 Parameter (java.lang.reflect.Parameter)4 ForestQueryParameter (com.dtflys.forest.http.ForestQueryParameter)3 ForestRequestType (com.dtflys.forest.http.ForestRequestType)3 ForestDataType (com.dtflys.forest.utils.ForestDataType)3 ContentType (com.dtflys.forest.backend.ContentType)2 OnError (com.dtflys.forest.callback.OnError)2 OnLoadCookie (com.dtflys.forest.callback.OnLoadCookie)2 OnProgress (com.dtflys.forest.callback.OnProgress)2 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)2 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnRedirection (com.dtflys.forest.callback.OnRedirection)1 OnSaveCookie (com.dtflys.forest.callback.OnSaveCookie)1 OnSuccess (com.dtflys.forest.callback.OnSuccess)1 VariableScope (com.dtflys.forest.config.VariableScope)1 ForestConverter (com.dtflys.forest.converter.ForestConverter)1 ForestEncoder (com.dtflys.forest.converter.ForestEncoder)1