UDPNotificationHandler extends Handler<DatagramPacket> { @Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); } UDPNotificationHandler(Map incoming, ExtensionWebSocketClient client); @Override void handleMessage(DatagramPacket packet);  }
@Test public void testEmptyConfig() { nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; String testStr = "{\"str\":\"msg\"}"; createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(new LinkedHashMap()); assert fakeClient.compareSource(sourceName); }
@Test public void testRegexSpeed() { incoming.put("passAllAddresses", true); Map<String, Object> regexParser = new LinkedHashMap<>(); String pattern = "<(\\d{1,3})>([1-9]\\d{0,2}) (?:(-)|(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\." + "\\d{1,6})?(?:Z|[-+]\\d{2}:\\d{2}))) (-|[!-~]{1,255}) (-|[!-~]{1,48}) (-|[!-~]{1,128}) (-|[!-~]{1,32}" + ") (?:(-)|((?:\\[[!-~&&[^ =\\]]]{1,32}(?: [!-~&&[^ =\\]]]{1,32}=\"(?:[\\u0000-\\uffff&&[^\"\\\\\\]]]|" + "(?:\\\\[\\\\\"\\]]))*\")*])+))(?: (?:((?!\\xef\\xbb\\xbf).*)|(?:\\xef\\xbb\\xbf([\\u0000-\\uffff]*))" + "))?"; regexParser.put("pattern", pattern); String[] locations = {"priority", "version", "noTimestamp", "timestamp", "hostName", "appName", "processId", "messageId", "noStructuredData", "structuredData", "standardMessage", "utfMessage"}; regexParser.put("locations", Arrays.asList(locations)); incoming.put("regexParser", regexParser); String testMessage = "<0>198 2018-07-23T15:26:20-07:00 remote iApp user branch [name] Here be a message for ya!"; byte[] packetBytes = testMessage.getBytes(); DatagramPacket packet= new DatagramPacket(packetBytes, packetBytes.length); assert Pattern.matches(pattern, testMessage); nHandler = new UDPNotificationHandler(incoming, fakeClient); nHandler.handleMessage(packet); int runCount = 100 * 1000; long currentTime = System.currentTimeMillis(); for (int i = 0; i < runCount; i ++) { packetBytes = testMessage.getBytes(); packetBytes[1] = (byte) ((i & 7) + 48); packet = new DatagramPacket(packetBytes, packetBytes.length); nHandler.handleMessage(packet); } long timeTaken = System.currentTimeMillis() - currentTime; System.out.println("Time taken for " + runCount + " messages was " + timeTaken + " ms"); System.out.println("This is " + ((double)timeTaken) / runCount + " ms per message and " + (int) (1.0 / timeTaken * runCount) + " thousand messages per second"); }
@Test public void testPassReceive() { incoming.put("passRecAddress", "Address"); incoming.put("passRecPort", "port#"); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; String expectedAddress = "127.0.0.1"; int port = 1234; Map<String,Object> expectedData = new LinkedHashMap<>(); expectedData.put("Address", expectedAddress); expectedData.put("port#", port); String testStr = "{\"str\":\"msg\"}"; createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); assert fakeClient.compareSource(sourceName); }
@Test public void testPassPure() { incoming.put("passPureMapIn", true); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; Map<String,Object> expectedData = new LinkedHashMap<>(); expectedData.put("str","msg"); String testStr = "{\"str\":\"msg\"}"; createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); assert fakeClient.compareSource(sourceName); }
@Test public void testTransform() { String[][] transforms = { {"str","message"}, {"str", "place.loc"}, {"str","invalid","nope"}, {"str"} }; List<List> transformations = new ArrayList<>(); for (String[] t : transforms) { List list = new ArrayList(Arrays.asList(t)); transformations.add(list); } incoming.put("transformations", transformations); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; Map<String,Object> expectedData = new LinkedHashMap<>(); String testStr = "{\"str\":\"msg\"}"; Map<String,Object> subMap = new LinkedHashMap<>(); subMap.put("loc","msg"); expectedData.put("place",subMap); expectedData.put("message","msg"); createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); assert fakeClient.compareSource(sourceName); }
@Test public void testPassUnspecifiedWithTransform() { incoming.put("passUnspecifiedIn", true); String[][] transforms = { {"str","message"}, {"str", "place.loc"}, {"str","invalid","nope"}, {"str"} }; List<List> transformations = new ArrayList<>(); for (String[] t : transforms) { List list = new ArrayList(Arrays.asList(t)); transformations.add(list); } incoming.put("transformations", transformations); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; Map<String,Object> expectedData = new LinkedHashMap<>(); String testStr = "{\"str\":\"msg\",\"stay\":\"here\"}"; Map<String,Object> subMap = new LinkedHashMap<>(); subMap.put("loc","msg"); expectedData.put("place",subMap); expectedData.put("message","msg"); expectedData.put("stay","here"); createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); assert fakeClient.compareSource(sourceName); }
@Test public void testXML() { incoming.put("expectXmlIn", true); incoming.put("passXmlRootNameIn", "root"); incoming.put("passPureMapIn", true); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; String testStr = "<rootElem><front>frontVal</front><back>backVal</back></rootElem>"; Map<String,Object> expectedData = new LinkedHashMap<>(); expectedData.put("front", "frontVal"); expectedData.put("back", "backVal"); expectedData.put("root", "rootElem"); createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); }
@Test public void testCSV() { incoming.put("expectCsvIn", true); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; String testStr = "first,second,last\na,b,c\nd,e,f"; Map<String,Object> subMap1 = new LinkedHashMap<>(); subMap1.put("first", "a"); subMap1.put("second", "b"); subMap1.put("last", "c"); Map<String,Object> subMap2 = new LinkedHashMap<>(); subMap2.put("first", "d"); subMap2.put("second", "e"); subMap2.put("last", "f"); Map[] expectedData = {subMap1,subMap2}; createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(Arrays.asList(expectedData)); }
@Test public void testPureBytes() { incoming.put("passBytesInAs", "bytes"); nHandler = new UDPNotificationHandler(incoming, fakeClient); String address = "localhost"; int port = 1234; String testStr = "I'm a test string"; Map<String,Object> expectedData = new LinkedHashMap<>(); expectedData.put("bytes", testStr); createPacket(testStr, address, port); nHandler.handleMessage(pack); assert fakeClient.compareData(expectedData); }