|
| 1 | +package io.virtualan.cucumblan.message.typeimpl; |
| 2 | + |
| 3 | + |
| 4 | +import com.jayway.jsonpath.JsonPath; |
| 5 | +import java.util.Map.Entry; |
| 6 | + |
| 7 | +public class JSONType implements |
| 8 | + io.virtualan.cucumblan.message.type.MessageType<String, String> { |
| 9 | + |
| 10 | + private static final java.util.logging.Logger LOGGER = java.util.logging.Logger |
| 11 | + .getLogger(JSONType.class.getName()); |
| 12 | + private static java.util.Properties jsonMessageTypeMapper = new java.util.Properties(); |
| 13 | + |
| 14 | + static { |
| 15 | + reload(); |
| 16 | + } |
| 17 | + |
| 18 | + private String type = "JSONType"; |
| 19 | + private String id; |
| 20 | + private String body; |
| 21 | + |
| 22 | + public JSONType() { |
| 23 | + } |
| 24 | + |
| 25 | + public JSONType(String id, String body) { |
| 26 | + this.body = body; |
| 27 | + this.id = id; |
| 28 | + } |
| 29 | + |
| 30 | + public static void reload() { |
| 31 | + try { |
| 32 | + java.io.InputStream stream = Thread.currentThread().getContextClassLoader() |
| 33 | + .getResourceAsStream("json-messagetype.properties"); |
| 34 | + if (stream == null) { |
| 35 | + stream = io.virtualan.cucumblan.props.ApplicationConfiguration.class.getClassLoader() |
| 36 | + .getResourceAsStream("json-messagetype.properties"); |
| 37 | + } |
| 38 | + if (stream != null) { |
| 39 | + jsonMessageTypeMapper.load(stream); |
| 40 | + } else { |
| 41 | + LOGGER.warning("unable to load json-messagetype.properties"); |
| 42 | + } |
| 43 | + } catch (Exception var1) { |
| 44 | + LOGGER.warning("json-messagetype.properties not found"); |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public String getType() { |
| 50 | + return this.type; |
| 51 | + } |
| 52 | + |
| 53 | + public java.util.List<org.apache.kafka.common.header.Header> getHeaders() { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + //Mandatory |
| 58 | + public String getId() { |
| 59 | + return this.id; |
| 60 | + } |
| 61 | + |
| 62 | + public String getKey() { |
| 63 | + return this.id; |
| 64 | + } |
| 65 | + |
| 66 | + public String getMessage() { |
| 67 | + return this.body; |
| 68 | + } |
| 69 | + |
| 70 | + //Mandatory |
| 71 | + public org.json.JSONObject getMessageAsJson() { |
| 72 | + return new org.json.JSONObject(this.body); |
| 73 | + } |
| 74 | + |
| 75 | + public io.virtualan.cucumblan.message.type.MessageType buildProducerMessage(Object messages, |
| 76 | + java.util.Map<String, Object> contextParam) |
| 77 | + throws io.virtualan.cucumblan.message.exception.MessageNotDefinedException { |
| 78 | + String message; |
| 79 | + try { |
| 80 | + org.json.JSONObject body; |
| 81 | + if (messages instanceof java.util.List) { |
| 82 | + message = (String) ((java.util.List) messages).stream() |
| 83 | + .collect(java.util.stream.Collectors.joining()); |
| 84 | + body = new org.json.JSONObject(message); |
| 85 | + return buildMessageType(body.toString(), contextParam); |
| 86 | + } else { |
| 87 | + |
| 88 | + message = io.virtualan.mapson.Mapson.buildMAPsonAsJson((java.util.Map) messages); |
| 89 | + body = new org.json.JSONObject(message); |
| 90 | + return buildMessageType(body.toString(), contextParam); |
| 91 | + } |
| 92 | + } catch (io.virtualan.mapson.exception.BadInputDataException | io.virtualan.cucumblan.message.exception.SkipMessageException exception) { |
| 93 | + throw new io.virtualan.cucumblan.message.exception.MessageNotDefinedException( |
| 94 | + exception.getMessage()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + //Mandatory |
| 99 | + public io.virtualan.cucumblan.message.type.MessageType buildConsumerMessage( |
| 100 | + org.apache.kafka.clients.consumer.ConsumerRecord<String, String> record, |
| 101 | + java.util.Map<String, Object> contextParam) |
| 102 | + throws io.virtualan.cucumblan.message.exception.SkipMessageException { |
| 103 | + return buildMessageType(record.value(), contextParam); |
| 104 | + } |
| 105 | + |
| 106 | + public String toString() { |
| 107 | + return "JSONMessageType{type='" + this.type + '\'' + ", id=" + this.id + ", body=" + this.body |
| 108 | + + '}'; |
| 109 | + } |
| 110 | + |
| 111 | + public JSONType buildMessageType(String body, java.util.Map<String, Object> contextParam) |
| 112 | + throws io.virtualan.cucumblan.message.exception.SkipMessageException { |
| 113 | + if (jsonMessageTypeMapper != null && !jsonMessageTypeMapper.isEmpty()) { |
| 114 | + for (Entry entry : jsonMessageTypeMapper.entrySet()) { |
| 115 | + try { |
| 116 | + if (contextParam.get("EVENT_NAME") != null |
| 117 | + && entry.getKey().toString() |
| 118 | + .equalsIgnoreCase(contextParam.get("EVENT_NAME").toString())) { |
| 119 | + String identifier = buildkey(body, entry.getValue().toString()); |
| 120 | + if (identifier != null) { |
| 121 | + return new JSONType(identifier, body); |
| 122 | + } |
| 123 | + } |
| 124 | + } catch (Exception e) { |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + throw new io.virtualan.cucumblan.message.exception.SkipMessageException(body); |
| 129 | + } |
| 130 | + |
| 131 | + private String buildkey(String body, String paths){ |
| 132 | + StringBuilder key = new StringBuilder(); |
| 133 | + for(String path : paths.split("(?<!\\\\),")) { |
| 134 | + String pathId = path.replace("\\\\,",","); |
| 135 | + Object identifier = JsonPath.read(body, pathId); |
| 136 | + if(identifier != null) { |
| 137 | + if (key.length() != 0) { |
| 138 | + key.append("_"); |
| 139 | + } |
| 140 | + key.append(identifier.toString()); |
| 141 | + } |
| 142 | + } |
| 143 | + return key.toString(); |
| 144 | + } |
| 145 | +} |
0 commit comments