Administrator
2018-10-30 7bf6a0582c7c62c90ee2ed8a88654f11d0479092
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.qcloud.cmq;
 
import java.util.TreeMap;
import java.util.Vector;
 
import com.qcloud.cmq.Json.JSONArray;
import com.qcloud.cmq.Json.JSONObject;
 
/**
 * TODO subscription class.
 *
 * @author York.
 *         Created 2016年9月27日.
 */
public class Subscription {
    protected String topicName;
    protected String subscriptionName;
    protected CMQClient client;
    /**
     * TODO construct .
     *
     * @param topicName
     * @param subscriptionName
     * @param client
     */
    Subscription(final String topicName , final String subscriptionName,CMQClient client)
    {
        this.topicName = topicName;
        this.subscriptionName = subscriptionName;
        this.client = client;    
    }
    public void ClearFilterTags() throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
 
        param.put("topicName",this.topicName);
        param.put("subscriptionName", this.subscriptionName);
        String result = this.client.call("ClearSUbscriptionFIlterTags",param);
        
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    
    }
    /**
     * TODO set subscription attributes.
     *
     * @param meta SubscriptionMeata object
     * @throws Exception
     */
    public void SetSubscriptionAttributes(SubscriptionMeta meta) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
 
        param.put("topicName",this.topicName);
        param.put("subscriptionName", this.subscriptionName);
        if( !meta.NotifyStrategy.equals(""))
            param.put("notifyStrategy",meta.NotifyStrategy);
        if( !meta.NotifyContentFormat.equals(""))
            param.put("notifyContentFormat", meta.NotifyContentFormat);
        if( meta.FilterTag != null )
        {
            int n = 1 ;
            for(String flag : meta.FilterTag)
            {
                param.put("filterTag."+Integer.toString(n), flag);
                ++n;
            }
        }
        if( meta.bindingKey != null )
        {
            int n = 1 ;
            for(String flag : meta.bindingKey)
            {
                param.put("bindingKey."+Integer.toString(n), flag);
                ++n;
            }
        }
    
        String result = this.client.call("SetSubscriptionAttributes", param);
        
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
    
    /**
     * TODO get subscription attributes.
     *
     * @return  subscription meta object
     * @throws Exception
     */
    public SubscriptionMeta getSubscriptionAttributes() throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
 
        param.put("topicName",this.topicName);
        param.put("subscriptionName", this.subscriptionName);
        
        String result = this.client.call("GetSubscriptionAttributes", param);
        
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
 
        SubscriptionMeta meta = new SubscriptionMeta();
        meta.FilterTag = new Vector<String>();
        if(jsonObj.has("endpoint"))
            meta.Endpoint = jsonObj.getString("endpoint");
        if(jsonObj.has("notifyStrategy"))
            meta.NotifyStrategy = jsonObj.getString("notifyStrategy");
        if(jsonObj.has("notifyContTentFormat"))
            meta.NotifyContentFormat = jsonObj.getString("notifyContTentFormat");
        if(jsonObj.has("protocol"))
            meta.Protocal = jsonObj.getString("protocol");
        if(jsonObj.has("createTime"))
            meta.CreateTime = jsonObj.getInt("createTime");
        if(jsonObj.has("lastModifyTime"))
            meta.LastModifyTime = jsonObj.getInt("lastModifyTime");
        if(jsonObj.has("msgCount"))
           meta.msgCount = jsonObj.getInt("msgCount");
        if(jsonObj.has("filterTag"))
        {
            JSONArray jsonArray = jsonObj.getJSONArray("filterTag");
             for(int i=0;i<jsonArray.length();i++)
            {    
                JSONObject obj = (JSONObject)jsonArray.get(i);
                meta.FilterTag.add(obj.toString());
            } 
        }
        if(jsonObj.has("bindingKey"))
        {
            JSONArray jsonArray = jsonObj.getJSONArray("bindingKey");
             for(int i=0;i<jsonArray.length();i++)
            {    
                JSONObject obj = (JSONObject)jsonArray.get(i);
                meta.bindingKey.add(obj.toString());
            } 
        }
    
        return meta;
    }
    
}