@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); }
@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 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 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); }
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); }
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);  }