SensorParserConfigServiceImpl implements SensorParserConfigService { @Override public JSONObject parseMessage(ParseMessageRequest parseMessageRequest) throws RestException { SensorParserConfig sensorParserConfig = parseMessageRequest.getSensorParserConfig(); if (sensorParserConfig == null) { throw new RestException("SensorParserConfig is missing from ParseMessageRequest"); } else if (sensorParserConfig.getParserClassName() == null) { throw new RestException("SensorParserConfig must have a parserClassName"); } else { MessageParser<JSONObject> parser; try { parser = (MessageParser<JSONObject>) Class.forName(sensorParserConfig.getParserClassName()) .newInstance(); } catch (Exception e) { throw new RestException(e.toString(), e.getCause()); } Path temporaryGrokPath = null; if (isGrokConfig(sensorParserConfig)) { String name = parseMessageRequest.getSensorParserConfig().getSensorTopic(); temporaryGrokPath = grokService.saveTemporary(parseMessageRequest.getGrokStatement(), name); sensorParserConfig.getParserConfig() .put(MetronRestConstants.GROK_PATH_KEY, new Path(temporaryGrokPath, name).toString()); } parser.configure(sensorParserConfig.getParserConfig()); parser.init(); Optional<MessageParserResult<JSONObject>> result = parser.parseOptionalResult(parseMessageRequest.getSampleData().getBytes( StandardCharsets.UTF_8)); if (!result.isPresent()) { throw new RestException("Unknown error parsing sample data"); } if (result.get().getMasterThrowable().isPresent()) { throw new RestException("Error parsing sample data",result.get().getMasterThrowable().get()); } if (result.get().getMessages().isEmpty()) { throw new RestException("No results from parsing sample data"); } if (isGrokConfig(sensorParserConfig) && temporaryGrokPath != null) { grokService.deleteTemporary(); } return result.get().getMessages().get(0); } } @Autowired  SensorParserConfigServiceImpl(ObjectMapper objectMapper, CuratorFramework client,
      GrokService grokService, ConfigurationsCache cache); @Override SensorParserConfig save(String name, SensorParserConfig sensorParserConfig); @Override SensorParserConfig findOne(String name); @Override Map<String, SensorParserConfig> getAll(); @Override boolean delete(String name); @Override List<String> getAllTypes(); @Override Map<String, String> getAvailableParsers(); @Override Map<String, String> reloadAvailableParsers(); @Override JSONObject parseMessage(ParseMessageRequest parseMessageRequest);  }
@Test public void parseMessageShouldProperlyReturnParsedResults() throws Exception { final SensorParserConfig sensorParserConfig = getTestSquidSensorParserConfig(); String grokStatement = "SQUID_DELIMITED %{NUMBER:timestamp}[^0-9]*%{INT:elapsed} %{IP:ip_src_addr} %{WORD:action}/%{NUMBER:code} %{NUMBER:bytes} %{WORD:method} %{NOTSPACE:url}[^0-9]*(%{IP:ip_dst_addr})?"; String sampleData = "1461576382.642 161 127.0.0.1 TCP_MISS/200 103701 GET http: ParseMessageRequest parseMessageRequest = new ParseMessageRequest(); parseMessageRequest.setSensorParserConfig(sensorParserConfig); parseMessageRequest.setGrokStatement(grokStatement); parseMessageRequest.setSampleData(sampleData); File grokRoot = new File("./target", user); grokRoot.mkdir(); File patternFile = new File(grokRoot, "squid"); Writer writer = new OutputStreamWriter(new FileOutputStream(patternFile), StandardCharsets.UTF_8); writer.write(grokStatement); writer.close(); assertEquals(new HashMap<String, Object>() {{ put("elapsed", 161); put("code", 200); put("ip_dst_addr", "199.27.79.73"); put("ip_src_addr", "127.0.0.1"); put("action", "TCP_MISS"); put("bytes", 103701); put("method", "GET"); put("url", "http: put("timestamp", 1461576382642L); put("original_string", "1461576382.642 161 127.0.0.1 TCP_MISS/200 103701 GET http: }}, sensorParserConfigService.parseMessage(parseMessageRequest)); }
@Test public void missingSensorParserConfigShouldThrowRestException() { ParseMessageRequest parseMessageRequest = new ParseMessageRequest(); assertThrows(RestException.class, () -> sensorParserConfigService.parseMessage(parseMessageRequest)); }
@Test public void missingParserClassShouldThrowRestException() { final SensorParserConfig sensorParserConfig = new SensorParserConfig(); sensorParserConfig.setSensorTopic("squid"); ParseMessageRequest parseMessageRequest = new ParseMessageRequest(); parseMessageRequest.setSensorParserConfig(sensorParserConfig); assertThrows(RestException.class, () -> sensorParserConfigService.parseMessage(parseMessageRequest)); }
@Test public void invalidParserClassShouldThrowRestException() { final SensorParserConfig sensorParserConfig = new SensorParserConfig(); sensorParserConfig.setSensorTopic("squid"); sensorParserConfig.setParserClassName("bad.class.package.BadClassName"); ParseMessageRequest parseMessageRequest = new ParseMessageRequest(); parseMessageRequest.setSensorParserConfig(sensorParserConfig); assertThrows(RestException.class, () -> sensorParserConfigService.parseMessage(parseMessageRequest)); }