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

📄 serviceindication.java

📁 MM7彩信对接网关示例
💻 JAVA
字号:
package com.rainbow.mms.golden.base;

import java.io.ByteArrayOutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * 
 * 从C#程序翻译而来的ServiceIndication指令部分
 */
public class ServiceIndication {
    public final static int SERVICEINDICATION_NotSet = 0;
    public final static int SERVICEINDICATION_signal_none = 1;
    public final static int SERVICEINDICATION_signal_low = 2;
    public final static int SERVICEINDICATION_signal_medium = 3;
    public final static int SERVICEINDICATION_signal_high = 4;
    public final static int SERVICEINDICATION_delete = 5;

    // Well known DTD token
    public static byte DOCUMENT_DTD_ServiceIndication = 0x05;            // ServiceIndication 1.0 Public Identifier

    // Tag Tokens
    public static byte TAGTOKEN_si = 0x5;
    public static byte TAGTOKEN_indication = 0x6;
    public static byte TAGTOKEN_info = 0x7;
    public static byte TAGTOKEN_item = 0x8;


    //service indication
    public static byte ATTRIBUTESTARTTOKEN_action_signal_none = (byte)0x5;
    public static byte ATTRIBUTESTARTTOKEN_action_signal_low = (byte)0x6;
    public static byte ATTRIBUTESTARTTOKEN_action_signal_medium = (byte)0x7;
    public static byte ATTRIBUTESTARTTOKEN_action_signal_high = (byte)0x8;
    public static byte ATTRIBUTESTARTTOKEN_action_signal_delete = (byte)0x9;
    public static byte ATTRIBUTESTARTTOKEN_created = (byte)0xA;
    public static byte ATTRIBUTESTARTTOKEN_href = (byte)0xB;
    public static byte ATTRIBUTESTARTTOKEN_href_http = (byte)0xC;            // http://
    public static byte ATTRIBUTESTARTTOKEN_href_http_www = (byte)0xD;        // http://www.
    public static byte ATTRIBUTESTARTTOKEN_href_https = (byte)0xE;           // https://
    public static byte ATTRIBUTESTARTTOKEN_href_https_www = (byte)0xE;       // https://www.
    public static byte ATTRIBUTESTARTTOKEN_si_expires = (byte)0x10;
    public static byte ATTRIBUTESTARTTOKEN_si_id = (byte)0x11;
    public static byte ATTRIBUTESTARTTOKEN_class = (byte)0x12;

    // Attribute Value Tokens
    public static byte ATTRIBUTEVALUETOKEN_com = (byte)0x85;                        // .com/
    public static byte ATTRIBUTEVALUETOKEN_edu = (byte)0x86;                        // .edu/
    public static byte ATTRIBUTEVALUETOKEN_net = (byte)0x87;                        // .net/
    public static byte ATTRIBUTEVALUETOKEN_org = (byte)0x88;                        // .org/

    private static Hashtable hrefStartTokens;
    private static Hashtable attributeValueTokens;

    public String Href;
    public String Text;
    public Date CreatedAt;
    public Date ExpiresAt;

    public int Action;      //ServiceIndicationAction

    static{
        hrefStartTokens = new Hashtable();
        hrefStartTokens.put("https://www.", new Byte(ATTRIBUTESTARTTOKEN_href_https_www));
        hrefStartTokens.put("http://www.", new Byte(ATTRIBUTESTARTTOKEN_href_http_www));
        hrefStartTokens.put("https://", new Byte(ATTRIBUTESTARTTOKEN_href_https));
        hrefStartTokens.put("http://", new Byte(ATTRIBUTESTARTTOKEN_href_http));

        attributeValueTokens = new Hashtable();
        attributeValueTokens.put(".com/", new Byte(ATTRIBUTEVALUETOKEN_com));
        attributeValueTokens.put(".edu/", new Byte(ATTRIBUTEVALUETOKEN_edu));
        attributeValueTokens.put(".net/", new Byte(ATTRIBUTEVALUETOKEN_net));
        attributeValueTokens.put(".org/", new Byte(ATTRIBUTEVALUETOKEN_org));
    }

    public ServiceIndication(String href, String text, int action){
        this.Href = href;
        this.Text = text;
        this.Action = action;
    }

    public ServiceIndication(String href, String text, Date createdAt, Date expiresAt){
        this(href, text, ServiceIndication.SERVICEINDICATION_NotSet);
        this.CreatedAt = createdAt;
        this.ExpiresAt = expiresAt;
    }


    public ServiceIndication(String href, String text, Date createdAt, Date expiresAt, int action){
        this (href, text, action);
        this.CreatedAt = createdAt;
        this.ExpiresAt = expiresAt;
    }


    /**
     * 生成字节数组表示的Service Indication
     * @return 字节数组表示的Service Indication
     */
    public byte[] GetWBXMLBytes()
    {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        // wbxml headers
        stream.write(WBXML.VERSION_1_1);
        stream.write(DOCUMENT_DTD_ServiceIndication);
        stream.write(WBXML.CHARSET_UTF_8);
        stream.write(WBXML.NULL);

        // start xml doc
        stream.write(WBXML.SetTagTokenIndications(TAGTOKEN_si, false, true));
        stream.write(WBXML.SetTagTokenIndications(TAGTOKEN_indication, true , true));

        // href attribute
        // this attribute has some well known start tokens that
        // are contained within a static hashtable. Iterate through
        // the table and chose the token.
        int i = 0;
        byte hrefTagToken = ATTRIBUTESTARTTOKEN_href;
        Enumeration keys = hrefStartTokens.keys();

        while(keys.hasMoreElements()){
            String startString = (String)keys.nextElement();
            if (this.Href.startsWith(startString)){
                hrefTagToken = ((Byte)hrefStartTokens.get(startString)).byteValue();
                i = startString.length();
                break;
            }
        }
        stream.write(hrefTagToken);

        WriteInlineString(stream, this.Href.substring(i));

        if (this.Action != ServiceIndication.SERVICEINDICATION_NotSet)
            stream.write(GetActionToken(this.Action));

        // close indication element attributes
        stream.write(WBXML.TAGTOKEN_END);

        // text of indication element
        WriteInlineString(stream, this.Text);

        // close indication element
        stream.write(WBXML.TAGTOKEN_END);
        // close si element
        stream.write(WBXML.TAGTOKEN_END);

        return stream.toByteArray();
    }


    /**
     * 获取Action的标志位
     * @param ServiceIndication Action
     * @return ServiceIndication Action的byte表示
     */
    public byte GetActionToken(int action)
    {
        switch (action)
        {
            case ServiceIndication.SERVICEINDICATION_delete:
                return ATTRIBUTESTARTTOKEN_action_signal_delete;

            case ServiceIndication.SERVICEINDICATION_signal_high:
                return ATTRIBUTESTARTTOKEN_action_signal_high;

            case ServiceIndication.SERVICEINDICATION_signal_low:
                return ATTRIBUTESTARTTOKEN_action_signal_low;

            case ServiceIndication.SERVICEINDICATION_signal_medium:
                return ATTRIBUTESTARTTOKEN_action_signal_medium;

            default :
                return ATTRIBUTESTARTTOKEN_action_signal_none;
        }
    }

    /// <summary>
    /// Encodes an inline string into the stream using UTF8 encoding
    /// </summary>
    /// <param name="stream">The target stream</param>
    /// <param name="text">The text to write</param>
    /**
     * Encodes an inline string into the stream using UTF8 encoding
     * @param stream the targe stream
     * @param text the text to write
     */
    protected void WriteInlineString(ByteArrayOutputStream stream, String text)
    {
        try{
            // indicate that the follow bytes comprise a string
            stream.write(WBXML.TOKEN_INLINE_STRING_FOLLOWS);

            // write character bytes
            byte[] buffer = text.getBytes("UTF-8");
            stream.write(buffer, 0, buffer.length);

            // end is indicated by a null byte
            stream.write(WBXML.NULL);
        }catch(Exception e){
           e.printStackTrace();
        }
    }


    /**
     *Encodes the DateTime to the stream.
     * DateTimes are encoded as Opaque Data with each number in the string represented
     * by its 4-bit binary value
     * eg: 1999-04-30 06:40:00
     * is encoded as 199904300640.
     * Trailing zero values are not included.
     * @param stream Target stream
     * @param date Date to encode
     */
    protected void WriteDate(ByteArrayOutputStream stream, Date date)
    {
        byte[] buffer = new byte[7];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        buffer[0] = (byte)(calendar.get(Calendar.YEAR) / 100);
        buffer[1] = (byte)(calendar.get(Calendar.YEAR) % 100);
        buffer[2] = (byte)calendar.get(Calendar.MONTH);
        buffer[3] = (byte)calendar.get(Calendar.DAY_OF_MONTH);

        int dateLength = 4;

        if (calendar.get(Calendar.HOUR) > 0)
        {
            buffer[4] = (byte)calendar.get(Calendar.HOUR);
            dateLength = 5;
        }

        if (calendar.get(Calendar.MINUTE) > 0)
        {
            buffer[5] = (byte)calendar.get(Calendar.MINUTE);
            dateLength = 6;
        }

        if (calendar.get(Calendar.SECOND) > 0)
        {
            buffer[6] = (byte)calendar.get(Calendar.SECOND);
            dateLength = 7;
        }

        // write to stream
        stream.write(WBXML.TOKEN_OPAQUEDATA_FOLLOWS);
        stream.write((byte)dateLength);
        stream.write(buffer, 0, dateLength);
    }
}

⌨️ 快捷键说明

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