⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outboundmessage.java

📁 基于java开发的一套短信应用框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib;

import java.util.*;

/**
 * Class representing an outbound sms message.
 */
public class OutboundMessage extends Message
{
	private String recipient;

	private Date dispatchDate;

	private int validityPeriod;

	private boolean statusReport;

	private boolean flashSms;

	private int srcPort;

	private int dstPort;

	private String from;

	private MessageStatuses messageStatus;

	private FailureCauses failureCause;

	private int retryCount;

	private MessagePriorities priority;

	private String refNo;

	/**
	 * Outbound message constructor. This parameterless constructor create an
	 * empty outbound message.
	 * 
	 * @see #OutboundMessage(String, String)
	 */
	public OutboundMessage()
	{
		super(MessageTypes.OUTBOUND, null, null);
		recipient = "";
		validityPeriod = -1;
		statusReport = false;
		flashSms = false;
		srcPort = -1;
		dstPort = -1;
		from = "";
		pid = 0;
		dcs = 0;
		dispatchDate = null;
		setDate(new Date());
		setEncoding(MessageEncodings.ENC7BIT);
		messageStatus = MessageStatuses.UNSENT;
		failureCause = FailureCauses.NO_ERROR;
		retryCount = 0;
		priority = MessagePriorities.NORMAL;
		refNo = "";
	}

	/**
	 * Outbound message constructor.
	 * 
	 * @param recipient
	 *            The recipient of the message.
	 * @param text
	 *            The text of the message.
	 */
	public OutboundMessage(String recipient, String text)
	{
		super(MessageTypes.OUTBOUND, new Date(), text);
		this.recipient = recipient;
		validityPeriod = -1;
		statusReport = false;
		flashSms = false;
		srcPort = -1;
		dstPort = -1;
		from = "";
		pid = 0;
		dcs = 0;
		dispatchDate = null;
		setDate(new Date());
		setEncoding(MessageEncodings.ENC7BIT);
		messageStatus = MessageStatuses.UNSENT;
		failureCause = FailureCauses.NO_ERROR;
		retryCount = 0;
		priority = MessagePriorities.NORMAL;
		refNo = "";
	}

	boolean isBig()
	{
		int messageLength;
		messageLength = getEncodedText().length() / 2;
		return (messageLength > maxSize() ? true : false);
	}

	int getNoOfParts()
	{
		int noOfParts = 0;
		int partSize;
		int messageLength;
		partSize = maxSize() - 8;
		messageLength = getEncodedText().length() / 2;
		noOfParts = messageLength / partSize;
		if ((noOfParts * partSize) < (messageLength)) noOfParts++;
		return noOfParts;
	}

	int maxSize()
	{
		return 140;
	}

	String getPart(int partNo, int udhLength)
	{
		int partSize;
		if (partNo != 0)
		{
			partSize = maxSize() - (udhLength / 2);
			partSize *= 2;
			if (((partSize * (partNo - 1)) + partSize) > getEncodedText().length()) return getEncodedText().substring(partSize * (partNo - 1));
			else 	return getEncodedText().substring(partSize * (partNo - 1), (partSize * (partNo - 1)) + partSize);
		}
		else return getEncodedText();
	}

	String getValidityPeriodBits()
	{
		String bits;
		int value;
		if (validityPeriod == -1) bits = "FF";
		else
		{
			if (validityPeriod <= 12) value = (validityPeriod * 12) - 1;
			else if (validityPeriod <= 24) value = (((validityPeriod - 12) * 2) + 143);
			else if (validityPeriod <= 720) value = (validityPeriod / 24) + 166;
			else value = (validityPeriod / 168) + 192;
			bits = Integer.toHexString(value);
			if (bits.length() != 2) bits = "0" + bits;
			if (bits.length() > 2) bits = "FF";
		}
		return bits;
	}

	String toBCDFormat(String s)
	{
		String bcd, ss;
		int i;
		ss = s;
		if ((ss.length() % 2) != 0) ss = ss + "F";
		bcd = "";
		for (i = 0; i < ss.length(); i += 2)
			bcd = bcd + ss.charAt(i + 1) + ss.charAt(i);
		return bcd;
	}

	String getPDU(String smscNumber, int mpRefNo, int partNo)
	{
		String pdu, udh, ud = "", dataLen = "";
		String str1, str2;
		int ud_length;
		pdu = "";
		udh = "";
		if ((smscNumber != null) && (smscNumber.length() != 0))
		{
			str1 = "91" + toBCDFormat(smscNumber.substring(1));
			str2 = Integer.toHexString(str1.length() / 2);
			if (str2.length() != 2) str2 = "0" + str2;
			pdu = pdu + str2 + str1;
		}
		else if ((smscNumber != null) && (smscNumber.length() == 0)) pdu = pdu + "00";
		if (((srcPort != -1) && (dstPort != -1)) || (isBig()))
		{
			if (statusReport) pdu = pdu + "71";
			else pdu = pdu + "51";
		}
		else
		{
			if (statusReport) pdu = pdu + "31";
			else pdu = pdu + "11";
		}
		pdu = pdu + "00";
		str1 = getRecipient();
		if (str1.charAt(0) == '+')
		{
			str1 = toBCDFormat(str1.substring(1));
			str2 = Integer.toHexString(getRecipient().length() - 1);
			str1 = "91" + str1;
		}
		else
		{
			str1 = toBCDFormat(str1);
			str2 = Integer.toHexString(getRecipient().length());
			str1 = "81" + str1;
		}
		if (str2.length() != 2) str2 = "0" + str2;
		pdu = pdu + str2 + str1;
		{
			String s;
			s = Integer.toHexString(pid);
			while (s.length() < 2)
				s = "0" + s;
			pdu = pdu + s;
		}
		if (getEncoding() == MessageEncodings.ENC7BIT)
		{
			if (flashSms) pdu = pdu + "10";
			else pdu = pdu + "00";
		}
		else if (getEncoding() == MessageEncodings.ENC8BIT)
		{
			if (flashSms) pdu = pdu + "14";
			else pdu = pdu + "04";
		}
		else if (getEncoding() == MessageEncodings.ENCUCS2)
		{
			if (flashSms) pdu = pdu + "18";
			else
			{
				if (getType() == MessageTypes.WAPSI) pdu = pdu + "F5";
				else pdu = pdu + "08";
			}
		}
		else if (getEncoding() == MessageEncodings.ENCCUSTOM)
		{
			String s = Integer.toHexString(dcs);
			while (s.length() < 2)
				s = "0" + s;
			pdu = pdu + s;
		}
		pdu = pdu + getValidityPeriodBits();
		if ((srcPort != -1) && (dstPort != -1))
		{
			String s;
			udh += "060504";
			s = Integer.toHexString(dstPort);
			while (s.length() < 4)
				s = "0" + s;
			udh += s;
			s = Integer.toHexString(srcPort);
			while (s.length() < 4)
				s = "0" + s;
			udh += s;
		}
		if (isBig())
		{
			String s;
			if ((srcPort != -1) && (dstPort != -1)) udh = "0C" + udh.substring(2) + "0804";
			else udh += "060804";
			s = Integer.toHexString(mpRefNo);
			while (s.length() < 4)
				s = "0" + s;
			udh += s;
			s = Integer.toHexString(getNoOfParts());
			while (s.length() < 2)
				s = "0" + s;
			udh += s;
			s = Integer.toHexString(partNo);
			while (s.length() < 2)
				s = "0" + s;
			udh += s;
		}
		if (getEncoding() == MessageEncodings.ENC7BIT)
		{
			ud = getPart(partNo, udh.length());
			ud_length = getText().length() % 8 == 7 ? ud.length() - 1 : ud.length();
			dataLen = Integer.toHexString(((ud_length + udh.length()) * 8 / 7) / 2);
		}
		else if (getEncoding() == MessageEncodings.ENC8BIT)
		{
			ud = getPart(partNo, udh.length());
			dataLen = Integer.toHexString((ud.length() + udh.length()) / 2);
		}
		else if (getEncoding() == MessageEncodings.ENCUCS2)
		{
			ud = getPart(partNo, udh.length());
			dataLen = Integer.toHexString((ud.length() + udh.length()) / 2);
		}
		else if (getEncoding() == MessageEncodings.ENCCUSTOM)
		{
			if ((dcs & 0x04) == 0)
			{
				ud = getPart(partNo, udh.length());
				ud_length = getText().length() % 8 == 7 ? ud.length() - 1 : ud.length();
				dataLen = Integer.toHexString(((ud_length + udh.length()) * 8 / 7) / 2);
			}
			else
			{
				ud = getPart(partNo, udh.length());
				dataLen = Integer.toHexString((ud.length() + udh.length()) / 2);
			}
		}
		if (dataLen.length() != 2) dataLen = "0" + dataLen;
		if (udh.length() != 0) pdu = pdu + dataLen + udh + ud;
		else pdu = pdu + dataLen + ud;
		return pdu.toUpperCase();
	}

	/**
	 * Returns the recipient of this outbound message.
	 * 
	 * @return The recipient of the message.
	 * @see #setRecipient(String)
	 */
	public String getRecipient()
	{
		return recipient;
	}

	/**
	 * Set the recipient of the message.
	 * 
	 * @param recipient
	 *            The recipient of the message.
	 * @see #getRecipient()
	 */
	protected void setRecipient(String recipient)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -