package com.etonenet.iiie.sdk;
import java.sql.Timestamp;
import java.util.LinkedList;
/**
*Feb 20, 2007
* Zhou JianGuo
* 小白
* 中国电信上海技术研究院
* MSN:zhuojianguo_leo@hotmail.com
*/
public abstract class Sender {
protected int status = -1;
protected String desc = null;
protected String messageId = null;
protected String user = null;
protected String password = null;
protected String serviceType = null;
protected String payer = null;
protected Timestamp time = null;
protected int esmClass = 0;
protected int protocolId = 0;
protected byte[] sm = null;
protected int dataCoding = 0;
protected String url = null;
protected int priorityFlag = 1;
protected boolean debug = true;
public boolean smc = false;
public void setDebug(boolean value) {
debug = value;
}
public void setURL(String url) {
this.url = url;
}
public String getURL() {
return url;
}
public void setProtocolId(int id) {
protocolId = id;
}
public int getProtocolId() {
return protocolId;
}
public void setShortMessage(byte[] sm, int dataCoding, int esmClass, int protocolId) {
this.sm = sm;
this.dataCoding = dataCoding;
this.esmClass = esmClass;
this.protocolId = protocolId;
}
/**
* Set service type.
*
* @param serviceType service type, default null.
*/
public void setServiceType(String serviceType) {
this.serviceType = serviceType;
}
public void setSmc(boolean v) {
smc = v;
}
public boolean getSmc() {
return smc;
}
/**
* Send sms send time.
*
* @param time timstamp, string format "yyyy-MM-dd HH:mm:ss"
*/
public void setTime(Timestamp time) {
this.time = time;
}
public Timestamp getTime() {
return time;
}
/**
* Set the user.
*
* @param user the user name.
*/
public void setUser(String user) {
this.user = user;
}
public String getUser() { return user; }
/**
* Set the password of this user. if the length of password large 32. it will trim to 32.
*
* @param password user password.
*/
public void setPassword(String password) {
if (password == null) {
this.password = "";
} else {
if (password.length() > 32) {
this.password = password.substring(0, 32);
} else {
this.password = password;
}
}
}
public String getPassword() {
return password;
}
/**
* Set fee information
*
* @param payer default null. must at international format.
*/
public void setPayer(String payer) {
this.payer = payer;
}
public String getPayer() {
return payer;
}
public void setPriorityFlag(int pf) {
priorityFlag = pf;
}
public int getPriorityFlag() {
return priorityFlag;
}
/**
* Get last post status.
*/
public int getStatus() {
return status;
}
public String getDescription() {
return desc;
}
public String getMessageId() {
return messageId;
}
/**
* Send sms to special addr through etonenet sms server.
*
* @param addr target phone number
*/
public abstract int send(String addr) throws Exception;
static char baseTable[] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
public static void base64(byte[] arr, StringBuffer sb) {
byte bytes[] = arr;
int n = arr != null ? arr.length : 0;
if (n < 1) return; // no bytes to encode!?!
byte buf[] = new byte[4]; // array of base64 characters
int n3byt = n / 3; // how 3 bytes groups?
int nrest = n % 3; // the remaining bytes from the grouping
int k = n3byt * 3; // we are doing 3 bytes at a time
int linelength = 0; // current linelength
int i = 0; // index
// do the 3-bytes groups ...
while ( i < k ) {
buf[0]= (byte)((bytes[i] & 0xFC) >> 2);
buf[1]= (byte)(((bytes[i] & 0x03) << 4)|((bytes[i+1] & 0xF0)>> 4));
buf[2]= (byte)(((bytes[i+1] & 0x0F)<< 2)|((bytes[i+2] & 0xC0)>> 6));
buf[3]= (byte)(bytes[i+2] & 0x3F);
sb.append(baseTable[buf[0]]);
sb.append(baseTable[buf[1]]);
sb.append(baseTable[buf[2]]);
sb.append(baseTable[buf[3]]);
i += 3;
}
// deals with with the padding ...
if (nrest==2) {
// 2 bytes left
buf[0] = (byte)(( bytes[k] & 0xFC) >> 2);
buf[1] = (byte)(((bytes[k] & 0x03) << 4) |
((bytes[k+1] & 0xF0) >> 4));
buf[2] = (byte)(( bytes[k+1] & 0x0F) << 2);
}
else if (nrest==1) {
// 1 byte left
buf[0] = (byte)((bytes[k] & 0xFC) >> 2);
buf[1] = (byte)((bytes[k] & 0x03) << 4);
}
if (nrest > 0) {
// send the padding
sb.append(baseTable[buf[0]]);
sb.append(baseTable[buf[1]]);
// Thanks to R. Claerman for the bug fix here!
if (nrest==2) {
sb.append(baseTable[buf[2]]);
}
else {
sb.append('=');
}
sb.append('=');
}
}
/**
* Send(group send) message to multi phones.
*
* @param list phone number list
*/
public void send(LinkedList list) throws Exception {
for(int i = 0; i < list.size(); i ++) {
String s = (String)list.get(i);
send(s);
}
}
}