
com.undefinedlabs.scope.settings.remote.ScopeRemoteSettingsInvoker Maven / Gradle / Ivy
package com.undefinedlabs.scope.settings.remote;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.undefinedlabs.scope.exceptions.InitializationException;
import com.undefinedlabs.scope.logger.ScopeLogger;
import com.undefinedlabs.scope.logger.ScopeLoggerResolver;
import com.undefinedlabs.scope.network.HttpClient;
import com.undefinedlabs.scope.network.HttpClientResolver;
import com.undefinedlabs.scope.network.NetworkException;
import com.undefinedlabs.scope.sender.internal.serializers.ObjectMapperFactory;
import com.undefinedlabs.scope.settings.ScopeSettings;
import com.undefinedlabs.scope.utils.Endpoints;
import com.undefinedlabs.scope.utils.StringUtils;
import com.undefinedlabs.scope.utils.metadata.MetadataCreator;
import java.io.IOException;
import java.util.Map;
import javax.net.ssl.SSLException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;
public enum ScopeRemoteSettingsInvoker {
INSTANCE;
private static final ObjectMapper JSON_SERIALIZER =
ObjectMapperFactory.newConfiguredObjectMapper();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ScopeLogger LOGGER = ScopeLoggerResolver.INSTANCE.get();
public ScopeRemoteSettings invoke(final ScopeSettings settings) {
final String repository = (String) settings.getSetting(ScopeSettings.SCOPE_REPOSITORY);
final String commit = (String) settings.getSetting(ScopeSettings.SCOPE_COMMIT_SHA);
if (StringUtils.isEmpty(repository) || StringUtils.isEmpty(commit)) {
LOGGER.info(
"CACHED tests will not be obtained because repository and/or commit could not be resolved.");
return ScopeRemoteSettings.EMPTY;
}
final HttpClient httpClient = HttpClientResolver.INSTANCE.get(settings);
final Map requestData = MetadataCreator.INSTANCE.createMetadata(settings);
final String requestDataString;
try {
requestDataString = JSON_SERIALIZER.writeValueAsString(requestData);
LOGGER.debug("--- METADATA: " + requestDataString);
} catch (final Exception e) {
return ScopeRemoteSettings.EMPTY;
}
try (Response response =
httpClient.post(
Endpoints.API_AGENT_CONFIG,
RequestBody.create(MediaType.parse("application/json"), requestDataString))) {
if (!response.isSuccessful() || response.body() == null) {
return ScopeRemoteSettings.EMPTY;
} else {
return OBJECT_MAPPER.readValue(response.body().charStream(), ScopeRemoteSettings.class);
}
} catch (final NetworkException e) {
if (e.getCause() instanceof SSLException) {
LOGGER.error("SSL Handshake could not be completed.");
LOGGER.error(
"Check https://docs.scope.dev/docs/java-troubleshooting for more information.");
} else {
LOGGER.error("Error connecting to /api/agent/config: " + e);
}
throw new InitializationException(e);
} catch (final IOException e) {
LOGGER.error("Error reading /api/agent/config response: " + e);
return ScopeRemoteSettings.EMPTY;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy