|
|
@@ -9,27 +9,31 @@ import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
import static cz.senslog.watchdog.util.json.BasicJson.jsonToObject;
|
|
|
import static java.util.Collections.emptyList;
|
|
|
|
|
|
public class SensLogWSConnection {
|
|
|
|
|
|
+ private static final int MAX_AUTH_ERRORS = 2;
|
|
|
+
|
|
|
private static final Logger logger = LogManager.getLogger(SensLogWSConnection.class);
|
|
|
|
|
|
private final HttpClient httpClient;
|
|
|
private final SensLogServerConfig config;
|
|
|
+ private final Map<Long, AtomicInteger> authError;
|
|
|
|
|
|
private Tuple<Boolean, HttpCookie> authCookie;
|
|
|
- private int authError;
|
|
|
|
|
|
public SensLogWSConnection(SensLogServerConfig config) {
|
|
|
this.config = config;
|
|
|
this.httpClient = HttpClient.newHttpClient();
|
|
|
this.authCookie = Tuple.of(false, null);
|
|
|
- this.authError = 0;
|
|
|
+ this.authError = new HashMap<>();
|
|
|
}
|
|
|
|
|
|
private synchronized HttpCookie getAuthCookie() {
|
|
|
@@ -46,7 +50,7 @@ public class SensLogWSConnection {
|
|
|
logger.info("Getting new auth cookie from the server: {}.", config.getBaseUrl());
|
|
|
|
|
|
HttpResponse response = httpClient.send(request);
|
|
|
- logger.info("Received data with the status '{}' from the server {}.", response.getStatus(), config.getBaseUrl());
|
|
|
+ logger.info("Received new auth token with the status '{}' from the server {}.", response.getStatus(), config.getBaseUrl());
|
|
|
|
|
|
if (response.isError()) {
|
|
|
logger.warn("Authorization failed. Error code {} with the reason '{}'.", response.getStatus(), response.getBody());
|
|
|
@@ -84,11 +88,13 @@ public class SensLogWSConnection {
|
|
|
logger.info("Getting new data from the server: {}.", request.getUrl());
|
|
|
|
|
|
HttpResponse response = httpClient.send(request);
|
|
|
- logger.info("Received data with the status '{}' from the server {}.", response.getStatus(), request.getUrl());
|
|
|
+ logger.info("Received new data with the status '{}' from the server {}.", response.getStatus(), request.getUrl());
|
|
|
|
|
|
- if (response.getStatus() == HttpCode.UNAUTHORIZED && authError <= 2) {
|
|
|
+ long threadId = Thread.currentThread().getId();
|
|
|
+ AtomicInteger authErrors = authError.computeIfAbsent(threadId, id -> new AtomicInteger(0));
|
|
|
+ if (response.getStatus() == HttpCode.UNAUTHORIZED && authErrors.get() <= MAX_AUTH_ERRORS) {
|
|
|
this.authCookie = Tuple.of(false, null);
|
|
|
- this.authError++;
|
|
|
+ authErrors.incrementAndGet();
|
|
|
return callRequest(reqBuilder);
|
|
|
}
|
|
|
|
|
|
@@ -97,7 +103,7 @@ public class SensLogWSConnection {
|
|
|
throw new IllegalStateException(response.getBody());
|
|
|
}
|
|
|
|
|
|
- this.authError = 0;
|
|
|
+ authErrors.set(0);
|
|
|
final Type lastObsType = new TypeToken<Collection<Map<String, Object>>>() {}.getType();
|
|
|
return jsonToObject(response.getBody(), lastObsType);
|
|
|
}
|