|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.quarkus.component.openapi.validator.deployment; |
| 18 | + |
| 19 | +import io.quarkus.deployment.annotations.BuildProducer; |
| 20 | +import io.quarkus.deployment.annotations.BuildStep; |
| 21 | +import io.quarkus.deployment.builditem.CombinedIndexBuildItem; |
| 22 | +import io.quarkus.deployment.builditem.FeatureBuildItem; |
| 23 | +import io.quarkus.deployment.builditem.IndexDependencyBuildItem; |
| 24 | +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; |
| 25 | +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceDirectoryBuildItem; |
| 26 | +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; |
| 27 | +import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem; |
| 28 | +import org.jboss.jandex.ClassInfo; |
| 29 | +import org.jboss.jandex.DotName; |
| 30 | + |
| 31 | +class OpenapiValidatorProcessor { |
| 32 | + |
| 33 | + private static final String FEATURE = "camel-openapi-validator"; |
| 34 | + private static final DotName ABSTRACT_KEYWORD_VALIDATOR = DotName |
| 35 | + .createSimple("com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator"); |
| 36 | + |
| 37 | + @BuildStep |
| 38 | + FeatureBuildItem feature() { |
| 39 | + return new FeatureBuildItem(FEATURE); |
| 40 | + } |
| 41 | + |
| 42 | + @BuildStep |
| 43 | + void indexDependencies(BuildProducer<IndexDependencyBuildItem> indexDependency) { |
| 44 | + indexDependency.produce(new IndexDependencyBuildItem("com.atlassian.oai", "swagger-request-validator-core")); |
| 45 | + indexDependency.produce(new IndexDependencyBuildItem("com.github.java-json-tools", "json-schema-validator")); |
| 46 | + indexDependency.produce(new IndexDependencyBuildItem("com.github.java-json-tools", "json-schema-core")); |
| 47 | + } |
| 48 | + |
| 49 | + @BuildStep |
| 50 | + void registerReflectiveClasses( |
| 51 | + CombinedIndexBuildItem combinedIndex, |
| 52 | + BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) { |
| 53 | + |
| 54 | + // Keyword validators are instantiated via ReflectionKeywordValidatorFactory using Constructor.newInstance() |
| 55 | + String[] keywordValidators = combinedIndex.getIndex() |
| 56 | + .getAllKnownSubclasses(ABSTRACT_KEYWORD_VALIDATOR) |
| 57 | + .stream() |
| 58 | + .map(ClassInfo::name) |
| 59 | + .map(DotName::toString) |
| 60 | + .toArray(String[]::new); |
| 61 | + reflectiveClasses.produce(ReflectiveClassBuildItem.builder(keywordValidators).constructors(true).build()); |
| 62 | + |
| 63 | + // Message bundles are instantiated via MessageBundles.getBundle() using Constructor.newInstance() |
| 64 | + reflectiveClasses.produce(ReflectiveClassBuildItem.builder( |
| 65 | + "com.atlassian.oai.validator.schema.SwaggerV20Library$ValidationBundle", |
| 66 | + "com.atlassian.oai.validator.schema.SwaggerV20Library$SyntaxBundle", |
| 67 | + "com.github.fge.jsonschema.core.messages.JsonSchemaCoreMessageBundle", |
| 68 | + "com.github.fge.jsonschema.core.messages.JsonSchemaSyntaxMessageBundle", |
| 69 | + "com.github.fge.jsonschema.messages.JsonSchemaConfigurationBundle", |
| 70 | + "com.github.fge.jsonschema.messages.JsonSchemaValidationBundle").constructors(true).build()); |
| 71 | + } |
| 72 | + |
| 73 | + @BuildStep |
| 74 | + void runtimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> runtimeInitializedClasses) { |
| 75 | + runtimeInitializedClasses |
| 76 | + .produce(new RuntimeInitializedClassBuildItem("com.github.fge.jackson.JsonNodeReader")); |
| 77 | + runtimeInitializedClasses |
| 78 | + .produce(new RuntimeInitializedClassBuildItem("com.github.fge.jackson.JsonLoader")); |
| 79 | + runtimeInitializedClasses |
| 80 | + .produce(new RuntimeInitializedClassBuildItem("com.github.fge.jsonschema.core.util.RegexECMA262Helper")); |
| 81 | + } |
| 82 | + |
| 83 | + @BuildStep |
| 84 | + void registerNativeImageResources( |
| 85 | + BuildProducer<NativeImageResourceBuildItem> nativeImageResources, |
| 86 | + BuildProducer<NativeImageResourceDirectoryBuildItem> nativeImageResourceDirectories) { |
| 87 | + nativeImageResources.produce(new NativeImageResourceBuildItem( |
| 88 | + "swagger/validation/default-levels.properties", |
| 89 | + "swagger/validation/schema-validation.properties", |
| 90 | + "swagger/validation/messages.properties", |
| 91 | + "com/github/fge/jsonschema/validator/validation.properties", |
| 92 | + "com/github/fge/jsonschema/validator/configuration.properties", |
| 93 | + "com/github/fge/jsonschema/core/core.properties", |
| 94 | + "com/github/fge/jsonschema/core/syntax.properties", |
| 95 | + "com/github/fge/jackson/jsonNodeReader.properties", |
| 96 | + "com/github/fge/jackson/jsonpointer.properties", |
| 97 | + "com/github/fge/uritemplate/messages.properties")); |
| 98 | + // Register entire draft schema directories to capture all schema files (schema, hyper-schema, links, etc.) |
| 99 | + nativeImageResourceDirectories.produce(new NativeImageResourceDirectoryBuildItem("draftv3")); |
| 100 | + nativeImageResourceDirectories.produce(new NativeImageResourceDirectoryBuildItem("draftv4")); |
| 101 | + } |
| 102 | +} |
0 commit comments