|
14 | 14 | package com.dynatrace.metric.util; |
15 | 15 |
|
16 | 16 | import com.dynatrace.metric.util.MetricLineConstants.ValidationMessages; |
| 17 | +import java.util.function.Supplier; |
17 | 18 |
|
18 | 19 | /** Offers normalization methods for metric key, dimension key and dimension value */ |
19 | 20 | final class Normalizer { |
@@ -257,6 +258,139 @@ static NormalizationResult normalizeDimensionValue(String value, int maxDimensio |
257 | 258 | return normalizeUnquotedStringDimValue(value, maxDimensionValueLength); |
258 | 259 | } |
259 | 260 |
|
| 261 | + static NormalizationResult normalizeMetadataString(String value, int maxLength) { |
| 262 | + if (StringValueValidator.isNullOrEmpty(value)) { |
| 263 | + return NormalizationResult.newValid(CodePoints.EMPTY_STRING); |
| 264 | + } |
| 265 | + |
| 266 | + // in a quoted string, quotes do not count towards the string character limit |
| 267 | + boolean isQuoted = |
| 268 | + value.startsWith(CodePoints.QUOTATION_MARK) && value.endsWith(CodePoints.QUOTATION_MARK); |
| 269 | + |
| 270 | + int numValidBytes = 0; |
| 271 | + boolean needsNormalization = false; |
| 272 | + boolean needsEscaping = false; |
| 273 | + |
| 274 | + // if quoted, start at index 1 (the one after the quote) |
| 275 | + int start = isQuoted ? 1 : 0; |
| 276 | + // if quoted, stop at length - 1 (the last byte before the quote) |
| 277 | + int end = isQuoted ? value.length() - 1 : value.length(); |
| 278 | + |
| 279 | + for (int i = start; i < end; ) { |
| 280 | + final int codePoint = value.codePointAt(i); |
| 281 | + final int codePointLength = Character.charCount(codePoint); |
| 282 | + |
| 283 | + // code point is valid but needs escaping |
| 284 | + if (codePointNeedsEscaping(codePoint)) { |
| 285 | + needsEscaping = true; |
| 286 | + } |
| 287 | + |
| 288 | + // code point is not valid, and needs normalizing (e.g. unicode control char) |
| 289 | + if (codePointNeedsNormalizing(codePoint)) { |
| 290 | + // if there are control characters, note that normalization is necessary but don't break yet |
| 291 | + // this way we know how many characters there will be in the output |
| 292 | + needsNormalization = true; |
| 293 | + } else { |
| 294 | + // if the char is valid, count up |
| 295 | + numValidBytes += codePointLength; |
| 296 | + // if the maxLength is reached, break. |
| 297 | + if (numValidBytes > maxLength) { |
| 298 | + needsNormalization = true; |
| 299 | + break; |
| 300 | + } |
| 301 | + } |
| 302 | + i += codePointLength; |
| 303 | + } |
| 304 | + |
| 305 | + // return early if no normalization is needed. |
| 306 | + if (!needsNormalization) { |
| 307 | + // if the string is already quoted or doesn't need escaping, return as is |
| 308 | + if (isQuoted || !needsEscaping) { |
| 309 | + return NormalizationResult.newValid(value); |
| 310 | + } else { |
| 311 | + // not quoted, needs escaping, but no normalization (e.g., it contains a '=' character). |
| 312 | + // turn the string into a quoted string, where the '=' sign does not need escaping. |
| 313 | + return NormalizationResult.newValid("\"" + value + "\""); |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + // do normalization |
| 318 | + StringBuilder builder = new StringBuilder(numValidBytes + 2); |
| 319 | + builder.append(CodePoints.QUOTATION_MARK); |
| 320 | + Supplier<String> warningMessageSupplier = null; |
| 321 | + |
| 322 | + // don't need to escape anything since we're working in a quoted string. |
| 323 | + for (int i = start; i < end; ) { |
| 324 | + final int codePoint = value.codePointAt(i); |
| 325 | + final int codePointLength = Character.charCount(codePoint); |
| 326 | + |
| 327 | + // special handling: only newlines are considered, since the resulting string will be quoted |
| 328 | + // either way. |
| 329 | + if (codePoint == CodePoints.NEWLINE) { |
| 330 | + // check if the escaped character fits, exit if it does not. |
| 331 | + if (i + CodePoints.ESCAPED_NEWLINE.length() > maxLength) { |
| 332 | + break; |
| 333 | + } |
| 334 | + builder.append(CodePoints.ESCAPED_NEWLINE); |
| 335 | + } else if (codePoint == CodePoints.QUOTE) { |
| 336 | + // check if the escaped character fits, exit if it does not. |
| 337 | + if (i + CodePoints.ESCAPED_QUOTES.length() > maxLength) { |
| 338 | + break; |
| 339 | + } |
| 340 | + builder.append(CodePoints.ESCAPED_QUOTES); |
| 341 | + } else if (!codePointNeedsNormalizing(codePoint)) { |
| 342 | + // code point is valid, check if it fits, then add. |
| 343 | + if (i + codePointLength > maxLength) { |
| 344 | + break; |
| 345 | + } |
| 346 | + builder.appendCodePoint(codePoint); |
| 347 | + } |
| 348 | + |
| 349 | + i += codePointLength; |
| 350 | + } |
| 351 | + builder.append(CodePoints.QUOTATION_MARK); |
| 352 | + |
| 353 | + String normalized = builder.toString(); |
| 354 | + if (normalized.length() == 2) { |
| 355 | + return NormalizationResult.newInvalid( |
| 356 | + () -> "no valid characters after normalization (input: " + value + ")"); |
| 357 | + } else |
| 358 | + return NormalizationResult.newWarning( |
| 359 | + normalized, |
| 360 | + () -> |
| 361 | + String.format( |
| 362 | + ValidationMessages.METADATA_VALUE_NORMALIZED_MESSAGE, value, normalized)); |
| 363 | + } |
| 364 | + |
| 365 | + private static boolean codePointNeedsEscaping(int codePoint) { |
| 366 | + return codePoint == CodePoints.NEWLINE |
| 367 | + || codePoint == CodePoints.BLANK |
| 368 | + || codePoint == CodePoints.COMMA |
| 369 | + || codePoint == CodePoints.EQUALS |
| 370 | + || codePoint == CodePoints.QUOTE |
| 371 | + || codePoint == CodePoints.BACKSLASH; |
| 372 | + } |
| 373 | + |
| 374 | + private static boolean codePointNeedsNormalizing(int codePoint) { |
| 375 | + int type = Character.getType(codePoint); |
| 376 | + |
| 377 | + // unassigned characters outside the range of Unicode 10.0 "Supplemental Symbols and |
| 378 | + // Pictographs" are not allowed. |
| 379 | + if (type == Character.UNASSIGNED) { |
| 380 | + // Unicode version 10 allowed Supplemental Symbols and pictographs |
| 381 | + // https://www.unicode.org/charts/PDF/Unicode-10.0/U100-1F900.pdf |
| 382 | + return codePoint <= CodePoints.UC_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS_START |
| 383 | + || codePoint >= CodePoints.UC_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS_END; |
| 384 | + } |
| 385 | + |
| 386 | + return type == Character.CONTROL |
| 387 | + || type == Character.PRIVATE_USE |
| 388 | + || type == Character.SURROGATE |
| 389 | + || type == Character.LINE_SEPARATOR |
| 390 | + || type == Character.PARAGRAPH_SEPARATOR |
| 391 | + || codePoint == CodePoints.QUOTE; |
| 392 | + } |
| 393 | + |
260 | 394 | /** |
261 | 395 | * Applies normalization to an unquoted string dimension value. |
262 | 396 | * |
|
0 commit comments