446 lines
17 KiB
Java
446 lines
17 KiB
Java
package it.acxent.skebby.api;
|
|
|
|
import com.google.gson.Gson;
|
|
import it.acxent.common.Parm;
|
|
import it.acxent.common.StatusMsg;
|
|
import it.acxent.db.ApplParm;
|
|
import it.acxent.db.ApplParmFull;
|
|
import it.acxent.db.DBAdapter;
|
|
import it.acxent.db.ResParm;
|
|
import it.acxent.mail.MailMessage;
|
|
import it.acxent.mail.MailProperties;
|
|
import java.util.Base64;
|
|
import okhttp3.MediaType;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.Response;
|
|
import org.json.JSONObject;
|
|
|
|
public class SmsSkebbyApi {
|
|
public static final String API_SMS_SKEBBY_DEFAULT_SERVER = "https://api.skebby.it/API/v1.0/REST/";
|
|
|
|
private static final String P_SMS_SKEBBY_USER = "SMS_SKEBBY_USER";
|
|
|
|
private static final String P_SMS_SKEBBY_PWD = "SMS_SKEBBY_PWD";
|
|
|
|
private static final String P_SMS_SKEBBY_API_SERVER = "SMS_SKEBBY_API_SERVER";
|
|
|
|
private static final String P_SMS_SKEBBY_DEFAULT_SENDER = "SMS_SKEBBY_DEFAULT_SENDER";
|
|
|
|
private static final String P_SMS_SKEBBY_MIN_CREDITI_ALLARME = "SMS_SKEBBY_MIN_CREDITI_ALLARME";
|
|
|
|
private static final String P_SMS_SKEBBY_MAILTO_ALLARME = "SMS_SKEBBY_MAILTO_ALLARME";
|
|
|
|
private static final String P_SMS_SKEBBY_CREDITO_RESIDUO = "SMS_SKEBBY_CREDITO_RESIDUO";
|
|
|
|
private static final String URI_CMD_LOGIN = "login/";
|
|
|
|
private static final String URI_CMD_TOKEN = "token/";
|
|
|
|
private static final String URI_CMD_SEND_SMS = "sms";
|
|
|
|
private static final String HEADER_ACCESS_TOKEN = "Access_token";
|
|
|
|
private static final String HEADER_SESSION_KEY = "Session_key";
|
|
|
|
private static final String HEADER_USER_KEY = "user_key";
|
|
|
|
public static final String SMS_TYPE_CLASSIC_PLUS = "GP";
|
|
|
|
public static final String SMS_TYPE_CLASSIC = "TI";
|
|
|
|
public static final String SMS_TYPE_BASIC = "SI";
|
|
|
|
private static boolean debug = false;
|
|
|
|
private String userKey;
|
|
|
|
private String sessionKey;
|
|
|
|
private String accessToken;
|
|
|
|
private boolean useSessionKey = true;
|
|
|
|
private ApplParmFull apFull = null;
|
|
|
|
public SmsSkebbyApi(ApplParmFull apFull) {
|
|
this.apFull = apFull;
|
|
}
|
|
|
|
public SmsSkebbyApi(ApplParmFull apFull, boolean useSessionKey) {
|
|
this.apFull = apFull;
|
|
this.useSessionKey = useSessionKey;
|
|
}
|
|
|
|
public SmsSkebbyApi() {}
|
|
|
|
public static void initApplicationParms(ApplParmFull ap) {
|
|
if (ap != null) {
|
|
DBAdapter.logDebug(true, "FACE initParms: start");
|
|
String l_tipoParm = "SMS-API-SKEBBY";
|
|
Parm bean = new Parm(ap);
|
|
StatusMsg.updateMsgByTag(ap, "INIT", l_tipoParm);
|
|
bean.findByCodice("SMS_SKEBBY_USER");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_USER");
|
|
bean.setDescrizione("SMS_SKEBBY_USER");
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("acolzi");
|
|
bean.setNota("API USER SKEBBY. DI SOLITO LA MAIL");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_PWD");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_PWD");
|
|
bean.setDescrizione("SMS_SKEBBY_PWD");
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("pio");
|
|
bean.setNota("API PWD ACCESSO SKEBBY");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_API_SERVER");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_API_SERVER");
|
|
bean.setDescrizione("SMS_SKEBBY_API_SERVER");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("https://api.skebby.it/API/v1.0/REST/");
|
|
bean.setNota("SERVER REST SKEBBY. DI SOLITO https://api.skebby.it/API/v1.0/REST/");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_DEFAULT_SENDER");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_DEFAULT_SENDER");
|
|
bean.setDescrizione("SMS_SKEBBY_DEFAULT_SENDER");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("INVIATO DA XXX...");
|
|
bean.setNota("SENDER SMS. PUO' ESSERE UN NUMERO O DEL TESTO?");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_MAILTO_ALLARME");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_MAILTO_ALLARME");
|
|
bean.setDescrizione("SMS_SKEBBY_MAILTO_ALLARME");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("acolzi19@gmail.com");
|
|
bean.setNota("INDIRIZZO EMAIL A CUI INVIARE IL MESSAGGIO DI ALLARME. SE VUOTO --> DISATTIVATO");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_MIN_CREDITI_ALLARME");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_MIN_CREDITI_ALLARME");
|
|
bean.setDescrizione("SMS_SKEBBY_MIN_CREDITI_ALLARME");
|
|
bean.setFlgTipo(1L);
|
|
bean.setNumero(1000.0D);
|
|
bean.setNota("CREDITI MINIMO SOTTO IL QUALE INVIO ALLARME VIA MAIL A MAILTO_ALLARME.<BR><=0--> DISATTIVATO");
|
|
bean.save();
|
|
bean.findByCodice("SMS_SKEBBY_CREDITO_RESIDUO");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("SMS_SKEBBY_CREDITO_RESIDUO");
|
|
bean.setDescrizione("SMS_SKEBBY_MIN_CREDITI_ALLARME");
|
|
bean.setFlgTipo(1L);
|
|
bean.setNumero(0.0D);
|
|
bean.setNota("NUMERO CREDITI RESIDUI, AGGIORNATO AD OGNI INVIO");
|
|
bean.save();
|
|
DBAdapter.logDebug(true, "FACE initParms: stop");
|
|
StatusMsg.deleteMsgByTag(ap, "INIT");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
String hostname = "localhost:3308";
|
|
String db = "face";
|
|
ApplParmFull ap = new ApplParmFull(new ApplParm(17, "//" + hostname + "/" + db, db, "root", "root", 1, 10, 300));
|
|
SmsSkebbyApi bean = new SmsSkebbyApi(ap, false);
|
|
SmsSkebbyMessage sms = new SmsSkebbyMessage("+393285726937", "prova 123 prova");
|
|
SmsSkebbyApiResult resF = bean._sendMessage(sms);
|
|
System.out.println(resF.getMsg() + "\n" + resF.getMsg());
|
|
}
|
|
|
|
protected String getBase64BasicCredential() {
|
|
String encoding = getApiUser() + ":" + getApiUser();
|
|
return "Basic " + Base64.getEncoder().encodeToString(encoding.getBytes());
|
|
}
|
|
|
|
public ApplParmFull getApFull() {
|
|
return this.apFull;
|
|
}
|
|
|
|
public void setApFull(ApplParmFull apFull) {
|
|
this.apFull = apFull;
|
|
}
|
|
|
|
private String getApiEndpoint() {
|
|
return this.apFull.getParm("SMS_SKEBBY_API_SERVER").getTesto().isEmpty() ? "https://api.skebby.it/API/v1.0/REST/" :
|
|
this.apFull.getParm("SMS_SKEBBY_API_SERVER").getTesto();
|
|
}
|
|
|
|
private String getApiPwd() {
|
|
return this.apFull.getParm("SMS_SKEBBY_PWD").getTesto().isEmpty() ? "G@ndalf1" :
|
|
this.apFull.getParm("SMS_SKEBBY_PWD").getTesto();
|
|
}
|
|
|
|
public SmsSkebbyApiResult _loginApache() {
|
|
SmsSkebbyApiResult resER = new SmsSkebbyApiResult();
|
|
return resER;
|
|
}
|
|
|
|
public SmsSkebbyApiResult _getSessionToken() {
|
|
SmsSkebbyApiResult resER = new SmsSkebbyApiResult();
|
|
OkHttpClient client = UnsafeOkHttpSSL.getUnsafeOkHttpClient();
|
|
RequestBody emptyBody = RequestBody.create(new byte[0], null);
|
|
Request request = new Request.Builder().url(getApiEndpoint() + "login/").header("Authorization", getBase64BasicCredential())
|
|
.header("Accept", "application/json").header("Content-Type", "application/json").header("Content-Language", "it-IT").get()
|
|
.build();
|
|
try {
|
|
if (debug)
|
|
System.out.println("SmsSkebbyApi --> trainingStop");
|
|
try (Response response = client.newCall(request).execute()) {
|
|
String content = (response.body() != null) ? response.body().string() : "";
|
|
int statusCode = response.code();
|
|
if (debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg("Errore! Dati login errati");
|
|
} else {
|
|
resER.setOk(true);
|
|
resER.setMsg("Login eseguito correttamente");
|
|
resER.setResult(content);
|
|
String[] parts = content.split(";");
|
|
setUserKey(parts[0]);
|
|
setSessionKey(parts[1]);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
private String getApiUser() {
|
|
return this.apFull.getParm("SMS_SKEBBY_USER").getTesto().isEmpty() ? "amministrazione@misericordiarifredi.org" :
|
|
this.apFull.getParm("SMS_SKEBBY_USER").getTesto();
|
|
}
|
|
|
|
public SmsSkebbyApiResult _getAccessToken() {
|
|
SmsSkebbyApiResult resER = new SmsSkebbyApiResult();
|
|
OkHttpClient client = UnsafeOkHttpSSL.getUnsafeOkHttpClient();
|
|
RequestBody emptyBody = RequestBody.create(new byte[0], null);
|
|
Request request = new Request.Builder().url(getApiEndpoint() + "token/").header("Authorization", getBase64BasicCredential())
|
|
.header("Accept", "application/json").header("Content-Type", "application/json").header("Content-Language", "it-IT").get()
|
|
.build();
|
|
try {
|
|
if (debug)
|
|
System.out.println("SmsSkebbyApi --> _getUserToken");
|
|
try (Response response = client.newCall(request).execute()) {
|
|
String content = (response.body() != null) ? response.body().string() : "";
|
|
int statusCode = response.code();
|
|
if (debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg("Errore! Dati login errati. Impossibile avere il token");
|
|
} else {
|
|
resER.setOk(true);
|
|
resER.setMsg("User Token ok");
|
|
resER.setResult(content);
|
|
String[] parts = content.split(";");
|
|
setUserKey(parts[0]);
|
|
setAccessToken(parts[1]);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
public SmsSkebbyApiResult _sendMessage(SmsSkebbyMessage smsMessage) {
|
|
SmsSkebbyApiResult res;
|
|
if (isUseSessionKey()) {
|
|
res = _sendMessageWithSessionKey(smsMessage);
|
|
} else {
|
|
res = _sendMessageWithAccessToken(smsMessage);
|
|
}
|
|
JSONObject joRes = res.getJsonObjectFromStringResult();
|
|
if (joRes.has("remaining_credits")) {
|
|
long remainingCredits = joRes.getLong("remaining_credits");
|
|
Parm parm = new Parm(getApFull());
|
|
parm.findByCodice("SMS_SKEBBY_CREDITO_RESIDUO");
|
|
parm.setNumero((double)remainingCredits);
|
|
parm.save();
|
|
}
|
|
return res;
|
|
}
|
|
|
|
private String getUserKey() {
|
|
return (this.userKey == null) ? "" : this.userKey.trim();
|
|
}
|
|
|
|
private void setUserKey(String userKey) {
|
|
this.userKey = userKey;
|
|
}
|
|
|
|
private String getSessionKey() {
|
|
return (this.sessionKey == null) ? "" : this.sessionKey.trim();
|
|
}
|
|
|
|
private void setSessionKey(String accessToken) {
|
|
this.sessionKey = accessToken;
|
|
}
|
|
|
|
private String getDefaultSender() {
|
|
return this.apFull.getParm("SMS_SKEBBY_DEFAULT_SENDER").getTesto().isEmpty() ? "+394390990925" :
|
|
this.apFull.getParm("SMS_SKEBBY_DEFAULT_SENDER").getTesto();
|
|
}
|
|
|
|
private String getAccessToken() {
|
|
return (this.accessToken == null) ? "" : this.accessToken.trim();
|
|
}
|
|
|
|
private void setAccessToken(String accessToken) {
|
|
this.accessToken = accessToken;
|
|
}
|
|
|
|
private boolean isUseSessionKey() {
|
|
return this.useSessionKey;
|
|
}
|
|
|
|
private void setUseSessionKey(boolean useSessionKey) {
|
|
this.useSessionKey = useSessionKey;
|
|
}
|
|
|
|
private SmsSkebbyApiResult _sendMessageWithSessionKey(SmsSkebbyMessage smsMessage) {
|
|
SmsSkebbyApiResult resER = new SmsSkebbyApiResult();
|
|
boolean debug = true;
|
|
if (getSessionKey().isEmpty())
|
|
resER = _getSessionToken();
|
|
if (resER.isOk()) {
|
|
if (debug)
|
|
DBAdapter.printDebug("SmsSkebbyApi --> _sendMessage: userkey:" + getUserKey() + " accesstoken: " + getSessionKey());
|
|
OkHttpClient client = UnsafeOkHttpSSL.getUnsafeOkHttpClient();
|
|
Gson gson = new Gson();
|
|
JSONObject joSms = new JSONObject(gson.toJson(smsMessage));
|
|
if (!joSms.has("sender"))
|
|
joSms.put("sender", getDefaultSender());
|
|
MediaType JSONmediaType = MediaType.get("application/json; charset=utf-8");
|
|
RequestBody body = RequestBody.create(joSms.toString(), MediaType.parse("application/json"));
|
|
Request request = new Request.Builder().url(getApiEndpoint() + "sms").addHeader("user_key", getUserKey())
|
|
.addHeader("Session_key", getSessionKey()).addHeader("Accept", "application/json")
|
|
.addHeader("Content-Type", "application/json").post(body).build();
|
|
try {
|
|
if (debug)
|
|
DBAdapter.printDebug("SmsSkebbyApi --> _sendMessage:\n" + joSms.toString());
|
|
try (Response response = client.newCall(request).execute()) {
|
|
String content = (response.body() != null) ? response.body().string() : "";
|
|
int statusCode = response.code();
|
|
if (debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg("Errore! Impossibile inviare sms. " + content);
|
|
} else {
|
|
resER.setOk(true);
|
|
resER.setMsg("SMS INVIATI CORRETTAMENTE");
|
|
resER.setResult(content);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
private SmsSkebbyApiResult _sendMessageWithAccessToken(SmsSkebbyMessage smsMessage) {
|
|
SmsSkebbyApiResult resER = new SmsSkebbyApiResult();
|
|
boolean debug = true;
|
|
if (getAccessToken().isEmpty())
|
|
resER = _getAccessToken();
|
|
if (resER.isOk()) {
|
|
if (debug)
|
|
DBAdapter.printDebug("SmsSkebbyApi --> _sendMessage: userkey:" + getUserKey() + " accesstoken: " + getAccessToken());
|
|
OkHttpClient client = UnsafeOkHttpSSL.getUnsafeOkHttpClient();
|
|
Gson gson = new Gson();
|
|
JSONObject joSms = new JSONObject(gson.toJson(smsMessage));
|
|
if (!joSms.has("sender"))
|
|
joSms.put("sender", getDefaultSender());
|
|
MediaType JSONmediaType = MediaType.get("application/json; charset=utf-8");
|
|
RequestBody body = RequestBody.create(joSms.toString(), MediaType.parse("application/json"));
|
|
Request request = new Request.Builder().url(getApiEndpoint() + "sms").addHeader("user_key", getUserKey())
|
|
.addHeader("Access_token", getAccessToken()).addHeader("Accept", "application/json")
|
|
.addHeader("Content-Type", "application/json").post(body).build();
|
|
try {
|
|
if (debug)
|
|
DBAdapter.printDebug("SmsSkebbyApi --> _sendMessage:\n" + joSms.toString());
|
|
try (Response response = client.newCall(request).execute()) {
|
|
String content = (response.body() != null) ? response.body().string() : "";
|
|
int statusCode = response.code();
|
|
if (debug) {
|
|
System.out.println("Status Code: " + statusCode);
|
|
System.out.println("\ncontent = " + content);
|
|
}
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg("Errore! Impossibile inviare sms. ");
|
|
} else {
|
|
resER.setOk(true);
|
|
resER.setMsg("SMS INVIATI CORRETTAMENTE");
|
|
resER.setResult(content);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
public ResParm controllaEInviaAllarme() {
|
|
ResParm rp = new ResParm();
|
|
if (getApFull().getParm("SMS_SKEBBY_MAILTO_ALLARME").getTesto().isEmpty() ||
|
|
getApFull().getParm("SMS_SKEBBY_MIN_CREDITI_ALLARME").getNumeroLong() <= 0L)
|
|
return rp;
|
|
Parm parm = new Parm(getApFull());
|
|
parm.findByCodice("SMS_SKEBBY_CREDITO_RESIDUO");
|
|
if (parm.getNumeroLong() <= getApFull().getParm("SMS_SKEBBY_MIN_CREDITI_ALLARME").getNumeroLong()) {
|
|
String mailMsg = "Attenzione!!! Credito residuo sotto la soglia di allarme di " + getApFull().getParm("SMS_SKEBBY_MIN_CREDITI_ALLARME").getNumeroLong() + " messaggi.\n\nCredito Residuo: " +
|
|
parm.getNumeroLong() + " messaggi.";
|
|
String subject = "Skebby Sms Credit Alert - " + getApFull().getDataFormat().format(DBAdapter.getToday());
|
|
MailProperties prop = new MailProperties();
|
|
prop.setProperty("TO", getApFull().getParm("SMS_SKEBBY_MAILTO_ALLARME").getTesto());
|
|
prop.setProperty("FROM", getApFull().getParm("FROM").getTesto());
|
|
prop.setProperty("SUBJECT", subject);
|
|
MailMessage mm = new MailMessage(getApFull());
|
|
mm.setTextMessage(mailMsg);
|
|
prop.setProperty("MSG", mm.getMessage());
|
|
prop.setProperty("ISHTML", "true");
|
|
mm.sendMailMessage(prop, false);
|
|
}
|
|
return rp;
|
|
}
|
|
}
|