Skip to content

Commit

Permalink
sylvainlaurent#26 Allow to download schema from http(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapicard committed Dec 18, 2021
1 parent 673ed69 commit 3f520f5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>json-schema-validator</artifactId>
<version>${json-schema-validator-version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions src/it/multiple-validation-sets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<include>src/main/resources/malformed.yml</include>
</includes>
</validationSet>
<validationSet>
<jsonSchema>https://swagger.io/v2/schema.json</jsonSchema>
<includes>
<include>src/main/resources/swagger-editor-example.yml</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.github.sylvainlaurent.maven.yamljsonvalidator;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

/**
* This mojo validates YAML and JSON files for well-formedness. If JSON schema is provided, it also
Expand Down Expand Up @@ -111,23 +113,37 @@ public void execute() throws MojoExecutionException {

InputStream openJsonSchema(String jsonSchemaFile) throws MojoExecutionException {
if (jsonSchemaFile != null && jsonSchemaFile.length() > 0) {
File file = new File(jsonSchemaFile);
if (file.isFile()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Could not load schema file ["+ jsonSchemaFile + "]", e);
}
} else {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(jsonSchemaFile);
if (inputStream != null) {
return inputStream;
try {
URI uri = new URI(jsonSchemaFile);
if(uri.getScheme() == null) {
File file = new File(jsonSchemaFile);
if (file.isFile()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Could not load schema file ["+ jsonSchemaFile + "]", e);
}
} else {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(jsonSchemaFile);
if (inputStream != null) {
return inputStream;
}
throw new MojoExecutionException("Could not load schema neither from filesystem nor classpath ["+ jsonSchemaFile + "]");
} catch (Exception e){
throw new MojoExecutionException("Could not load schema file from classpath ["+ jsonSchemaFile + "]", e);
}
}
} else {
try (final CloseableHttpClient httpclient = HttpClients.custom().useSystemProperties().build()) {
HttpGet get = new HttpGet();
get.setURI(uri);
return httpclient.execute(get).getEntity().getContent();
}
throw new MojoExecutionException("Could not load schema neither from filesystem nor classpath ["+ jsonSchemaFile + "]");
} catch (Exception e){
throw new MojoExecutionException("Could not load schema file from classpath ["+ jsonSchemaFile + "]", e);
}
} catch (URISyntaxException | IOException use) {
getLog().warn("An error occurs while reading schema from URI " + jsonSchemaFile);
getLog().debug(use);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public void accept_schema_from_filepath() throws MojoExecutionException {
assertNotNull(validateMojo.openJsonSchema("src/test/resources/schema-with-ref.json"));
}

@Test
public void accept_schema_from_http() throws MojoExecutionException {
ValidateMojo validateMojo = new ValidateMojo();
assertNotNull(validateMojo.openJsonSchema("https://json.schemastore.org/geojson.json"));
}


@Test
public void accept_schema_from_classpath() throws MojoExecutionException {
Expand Down

0 comments on commit 3f520f5

Please sign in to comment.