@@ -90,6 +90,7 @@ public class SearchImageService {
|
||||
}
|
||||
|
||||
private OkHttpClient createHttpClient() {
|
||||
boolean useProxy = searchProperties.getProxy().isEnabled();
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
||||
.connectTimeout(35, TimeUnit.SECONDS)
|
||||
.writeTimeout(35, TimeUnit.SECONDS)
|
||||
@@ -99,12 +100,12 @@ public class SearchImageService {
|
||||
.followSslRedirects(true)
|
||||
.retryOnConnectionFailure(true);
|
||||
|
||||
// if (useProxy && searchProperties.getProxy().isEnabled()) {
|
||||
// Proxy proxy = new Proxy(Proxy.Type.HTTP,
|
||||
// new InetSocketAddress(searchProperties.getProxy().getHost(),
|
||||
// searchProperties.getProxy().getPort()));
|
||||
// builder.proxy(proxy);
|
||||
// }
|
||||
if (useProxy && searchProperties.getProxy().isEnabled()) {
|
||||
Proxy proxy = new Proxy(Proxy.Type.HTTP,
|
||||
new InetSocketAddress(searchProperties.getProxy().getHost(),
|
||||
searchProperties.getProxy().getPort()));
|
||||
builder.proxy(proxy);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
@@ -114,9 +115,6 @@ public class SearchImageService {
|
||||
throw new IllegalStateException("SearchAPI key not configured");
|
||||
}
|
||||
|
||||
// boolean useProxy = searchProperties.getProxy().isEnabled();
|
||||
|
||||
// OkHttpClient client = createHttpClient(useProxy);
|
||||
OkHttpClient client = createHttpClient();
|
||||
|
||||
HttpUrl url = HttpUrl.parse("https://www.searchapi.io/api/v1/search")
|
||||
@@ -136,25 +134,79 @@ public class SearchImageService {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
// try (Response response = client.newCall(request).execute()) {
|
||||
// long duration = System.currentTimeMillis() - start;
|
||||
// log.info("SearchAPI response code={}, duration={}ms, engine={}",
|
||||
// response.code(), duration, engine);
|
||||
//
|
||||
// if (!response.isSuccessful()) {
|
||||
// String errorBody = response.body() != null ? response.body().string() : "null";
|
||||
// throw new IOException("API error " + response.code() + ": " + errorBody);
|
||||
// }
|
||||
//
|
||||
// ResponseBody body = response.body();
|
||||
// if (body == null) {
|
||||
// throw new IOException("Empty response body");
|
||||
// }
|
||||
//
|
||||
// return body.string();
|
||||
// }
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
long duration = System.currentTimeMillis() - start;
|
||||
log.info("SearchAPI response code={}, duration={}ms, engine={}",
|
||||
response.code(), duration, engine);
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
String errorBody = response.body() != null ? response.body().string() : "null";
|
||||
throw new IOException("API error " + response.code() + ": " + errorBody);
|
||||
}
|
||||
|
||||
ResponseBody body = response.body();
|
||||
if (body == null) {
|
||||
throw new IOException("Empty response body");
|
||||
}
|
||||
|
||||
return body.string();
|
||||
String responseBody = readWithTimeout(body, 30000);
|
||||
|
||||
log.info("Read {} bytes (may be partial)", responseBody.length());
|
||||
|
||||
if (responseBody.length() > 0) {
|
||||
String preview = responseBody.length() > 500 ?
|
||||
responseBody.substring(0, 500) + "..." : responseBody;
|
||||
log.info("Response preview: {}", preview);
|
||||
}
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("API error " + response.code() + ": " +
|
||||
(responseBody.length() > 200 ? responseBody.substring(0, 200) : responseBody));
|
||||
}
|
||||
|
||||
return responseBody;
|
||||
|
||||
} catch (InterruptedIOException e) {
|
||||
log.error("Timeout during reading, partial data may be lost");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private String readWithTimeout(ResponseBody body, long timeoutMs) throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(body.charStream())) {
|
||||
char[] buffer = new char[8192];
|
||||
int read;
|
||||
|
||||
while ((read = reader.read(buffer)) != -1) {
|
||||
result.append(buffer, 0, read);
|
||||
|
||||
if (System.currentTimeMillis() - startTime > timeoutMs) {
|
||||
log.warn("Partial read timeout after {} ms, received {} bytes",
|
||||
timeoutMs, result.length());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
|
||||
throws IOException {
|
||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
|
||||
Reference in New Issue
Block a user