@Test public void liveTest() throws IOException { File configFile = new File(System.getProperty("user.home"), "ProjectForge" + File.separatorChar + "smsTestConfig.yaml"); if (!configFile.exists()) { log.info("Skipping live testing of sms service (OK). For live testing, please refer this code for the required config file: " + configFile.getAbsolutePath()); return; } Yaml yaml = new Yaml(); Map<String, Object> map = yaml.load(new FileReader(configFile)); String httpMethodType = (String) getFromMap(map, "projectforge", "sms", "httpMethod"); String url = (String) getFromMap(map, "projectforge", "sms", "url"); String testNumber = (String) getFromMap(map, "projectforge", "sms", "testNumber"); Map<String, String> httpParams = (Map<String, String>) getFromMap(map, "projectforge", "sms", "httpParameters"); SmsSenderConfig config = new SmsSenderConfig(); config.setHttpMethodType(httpMethodType).setUrl(url); config.setHttpParams(httpParams); config.setSmsMaxMessageLength((int) getFromMap(map, "projectforge", "sms", "smsMaxMessageLength")); config.setSmsReturnPatternSuccess((String) getFromMap(map, "projectforge", "sms", "returnCodePattern", "success")); config.setSmsReturnPatternNumberError((String) getFromMap(map, "projectforge", "sms", "returnCodePattern", "numberError")); config.setSmsReturnPatternMessageToLargeError((String) getFromMap(map, "projectforge", "sms", "returnCodePattern", "messageToLargeError")); config.setSmsReturnPatternMessageError((String) getFromMap(map, "projectforge", "sms", "returnCodePattern", "messageError")); config.setSmsReturnPatternError((String) getFromMap(map, "projectforge", "sms", "returnCodePattern", "error")); SmsSender sender = new SmsSender(config); sender.send(testNumber, "Hello world! With love by ProjectForge: " + new Date()); }
public HttpResponseCode send(String phoneNumber, String message) { if (message == null) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is null!"); return HttpResponseCode.MESSAGE_ERROR; } if (message.length() > config.getSmsMaxMessageLength()) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is to large, max length is " + config.getSmsMaxMessageLength() + ", but current message size is " + message.length()); return HttpResponseCode.MESSAGE_TO_LARGE; } String proceededUrl = replaceVariables(config.getUrl(), phoneNumber, message, true); HttpMethodBase method = createHttpMethod(proceededUrl); if (config.getHttpMethodType() == SmsSenderConfig.HttpMethodType.GET) { if (MapUtils.isNotEmpty(config.getHttpParams())) { NameValuePair[] params = new NameValuePair[config.getHttpParams().size()]; int index = 0; for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, true); params[index++] = new NameValuePair(entry.getKey(), value); } ((GetMethod) method).setQueryString(params); } } else { if (MapUtils.isNotEmpty(config.getHttpParams())) { for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, false); ((PostMethod) method).addParameter(entry.getKey(), value); } } } final HttpClient client = createHttpClient(); try { int responseNumber = client.executeMethod(method); final String response = method.getResponseBodyAsString(); log.info("Tried to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Response from service: " + response); HttpResponseCode responseCode = null; if (response == null || responseNumber != 200) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternNumberError())) { responseCode = HttpResponseCode.NUMBER_ERROR; } else if (matches(response, config.getSmsReturnPatternMessageToLargeError())) { responseCode = HttpResponseCode.MESSAGE_TO_LARGE; } else if (matches(response, config.getSmsReturnPatternMessageError())) { responseCode = HttpResponseCode.MESSAGE_ERROR; } else if (matches(response, config.getSmsReturnPatternError())) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternSuccess())) { responseCode = HttpResponseCode.SUCCESS; } else { responseCode = HttpResponseCode.UNKNOWN_ERROR; } if (responseCode != HttpResponseCode.SUCCESS) { log.error("Unexpected response from sms gateway: " + responseNumber + ": " + response + " (if this call was successful, did you configured projectforge.sms.returnCodePattern.success?)."); } return responseCode; } catch (final IOException ex) { String errorKey = "Call failed. Please contact administrator."; log.error(errorKey + ": " + proceededUrl + StringHelper.hideStringEnding(String.valueOf(phoneNumber), 'x', 3)); throw new RuntimeException(ex); } finally { method.releaseConnection(); } }
SmsSender { public HttpResponseCode send(String phoneNumber, String message) { if (message == null) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is null!"); return HttpResponseCode.MESSAGE_ERROR; } if (message.length() > config.getSmsMaxMessageLength()) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is to large, max length is " + config.getSmsMaxMessageLength() + ", but current message size is " + message.length()); return HttpResponseCode.MESSAGE_TO_LARGE; } String proceededUrl = replaceVariables(config.getUrl(), phoneNumber, message, true); HttpMethodBase method = createHttpMethod(proceededUrl); if (config.getHttpMethodType() == SmsSenderConfig.HttpMethodType.GET) { if (MapUtils.isNotEmpty(config.getHttpParams())) { NameValuePair[] params = new NameValuePair[config.getHttpParams().size()]; int index = 0; for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, true); params[index++] = new NameValuePair(entry.getKey(), value); } ((GetMethod) method).setQueryString(params); } } else { if (MapUtils.isNotEmpty(config.getHttpParams())) { for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, false); ((PostMethod) method).addParameter(entry.getKey(), value); } } } final HttpClient client = createHttpClient(); try { int responseNumber = client.executeMethod(method); final String response = method.getResponseBodyAsString(); log.info("Tried to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Response from service: " + response); HttpResponseCode responseCode = null; if (response == null || responseNumber != 200) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternNumberError())) { responseCode = HttpResponseCode.NUMBER_ERROR; } else if (matches(response, config.getSmsReturnPatternMessageToLargeError())) { responseCode = HttpResponseCode.MESSAGE_TO_LARGE; } else if (matches(response, config.getSmsReturnPatternMessageError())) { responseCode = HttpResponseCode.MESSAGE_ERROR; } else if (matches(response, config.getSmsReturnPatternError())) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternSuccess())) { responseCode = HttpResponseCode.SUCCESS; } else { responseCode = HttpResponseCode.UNKNOWN_ERROR; } if (responseCode != HttpResponseCode.SUCCESS) { log.error("Unexpected response from sms gateway: " + responseNumber + ": " + response + " (if this call was successful, did you configured projectforge.sms.returnCodePattern.success?)."); } return responseCode; } catch (final IOException ex) { String errorKey = "Call failed. Please contact administrator."; log.error(errorKey + ": " + proceededUrl + StringHelper.hideStringEnding(String.valueOf(phoneNumber), 'x', 3)); throw new RuntimeException(ex); } finally { method.releaseConnection(); } } }
SmsSender { public HttpResponseCode send(String phoneNumber, String message) { if (message == null) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is null!"); return HttpResponseCode.MESSAGE_ERROR; } if (message.length() > config.getSmsMaxMessageLength()) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is to large, max length is " + config.getSmsMaxMessageLength() + ", but current message size is " + message.length()); return HttpResponseCode.MESSAGE_TO_LARGE; } String proceededUrl = replaceVariables(config.getUrl(), phoneNumber, message, true); HttpMethodBase method = createHttpMethod(proceededUrl); if (config.getHttpMethodType() == SmsSenderConfig.HttpMethodType.GET) { if (MapUtils.isNotEmpty(config.getHttpParams())) { NameValuePair[] params = new NameValuePair[config.getHttpParams().size()]; int index = 0; for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, true); params[index++] = new NameValuePair(entry.getKey(), value); } ((GetMethod) method).setQueryString(params); } } else { if (MapUtils.isNotEmpty(config.getHttpParams())) { for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, false); ((PostMethod) method).addParameter(entry.getKey(), value); } } } final HttpClient client = createHttpClient(); try { int responseNumber = client.executeMethod(method); final String response = method.getResponseBodyAsString(); log.info("Tried to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Response from service: " + response); HttpResponseCode responseCode = null; if (response == null || responseNumber != 200) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternNumberError())) { responseCode = HttpResponseCode.NUMBER_ERROR; } else if (matches(response, config.getSmsReturnPatternMessageToLargeError())) { responseCode = HttpResponseCode.MESSAGE_TO_LARGE; } else if (matches(response, config.getSmsReturnPatternMessageError())) { responseCode = HttpResponseCode.MESSAGE_ERROR; } else if (matches(response, config.getSmsReturnPatternError())) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternSuccess())) { responseCode = HttpResponseCode.SUCCESS; } else { responseCode = HttpResponseCode.UNKNOWN_ERROR; } if (responseCode != HttpResponseCode.SUCCESS) { log.error("Unexpected response from sms gateway: " + responseNumber + ": " + response + " (if this call was successful, did you configured projectforge.sms.returnCodePattern.success?)."); } return responseCode; } catch (final IOException ex) { String errorKey = "Call failed. Please contact administrator."; log.error(errorKey + ": " + proceededUrl + StringHelper.hideStringEnding(String.valueOf(phoneNumber), 'x', 3)); throw new RuntimeException(ex); } finally { method.releaseConnection(); } } SmsSender(SmsSenderConfig config); }
SmsSender { public HttpResponseCode send(String phoneNumber, String message) { if (message == null) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is null!"); return HttpResponseCode.MESSAGE_ERROR; } if (message.length() > config.getSmsMaxMessageLength()) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is to large, max length is " + config.getSmsMaxMessageLength() + ", but current message size is " + message.length()); return HttpResponseCode.MESSAGE_TO_LARGE; } String proceededUrl = replaceVariables(config.getUrl(), phoneNumber, message, true); HttpMethodBase method = createHttpMethod(proceededUrl); if (config.getHttpMethodType() == SmsSenderConfig.HttpMethodType.GET) { if (MapUtils.isNotEmpty(config.getHttpParams())) { NameValuePair[] params = new NameValuePair[config.getHttpParams().size()]; int index = 0; for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, true); params[index++] = new NameValuePair(entry.getKey(), value); } ((GetMethod) method).setQueryString(params); } } else { if (MapUtils.isNotEmpty(config.getHttpParams())) { for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, false); ((PostMethod) method).addParameter(entry.getKey(), value); } } } final HttpClient client = createHttpClient(); try { int responseNumber = client.executeMethod(method); final String response = method.getResponseBodyAsString(); log.info("Tried to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Response from service: " + response); HttpResponseCode responseCode = null; if (response == null || responseNumber != 200) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternNumberError())) { responseCode = HttpResponseCode.NUMBER_ERROR; } else if (matches(response, config.getSmsReturnPatternMessageToLargeError())) { responseCode = HttpResponseCode.MESSAGE_TO_LARGE; } else if (matches(response, config.getSmsReturnPatternMessageError())) { responseCode = HttpResponseCode.MESSAGE_ERROR; } else if (matches(response, config.getSmsReturnPatternError())) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternSuccess())) { responseCode = HttpResponseCode.SUCCESS; } else { responseCode = HttpResponseCode.UNKNOWN_ERROR; } if (responseCode != HttpResponseCode.SUCCESS) { log.error("Unexpected response from sms gateway: " + responseNumber + ": " + response + " (if this call was successful, did you configured projectforge.sms.returnCodePattern.success?)."); } return responseCode; } catch (final IOException ex) { String errorKey = "Call failed. Please contact administrator."; log.error(errorKey + ": " + proceededUrl + StringHelper.hideStringEnding(String.valueOf(phoneNumber), 'x', 3)); throw new RuntimeException(ex); } finally { method.releaseConnection(); } } SmsSender(SmsSenderConfig config); HttpResponseCode send(String phoneNumber, String message); SmsSender setConfig(SmsSenderConfig config); }
SmsSender { public HttpResponseCode send(String phoneNumber, String message) { if (message == null) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is null!"); return HttpResponseCode.MESSAGE_ERROR; } if (message.length() > config.getSmsMaxMessageLength()) { log.error("Failed to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Message is to large, max length is " + config.getSmsMaxMessageLength() + ", but current message size is " + message.length()); return HttpResponseCode.MESSAGE_TO_LARGE; } String proceededUrl = replaceVariables(config.getUrl(), phoneNumber, message, true); HttpMethodBase method = createHttpMethod(proceededUrl); if (config.getHttpMethodType() == SmsSenderConfig.HttpMethodType.GET) { if (MapUtils.isNotEmpty(config.getHttpParams())) { NameValuePair[] params = new NameValuePair[config.getHttpParams().size()]; int index = 0; for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, true); params[index++] = new NameValuePair(entry.getKey(), value); } ((GetMethod) method).setQueryString(params); } } else { if (MapUtils.isNotEmpty(config.getHttpParams())) { for (Map.Entry<String, String> entry : config.getHttpParams().entrySet()) { String value = replaceVariables(entry.getValue(), phoneNumber, message, false); ((PostMethod) method).addParameter(entry.getKey(), value); } } } final HttpClient client = createHttpClient(); try { int responseNumber = client.executeMethod(method); final String response = method.getResponseBodyAsString(); log.info("Tried to send message to destination number: '" + StringHelper.hideStringEnding(phoneNumber, 'x', 3) + ". Response from service: " + response); HttpResponseCode responseCode = null; if (response == null || responseNumber != 200) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternNumberError())) { responseCode = HttpResponseCode.NUMBER_ERROR; } else if (matches(response, config.getSmsReturnPatternMessageToLargeError())) { responseCode = HttpResponseCode.MESSAGE_TO_LARGE; } else if (matches(response, config.getSmsReturnPatternMessageError())) { responseCode = HttpResponseCode.MESSAGE_ERROR; } else if (matches(response, config.getSmsReturnPatternError())) { responseCode = HttpResponseCode.UNKNOWN_ERROR; } else if (matches(response, config.getSmsReturnPatternSuccess())) { responseCode = HttpResponseCode.SUCCESS; } else { responseCode = HttpResponseCode.UNKNOWN_ERROR; } if (responseCode != HttpResponseCode.SUCCESS) { log.error("Unexpected response from sms gateway: " + responseNumber + ": " + response + " (if this call was successful, did you configured projectforge.sms.returnCodePattern.success?)."); } return responseCode; } catch (final IOException ex) { String errorKey = "Call failed. Please contact administrator."; log.error(errorKey + ": " + proceededUrl + StringHelper.hideStringEnding(String.valueOf(phoneNumber), 'x', 3)); throw new RuntimeException(ex); } finally { method.releaseConnection(); } } SmsSender(SmsSenderConfig config); HttpResponseCode send(String phoneNumber, String message); SmsSender setConfig(SmsSenderConfig config);  }