|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.graphql; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 28 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
| 29 | +import org.springframework.boot.context.properties.bind.Binder; |
| 30 | +import org.springframework.context.annotation.Condition; |
| 31 | +import org.springframework.context.annotation.ConditionContext; |
| 32 | +import org.springframework.context.annotation.ConfigurationCondition; |
| 33 | +import org.springframework.core.io.Resource; |
| 34 | +import org.springframework.core.io.support.ResourcePatternResolver; |
| 35 | +import org.springframework.core.io.support.ResourcePatternUtils; |
| 36 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 37 | + |
| 38 | +/** |
| 39 | + * {@link Condition} that checks whether a GraphQL schema has been defined in the |
| 40 | + * application. This is looking for: |
| 41 | + * <ul> |
| 42 | + * <li>schema files in the {@link GraphQlProperties configured locations}</li> |
| 43 | + * <li>or infrastructure beans such as {@link GraphQlSourceBuilderCustomizer}</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * @author Brian Clozel |
| 47 | + * @see ConditionalOnGraphQlSchema |
| 48 | + */ |
| 49 | +class DefaultGraphQlSchemaCondition extends SpringBootCondition implements ConfigurationCondition { |
| 50 | + |
| 51 | + @Override |
| 52 | + public ConfigurationCondition.ConfigurationPhase getConfigurationPhase() { |
| 53 | + return ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 58 | + boolean match = false; |
| 59 | + List<ConditionMessage> messages = new ArrayList<>(2); |
| 60 | + ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnGraphQlSchema.class); |
| 61 | + |
| 62 | + Binder binder = Binder.get(context.getEnvironment()); |
| 63 | + GraphQlProperties.Schema schema = binder.bind("spring.graphql.schema", GraphQlProperties.Schema.class) |
| 64 | + .orElse(new GraphQlProperties.Schema()); |
| 65 | + ResourcePatternResolver resourcePatternResolver = ResourcePatternUtils |
| 66 | + .getResourcePatternResolver(context.getResourceLoader()); |
| 67 | + List<Resource> schemaResources = resolveSchemaResources(resourcePatternResolver, schema.getLocations(), |
| 68 | + schema.getFileExtensions()); |
| 69 | + if (!schemaResources.isEmpty()) { |
| 70 | + match = true; |
| 71 | + messages.add(message.found("schema", "schemas").items(ConditionMessage.Style.QUOTE, schemaResources)); |
| 72 | + } |
| 73 | + else { |
| 74 | + messages.add(message.didNotFind("schema files in locations").items(ConditionMessage.Style.QUOTE, |
| 75 | + Arrays.asList(schema.getLocations()))); |
| 76 | + } |
| 77 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 78 | + String[] customizerBeans = beanFactory.getBeanNamesForType(GraphQlSourceBuilderCustomizer.class, false, false); |
| 79 | + if (customizerBeans.length != 0) { |
| 80 | + match = true; |
| 81 | + messages.add(message.found("customizer", "customizers").items(Arrays.asList(customizerBeans))); |
| 82 | + } |
| 83 | + else { |
| 84 | + messages.add((message.didNotFind("GraphQlSourceBuilderCustomizer").atAll())); |
| 85 | + } |
| 86 | + return new ConditionOutcome(match, ConditionMessage.of(messages)); |
| 87 | + } |
| 88 | + |
| 89 | + private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, String[] locations, |
| 90 | + String[] extensions) { |
| 91 | + List<Resource> resources = new ArrayList<>(); |
| 92 | + for (String location : locations) { |
| 93 | + for (String extension : extensions) { |
| 94 | + resources.addAll(resolveSchemaResources(resolver, location + "*" + extension)); |
| 95 | + } |
| 96 | + } |
| 97 | + return resources; |
| 98 | + } |
| 99 | + |
| 100 | + private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver, String pattern) { |
| 101 | + try { |
| 102 | + return Arrays.asList(resolver.getResources(pattern)); |
| 103 | + } |
| 104 | + catch (IOException ex) { |
| 105 | + return Collections.emptyList(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments