use of org.leadpony.justify.api.ProblemHandler in project zilla by aklivity.
the class ConfigureTask method call.
@Override
public Void call() throws Exception {
String configText;
if (configURL == null) {
configText = "{\"name\":\"default\"}";
} else if ("https".equals(configURL.getProtocol()) || "https".equals(configURL.getProtocol())) {
HttpClient client = HttpClient.newBuilder().version(HTTP_2).followRedirects(NORMAL).build();
HttpRequest request = HttpRequest.newBuilder().GET().uri(configURL.toURI()).build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();
configText = body;
} else {
URLConnection connection = configURL.openConnection();
try (InputStream input = connection.getInputStream()) {
configText = new String(input.readAllBytes(), UTF_8);
}
}
if (config.configSyntaxMustache()) {
configText = Mustache.resolve(configText, System::getenv);
}
logger.accept(configText);
List<String> errors = new LinkedList<>();
parse: try {
InputStream schemaInput = Engine.class.getResourceAsStream("internal/schema/engine.schema.json");
JsonProvider schemaProvider = JsonProvider.provider();
JsonReader schemaReader = schemaProvider.createReader(schemaInput);
JsonObject schemaObject = schemaReader.readObject();
for (URL schemaType : schemaTypes) {
InputStream schemaPatchInput = schemaType.openStream();
JsonReader schemaPatchReader = schemaProvider.createReader(schemaPatchInput);
JsonArray schemaPatchArray = schemaPatchReader.readArray();
JsonPatch schemaPatch = schemaProvider.createPatch(schemaPatchArray);
schemaObject = schemaPatch.apply(schemaObject);
}
JsonParser schemaParser = schemaProvider.createParserFactory(null).createParser(new StringReader(schemaObject.toString()));
JsonValidationService service = JsonValidationService.newInstance();
ProblemHandler handler = service.createProblemPrinter(errors::add);
JsonSchemaReader reader = service.createSchemaReader(schemaParser);
JsonSchema schema = new UniquePropertyKeysSchema(reader.read());
JsonProvider provider = service.createJsonProvider(schema, parser -> handler);
provider.createReader(new StringReader(configText)).read();
if (!errors.isEmpty()) {
break parse;
}
JsonbConfig config = new JsonbConfig().withAdapters(new NamespaceAdapter());
Jsonb jsonb = JsonbBuilder.newBuilder().withProvider(provider).withConfig(config).build();
NamespaceConfig namespace = jsonb.fromJson(configText, NamespaceConfig.class);
if (!errors.isEmpty()) {
break parse;
}
namespace.id = supplyId.applyAsInt(namespace.name);
for (BindingConfig binding : namespace.bindings) {
binding.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.entry));
if (binding.vault != null) {
binding.vault.id = NamespacedId.id(supplyId.applyAsInt(ofNullable(binding.vault.namespace).orElse(namespace.name)), supplyId.applyAsInt(binding.vault.name));
}
// TODO: consider route exit namespace
for (RouteConfig route : binding.routes) {
route.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(route.exit));
}
// TODO: consider binding exit namespace
if (binding.exit != null) {
binding.exit.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.exit.exit));
}
tuning.affinity(binding.id, tuning.affinity(binding.id));
}
for (VaultConfig vault : namespace.vaults) {
vault.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(vault.name));
}
CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
for (DispatchAgent dispatcher : dispatchers) {
future = CompletableFuture.allOf(future, dispatcher.attach(namespace));
}
future.join();
extensions.forEach(e -> e.onConfigured(context));
} catch (Throwable ex) {
errorHandler.onError(ex);
}
if (!errors.isEmpty()) {
errors.forEach(msg -> errorHandler.onError(new JsonException(msg)));
}
return null;
}
use of org.leadpony.justify.api.ProblemHandler in project zilla by aklivity.
the class ConfigSchemaRule method apply.
@Override
public Statement apply(Statement base, Description description) {
Objects.requireNonNull(schemaName, "schema");
schemaPatchNames.forEach(n -> Objects.requireNonNull(n, "schemaPatch"));
Function<String, InputStream> findResource = description.getTestClass().getClassLoader()::getResourceAsStream;
InputStream schemaInput = findResource.apply(schemaName);
JsonProvider schemaProvider = JsonProvider.provider();
JsonReader schemaReader = schemaProvider.createReader(schemaInput);
JsonObject schemaObject = schemaReader.readObject();
for (String schemaPatchName : schemaPatchNames) {
InputStream schemaPatchInput = findResource.apply(schemaPatchName);
Objects.requireNonNull(schemaPatchInput, "schemaPatch");
JsonReader schemaPatchReader = schemaProvider.createReader(schemaPatchInput);
JsonArray schemaPatchArray = schemaPatchReader.readArray();
JsonPatch schemaPatch = schemaProvider.createPatch(schemaPatchArray);
schemaObject = schemaPatch.apply(schemaObject);
}
JsonParser schemaParser = schemaProvider.createParserFactory(null).createParser(new StringReader(schemaObject.toString()));
JsonValidationService service = JsonValidationService.newInstance();
ProblemHandler handler = service.createProblemPrinter(msg -> rethrowUnchecked(new JsonException(msg)));
JsonSchemaReader reader = service.createSchemaReader(schemaParser);
JsonSchema schema = reader.read();
provider = service.createJsonProvider(schema, parser -> handler);
if (configurationRoot != null) {
String configFormat = String.format("%s/%%s", configurationRoot);
findConfig = configName -> findResource.apply(String.format(configFormat, configName));
} else {
Class<?> testClass = description.getTestClass();
String configFormat = String.format("%s-%%s", testClass.getSimpleName());
findConfig = configName -> testClass.getResourceAsStream(String.format(configFormat, configName));
}
return base;
}
use of org.leadpony.justify.api.ProblemHandler in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeJsonSchemaPersonalization method loadGraphQLSchemaPersonalization.
/**
* Let's load the schema personalization from the configuration json file.
*
* @return
*
* @throws IOException
* @throws ProcessingException
* When the JSON file is not valid
* @throws URISyntaxException
* If we can't process the JSON URIs. It would be an internal error.
*/
public SchemaPersonalization loadGraphQLSchemaPersonalization() throws IOException, URISyntaxException {
if (((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile() == null) {
return null;
} else {
// Let's check that the JSON is valid
JsonValidationService service = JsonValidationService.newInstance();
// Reads the JSON schema
//
// There is an issue in reading the json schema, when the project is built with Gradle 7
// It seems to be linked to https://issues.apache.org/jira/browse/JOHNZON-147 (solved in Johnzon 1.1.6, in
// february 2018)
// A workaround is to first read the schema in a string, then call the readSchema
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/" + JSON_SCHEMA_FILENAME), Charset.forName("UTF-8")));
String text = br.lines().collect(Collectors.joining("\n"));
// readSchema should directly read the InputStream
JsonSchema schema = service.readSchema(new StringReader(text));
// Problem handler which will print problems found
ProblemHandler handler = service.createProblemPrinter(this::logParsingError);
// Reads the JSON instance by javax.json.JsonReader
nbErrors = 0;
try (JsonReader reader = service.createReader(new FileInputStream(((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile()), schema, handler)) {
// JsonValue value =
reader.readValue();
// Do something useful here
}
if (nbErrors > 0) {
throw new RuntimeException("The json file '" + ((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile().getAbsolutePath() + "' is invalid. See the logs for details");
}
// Let's read the flow definition
logger.info("Loading file " + ((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile().getAbsolutePath());
ObjectMapper objectMapper = new ObjectMapper();
SchemaPersonalization ret;
try (InputStream isFlowJson = new FileInputStream(((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile())) {
ret = objectMapper.readValue(isFlowJson, SchemaPersonalization.class);
}
return ret;
}
}
Aggregations