Search in sources :

Example 6 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class ForestBeanRegister method registerForestConfiguration.

public ForestConfiguration registerForestConfiguration(ForestConfigurationProperties forestConfigurationProperties) {
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ForestConfiguration.class);
    String id = forestConfigurationProperties.getBeanId();
    if (StringUtils.isBlank(id)) {
        id = "forestConfiguration";
    }
    Class<? extends ForestLogHandler> logHandlerClass = forestConfigurationProperties.getLogHandler();
    ForestLogHandler logHandler = null;
    if (logHandlerClass != null) {
        try {
            logHandler = logHandlerClass.newInstance();
        } catch (InstantiationException e) {
            throw new ForestRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new ForestRuntimeException(e);
        }
    }
    beanDefinitionBuilder.addPropertyValue("maxAsyncThreadSize", forestConfigurationProperties.getMaxAsyncThreadSize()).addPropertyValue("maxConnections", forestConfigurationProperties.getMaxConnections()).addPropertyValue("maxRouteConnections", forestConfigurationProperties.getMaxRouteConnections()).addPropertyValue("timeout", forestConfigurationProperties.getTimeout()).addPropertyValue("connectTimeout", forestConfigurationProperties.getConnectTimeoutMillis()).addPropertyValue("readTimeout", forestConfigurationProperties.getReadTimeoutMillis()).addPropertyValue("charset", forestConfigurationProperties.getCharset()).addPropertyValue("retryer", forestConfigurationProperties.getRetryer()).addPropertyValue("maxRetryCount", forestConfigurationProperties.getMaxRetryCount()).addPropertyValue("maxRetryInterval", forestConfigurationProperties.getMaxRetryInterval()).addPropertyValue("autoRedirection", forestConfigurationProperties.isAutoRedirection()).addPropertyValue("logEnabled", forestConfigurationProperties.isLogEnabled()).addPropertyValue("logRequest", forestConfigurationProperties.isLogRequest()).addPropertyValue("logResponseStatus", forestConfigurationProperties.isLogResponseStatus()).addPropertyValue("logResponseContent", forestConfigurationProperties.isLogResponseContent()).addPropertyValue("logHandler", logHandler).addPropertyValue("backendName", forestConfigurationProperties.getBackend()).addPropertyValue("baseAddressScheme", forestConfigurationProperties.getBaseAddressScheme()).addPropertyValue("baseAddressHost", forestConfigurationProperties.getBaseAddressHost()).addPropertyValue("baseAddressPort", forestConfigurationProperties.getBaseAddressPort()).addPropertyValue("baseAddressSourceClass", forestConfigurationProperties.getBaseAddressSource()).addPropertyValue("successWhenClass", forestConfigurationProperties.getSuccessWhen()).addPropertyValue("retryWhenClass", forestConfigurationProperties.getRetryWhen()).addPropertyValue("interceptors", forestConfigurationProperties.getInterceptors()).addPropertyValue("sslProtocol", forestConfigurationProperties.getSslProtocol()).addPropertyValue("variables", forestConfigurationProperties.getVariables()).setLazyInit(false).setFactoryMethod("configuration");
    BeanDefinition forestPropertiesBean = registerForestPropertiesBean();
    beanDefinitionBuilder.addPropertyValue("properties", forestPropertiesBean);
    BeanDefinition forestObjectFactoryBeanDefinition = registerForestObjectFactoryBean();
    beanDefinitionBuilder.addPropertyValue("forestObjectFactory", forestObjectFactoryBeanDefinition);
    BeanDefinition interceptorFactoryBeanDefinition = registerInterceptorFactoryBean();
    beanDefinitionBuilder.addPropertyValue("interceptorFactory", interceptorFactoryBeanDefinition);
    List<ForestSSLKeyStoreProperties> sslKeyStorePropertiesList = forestConfigurationProperties.getSslKeyStores();
    ManagedMap<String, BeanDefinition> sslKeystoreMap = new ManagedMap<>();
    for (ForestSSLKeyStoreProperties keyStoreProperties : sslKeyStorePropertiesList) {
        registerSSLKeyStoreBean(sslKeystoreMap, keyStoreProperties);
    }
    BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    beanDefinition.getPropertyValues().addPropertyValue("sslKeyStores", sslKeystoreMap);
    BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
    beanFactory.registerBeanDefinition(id, beanDefinition);
    ForestConfiguration configuration = applicationContext.getBean(id, ForestConfiguration.class);
    Map<String, Class> filters = forestConfigurationProperties.getFilters();
    for (Map.Entry<String, Class> entry : filters.entrySet()) {
        String filterName = entry.getKey();
        Class filterClass = entry.getValue();
        configuration.registerFilter(filterName, filterClass);
    }
    ForestConvertProperties convertProperties = forestConfigurationProperties.getConverters();
    if (convertProperties != null) {
        registerConverter(configuration, ForestDataType.TEXT, convertProperties.getText());
        registerConverter(configuration, ForestDataType.JSON, convertProperties.getJson());
        registerConverter(configuration, ForestDataType.XML, convertProperties.getXml());
        registerConverter(configuration, ForestDataType.BINARY, convertProperties.getBinary());
        registerConverter(configuration, ForestDataType.PROTOBUF, convertProperties.getProtobuf());
    }
    registerConverterBeanListener(configuration);
    return configuration;
}
Also used : ForestSSLKeyStoreProperties(com.dtflys.forest.springboot.properties.ForestSSLKeyStoreProperties) ForestConvertProperties(com.dtflys.forest.springboot.properties.ForestConvertProperties) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 7 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class OkHttp3Executor method logResponse.

public void logResponse(ForestResponse response) {
    LogConfiguration logConfiguration = request.getLogConfiguration();
    if (!logConfiguration.isLogEnabled() || response.isLogged()) {
        return;
    }
    response.setLogged(true);
    ResponseLogMessage logMessage = new ResponseLogMessage(response, response.getStatusCode());
    ForestLogHandler logHandler = logConfiguration.getLogHandler();
    if (logHandler != null) {
        if (logConfiguration.isLogResponseStatus()) {
            logHandler.logResponseStatus(logMessage);
        }
        if (logConfiguration.isLogResponseContent()) {
            logHandler.logResponseContent(logMessage);
        }
    }
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ResponseLogMessage(com.dtflys.forest.logging.ResponseLogMessage)

Example 8 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class LogHandlerLifeCycle method onMethodInitialized.

@Override
public void onMethodInitialized(ForestMethod method, LogHandler annotation) {
    MetaRequest metaRequest = method.getMetaRequest();
    if (metaRequest == null) {
        return;
    }
    ForestConfiguration configuration = method.getConfiguration();
    LogConfiguration logConfiguration = metaRequest.getLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        logConfiguration.setLogEnabled(configuration.isLogEnabled());
        logConfiguration.setLogRequest(configuration.isLogRequest());
        logConfiguration.setLogResponseStatus(configuration.isLogResponseStatus());
        logConfiguration.setLogResponseContent(configuration.isLogResponseContent());
        metaRequest.setLogConfiguration(logConfiguration);
    }
    Class<? extends ForestLogHandler> logHandlerClass = annotation.value();
    ForestLogHandler logHandler = null;
    try {
        logHandler = logHandlerClass.newInstance();
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    }
    if (logHandler != null) {
        logConfiguration.setLogHandler(logHandler);
    } else {
        logConfiguration.setLogHandler(configuration.getLogHandler());
    }
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 9 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class ForestConfigurationBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(FOREST_CONFIGURATION_CLASS);
    beanDefinition.setLazyInit(false);
    beanDefinition.setFactoryMethodName("configuration");
    String id = element.getAttribute("id");
    BeanDefinition objectFactoryBean = createForestObjectFactoryBean();
    beanDefinition.getPropertyValues().addPropertyValue("forestObjectFactory", objectFactoryBean);
    BeanDefinition interceptorFactoryBean = createInterceptorFactoryBean();
    beanDefinition.getPropertyValues().addPropertyValue("interceptorFactory", interceptorFactoryBean);
    id = ClientFactoryBeanUtils.getBeanId(id, FOREST_CONFIGURATION_CLASS, parserContext);
    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new IllegalStateException("Duplicate spring bean id " + id);
        }
        parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
        ConstructorArgumentValues argumentValues = new ConstructorArgumentValues();
        argumentValues.addIndexedArgumentValue(0, id);
        beanDefinition.setConstructorArgumentValues(argumentValues);
    }
    Method[] methods = FOREST_CONFIGURATION_CLASS.getMethods();
    for (Method method : methods) {
        String methodName = method.getName();
        Class[] paramTypes = method.getParameterTypes();
        if (paramTypes.length == 0 || paramTypes.length > 1) {
            continue;
        }
        Class paramType = paramTypes[0];
        if (Collections.class.isAssignableFrom(paramType) || Map.class.isAssignableFrom(paramType)) {
            continue;
        }
        if (methodName.length() >= 3 && methodName.startsWith("set")) {
            String attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
            String attributeValue = element.getAttribute(attributeName);
            if (StringUtils.isNotEmpty(attributeValue)) {
                if ("backend".equals(attributeName)) {
                    beanDefinition.getPropertyValues().addPropertyValue("backendName", attributeValue);
                } else if ("logHandler".equals(attributeName)) {
                    try {
                        Class clazz = Class.forName(attributeValue);
                        if (!ForestLogHandler.class.isAssignableFrom(clazz)) {
                            throw new ForestRuntimeException("property 'logHandler' must be a class extending from com.dtflys.forest.logging.ForestLogHandler");
                        }
                        ForestLogHandler handler = (ForestLogHandler) clazz.newInstance();
                        beanDefinition.getPropertyValues().addPropertyValue("logHandler", handler);
                    } catch (ClassNotFoundException e) {
                        throw new ForestRuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new ForestRuntimeException(e);
                    } catch (InstantiationException e) {
                        throw new ForestRuntimeException(e);
                    }
                } else {
                    beanDefinition.getPropertyValues().addPropertyValue(attributeName, attributeValue);
                }
            }
        }
    }
    parseChildren(element.getChildNodes(), beanDefinition);
    LOG.info("[Forest] Created Forest Configuration Bean: " + beanDefinition);
    return beanDefinition;
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ObjectWithReflectiveEqualsHashCodeToString(org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Collections(java.util.Collections) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)9 LogConfiguration (com.dtflys.forest.logging.LogConfiguration)6 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)5 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)3 ResponseLogMessage (com.dtflys.forest.logging.ResponseLogMessage)2 Map (java.util.Map)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ManagedMap (org.springframework.beans.factory.support.ManagedMap)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)1 DownloadFile (com.dtflys.forest.extensions.DownloadFile)1 ForestLogger (com.dtflys.forest.logging.ForestLogger)1 RequestLogMessage (com.dtflys.forest.logging.RequestLogMessage)1 MetaRequest (com.dtflys.forest.reflection.MetaRequest)1 ForestConvertProperties (com.dtflys.forest.springboot.properties.ForestConvertProperties)1 ForestSSLKeyStoreProperties (com.dtflys.forest.springboot.properties.ForestSSLKeyStoreProperties)1 ForestDataType (com.dtflys.forest.utils.ForestDataType)1 JsonTestUser2 (com.dtflys.test.http.model.JsonTestUser2)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1