Regalamiunsorriso/decompiled-libs/www/acxent-common-1.0.1/it/acxent/sms/SmsSender.java

311 lines
10 KiB
Java
Raw Normal View History

2026-04-22 18:41:37 +02:00
package it.acxent.sms;
import it.acxent.db.ResParm;
import it.acxent.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SmsSender implements Runnable {
private String[] recipients;
private String username;
private String password;
private String url;
private String messaggio;
private int tipoInvio;
public static final int TIPO_INVIO_BULK = 1;
public static final int TIPO_INVIO_SKEBBY = 2;
private static final String CHARSET = "UTF-8";
public static void sendSms(String telefono, String username, String password, String url, String messaggio, int tipoInvio) {
SmsSender sms = new SmsSender();
sms.setRecipients(new String[] { telefono });
sms.setUsername(username);
sms.setPassword(password);
sms.setUrl(url);
sms.setMessaggio(messaggio);
sms.setTipoInvio(tipoInvio);
Thread t = new Thread(sms);
t.start();
}
public String[] getRecipients() {
return this.recipients;
}
public void setRecipients(String[] recipients) {
this.recipients = recipients;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public int getTipoInvio() {
return this.tipoInvio;
}
public void setTipoInvio(int tipoInvio) {
this.tipoInvio = tipoInvio;
}
public void run() {
switch (this.tipoInvio) {
case 1:
sendBulk();
break;
case 2:
sendSkebby();
break;
}
}
public ResParm sendBulk() {
ResParm rp = new ResParm(true);
try {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(getUrl());
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", getUsername()));
params.add(new BasicNameValuePair("password", getPassword()));
params.add(new BasicNameValuePair("message", getMessaggio()));
params.add(new BasicNameValuePair("msisdn", this.recipients[0]));
params.add(new BasicNameValuePair("want_report", "1"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = closeableHttpClient.execute((HttpUriRequest)httppost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null)
result.append(line);
rd.close();
System.out.println(result.toString());
StringTokenizer st = new StringTokenizer(result.toString(), "|");
if (st.countToken() == 3) {
String statusCode = st.nextToken();
String statusDescription = st.nextToken();
String batchId = st.nextToken();
System.out.println("SMS RESPONSE (" + statusCode + "):");
if (statusCode.equals("0")) {
rp.setStatus(true);
rp.setMsg("Inviato con successo");
} else {
rp.setStatus(false);
if (statusCode.equals("1")) {
rp.setMsg("Schedulato e non inviato");
} else if (statusCode.equals("22")) {
rp.setMsg("Internal fatal error");
} else if (statusCode.equals("23")) {
rp.setMsg("Authentication failure");
} else if (statusCode.equals("24")) {
rp.setMsg("Data validation failed");
} else if (statusCode.equals("25")) {
rp.setMsg("You do not have sufficient credits");
} else if (statusCode.equals("26")) {
rp.setMsg("Upstream credits not available");
} else if (statusCode.equals("27")) {
rp.setMsg("You have exceeded your daily quota");
} else if (statusCode.equals("28")) {
rp.setMsg("Upstream quota exceeded");
} else if (statusCode.equals("40")) {
rp.setMsg("Temporarily unavailable");
} else if (statusCode.equals("201")) {
rp.setMsg("Maximum batch size exceeded");
}
}
} else {
rp = new ResParm(false, "Response error! " + result.toString());
}
System.out.println(rp.getMsg());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
rp = new ResParm(false, e);
} catch (IOException e) {
e.printStackTrace();
rp = new ResParm(false, e);
}
return rp;
}
public ResParm sendSkebby() {
ResParm rp = new ResParm(true);
try {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(getUrl());
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("method", "send_sms_classic"));
params.add(new BasicNameValuePair("username", getUsername()));
params.add(new BasicNameValuePair("password", getPassword()));
params.add(new BasicNameValuePair("recipients[]", this.recipients[0]));
params.add(new BasicNameValuePair("text", getMessaggio()));
params.add(new BasicNameValuePair("charset", "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = closeableHttpClient.execute((HttpUriRequest)httppost);
HttpEntity resultEntity = response.getEntity();
if (null != resultEntity)
System.out.println(EntityUtils.toString(resultEntity));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rp;
}
public String getMessaggio() {
return (this.messaggio == null) ? "" : this.messaggio.trim();
}
public void setMessaggio(String messaggio) {
this.messaggio = messaggio;
}
public ResParm sendBulk2() {
ResParm rp = new ResParm(true);
try {
String data = "";
data = data + "username=" + data;
data = data + "&password=" + data;
data = data + "&message=" + data;
data = data + "&dca=16bit";
data = data + "&want_report=1";
data = data + "&msisdn=" + data;
URL url = new URL(getUrl());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
System.out.println(line);
}
wr.close();
rd.close();
System.out.println(result.toString());
StringTokenizer st = new StringTokenizer(result.toString(), "|");
if (st.countToken() == 3) {
String statusCode = st.nextToken();
String statusDescription = st.nextToken();
String batchId = st.nextToken();
System.out.println("SMS RESPONSE (" + statusCode + "):");
if (statusCode.equals("0")) {
rp.setStatus(true);
rp.setMsg("Inviato con successo");
} else {
rp.setStatus(false);
if (statusCode.equals("1")) {
rp.setMsg("Schedulato e non inviato");
} else if (statusCode.equals("22")) {
rp.setMsg("Internal fatal error");
} else if (statusCode.equals("23")) {
rp.setMsg("Authentication failure");
} else if (statusCode.equals("24")) {
rp.setMsg("Data validation failed");
} else if (statusCode.equals("25")) {
rp.setMsg("You do not have sufficient credits");
} else if (statusCode.equals("26")) {
rp.setMsg("Upstream credits not available");
} else if (statusCode.equals("27")) {
rp.setMsg("You have exceeded your daily quota");
} else if (statusCode.equals("28")) {
rp.setMsg("Upstream quota exceeded");
} else if (statusCode.equals("40")) {
rp.setMsg("Temporarily unavailable");
} else if (statusCode.equals("201")) {
rp.setMsg("Maximum batch size exceeded");
}
}
} else {
rp = new ResParm(false, "Response error! " + result.toString());
}
System.out.println(rp.getMsg());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
rp = new ResParm(false, e);
} catch (IOException e) {
e.printStackTrace();
rp = new ResParm(false, e);
}
return rp;
}
public static ResParm sendSmsSync(String telefono, String username, String password, String url, String messaggio, int tipoInvio) {
SmsSender sms = new SmsSender();
ResParm rp = new ResParm(false);
sms.setRecipients(new String[] { telefono });
sms.setUsername(username);
sms.setPassword(password);
sms.setUrl(url);
sms.setMessaggio(messaggio);
sms.setTipoInvio(tipoInvio);
switch (tipoInvio) {
case 1:
rp = sms.sendBulk();
break;
case 2:
sms.sendSkebby();
break;
}
return rp;
}
public static String stringToHex(String s) {
char[] chars = s.toCharArray();
StringBuffer output = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
String next = Integer.toHexString(chars[i]);
for (int j = 0; j < 4 - next.length(); j++)
output.append("0");
output.append(next);
}
return output.toString();
}
}