Search in sources :

Example 1 with JsonInspectionService

use of io.atlasmap.json.inspect.JsonInspectionService in project atlasmap by atlasmap.

the class GenerateInspectionsMojo method generateJsonInstanceInspection.

private void generateJsonInstanceInspection(String fileName) throws MojoFailureException {
    try {
        Path path = Paths.get(fileName);
        String schema = new String(Files.readAllBytes(path));
        JsonDocument d = new JsonInspectionService().inspectJsonDocument(schema);
        String name = path.getFileName().toString();
        String outputName = name.substring(0, name.length() - 5);
        writeToJsonFile(DEFAULT_OUTPUT_FILE_PREFIX + "-" + outputName, d);
    } catch (Exception e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) JsonInspectionService(io.atlasmap.json.inspect.JsonInspectionService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JsonDocument(io.atlasmap.json.v2.JsonDocument) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 2 with JsonInspectionService

use of io.atlasmap.json.inspect.JsonInspectionService in project atlasmap by atlasmap.

the class GenerateInspectionsMojo method generateJsonSchemaInspection.

private void generateJsonSchemaInspection(String fileName) throws MojoFailureException {
    try {
        Path path = Paths.get(fileName);
        String schema = new String(Files.readAllBytes(path));
        JsonDocument d = new JsonInspectionService().inspectJsonSchema(schema);
        String name = path.getFileName().toString();
        String outputName = name.substring(0, name.length() - 5);
        writeToJsonFile(DEFAULT_OUTPUT_FILE_PREFIX + "-" + outputName, d);
    } catch (Exception e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) JsonInspectionService(io.atlasmap.json.inspect.JsonInspectionService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JsonDocument(io.atlasmap.json.v2.JsonDocument) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with JsonInspectionService

use of io.atlasmap.json.inspect.JsonInspectionService in project atlasmap by atlasmap.

the class JsonService method inspect.

/**
 * Inspect a JSON schema or instance and return a Document object.
 * @param requestIn request
 * @return {@link JsonInspectionResponse}
 */
@POST
@Path("/inspect")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Inspect JSON", description = "Inspect a JSON schema or instance and return a Document object")
@RequestBody(description = "JsonInspectionRequest object", content = @Content(schema = @Schema(implementation = JsonInspectionRequest.class)))
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = JsonInspectionResponse.class)), description = "Return a Document object represented by JsonDocument"))
public Response inspect(InputStream requestIn) {
    JsonInspectionRequest request = fromJson(requestIn, JsonInspectionRequest.class);
    long startTime = System.currentTimeMillis();
    JsonInspectionResponse response = new JsonInspectionResponse();
    JsonDocument d = null;
    try {
        if (request.getType() == null || request.getJsonData() == null) {
            response.setErrorMessage("Json data and Instance or Schema inspection type must be specified in request");
        } else {
            JsonInspectionService s = new JsonInspectionService();
            String jsonData = cleanJsonData(request.getJsonData());
            if (!validJsonData(jsonData)) {
                response.setErrorMessage("Invalid json payload specified");
            } else {
                switch(request.getType()) {
                    case INSTANCE:
                        d = s.inspectJsonDocument(jsonData);
                        break;
                    case SCHEMA:
                        d = s.inspectJsonSchema(jsonData);
                        break;
                    default:
                        response.setErrorMessage("Unsupported inspection type: " + request.getType());
                        break;
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Error inspecting json: " + e.getMessage(), e);
        response.setErrorMessage(e.getMessage());
    } finally {
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    AtlasUtil.excludeNotRequestedFields(d, request.getInspectPaths());
    response.setJsonDocument(d);
    return Response.ok().entity(toJson(response)).build();
}
Also used : JsonInspectionService(io.atlasmap.json.inspect.JsonInspectionService) JsonInspectionResponse(io.atlasmap.json.v2.JsonInspectionResponse) JsonInspectionRequest(io.atlasmap.json.v2.JsonInspectionRequest) JsonDocument(io.atlasmap.json.v2.JsonDocument) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Aggregations

JsonInspectionService (io.atlasmap.json.inspect.JsonInspectionService)3 JsonDocument (io.atlasmap.json.v2.JsonDocument)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonInspectionRequest (io.atlasmap.json.v2.JsonInspectionRequest)1 JsonInspectionResponse (io.atlasmap.json.v2.JsonInspectionResponse)1 Operation (io.swagger.v3.oas.annotations.Operation)1 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)1 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1