Search in sources :

Example 6 with MappingTemplate

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

the class RetryLifeCycle method onInvokeMethod.

@Override
public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
    Retry annotation = (Retry) request.getMethod().getExtensionParameterValue(PARAM_KEY_RETRY);
    Object retryWhen = request.getMethod().getExtensionParameterValue(PARAM_KEY_RETRY_WHEN);
    String maxRetryCountStr = annotation.maxRetryCount();
    String maxRetryIntervalStr = annotation.maxRetryInterval();
    if (StringUtils.isNotBlank(maxRetryCountStr)) {
        MappingTemplate maxRetryCountTemplate = method.makeTemplate(Retry.class, "maxRetryCount", maxRetryCountStr);
        try {
            Integer maxRetryCount = Integer.parseInt(maxRetryCountTemplate.render(args));
            request.maxRetryCount(maxRetryCount);
        } catch (Throwable ignored) {
        }
    }
    if (StringUtils.isNotBlank(maxRetryIntervalStr)) {
        try {
            MappingTemplate maxRetryIntervalTemplate = method.makeTemplate(Retry.class, "maxRetryInterval", maxRetryIntervalStr);
            Long maxRetryInterval = Long.parseLong(maxRetryIntervalTemplate.render(args));
            request.setMaxRetryInterval(maxRetryInterval);
        } catch (Throwable ignored) {
        }
    }
    if (retryWhen != null) {
        request.retryWhen((RetryWhen) retryWhen);
    }
}
Also used : MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) Retry(com.dtflys.forest.annotation.Retry)

Example 7 with MappingTemplate

use of com.dtflys.forest.mapping.MappingTemplate 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)

Example 8 with MappingTemplate

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

the class ForestMethod method addMetaRequestAnnotation.

/**
 * 添加元请求注释
 * @param annotation 注解
 * @param interceptorClass 拦截器类
 */
private void addMetaRequestAnnotation(Annotation annotation, Class interceptorClass) {
    Class<? extends Annotation> annType = annotation.annotationType();
    RequestAttributes requestAttributesAnn = annType.getAnnotation(RequestAttributes.class);
    if (requestAttributesAnn != null) {
        Map<String, Object> attrTemplates = ReflectUtils.getAttributesFromAnnotation(annotation);
        for (String key : attrTemplates.keySet()) {
            Object value = attrTemplates.get(key);
            if (value instanceof CharSequence) {
                MappingTemplate template = makeTemplate(annType, key, value.toString());
                attrTemplates.put(key, template);
            } else if (String[].class.isAssignableFrom(value.getClass())) {
                String[] stringArray = (String[]) value;
                int len = stringArray.length;
                MappingTemplate[] templates = new MappingTemplate[stringArray.length];
                for (int i = 0; i < len; i++) {
                    String item = stringArray[i];
                    MappingTemplate template = makeTemplate(annType, key, item);
                    templates[i] = template;
                }
                attrTemplates.put(key, templates);
            }
        }
        InterceptorAttributes attributes = new InterceptorAttributes(interceptorClass, attrTemplates);
        if (interceptorAttributesList == null) {
            interceptorAttributesList = new LinkedList<>();
        }
        interceptorAttributesList.add(attributes);
    }
    Interceptor interceptor = addInterceptor(interceptorClass);
    if (interceptor instanceof MethodAnnotationLifeCycle) {
        MethodAnnotationLifeCycle lifeCycle = (MethodAnnotationLifeCycle) interceptor;
        lifeCycle.onMethodInitialized(this, annotation);
    }
}
Also used : InterceptorAttributes(com.dtflys.forest.interceptor.InterceptorAttributes) MethodAnnotationLifeCycle(com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) RequestAttributes(com.dtflys.forest.annotation.RequestAttributes) Interceptor(com.dtflys.forest.interceptor.Interceptor)

Example 9 with MappingTemplate

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

the class HTTPProxyLifeCycle method beforeExecute.

@Override
public boolean beforeExecute(ForestRequest request) {
    MappingTemplate hostTemplate = (MappingTemplate) getAttribute(request, "host_temp");
    MappingTemplate portTemplate = (MappingTemplate) getAttribute(request, "port_temp");
    MappingTemplate usernameTemplate = (MappingTemplate) getAttribute(request, "username_temp");
    MappingTemplate passwordTemplate = (MappingTemplate) getAttribute(request, "password_temp");
    Object httpProxySource = request.getMethod().getExtensionParameterValue(PARAM_KEY_HTTP_PROXY_SOURCE);
    Object[] args = request.getArguments();
    String host = hostTemplate.render(args);
    String portStr = portTemplate.render(args);
    String username = null, password = null;
    if (usernameTemplate != null) {
        username = usernameTemplate.render(args);
    }
    if (passwordTemplate != null) {
        password = passwordTemplate.render(args);
    }
    int port = 80;
    if (StringUtils.isBlank(host)) {
        if (httpProxySource != null && httpProxySource instanceof HTTPProxySource) {
            request.setProxy(((HTTPProxySource) httpProxySource).getProxy(request));
            return true;
        }
        throw new ForestRuntimeException("[Forest] Proxy host cannot be empty!");
    }
    if (StringUtils.isNotBlank(portStr)) {
        try {
            port = Integer.parseInt(portStr);
        } catch (Throwable th) {
        }
    }
    ForestProxy proxy = new ForestProxy(host, port);
    if (StringUtils.isNotEmpty(username)) {
        proxy.setUsername(username);
    }
    if (StringUtils.isNotEmpty(password)) {
        proxy.setPassword(password);
    }
    request.setProxy(proxy);
    return true;
}
Also used : MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HTTPProxySource(com.dtflys.forest.callback.HTTPProxySource) ForestProxy(com.dtflys.forest.http.ForestProxy)

Aggregations

MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)9 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 AddressSource (com.dtflys.forest.callback.AddressSource)2 ForestAddress (com.dtflys.forest.http.ForestAddress)2 ForestQueryParameter (com.dtflys.forest.http.ForestQueryParameter)2 ForestRequestType (com.dtflys.forest.http.ForestRequestType)2 Interceptor (com.dtflys.forest.interceptor.Interceptor)2 InterceptorAttributes (com.dtflys.forest.interceptor.InterceptorAttributes)2 MappingParameter (com.dtflys.forest.mapping.MappingParameter)2 ForestMultipartFactory (com.dtflys.forest.multipart.ForestMultipartFactory)2 ForestDataType (com.dtflys.forest.utils.ForestDataType)2 Address (com.dtflys.forest.annotation.Address)1 RequestAttributes (com.dtflys.forest.annotation.RequestAttributes)1 Retry (com.dtflys.forest.annotation.Retry)1 ContentType (com.dtflys.forest.backend.ContentType)1 HTTPProxySource (com.dtflys.forest.callback.HTTPProxySource)1 OnError (com.dtflys.forest.callback.OnError)1 OnLoadCookie (com.dtflys.forest.callback.OnLoadCookie)1 OnProgress (com.dtflys.forest.callback.OnProgress)1 OnRedirection (com.dtflys.forest.callback.OnRedirection)1