|
1 | 1 | package com.sap.hcp.cf.logging.common.converter;
|
2 | 2 |
|
3 |
| -import java.util.ArrayList; |
| 3 | +import java.io.IOException; |
4 | 4 | import java.util.Collections;
|
| 5 | +import java.util.HashMap; |
5 | 6 | import java.util.List;
|
6 | 7 | import java.util.Map;
|
7 | 8 |
|
8 | 9 | import org.slf4j.LoggerFactory;
|
9 | 10 |
|
| 11 | +import com.fasterxml.jackson.core.JsonProcessingException; |
10 | 12 | import com.fasterxml.jackson.jr.ob.JSON;
|
11 | 13 | import com.fasterxml.jackson.jr.ob.JSONComposer;
|
| 14 | +import com.fasterxml.jackson.jr.ob.JSONObjectException; |
| 15 | +import com.fasterxml.jackson.jr.ob.comp.ArrayComposer; |
12 | 16 | import com.fasterxml.jackson.jr.ob.comp.ObjectComposer;
|
13 | 17 | import com.sap.hcp.cf.logging.common.customfields.CustomField;
|
14 | 18 |
|
15 | 19 | public class DefaultCustomFieldsConverter {
|
16 | 20 |
|
17 |
| - private String fieldName = null; |
18 |
| - private boolean embed = true; |
| 21 | + private String fieldName = null; |
| 22 | + private boolean embed = true; |
| 23 | + private List<String> customFieldKeyNames; |
19 | 24 |
|
20 |
| - public void setFieldName(String fieldName) { |
21 |
| - if (fieldName != null) { |
22 |
| - this.fieldName = fieldName; |
23 |
| - embed = false; |
24 |
| - } |
25 |
| - } |
| 25 | + public void setFieldName(String fieldName) { |
| 26 | + if (fieldName != null) { |
| 27 | + this.fieldName = fieldName; |
| 28 | + embed = false; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public void setCustomFieldKeyNames(List<String> customFieldKeyNames) { |
| 33 | + this.customFieldKeyNames = customFieldKeyNames; |
| 34 | + } |
26 | 35 |
|
27 |
| - public void convert(StringBuilder appendTo, Map<String, String> mdcCustomFields, Object... arguments) { |
28 |
| - List<CustomField> customFields = getCustomFields(arguments); |
| 36 | + public void convert(StringBuilder appendTo, Map<String, String> mdcPropertiesMap, Object... arguments) { |
| 37 | + if (customFieldKeyNames.isEmpty()) { |
| 38 | + return; |
| 39 | + } |
| 40 | + Map<String, CustomField> customFields = getRegisteredCustomFields(arguments); |
| 41 | + Map<String, String> mdcCustomFields = getRegisteredMdcCustomFields(mdcPropertiesMap); |
29 | 42 | if (!customFields.isEmpty() || !mdcCustomFields.isEmpty()) {
|
30 | 43 | try {
|
31 |
| - if (!embed) { |
32 |
| - appendTo.append(JSON.std.asString(fieldName)).append(":"); |
33 |
| - } |
34 |
| - /* |
35 |
| - * -- no matter whether we embed or not, it seems easier to |
36 |
| - * compose -- a JSON object from the key/value pairs. -- if we |
37 |
| - * embed that object, we simply chop off the outermost curly |
38 |
| - * braces. |
39 |
| - */ |
40 |
| - ObjectComposer<JSONComposer<String>> oc = JSON.std.composeString().startObject(); |
41 |
| - for (CustomField cf : customFields) { |
42 |
| - oc.putObject(cf.getKey(), cf.getValue()); |
43 |
| - } |
44 |
| - for (Map.Entry<String, String> mdcField : mdcCustomFields.entrySet()) { |
45 |
| - oc.put(mdcField.getKey(), mdcField.getValue()); |
46 |
| - } |
47 |
| - String result = oc.end().finish().trim(); |
48 |
| - if (embed) { |
49 |
| - /* -- chop off curly braces -- */ |
50 |
| - appendTo.append(result.substring(1, result.length() - 1)); |
51 |
| - } else { |
52 |
| - appendTo.append(result); |
53 |
| - } |
| 44 | + ArrayComposer<ObjectComposer<JSONComposer<String>>> oc = startJson(appendTo); |
| 45 | + addCustomFields(oc, customFields, mdcCustomFields); |
| 46 | + finishJson(oc, appendTo); |
54 | 47 | } catch (Exception ex) {
|
55 | 48 | /* -- avoids substitute logger warnings on startup -- */
|
56 | 49 | LoggerFactory.getLogger(DefaultCustomFieldsConverter.class).error("Conversion failed ", ex);
|
57 | 50 | }
|
58 | 51 | }
|
59 | 52 | }
|
60 | 53 |
|
61 |
| - private List<CustomField> getCustomFields(Object[] arguments) { |
62 |
| - if (arguments == null || arguments.length == 0) { |
63 |
| - return Collections.emptyList(); |
| 54 | + private Map<String, String> getRegisteredMdcCustomFields(Map<String, String> mdcPropertiesMap) { |
| 55 | + if (mdcPropertiesMap.isEmpty()) { |
| 56 | + return Collections.emptyMap(); |
| 57 | + } |
| 58 | + Map<String, String> mdcCustomFields = new HashMap<>(mdcPropertiesMap.size()); |
| 59 | + for (Map.Entry<String, String> current : mdcPropertiesMap.entrySet()) { |
| 60 | + if (customFieldKeyNames.contains(current.getKey())) { |
| 61 | + mdcCustomFields.put(current.getKey(), current.getValue()); |
| 62 | + } |
| 63 | + } |
| 64 | + return mdcCustomFields; |
| 65 | + } |
| 66 | + |
| 67 | + private Map<String, CustomField> getRegisteredCustomFields(Object... arguments) { |
| 68 | + if (arguments == null) { |
| 69 | + return Collections.emptyMap(); |
64 | 70 | }
|
65 |
| - List<CustomField> customFields = new ArrayList<CustomField>(); |
66 |
| - for (Object argument : arguments) { |
67 |
| - if (argument instanceof CustomField) { |
68 |
| - customFields.add((CustomField) argument); |
| 71 | + Map<String, CustomField> result = new HashMap<>(); |
| 72 | + for (Object current : arguments) { |
| 73 | + if (current instanceof CustomField) { |
| 74 | + CustomField field = (CustomField) current; |
| 75 | + if (customFieldKeyNames.contains(field.getKey())) { |
| 76 | + result.put(field.getKey(), field); |
| 77 | + } |
69 | 78 | }
|
70 | 79 | }
|
71 |
| - return customFields; |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + private ArrayComposer<ObjectComposer<JSONComposer<String>>> startJson(StringBuilder appendTo) |
| 84 | + throws IOException, JSONObjectException, JsonProcessingException { |
| 85 | + if (!embed) { |
| 86 | + appendTo.append(JSON.std.asString(fieldName)).append(":"); |
| 87 | + } |
| 88 | + /* |
| 89 | + * -- no matter whether we embed or not, it seems easier to compose -- a JSON |
| 90 | + * object from the key/value pairs. -- if we embed that object, we simply chop |
| 91 | + * off the outermost curly braces. |
| 92 | + */ |
| 93 | + return JSON.std.composeString().startObject().startArrayField("string"); |
| 94 | + } |
| 95 | + |
| 96 | + private void addCustomFields(ArrayComposer<ObjectComposer<JSONComposer<String>>> oc, |
| 97 | + Map<String, CustomField> customFields, Map<String, String> mdcCustomFields) |
| 98 | + throws IOException, JsonProcessingException { |
| 99 | + for (int i = 0; i < customFieldKeyNames.size(); i++) { |
| 100 | + String key = customFieldKeyNames.get(i); |
| 101 | + String value = mdcCustomFields.get(key); |
| 102 | + // Let argument CustomField take precedence over MDC |
| 103 | + CustomField field = customFields.get(key); |
| 104 | + if (field != null) { |
| 105 | + value = field.getValue(); |
| 106 | + } |
| 107 | + if (value != null) { |
| 108 | + oc.startObject().put("k", key).put("v", value).put("i", i).end(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void finishJson(ArrayComposer<ObjectComposer<JSONComposer<String>>> oc, StringBuilder appendTo) |
| 114 | + throws IOException, JsonProcessingException { |
| 115 | + ObjectComposer<JSONComposer<String>> end = oc.end(); |
| 116 | + String result = end.end().finish().trim(); |
| 117 | + if (embed) { |
| 118 | + /* -- chop off curly braces -- */ |
| 119 | + appendTo.append(result.substring(1, result.length() - 1)); |
| 120 | + } else { |
| 121 | + appendTo.append(result); |
| 122 | + } |
72 | 123 | }
|
73 | 124 | }
|
0 commit comments