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);
}
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();
}
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;
}
}
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);
}
}
}
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);
}
}
Aggregations