Permalink
Browse files

AS7-4739 Fix Locale parsing

  • Loading branch information...
1 parent fdc81e0 commit 60cba145a7385efce28c71f20b318b5aeac3a0ca @bstansberry bstansberry committed May 8, 2012
@@ -2476,4 +2476,6 @@
@Message(id = 14859, value = "Failed to rename temp file %s to %s")
ConfigurationPersistenceException failedToRenameTempFile(@Cause Throwable cause, File temp, File file);
+ @Message(id = 14860, value = "Invalid locale format: %s")
+ OperationFailedException invalidLocaleString(String unparsed);
}
@@ -469,7 +469,7 @@ public void doExecute(OperationContext context, ModelNode operation) throws Oper
}
}
- private ModelNode getNodeDescription(ImmutableManagementResourceRegistration registry, ModelNode operation) {
+ private ModelNode getNodeDescription(ImmutableManagementResourceRegistration registry, ModelNode operation) throws OperationFailedException {
final DescriptionProvider descriptionProvider = registry.getModelDescription(PathAddress.EMPTY_ADDRESS);
final Locale locale = getLocale(operation);
return descriptionProvider.getModelDescription(locale);
@@ -1186,11 +1186,46 @@ private static ModelNode safeReadModel(final OperationContext context) {
return result;
}
- private static Locale getLocale(final ModelNode operation) {
+ private static Locale getLocale(final ModelNode operation) throws OperationFailedException {
if (!operation.hasDefined(LOCALE)) {
return null;
}
- return new Locale(operation.get(LOCALE).asString());
+ String unparsed = operation.get(LOCALE).asString();
+ int len = unparsed.length();
+ if (len != 2 && len != 5 && len < 7) {
+ throw MESSAGES.invalidLocaleString(unparsed);
+ }
+
+ char char0 = unparsed.charAt(0);
+ char char1 = unparsed.charAt(1);
+ if (char0 < 'a' || char0 > 'z' || char1 < 'a' || char1 > 'z') {
+ throw MESSAGES.invalidLocaleString(unparsed);
+ }
+ if (len == 2) {
+ return new Locale(unparsed, "");
+ }
+
+ if (unparsed.charAt(2) != '_') {
+ throw MESSAGES.invalidLocaleString(unparsed);
+ }
+ char char3 = unparsed.charAt(3);
+ if (char3 == '_') {
+ // no country
+ return new Locale(unparsed.substring(0, 2), "", unparsed.substring(4));
+ }
+
+ char char4 = unparsed.charAt(4);
+ if (char3 < 'A' || char3 > 'Z' || char4 < 'A' || char4 > 'Z') {
+ throw MESSAGES.invalidLocaleString(unparsed);
+ }
+ if (len == 5) {
+ return new Locale(unparsed.substring(0, 2), unparsed.substring(3));
+ }
+
+ if (unparsed.charAt(5) != '_') {
+ throw MESSAGES.invalidLocaleString(unparsed);
+ }
+ return new Locale(unparsed.substring(0, 2), unparsed.substring(3, 5), unparsed.substring(6));
}

0 comments on commit 60cba14

Please sign in to comment.