Search in sources :

Example 1 with ForestRuntimeException

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

the class ForestConfigurationBeanDefinitionParser method parseConverter.

private static void parseConverter(Element elem, ManagedMap<ForestDataType, BeanDefinition> converterMap) {
    String dataTypeName = elem.getAttribute("dataType");
    ForestDataType dataType = ForestDataType.findOrCreateDataType(dataTypeName);
    if (dataType == null) {
        throw new ForestRuntimeException("Cannot find data type named '" + dataTypeName + "'");
    }
    String className = elem.getAttribute("class");
    BeanDefinition definition = createConverterBean(className);
    NodeList nodeList = elem.getChildNodes();
    int nodeLength = nodeList.getLength();
    if (nodeLength > 0) {
        for (int i = 0; i < nodeLength; i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element childElem = (Element) node;
                String childElemName = childElem.getLocalName();
                if (childElemName.equals("parameter")) {
                    String paramName = childElem.getAttribute("name");
                    String paramValue = childElem.getAttribute("value");
                    definition.getPropertyValues().addPropertyValue(paramName, paramValue);
                }
            }
        }
    }
    converterMap.put(dataType, definition);
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ForestDataType(com.dtflys.forest.utils.ForestDataType) ObjectWithReflectiveEqualsHashCodeToString(org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 2 with ForestRuntimeException

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

the class SSLTest method testSSL.

public void testSSL() {
    applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath:ssl-test.xml" });
    ForestConfiguration configuration = (ForestConfiguration) applicationContext.getBean("forestConfiguration");
    SSLKeyStore keyStore = configuration.getKeyStore("keystore1");
    assertNotNull(keyStore);
    assertNotNull(keyStore.getInputStream());
    assertEquals("keystore1", keyStore.getId());
    assertEquals("123456", keyStore.getKeystorePass());
    assertEquals("jks", keyStore.getKeystoreType());
    assertThat(keyStore.getSslSocketFactoryBuilder()).isNotNull().isInstanceOf(MySSLSocketFactoryBuilder.class);
    assertThat(keyStore.getHostnameVerifier()).isNotNull().isInstanceOf(MyHostnameVerifier.class);
    BeastshopClient beastshopClient = (BeastshopClient) applicationContext.getBean("beastshopClient");
    assertNotNull(beastshopClient);
    String result = beastshopClient.index();
    assertNotNull(result);
    Throwable th = null;
    try {
        beastshopClient.index2();
    } catch (ForestRuntimeException ex) {
        th = ex.getCause();
    }
    assertThat(th).isNotNull();
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) BeastshopClient(com.dtflys.spring.test.client0.BeastshopClient) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) SSLKeyStore(com.dtflys.forest.ssl.SSLKeyStore)

Example 3 with ForestRuntimeException

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

the class HttpclientExecutor method execute.

@Override
public void execute(LifeCycleHandler lifeCycleHandler) {
    prepare(lifeCycleHandler);
    Date startDate = new Date();
    ForestResponseFactory forestResponseFactory = new HttpclientForestResponseFactory();
    try {
        requestSender.sendRequest(request, this, httpclientResponseHandler, httpRequest, lifeCycleHandler, cookieStore, startDate);
    } catch (IOException e) {
        httpRequest.abort();
        response = forestResponseFactory.createResponse(request, null, lifeCycleHandler, e, startDate);
        lifeCycleHandler.handleSyncWithException(request, response, e);
        return;
    } catch (ForestRuntimeException e) {
        httpRequest.abort();
        throw e;
    }
}
Also used : HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) IOException(java.io.IOException) ForestResponseFactory(com.dtflys.forest.http.ForestResponseFactory) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) Date(java.util.Date)

Example 4 with ForestRuntimeException

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

the class SyncHttpclientRequestSender method sendRequest.

@Override
public void sendRequest(ForestRequest request, AbstractHttpExecutor executor, HttpclientResponseHandler responseHandler, HttpUriRequest httpRequest, LifeCycleHandler lifeCycleHandler, CookieStore cookieStore, Date startDate) {
    HttpResponse httpResponse = null;
    ForestResponse response = null;
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute("REQUEST", request);
    HttpClientContext httpClientContext = HttpClientContext.adapt(httpContext);
    client = getHttpClient(cookieStore);
    ForestResponseFactory forestResponseFactory = new HttpclientForestResponseFactory();
    try {
        logRequest(request.getCurrentRetryCount(), (HttpRequestBase) httpRequest);
        httpResponse = client.execute(httpRequest, httpClientContext);
    } catch (Throwable e) {
        httpRequest.abort();
        response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, e, startDate);
        ForestRetryException retryException = new ForestRetryException(e, request, request.getRetryCount(), request.getCurrentRetryCount());
        try {
            request.canRetry(response, retryException);
        } catch (Throwable throwable) {
            response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, throwable, startDate);
            lifeCycleHandler.handleSyncWithException(request, response, e);
            return;
        }
        response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
        logResponse(response);
        executor.execute(lifeCycleHandler);
        return;
    } finally {
        if (response == null) {
            response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
        }
        logResponse(response);
    }
    response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
    // 检查是否重试
    ForestRetryException retryEx = request.canRetry(response);
    if (retryEx != null && retryEx.isNeedRetry() && !retryEx.isMaxRetryCountReached()) {
        executor.execute(lifeCycleHandler);
        return;
    }
    // 检查响应是否失败
    if (response.isError()) {
        ForestNetworkException networkException = new ForestNetworkException("", response.getStatusCode(), response);
        ForestRetryException retryException = new ForestRetryException(networkException, request, request.getRetryCount(), request.getCurrentRetryCount());
        try {
            request.canRetry(response, retryException);
        } catch (Throwable throwable) {
            responseHandler.handleSync(httpResponse, response);
            return;
        }
        executor.execute(lifeCycleHandler);
        return;
    }
    try {
        lifeCycleHandler.handleSaveCookie(request, getCookiesFromHttpCookieStore(cookieStore));
        responseHandler.handleSync(httpResponse, response);
    } catch (Exception ex) {
        if (ex instanceof ForestRuntimeException) {
            throw ex;
        } else {
            throw new ForestRuntimeException(ex);
        }
    }
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ForestResponseFactory(com.dtflys.forest.http.ForestResponseFactory) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) IOException(java.io.IOException)

Example 5 with ForestRuntimeException

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

the class HttpclientConnectionManager method init.

@Override
public void init(ForestConfiguration configuration) {
    try {
        Integer maxConnections = configuration.getMaxConnections() != null ? configuration.getMaxConnections() : HttpConnectionConstants.DEFAULT_MAX_TOTAL_CONNECTIONS;
        Integer maxRouteConnections = configuration.getMaxRouteConnections() != null ? configuration.getMaxRouteConnections() : HttpConnectionConstants.DEFAULT_MAX_TOTAL_CONNECTIONS;
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", new ForestSSLConnectionFactory()).register("http", new PlainConnectionSocketFactory()).build();
        tsConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        tsConnectionManager.setMaxTotal(maxConnections);
        tsConnectionManager.setDefaultMaxPerRoute(maxRouteConnections);
    } catch (Throwable th) {
        throw new ForestRuntimeException(th);
    }
}
Also used : PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

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