Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
flben233 committed Oct 30, 2024
2 parents 65b1bca + 224d973 commit 9ca4c58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -70,6 +80,7 @@ public static String postForString(String url, MultiValueMap<String, String> dat
});
}

@SneakyThrows
public static <T> T getByHttpClient(String url, String referer, CookieStore cookieStore, HttpClientResponseHandler<T> handler) {
HttpGet httpGet = new HttpGet(url);
if (!"".equals(referer)) {
Expand All @@ -79,6 +90,7 @@ public static <T> 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()
Expand All @@ -98,6 +110,7 @@ public static <T> 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()
Expand All @@ -107,4 +120,17 @@ public static <T> 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();
}
}

0 comments on commit 9ca4c58

Please sign in to comment.