Search in sources :

Example 1 with SchemaFactoryWrapper

use of com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper in project graylog2-server by Graylog2.

the class RetentionStrategyResource method getRetentionStrategyDescription.

private RetentionStrategyDescription getRetentionStrategyDescription(@ApiParam(name = "strategy", value = "The name of the retention strategy", required = true) @PathParam("strategy") @NotEmpty String strategyName) {
    final Provider<RetentionStrategy> provider = retentionStrategies.get(strategyName);
    if (provider == null) {
        throw new NotFoundException("Couldn't find retention strategy for given type " + strategyName);
    }
    final RetentionStrategy retentionStrategy = provider.get();
    final RetentionStrategyConfig defaultConfig = retentionStrategy.defaultConfiguration();
    final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    try {
        objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(retentionStrategy.configurationClass()), visitor);
    } catch (JsonMappingException e) {
        throw new InternalServerErrorException("Couldn't generate JSON schema for retention strategy " + strategyName, e);
    }
    return RetentionStrategyDescription.create(strategyName, defaultConfig, visitor.finalSchema());
}
Also used : RetentionStrategyConfig(org.graylog2.plugin.indexer.retention.RetentionStrategyConfig) RetentionStrategy(org.graylog2.plugin.indexer.retention.RetentionStrategy) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) NotFoundException(javax.ws.rs.NotFoundException) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Example 2 with SchemaFactoryWrapper

use of com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper in project graylog2-server by Graylog2.

the class RotationStrategyResource method getRotationStrategyDescription.

private RotationStrategyDescription getRotationStrategyDescription(String strategyName) {
    final Provider<RotationStrategy> provider = rotationStrategies.get(strategyName);
    if (provider == null) {
        throw new NotFoundException("Couldn't find rotation strategy for given type " + strategyName);
    }
    final RotationStrategy rotationStrategy = provider.get();
    final RotationStrategyConfig defaultConfig = rotationStrategy.defaultConfiguration();
    final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    try {
        objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(rotationStrategy.configurationClass()), visitor);
    } catch (JsonMappingException e) {
        throw new InternalServerErrorException("Couldn't generate JSON schema for rotation strategy " + strategyName, e);
    }
    return RotationStrategyDescription.create(strategyName, defaultConfig, visitor.finalSchema());
}
Also used : RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RotationStrategyConfig(org.graylog2.plugin.indexer.rotation.RotationStrategyConfig) NotFoundException(javax.ws.rs.NotFoundException) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Example 3 with SchemaFactoryWrapper

use of com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper in project graylog2-server by Graylog2.

the class Generator method schemaForType.

private Map<String, Object> schemaForType(Type valueType) {
    final SchemaFactoryWrapper schemaFactoryWrapper = new SchemaFactoryWrapper() {

        @Override
        public JsonAnyFormatVisitor expectAnyFormat(JavaType convertedType) {
            /*final ObjectSchema s = schemaProvider.objectSchema();
                s.putProperty("anyType", schemaProvider.stringSchema());
                this.schema = s;
                return visitorFactory.anyFormatVisitor(new AnySchema());*/
            return super.expectAnyFormat(convertedType);
        }
    };
    final JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(mapper, schemaFactoryWrapper);
    try {
        final JsonSchema schema = schemaGenerator.generateSchema(mapper.getTypeFactory().constructType(valueType));
        final Map<String, Object> schemaMap = mapper.readValue(mapper.writeValueAsBytes(schema), Map.class);
        if (schemaMap.containsKey("additional_properties") && !schemaMap.containsKey("properties")) {
            schemaMap.put("properties", Collections.emptyMap());
        }
        if (schemaMap.equals(Collections.singletonMap("type", "any"))) {
            return ImmutableMap.of("type", "object", "properties", Collections.emptyMap());
        }
        return schemaMap;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) IOException(java.io.IOException) JsonSchemaGenerator(com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator)

Example 4 with SchemaFactoryWrapper

use of com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper in project tutorials by eugenp.

the class ExtraAnnotationUnitTest method whenUsingJsonPropertyDescriptionAnnotation_thenCorrect.

@Test
public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    SchemaFactoryWrapper wrapper = new SchemaFactoryWrapper();
    mapper.acceptJsonFormatVisitor(PropertyDescriptionBean.class, wrapper);
    JsonSchema jsonSchema = wrapper.finalSchema();
    String jsonString = mapper.writeValueAsString(jsonSchema);
    System.out.println(jsonString);
    assertThat(jsonString, containsString("This is a description of the name property"));
}
Also used : JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with SchemaFactoryWrapper

use of com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper in project graylog2-server by Graylog2.

the class ClusterConfigResource method schema.

@GET
@Path("{configClass}")
@Produces(MoreMediaTypes.APPLICATION_SCHEMA_JSON)
@ApiOperation(value = "Get JSON schema of configuration class")
@Timed
@RequiresPermissions(RestPermissions.CLUSTER_CONFIG_ENTRY_READ)
public JsonSchema schema(@ApiParam(name = "configClass", value = "The name of the cluster configuration class", required = true) @PathParam("configClass") @NotBlank String configClass) {
    final Class<?> cls = classFromName(configClass);
    if (cls == null) {
        throw new NotFoundException(createNoClassMsg(configClass));
    }
    final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    try {
        objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(cls), visitor);
    } catch (JsonMappingException e) {
        throw new InternalServerErrorException("Couldn't generate JSON schema for configuration class " + configClass, e);
    }
    return visitor.finalSchema();
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) NotFoundException(javax.ws.rs.NotFoundException) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SchemaFactoryWrapper (com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 NotFoundException (javax.ws.rs.NotFoundException)3 JsonSchema (com.fasterxml.jackson.module.jsonSchema.JsonSchema)2 Timed (com.codahale.metrics.annotation.Timed)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonSchemaGenerator (com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 RetentionStrategy (org.graylog2.plugin.indexer.retention.RetentionStrategy)1 RetentionStrategyConfig (org.graylog2.plugin.indexer.retention.RetentionStrategyConfig)1 RotationStrategy (org.graylog2.plugin.indexer.rotation.RotationStrategy)1 RotationStrategyConfig (org.graylog2.plugin.indexer.rotation.RotationStrategyConfig)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1