China Telecom SMS(SGIP)

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Properties;

import sgip.tool.Bind;
import sgip.tool.BindResp;
import sgip.tool.SGIP_Command;
import sgip.tool.Submit;
import sgip.tool.SubmitResp;
import sgip.tool.Unbind;

import com.xx.entity.Message;
import com.xx.util.MessageUtil;

public class ChinaTelecomConnector implements Connector{
	public static final String UCS2 = "ISO-10646-UCS-2";
	
	private static final String ip = "SERVERIP";
	private static final String port = "PORT";
	private static final String username = "USERNAME";
	private static final String password = "PASSWORD";
	private static final String companypid = "COMPANYPID";
	private static final String spid = "SPID";
	
	private Socket socket = null;
	private String serverIp;
	private int serverPort;
	private String loginUsername;
	private String loginPassword;
	private Long loginCompanyId;
	private String loginSPID;
	
	private String chargerNumber;
	
	//public ChinaTelecomConnector(){}
	//由spring注入一些參數
	public ChinaTelecomConnector(Properties properties){
		intSocketParamters(properties);
	}
	
	
	private  void intSocketParamters(Properties properties){
		if(properties.containsKey(ip)){
			serverIp = (String) properties.get(ip);
		}
		if(properties.containsKey(port)){
			serverPort = Integer.parseInt((String) properties.get(port));
		}
		if(properties.containsKey(username)){
			loginUsername = (String) properties.get(username);
		}
		if(properties.containsKey(password)){
			loginPassword = properties.getProperty(password);
		}
		
		if(properties.containsKey(companypid)){
			loginCompanyId = Long.parseLong((String) properties.get(companypid));
		}
		
		if(properties.containsKey(spid)){
			loginSPID = (String) properties.get(spid);
			chargerNumber = loginSPID;
		}
		
		//socket = new Socket(serverIp, serverPort)
	}
	
	
	/**
	 * 每一個Submit提交最後可以提交140字節,也是70個漢字,如果發送的是普通短信,TP_udhi爲0,如果爲長短信tp_udhi爲1
	 * 發送長短信時,要將短信息UCS2編碼,然後拆分成 67個字,也就是134個字段爲一條短信用submit提交,因爲還有6個字節要加th_udhiHead做爲長短信的標識
	 * 這裏結果,設爲只要成功發送長信息中的一條短信成功,就返true
	 */
	@Override
	public  boolean sendSms(Message message,int count,String number) {
		// TODO Auto-generated method stub
		boolean bool = false;
		try {
			System.out.println(chargerNumber + " - " + loginSPID);
			//String sendContent = changeCharset(message.getContent(),GBK);
			String sendContent = message.getContent();
			socket = new Socket(serverIp, serverPort);
			OutputStream out = socket.getOutputStream();
			InputStream input = socket.getInputStream();
			SGIP_Command sgipCommand = new SGIP_Command();
			SGIP_Command tmp = null;
			BindResp bindResp = null;
			Submit submit = null;
			SubmitResp submitResp = null;
			Unbind unbind = null;
			
			//Step 1 -- Bind
			Bind bind = new Bind(Long.parseLong(loginSPID),1, loginUsername, loginPassword);
			bind.write(out);
			
			tmp = sgipCommand.read(input);
			if(tmp.getCommandID() == SGIP_Command.ID_SGIP_BIND_RESP){
				bindResp = (BindResp) tmp;
				bindResp.readbody();
				if(bindResp.GetResult() == 0){
					
				}
			}
			
			//Step 2 Submit
			//System.out.println("Content Length " + sendContent.length() );
			String[] messages = MessageUtil.splitMessage(sendContent);
			submit = new Submit(Long.parseLong(loginSPID),// node id
					loginSPID,//########################################SPNumber
					chargerNumber,//###############################################ChargeNumbers
					count,//############################################UserCount
					number,//###########################################UserNumbers
					loginCompanyId+"",//#######################################CorporateId corpId
					"",//##########################################ServiceType
					0,//################################################FeeType
					"0",//##############################################FeeValue
					"0",//##############################################GivenValue
					0,//################################################AgentFlag
					0,//################################################MorelatetoMTFlag
					0,//################################################Priority
					"",//###############################################ExpireTime
					"",//###############################################ScheduleTime
					1,//################################################ReportFlag
					0,//################################################TP_pid
					messages.length > 1 ? 1 : 0,//######################TP_udhi
					8,//################################################MessageCoding
					0,//################################################MessageType
					sendContent.length(),//####################MessageLength 最後兩個字段的內容可以說是多餘的,真正發送信息的是submit.setContent(8,ucs2String)發法
					sendContent//##############################MessageContent
					);
			//submit.setBinContent(8, sendContent.getBytes());
			
			
			byte[] allContentByte = sendContent.getBytes("ISO-10646-UCS-2");
			//System.out.println("byte Length " + allContentByte.length);
			int maxMsgLen = 140;
			
			for(int i=0;i<messages.length;i++){
				byte[] shortMessageUCSE = messages[i].getBytes("ISO-10646-UCS-2");
				byte[] tp_udhiHead = getTPUDHIHead(messages.length, i+1);// Get Tp_udhiHead
				byte[] contentBytes = null;
				if(messages.length > 1){
					//contentBytes = addByte(shortMessageUCSE, tp_udhiHead);
					//拆分短信字節和tp_udhiHead拼成submit需要提交字節,然後把轉成字條串,再調用submit.setContent方法
					contentBytes = getSpliteLongMessageBytes(tp_udhiHead,allContentByte,i*(maxMsgLen - 6),(i+1)*(maxMsgLen-6));
				}else{
					contentBytes = shortMessageUCSE;
				}
				String ucs2String = new String(contentBytes,"ISO8859-1");
				submit.setContent(8,ucs2String);
				submit.write(out);
				out.flush();
				tmp = sgipCommand.read(input);
				if(tmp.getCommandID() == SGIP_Command.ID_SGIP_SUBMIT_RESP){
					submitResp = (SubmitResp) tmp;
					submitResp.readbody();
					if(submitResp.getResult() == 0){
						bool = true;//Successful Send
					}
					/*else{
						System.out.println(submitResp.getResult());
						bool = false;
					}*/
				}
			}
			
			//Step 3 Unbind
			unbind = new Unbind(Long.parseLong(loginSPID));
			unbind.write(out);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return bool;
	}

	@Override
	public String getResponse() {return null;}

	
	public static String changeCharset(String str, String newCharset) {
		String returnStr = null;
		try {
			if (str != null) {
				byte[] bs = str.getBytes();
				returnStr =  new String(bs, newCharset);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public void setLoginSPID(String loginSPID) {
		this.loginSPID = loginSPID;
	}

	public void setChargerNumber(String chargerNumber) {
		this.chargerNumber = chargerNumber;
	}
	
	
	public static byte[] getSpliteLongMessageBytes(byte[] tpUdhiHead,byte[] messageUCS2 ,int start,int end){
		if(end > messageUCS2.length){
			end = messageUCS2.length; 
		}
		byte[] msgb = new byte[end - start + 6];
		System.arraycopy(tpUdhiHead, 0, msgb, 0, 6);
		System.arraycopy(messageUCS2, start, msgb, 6, end - start);
		return msgb;
		
	}
	
	public static byte[] getTPUDHIHead(int messageLength,int number){
		byte[] tp_udhiHead = new byte[6];
		tp_udhiHead[0] = 0x05;
		tp_udhiHead[1] = 0x00;
		tp_udhiHead[2] = 0x03;
		tp_udhiHead[3] = 0x0A;
		tp_udhiHead[4] = (byte)messageLength;//長短信總長度
		tp_udhiHead[5] = (byte)number;//第幾條短信
		return tp_udhiHead;
	}
	
	public static byte[] addByte(byte[] tp_udhiHead,byte[] content){
		int allLength = tp_udhiHead.length + content.length;
		byte[] udhi = new byte[allLength];
		for(int i = 0;i< tp_udhiHead.length;i++){
			udhi[i] = tp_udhiHead[i];
		}
		for(int j=0;j<content.length;j++){
			udhi[tp_udhiHead.length+j] = content[j];
		}
		return udhi;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章