-
Notifications
You must be signed in to change notification settings - Fork 138
/
MessageInterpolatorImpl.java
350 lines (312 loc) · 13.5 KB
/
MessageInterpolatorImpl.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.jvnet.hk2.config;
import jakarta.validation.MessageInterpolator;
import jakarta.validation.Payload;
import jakarta.validation.metadata.ConstraintDescriptor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Custom MessageInterpolatorImpl for HK2
*
* This message interpolator is different from the default one in the following
* ways:
*
* 1. It uses a class specified in the "payload" argument to the annotation to
* find the class loader to find the resource bundle for messages. This allows
* classes that are in OSGi modules to specify a resource bundle for bean
* validation messages.
*
* 2. The "LocalStrings" resource bundle within the same package as the payload
* class is used to search for messages. If a message is not found, the message
* is obtained from the default bean validation resource bundle.
*
* This class borrows heavily from the RI implementation of the ResourcBundleMessageInterpolator
* authored by Emmanuel Bernard, Hardy Ferentschik, and Gunnar Morling.
*/
public class MessageInterpolatorImpl implements MessageInterpolator {
/**
* The name of the default message bundle.
*/
public static final String DEFAULT_VALIDATION_MESSAGES = "org.hibernate.validator.ValidationMessages";
/**
* The name of the user-provided message bundle as defined in the specification.
*/
public static final String USER_VALIDATION_MESSAGES = "ValidationMessages";
/**
* Regular expression used to do message interpolation.
*/
private static final Pattern MESSAGE_PARAMETER_PATTERN = Pattern.compile("(\\{[^\\}]+?\\})");
/**
* The default locale for the current user.
*/
private final Locale defaultLocale = Locale.getDefault();
/**
* Step 1-3 of message interpolation can be cached. We do this in this map.
*/
private final Map<LocalisedMessage, String> resolvedMessages = new WeakHashMap<>();
/**
* Flag indicating whether this interpolator should chance some of the interpolation steps.
*/
private static final boolean cacheMessages = true;
public MessageInterpolatorImpl() { }
@Override
public String interpolate(String message, Context context) {
// probably no need for caching, but it could be done by parameters since the map
// is immutable and uniquely built per Validation definition, the comparison has to be based on == and not equals though
return interpolate(message, context, defaultLocale);
}
/**
* Runs the message interpolation according to algorithm specified in JSR 303.
* <br/>
* Note:
* <br/>
* Look-ups in user bundles is recursive whereas look-ups in default bundle are not!
*
* @param message the message to interpolate
* @param annotationParameters the parameters of the annotation for which to interpolate this message
* @param locale the {@code Locale} to use for the resource bundle.
*
* @return the interpolated message.
*/
@Override
public String interpolate(String message, Context context, Locale locale) {
Map<String, Object> annotationParameters = context.getConstraintDescriptor().getAttributes();
LocalisedMessage localisedMessage = new LocalisedMessage(message, locale);
String resolvedMessage = null;
if (cacheMessages) {
resolvedMessage = resolvedMessages.get(localisedMessage);
}
// if the message is not already in the cache we have to run step 1-3 of the message resolution
if (resolvedMessage == null) {
ResourceBundle userResourceBundle = new ContextResourceBundle(context, locale);
ClassLoader classLoader = MessageInterpolatorImpl.class.getClassLoader();
ResourceBundle defaultResourceBundle = null;
try {
defaultResourceBundle = ResourceBundle.getBundle(DEFAULT_VALIDATION_MESSAGES,
locale,
classLoader);
}
catch (MissingResourceException mre) {
// defaultResourceBundle is already null
}
String userBundleResolvedMessage;
resolvedMessage = message;
boolean evaluatedDefaultBundleOnce = false;
do {
// search the user bundle recursive (step1)
userBundleResolvedMessage = replaceVariables(
resolvedMessage, userResourceBundle, locale, true);
// exit condition - we have at least tried to validate against the default bundle and there was no
// further replacements
if (evaluatedDefaultBundleOnce
&& !hasReplacementTakenPlace(userBundleResolvedMessage, resolvedMessage)) {
break;
}
// search the default bundle non recursive (step2)
resolvedMessage = replaceVariables(userBundleResolvedMessage, defaultResourceBundle, locale, false);
evaluatedDefaultBundleOnce = true;
if (cacheMessages) {
resolvedMessages.put(localisedMessage, resolvedMessage);
}
} while (true);
}
// resolve annotation attributes (step 4)
resolvedMessage = replaceAnnotationAttributes(resolvedMessage, annotationParameters);
// last but not least we have to take care of escaped literals
resolvedMessage = resolvedMessage.replace("\\{", "{");
resolvedMessage = resolvedMessage.replace("\\}", "}");
resolvedMessage = resolvedMessage.replace("\\\\", "\\");
return resolvedMessage;
}
private boolean hasReplacementTakenPlace(String origMessage, String newMessage) {
return !origMessage.equals(newMessage);
}
private String replaceVariables(String message, ResourceBundle bundle, Locale locale, boolean recurse) {
Matcher matcher = MESSAGE_PARAMETER_PATTERN.matcher(message);
StringBuffer sb = new StringBuffer();
String resolvedParameterValue;
while (matcher.find()) {
String parameter = matcher.group(1);
resolvedParameterValue = resolveParameter(
parameter, bundle, locale, recurse);
matcher.appendReplacement(sb, escapeMetaCharacters(resolvedParameterValue));
}
matcher.appendTail(sb);
return sb.toString();
}
private String replaceAnnotationAttributes(String message, Map<String, Object> annotationParameters) {
Matcher matcher = MESSAGE_PARAMETER_PATTERN.matcher(message);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String resolvedParameterValue;
String parameter = matcher.group(1);
Object variable = annotationParameters.get(removeCurlyBrace(parameter));
if (variable != null) {
resolvedParameterValue = escapeMetaCharacters(variable.toString());
} else {
resolvedParameterValue = parameter;
}
matcher.appendReplacement(sb, resolvedParameterValue);
}
matcher.appendTail(sb);
return sb.toString();
}
private String resolveParameter(String parameterName, ResourceBundle bundle, Locale locale, boolean recurse) {
String parameterValue;
try {
if (bundle != null) {
parameterValue = bundle.getString(removeCurlyBrace(parameterName));
if (recurse) {
parameterValue = replaceVariables(parameterValue, bundle, locale, recurse);
}
} else {
parameterValue = parameterName;
}
} catch (MissingResourceException e) {
// return parameter itself
parameterValue = parameterName;
}
return parameterValue;
}
private String removeCurlyBrace(String parameter) {
return parameter.substring(1, parameter.length() - 1);
}
/**
* @param s The string in which to replace the meta characters '$' and '\'.
*
* @return A string where meta characters relevant for {@link Matcher#appendReplacement} are escaped.
*/
private String escapeMetaCharacters(String s) {
String escapedString = s.replace("\\", "\\\\");
escapedString = escapedString.replace("$", "\\$");
return escapedString;
}
/*
* A resource bundle that takes strings from the annotation context. This class
* looks for the "LocalStrings" resource bundle in the same package as the
* class that is specified in the payload of the annotation. If a String
* cannot be found there, then it looks in the user resource bundle as defined
* in JSR-303.
*/
private static class ContextResourceBundle extends ResourceBundle {
ResourceBundle contextBundle;
ResourceBundle userBundle;
ContextResourceBundle(Context context, Locale locale) {
ConstraintDescriptor descriptor = context.getConstraintDescriptor();
Set<Class<? extends Payload>> payload = descriptor.getPayload();
// Load the LogStrings.properties for the argument Locale, from the ClassLoader
// of the payload.
if (!payload.isEmpty()) {
final Class payloadClass = payload.iterator().next();
String baseName = payloadClass.getPackage().getName() + ".LocalStrings";
ClassLoader cl;
if (System.getSecurityManager()!=null) {
cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return payloadClass.getClassLoader();
}
});
} else {
cl = payloadClass.getClassLoader();
}
try {
contextBundle = ResourceBundle.getBundle(baseName, locale, cl);
} catch (MissingResourceException mre) {
contextBundle = null;
}
}
try {
ClassLoader cl = System.getSecurityManager() == null
? Thread.currentThread().getContextClassLoader()
: AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
});
userBundle = ResourceBundle.getBundle(USER_VALIDATION_MESSAGES, locale, cl);
} catch (MissingResourceException mre) {
userBundle = null;
}
if (userBundle != null) {
setParent(userBundle);
}
}
@Override
protected Object handleGetObject(String key) {
if (contextBundle != null) {
return contextBundle.getObject(key);
}
return null;
}
@Override
public Enumeration<String> getKeys() {
Set<String> keys = new TreeSet<>();
if (contextBundle != null) {
keys.addAll(Collections.list(contextBundle.getKeys()));
}
if (userBundle != null) {
keys.addAll(Collections.list(userBundle.getKeys()));
}
return Collections.enumeration(keys);
}
}
private static class LocalisedMessage {
private final String message;
private final Locale locale;
LocalisedMessage(String message, Locale locale) {
this.message = message;
this.locale = locale;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LocalisedMessage that = (LocalisedMessage) o;
if (locale != null ? !locale.equals(that.locale) : that.locale != null) {
return false;
}
if (message != null ? !message.equals(that.message) : that.message != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = message != null ? message.hashCode() : 0;
result = 31 * result + (locale != null ? locale.hashCode() : 0);
return result;
}
}
}