forked from graphql-java/graphql-java-extended-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceBundleMessageInterpolator.java
206 lines (177 loc) · 9.46 KB
/
ResourceBundleMessageInterpolator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package graphql.validation.interpolation;
import graphql.ErrorClassification;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.execution.ResultPath;
import graphql.schema.GraphQLAppliedDirective;
import graphql.validation.el.StandardELVariables;
import graphql.validation.rules.ValidationEnvironment;
import jakarta.validation.Constraint;
import jakarta.validation.Path;
import jakarta.validation.Payload;
import org.hibernate.validator.internal.engine.MessageInterpolatorContext;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel;
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This message interpolator will try to convert message templates into I18N messages and then run message property replacement
* and expression interpolation.
* <p>
*
* By default this looks for a resource bundle file called "ValidationMessages.properties" on the class path but you can can
* override {@link #getResourceBundle(java.util.Locale)} to provide your own resource bundle
* <p>
* If it finds no resources then it will use the message template as is and do parameter and expression replacement
* on it
* <p>
* This class is heavily inspired by the Hibernate Validator projects ResourceBundleMessageInterpolator implementation and in fact
* uses that in its implementation and hence the standard facilities such as well known "parameters" like "validatedValue" and "format"
* are available.
* <p>
* See the <a href="https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-interpolation-with-message-expressions">Hibernate Validation documentation </a> for more details
*/
public class ResourceBundleMessageInterpolator implements MessageInterpolator {
private ResourceBundleLocator userResourceBundleLocator = new PlatformResourceBundleLocator("ValidationMessages");
private ResourceBundleLocator systemResourceBundleLocator = new PlatformResourceBundleLocator("graphql.validation.ValidationMessages");
private Locale defaultLocale = Locale.getDefault();
/**
* Override this method to build your own ErrorClassification
*
* @param messageTemplate the message template
* @param messageParams the parameters
* @param validationEnvironment the rule environment
*
* @return an ErrorClassification
*/
@SuppressWarnings("unused")
protected ErrorClassification buildErrorClassification(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
ResultPath fieldOrArgumentPath = validationEnvironment.getValidatedPath();
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
return new ValidationErrorType(fieldOrArgumentPath, directive);
}
/**
* You can override this to provide your own resource bundles for a given locale
*
* @param locale the locale in question
*
* @return a resource bundle OR null if you don't have one
*/
@SuppressWarnings("unused")
protected ResourceBundle getResourceBundle(Locale locale) {
return null;
}
@Override
public GraphQLError interpolate(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
ErrorClassification errorClassification = buildErrorClassification(messageTemplate, messageParams, validationEnvironment);
String message = interpolateMessageImpl(messageTemplate, messageParams, validationEnvironment);
GraphqlErrorBuilder errorBuilder = GraphqlErrorBuilder.newError()
.message(message)
.errorType(errorClassification)
.path(validationEnvironment.getExecutionPath());
if (validationEnvironment.getLocation() != null) {
errorBuilder.location(validationEnvironment.getLocation());
}
return errorBuilder.build();
}
private String interpolateMessageImpl(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
Locale locale = validationEnvironment.getLocale() == null ? defaultLocale : validationEnvironment.getLocale();
messageTemplate = loadMessageResource(messageTemplate, locale);
MessageInterpolatorContext context = buildHibernateContext(messageParams, validationEnvironment);
if (locale == null) {
// let hibernate code do the local defaulting
return hibernateInterpolator().interpolate(messageTemplate, context);
} else {
return hibernateInterpolator().interpolate(messageTemplate, context, locale);
}
}
private String loadMessageResource(String messageTemplate, Locale locale) {
ResourceBundle resourceBundle = getResourceBundle(locale);
Optional<String> bundleMessage = loadMessageFromBundle(messageTemplate, resourceBundle);
if (!bundleMessage.isPresent()) {
bundleMessage = loadMessageFromBundle(messageTemplate, userResourceBundleLocator.getResourceBundle(locale));
if (!bundleMessage.isPresent()) {
bundleMessage = loadMessageFromBundle(messageTemplate, systemResourceBundleLocator.getResourceBundle(locale));
}
}
return bundleMessage.orElse(messageTemplate);
}
private Optional<String> loadMessageFromBundle(String messageTemplate, ResourceBundle resourceBundle) {
if (resourceBundle == null) {
return Optional.empty();
}
try {
//noinspection ConstantConditions
return Optional.ofNullable(resourceBundle.getString(messageTemplate));
} catch (MissingResourceException ignored) {
return Optional.empty();
}
}
@SuppressWarnings("ConstantConditions")
private MessageInterpolatorContext buildHibernateContext(Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();
ConstraintAnnotationDescriptor<BridgeAnnotation> annotationDescriptor
= new ConstraintAnnotationDescriptor.Builder<>(BridgeAnnotation.class).build();
ConstraintDescriptorImpl<BridgeAnnotation> constraintDescriptor
= new ConstraintDescriptorImpl<>(
ConstraintHelper.forAllBuiltinConstraints(), null,
annotationDescriptor, ConstraintLocationKind.FIELD, ConstraintType.GENERIC
);
Map<String, Object> expressionVariables = StandardELVariables.standardELVars(validationEnvironment);
Class<?> rootBeanType = null;
Path propertyPath = null;
return new MessageInterpolatorContext(
constraintDescriptor, validatedValue, rootBeanType,
propertyPath, messageParams, expressionVariables, ExpressionLanguageFeatureLevel.BEAN_PROPERTIES, true);
}
private org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator hibernateInterpolator() {
return new org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator();
}
/// we just need an annotation to compile - we never use its
@SuppressWarnings("unused")
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
private @interface BridgeAnnotation {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
private static class ValidationErrorType implements ErrorClassification {
private final ResultPath fieldOrArgumentPath;
private final GraphQLAppliedDirective directive;
ValidationErrorType(ResultPath fieldOrArgumentPath, GraphQLAppliedDirective directive) {
this.fieldOrArgumentPath = fieldOrArgumentPath;
this.directive = directive;
}
@Override
public Object toSpecification(GraphQLError error) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", "ExtendedValidationError");
map.put("validatedPath", fieldOrArgumentPath.toList());
if (directive != null) {
map.put("constraint", "@" + directive.getName());
}
return map;
}
}
}