Skip to content

Commit

Permalink
Issue #9 chores: Removing unwanted code
Browse files Browse the repository at this point in the history
  • Loading branch information
shriharshs committed Nov 13, 2017
1 parent cbf7b02 commit 6c59719
Showing 1 changed file with 1 addition and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

import com.amazonaws.util.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jboss.logging.Logger;
Expand All @@ -23,8 +17,6 @@

import javax.ws.rs.HttpMethod;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -168,7 +160,7 @@ private boolean sendSms(String mobileNumber, String smsText) {
private String removePlusFromMobileNumber(String mobileNumber) {
logger.debug("Msg91SmsProvider - removePlusFromMobileNumber " + mobileNumber);

if (mobileNumber.startsWith("+")){
if (mobileNumber.startsWith("+")) {
return mobileNumber.substring(1);
}
return mobileNumber;
Expand All @@ -185,140 +177,4 @@ private String getCompletePath(String gateWayUrl, String sender, String smsRoute
return completeUrl;
}


private boolean sendSmsCode(String mobileNumber, String smsText) {
// Send an SMS
logger.debug("Sending " + smsText + " to mobileNumber " + mobileNumber);

String smsUrl = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_BASE_URL);
String smsUsr = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_USERNAME);
String smsPwd = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_PASSWORD);

String proxyUrl = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_PROXY_URL);
String proxyUsr = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_PROXY_USERNAME);
String proxyPwd = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_PROXY_PASSWORD);
String contentType = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_CONTENT_TYPE);

CloseableHttpClient httpClient = null;
try {
URL smsURL = (smsUrl != null && smsUrl.length() > 0) ? new URL(smsUrl) : null;
URL proxyURL = (proxyUrl != null && proxyUrl.length() > 0) ? new URL(proxyUrl) : null;

if (smsURL == null) {
logger.error("SMS gateway URL is not configured.");
return false;
}


CredentialsProvider credsProvider;
if (SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_AUTHTYPE, "").equals(SmsConfigurationConstants.CONF_AUTH_METHOD_INMESSAGE)) {
credsProvider = getCredentialsProvider(null, null, proxyUsr, proxyPwd, smsURL, proxyURL);
} else {
credsProvider = getCredentialsProvider(smsUsr, smsPwd, proxyUsr, proxyPwd, smsURL, proxyURL);
}

HttpHost target = new HttpHost(smsURL.getHost(), smsURL.getPort(), smsURL.getProtocol());
HttpHost proxy = (proxyURL != null) ? new HttpHost(proxyURL.getHost(), proxyURL.getPort(), proxyURL.getProtocol()) : null;

httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();

RequestConfig requestConfig;
requestConfig = RequestConfig.custom()
.setProxy(proxy)
.build();

String httpMethod = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_METHOD_TYPE);
if (httpMethod.equals(HttpMethod.GET)) {

String path = getPath(mobileNumber, smsURL, smsText);

HttpGet httpGet = new HttpGet(path);
httpGet.setConfig(requestConfig);
if (isNotEmpty(contentType)) {
httpGet.addHeader("Content-type", contentType);
}

logger.debug("Executing request " + httpGet.getRequestLine() + " to " + target + " via " + proxy);

CloseableHttpResponse response = httpClient.execute(target, httpGet);
StatusLine sl = response.getStatusLine();
response.close();
if (sl.getStatusCode() != 200) {
logger.error("SMS code for " + mobileNumber + " could not be sent: " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
}
return sl.getStatusCode() == 200;

} else if (httpMethod.equals(HttpMethod.POST)) {

String path = getPath(mobileNumber, smsURL, smsText);
String uri = smsURL.getProtocol() + "://" + smsURL.getHost() + ":" + smsURL.getPort() + path;

HttpPost httpPost = new HttpPost(uri);
httpPost.setConfig(requestConfig);
if (isNotEmpty(contentType)) {
httpPost.addHeader("Content-type", contentType);
}

HttpEntity entity = new ByteArrayEntity(smsText.getBytes("UTF-8"));
httpPost.setEntity(entity);

CloseableHttpResponse response = httpClient.execute(httpPost);
StatusLine sl = response.getStatusLine();
response.close();
if (sl.getStatusCode() != 200) {
logger.error("SMS code for " + mobileNumber + " could not be sent: " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
}
return sl.getStatusCode() == 200;
}
return true;
} catch (IOException e) {
logger.error(e);
return false;
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException ignore) {
// Ignore ...
}
}
}
}

private CredentialsProvider getCredentialsProvider(String smsUsr, String smsPwd, String proxyUsr, String proxyPwd, URL smsURL, URL proxyURL) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();

// If defined, add BASIC Authentication parameters
if (isNotEmpty(smsUsr) && isNotEmpty(smsPwd)) {
credsProvider.setCredentials(
new AuthScope(smsURL.getHost(), smsURL.getPort()),
new UsernamePasswordCredentials(smsUsr, smsPwd));

}

// If defined, add Proxy Authentication parameters
if (isNotEmpty(proxyUsr) && isNotEmpty(proxyPwd)) {
credsProvider.setCredentials(
new AuthScope(proxyURL.getHost(), proxyURL.getPort()),
new UsernamePasswordCredentials(proxyUsr, proxyPwd));

}
return credsProvider;
}

private String getPath(String mobileNumber, URL smsURL, String smsText) throws UnsupportedEncodingException {
String path = smsURL.getPath();
if (smsURL.getQuery() != null && smsURL.getQuery().length() > 0) {
path += smsURL.getQuery();
}
path = path.replaceFirst("\\{message\\}", URLEncoder.encode(smsText, "UTF-8"));
path = path.replaceFirst("\\{phonenumber\\}", URLEncoder.encode(mobileNumber, "UTF-8"));
return path;
}

private boolean isNotEmpty(String s) {
return (s != null && s.length() > 0);
}
}

0 comments on commit 6c59719

Please sign in to comment.