@Test public void testArgumentEscaping() throws Exception { final String ALERT_DEFINITION_NAME = "mock_alert_with_quotes"; final String ALERT_DEFINITION_LABEL = "Mock alert with Quotes"; final String ALERT_LABEL = "Alert Label"; final String ALERT_SERVICE_NAME = "FOO_SERVICE"; final String ALERT_TEXT = "Did you know, \"Quotes are hard!!!\""; final String ALERT_TEXT_ESCAPED = "Did you know, \\\"Quotes are hard\\!\\!\\!\\\""; final String ALERT_HOST = "mock_host"; final long ALERT_TIMESTAMP = 1111111l; DispatchCallback callback = EasyMock.createNiceMock(DispatchCallback.class); AlertNotification notification = new AlertNotification(); notification.Callback = callback; notification.CallbackIds = Collections.singletonList(UUID.randomUUID().toString()); AlertDefinitionEntity definition = new AlertDefinitionEntity(); definition.setDefinitionName(ALERT_DEFINITION_NAME); definition.setLabel(ALERT_DEFINITION_LABEL); AlertHistoryEntity history = new AlertHistoryEntity(); history.setAlertDefinition(definition); history.setAlertLabel(ALERT_LABEL); history.setAlertText(ALERT_TEXT); history.setAlertState(AlertState.OK); history.setServiceName(ALERT_SERVICE_NAME); history.setHostName(ALERT_HOST); history.setAlertTimestamp(ALERT_TIMESTAMP); AlertInfo alertInfo = new AlertInfo(history); notification.setAlertInfo(alertInfo); AlertScriptDispatcher dispatcher = new AlertScriptDispatcher(); m_injector.injectMembers(dispatcher); ProcessBuilder processBuilder = dispatcher.getProcessBuilder(SCRIPT_CONFIG_VALUE, notification); List<String> commands = processBuilder.command(); Assert.assertEquals(3, commands.size()); Assert.assertEquals("sh", commands.get(0)); Assert.assertEquals("-c", commands.get(1)); StringBuilder buffer = new StringBuilder(); buffer.append(SCRIPT_CONFIG_VALUE).append(" "); buffer.append(ALERT_DEFINITION_NAME).append(" "); buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" "); buffer.append(ALERT_SERVICE_NAME).append(" "); buffer.append(AlertState.OK).append(" "); buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" "); buffer.append(ALERT_TIMESTAMP).append(" "); buffer.append(ALERT_HOST); Assert.assertEquals(buffer.toString(), commands.get(2)); history.setHostName(null); alertInfo = new AlertInfo(history); notification.setAlertInfo(alertInfo); processBuilder = dispatcher.getProcessBuilder(SCRIPT_CONFIG_VALUE, notification); commands = processBuilder.command(); buffer = new StringBuilder(); buffer.append(SCRIPT_CONFIG_VALUE).append(" "); buffer.append(ALERT_DEFINITION_NAME).append(" "); buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" "); buffer.append(ALERT_SERVICE_NAME).append(" "); buffer.append(AlertState.OK).append(" "); buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" "); buffer.append(ALERT_TIMESTAMP).append(" "); buffer.append(""); Assert.assertEquals(buffer.toString(), commands.get(2)); }
ProcessBuilder getProcessBuilder(String script, AlertNotification notification) { final String shellCommand; final String shellCommandOption; if (SystemUtils.IS_OS_WINDOWS) { shellCommand = "cmd"; shellCommandOption = "/c"; } else { shellCommand = "sh"; shellCommandOption = "-c"; } AlertInfo alertInfo = notification.getAlertInfo(); AlertDefinitionEntity definition = alertInfo.getAlertDefinition(); String definitionName = definition.getDefinitionName(); AlertState alertState = alertInfo.getAlertState(); String serviceName = alertInfo.getServiceName(); String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; long alertTimestamp = alertInfo.getAlertTimestamp(); String hostName = alertInfo.getHostName(); Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); return new ProcessBuilder(shellCommand, shellCommandOption, foo); }
AlertScriptDispatcher implements NotificationDispatcher { ProcessBuilder getProcessBuilder(String script, AlertNotification notification) { final String shellCommand; final String shellCommandOption; if (SystemUtils.IS_OS_WINDOWS) { shellCommand = "cmd"; shellCommandOption = "/c"; } else { shellCommand = "sh"; shellCommandOption = "-c"; } AlertInfo alertInfo = notification.getAlertInfo(); AlertDefinitionEntity definition = alertInfo.getAlertDefinition(); String definitionName = definition.getDefinitionName(); AlertState alertState = alertInfo.getAlertState(); String serviceName = alertInfo.getServiceName(); String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; long alertTimestamp = alertInfo.getAlertTimestamp(); String hostName = alertInfo.getHostName(); Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); return new ProcessBuilder(shellCommand, shellCommandOption, foo); } }
AlertScriptDispatcher implements NotificationDispatcher { ProcessBuilder getProcessBuilder(String script, AlertNotification notification) { final String shellCommand; final String shellCommandOption; if (SystemUtils.IS_OS_WINDOWS) { shellCommand = "cmd"; shellCommandOption = "/c"; } else { shellCommand = "sh"; shellCommandOption = "-c"; } AlertInfo alertInfo = notification.getAlertInfo(); AlertDefinitionEntity definition = alertInfo.getAlertDefinition(); String definitionName = definition.getDefinitionName(); AlertState alertState = alertInfo.getAlertState(); String serviceName = alertInfo.getServiceName(); String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; long alertTimestamp = alertInfo.getAlertTimestamp(); String hostName = alertInfo.getHostName(); Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); return new ProcessBuilder(shellCommand, shellCommandOption, foo); }  }
AlertScriptDispatcher implements NotificationDispatcher { ProcessBuilder getProcessBuilder(String script, AlertNotification notification) { final String shellCommand; final String shellCommandOption; if (SystemUtils.IS_OS_WINDOWS) { shellCommand = "cmd"; shellCommandOption = "/c"; } else { shellCommand = "sh"; shellCommandOption = "-c"; } AlertInfo alertInfo = notification.getAlertInfo(); AlertDefinitionEntity definition = alertInfo.getAlertDefinition(); String definitionName = definition.getDefinitionName(); AlertState alertState = alertInfo.getAlertState(); String serviceName = alertInfo.getServiceName(); String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; long alertTimestamp = alertInfo.getAlertTimestamp(); String hostName = alertInfo.getHostName(); Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); return new ProcessBuilder(shellCommand, shellCommandOption, foo); }  String getScriptConfigurationKey( Notification notification ); long getScriptConfigurationTimeout(); @Override final String getType(); @Override final boolean isNotificationContentGenerationRequired(); @Override void dispatch(Notification notification); @Override final boolean isDigestSupported(); @Override final TargetConfigurationResult validateTargetConfig(Map<String, Object> properties); }
AlertScriptDispatcher implements NotificationDispatcher { ProcessBuilder getProcessBuilder(String script, AlertNotification notification) { final String shellCommand; final String shellCommandOption; if (SystemUtils.IS_OS_WINDOWS) { shellCommand = "cmd"; shellCommandOption = "/c"; } else { shellCommand = "sh"; shellCommandOption = "-c"; } AlertInfo alertInfo = notification.getAlertInfo(); AlertDefinitionEntity definition = alertInfo.getAlertDefinition(); String definitionName = definition.getDefinitionName(); AlertState alertState = alertInfo.getAlertState(); String serviceName = alertInfo.getServiceName(); String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; long alertTimestamp = alertInfo.getAlertTimestamp(); String hostName = alertInfo.getHostName(); Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); return new ProcessBuilder(shellCommand, shellCommandOption, foo); }  String getScriptConfigurationKey( Notification notification ); long getScriptConfigurationTimeout(); @Override final String getType(); @Override final boolean isNotificationContentGenerationRequired(); @Override void dispatch(Notification notification); @Override final boolean isDigestSupported(); @Override final TargetConfigurationResult validateTargetConfig(Map<String, Object> properties); static final String SCRIPT_CONFIG_DEFAULT_KEY; static final String SCRIPT_CONFIG_TIMEOUT_KEY; static final String DISPATCH_PROPERTY_SCRIPT_CONFIG_KEY; static final Escaper SHELL_ESCAPE; }