Search in sources :

Example 41 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class ForestRequest method getSSLSocketFactory.

/**
 * 获取 SSL Socket 工厂
 *
 * @return SSL Socket 工厂
 */
public SSLSocketFactory getSSLSocketFactory() {
    if (sslSocketFactoryBuilder != null) {
        try {
            return sslSocketFactoryBuilder.getSSLSocketFactory(this, getSslProtocol());
        } catch (Exception e) {
            throw new ForestRuntimeException(e);
        }
    }
    SSLKeyStore keyStore = this.getKeyStore();
    if (keyStore == null) {
        return SSLUtils.getDefaultSSLSocketFactory(this, getSslProtocol());
    }
    SSLSocketFactoryBuilder sslSocketFactoryBuilder = keyStore.getSslSocketFactoryBuilder();
    if (sslSocketFactoryBuilder == null) {
        return SSLUtils.getDefaultSSLSocketFactory(this, getSslProtocol());
    }
    try {
        SSLSocketFactory sslSocketFactory = sslSocketFactoryBuilder.getSSLSocketFactory(this, getSslProtocol());
        if (sslSocketFactory == null) {
            return SSLUtils.getDefaultSSLSocketFactory(this, getSslProtocol());
        }
        return sslSocketFactory;
    } catch (Exception e) {
        throw new ForestRuntimeException(e);
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) SSLKeyStore(com.dtflys.forest.ssl.SSLKeyStore) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestVariableUndefinedException(com.dtflys.forest.exceptions.ForestVariableUndefinedException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) SSLSocketFactoryBuilder(com.dtflys.forest.ssl.SSLSocketFactoryBuilder)

Example 42 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class ForestURL method setBasePath.

/**
 * 设置URL根路径
 * <p>该路径为整个URL去除前面协议 + Host + Port 后部分
 *
 * @param basePath
 */
public ForestURL setBasePath(String basePath) {
    if (basePath == null) {
        return this;
    }
    this.basePath = basePath.trim();
    if (!this.basePath.startsWith("/")) {
        if (URLUtils.isURL(this.basePath)) {
            try {
                URL url = new URL(this.basePath);
                this.scheme = url.getProtocol();
                this.userInfo = url.getUserInfo();
                this.host = url.getHost();
                this.port = url.getPort();
                this.basePath = url.getPath();
            } catch (MalformedURLException e) {
                throw new ForestRuntimeException(e);
            }
        } else {
            this.basePath = "/" + this.basePath;
        }
    }
    this.originalUrl = toURLString();
    return this;
}
Also used : MalformedURLException(java.net.MalformedURLException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) URL(java.net.URL)

Example 43 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class DefaultInterceptorFactory method createInterceptor.

protected <T extends Interceptor> Interceptor createInterceptor(Class<T> clazz) {
    Interceptor interceptor;
    try {
        interceptor = clazz.newInstance();
        interceptorMap.put(clazz, interceptor);
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    }
    return interceptor;
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 44 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException 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)

Example 45 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class ConverterBeanListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationContextEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();
    ForestConfiguration forestConfiguration = this.forestConfiguration;
    if (forestConfiguration == null) {
        try {
            forestConfiguration = applicationContext.getBean(ForestConfiguration.class);
        } catch (Exception ignored) {
            throw new ForestRuntimeException("property forestConfiguration undefined", ignored);
        }
    }
    Map<String, ForestConverter> forestConverterMap = applicationContext.getBeansOfType(ForestConverter.class);
    for (ForestConverter forestConverter : forestConverterMap.values()) {
        forestConfiguration.getConverterMap().put(forestConverter.getDataType(), forestConverter);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestConverter(com.dtflys.forest.converter.ForestConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Aggregations

ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)64 Test (org.junit.Test)14 Map (java.util.Map)9 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)7 MetaRequest (com.dtflys.forest.reflection.MetaRequest)6 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)5 MappingParameter (com.dtflys.forest.mapping.MappingParameter)5 Method (java.lang.reflect.Method)5 Parameter (java.lang.reflect.Parameter)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 SSLKeyStore (com.dtflys.forest.ssl.SSLKeyStore)4 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 ForestConverter (com.dtflys.forest.converter.ForestConverter)3 ForestRequest (com.dtflys.forest.http.ForestRequest)3 Interceptor (com.dtflys.forest.interceptor.Interceptor)3