diff --git a/src/main/java/org/shirakawatyu/handixikebackend/aop/ApiLayerAspect.java b/src/main/java/org/shirakawatyu/handixikebackend/aop/ApiLayerAspect.java index 84e78f9..35824f3 100644 --- a/src/main/java/org/shirakawatyu/handixikebackend/aop/ApiLayerAspect.java +++ b/src/main/java/org/shirakawatyu/handixikebackend/aop/ApiLayerAspect.java @@ -68,7 +68,12 @@ public Object around(ProceedingJoinPoint point) { try { return point.proceed(); } catch (Throwable e) { - switch (e.getCause()) { + Throwable cause = e.getCause(); + if (cause == null) { + log.error("Err in Aspect", e); + return null; + } + switch (cause) { case ResourceAccessException e1 -> { Throwable rootCause = e1.getRootCause(); if (rootCause instanceof HttpServerErrorException.GatewayTimeout | rootCause instanceof SocketTimeoutException | @@ -103,7 +108,7 @@ public Count(int times, long lastCountTime) { private void defaultHandler(Count cnt, String method, Throwable e) { cnt.times.updateAndGet((x) -> { - log.warn("{}: {} {}", e.getClass().getSimpleName(), method, cnt.times); + log.warn("{}: {} {}", e.getClass(), method, cnt.times, e); return x + 1; }); throw new CircuitBreakerException(); diff --git a/src/main/java/org/shirakawatyu/handixikebackend/utils/Requests.java b/src/main/java/org/shirakawatyu/handixikebackend/utils/Requests.java index 908c191..1d937da 100644 --- a/src/main/java/org/shirakawatyu/handixikebackend/utils/Requests.java +++ b/src/main/java/org/shirakawatyu/handixikebackend/utils/Requests.java @@ -1,5 +1,6 @@ package org.shirakawatyu.handixikebackend.utils; +import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpPost; @@ -10,15 +11,24 @@ import org.apache.hc.client5.http.impl.DefaultRedirectStrategy; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder; +import org.apache.hc.client5.http.ssl.TrustAllStrategy; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.apache.hc.core5.ssl.SSLContextBuilder; import org.apache.hc.core5.util.Timeout; import org.shirakawatyu.handixikebackend.exception.RequestException; import org.springframework.util.MultiValueMap; import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; /** @@ -70,6 +80,7 @@ public static String postForString(String url, MultiValueMap dat }); } + @SneakyThrows public static T getByHttpClient(String url, String referer, CookieStore cookieStore, HttpClientResponseHandler handler) { HttpGet httpGet = new HttpGet(url); if (!"".equals(referer)) { @@ -79,6 +90,7 @@ public static T getByHttpClient(String url, String referer, CookieStore cook httpGet.addHeader("User-Agent", USER_AGENT); try (CloseableHttpClient client = HttpClients.custom() .setDefaultRequestConfig(requestConfig) + .setConnectionManager(getManager()) .setRedirectStrategy(new DefaultRedirectStrategy()) .setDefaultCookieStore(cookieStore) .disableDefaultUserAgent() @@ -98,6 +110,7 @@ public static T postByHttpClient(String url, CookieStore cookieStore, MultiV httpPost.setEntity(new UrlEncodedFormEntity(params)); try (CloseableHttpClient client = HttpClients.custom() .setDefaultRequestConfig(requestConfig) + .setConnectionManager(getManager()) .setRedirectStrategy(new DefaultRedirectStrategy()) .setDefaultCookieStore(cookieStore) .disableDefaultUserAgent() @@ -107,4 +120,17 @@ public static T postByHttpClient(String url, CookieStore cookieStore, MultiV throw new RequestException(e); } } + + + @SneakyThrows + private PoolingHttpClientConnectionManager getManager() { + return PoolingHttpClientConnectionManagerBuilder.create() + .setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create() + .setSslContext(SSLContextBuilder.create() + .loadTrustMaterial(TrustAllStrategy.INSTANCE) + .build()) + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .build()) + .build(); + } }