| 16530 |
manish.sha |
1 |
package in.shop2020.util;
|
|
|
2 |
|
|
|
3 |
import java.util.Map;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
|
|
|
6 |
import org.json.JSONArray;
|
|
|
7 |
import org.json.JSONObject;
|
|
|
8 |
import org.json.JSONException;
|
|
|
9 |
|
|
|
10 |
public class SMTPAPI {
|
|
|
11 |
|
|
|
12 |
private static final String VERSION = "1.2.0";
|
|
|
13 |
|
|
|
14 |
private JSONObject header = new JSONObject();
|
|
|
15 |
|
|
|
16 |
public SMTPAPI() {
|
|
|
17 |
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
public SMTPAPI(JSONObject header) {
|
|
|
21 |
this.header = header;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public String getVersion() {
|
|
|
25 |
return VERSION;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
private static String[] toArray(JSONArray json) throws JSONException {
|
|
|
29 |
ArrayList<String> parse = new ArrayList<String>();
|
|
|
30 |
for (int i = 0; i < json.length(); i++) {
|
|
|
31 |
parse.add(json.getString(i));
|
|
|
32 |
}
|
|
|
33 |
return parse.toArray(new String[parse.size()]);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public SMTPAPI addTo(String to) throws JSONException {
|
|
|
37 |
if (!this.header.has("to")) {
|
|
|
38 |
this.header.put("to", new JSONArray());
|
|
|
39 |
}
|
|
|
40 |
this.header.accumulate("to", to);
|
|
|
41 |
return this;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public SMTPAPI addTos(String[] to) throws JSONException {
|
|
|
45 |
for(int i = 0; i < to.length; i ++) {
|
|
|
46 |
addTo(to[i]);
|
|
|
47 |
}
|
|
|
48 |
return this;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public SMTPAPI setTos(String[] to) throws JSONException {
|
|
|
52 |
this.header.put("to", new JSONArray(to));
|
|
|
53 |
return this;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public String[] getTos() throws JSONException {
|
|
|
57 |
return SMTPAPI.toArray(this.header.getJSONArray("to"));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public SMTPAPI addSubstitution(String key, String val) throws JSONException {
|
|
|
61 |
if (this.header.isNull("sub")) {
|
|
|
62 |
this.header.put("sub", new JSONObject());
|
|
|
63 |
}
|
|
|
64 |
JSONObject subs = this.header.getJSONObject("sub");
|
|
|
65 |
if (!subs.has(key)) {
|
|
|
66 |
subs.put(key, new JSONArray());
|
|
|
67 |
}
|
|
|
68 |
subs.accumulate(key, val);
|
|
|
69 |
return this;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public SMTPAPI addSubstitutions(String key, String[] val) throws JSONException {
|
|
|
73 |
for (int i = 0; i < val.length; i++) {
|
|
|
74 |
addSubstitution(key, val[i]);
|
|
|
75 |
}
|
|
|
76 |
return this;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public SMTPAPI setSubstitutions(JSONObject subs) throws JSONException {
|
|
|
80 |
this.header.put("sub", subs);
|
|
|
81 |
return this;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public JSONObject getSubstitutions() throws JSONException {
|
|
|
85 |
return this.header.getJSONObject("sub");
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public SMTPAPI addUniqueArg(String key, String val) throws JSONException {
|
|
|
89 |
if (this.header.isNull("unique_args")) {
|
|
|
90 |
this.header.put("unique_args", new JSONObject());
|
|
|
91 |
}
|
|
|
92 |
this.header.getJSONObject("unique_args").put(key, val);
|
|
|
93 |
return this;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public SMTPAPI setUniqueArgs(Map<String, String> args) throws JSONException {
|
|
|
97 |
this.header.put("unique_args", args);
|
|
|
98 |
return this;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
public SMTPAPI setUniqueArgs(JSONObject args) throws JSONException {
|
|
|
102 |
this.header.put("unique_args", args);
|
|
|
103 |
return this;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public JSONObject getUniqueArgs() throws JSONException {
|
|
|
107 |
return this.header.getJSONObject("unique_args");
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public SMTPAPI addCategory(String val) throws JSONException {
|
|
|
111 |
if (!this.header.has("category")) {
|
|
|
112 |
this.header.put("category", new JSONArray());
|
|
|
113 |
}
|
|
|
114 |
this.header.accumulate("category", val);
|
|
|
115 |
return this;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public SMTPAPI addCategories(String[] vals) throws JSONException {
|
|
|
119 |
for (int i = 0; i < vals.length; i++) {
|
|
|
120 |
addCategory(vals[i]);
|
|
|
121 |
}
|
|
|
122 |
return this;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public SMTPAPI setCategories(String[] cat) throws JSONException {
|
|
|
126 |
this.header.put("category", cat);
|
|
|
127 |
return this;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public String[] getCategories() throws JSONException {
|
|
|
131 |
return SMTPAPI.toArray(this.header.getJSONArray("category"));
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public SMTPAPI addSection(String key, String val) throws JSONException {
|
|
|
135 |
if (this.header.isNull("section")) {
|
|
|
136 |
this.header.put("section", new JSONObject());
|
|
|
137 |
}
|
|
|
138 |
this.header.getJSONObject("section").put(key, val);
|
|
|
139 |
return this;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public SMTPAPI setSections(Map<String, String> sec) throws JSONException {
|
|
|
143 |
return this.setSections(new JSONObject(sec));
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
public SMTPAPI setSections(JSONObject sec) throws JSONException {
|
|
|
148 |
this.header.put("section", sec);
|
|
|
149 |
return this;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public JSONObject getSections() throws JSONException {
|
|
|
153 |
return this.header.getJSONObject("section");
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
public SMTPAPI addFilter(String filter, String setting, String val) throws JSONException {
|
|
|
157 |
if (this.header.isNull("filters")) {
|
|
|
158 |
this.header.put("filters", new JSONObject());
|
|
|
159 |
}
|
|
|
160 |
if (this.header.getJSONObject("filters").isNull(filter)) {
|
|
|
161 |
this.header.getJSONObject("filters").put(filter, new JSONObject());
|
|
|
162 |
this.header.getJSONObject("filters").getJSONObject(filter).put("settings", new JSONObject());
|
|
|
163 |
}
|
|
|
164 |
this.header.getJSONObject("filters").getJSONObject(filter).getJSONObject("settings").put(setting, val);
|
|
|
165 |
return this;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public SMTPAPI addFilter(String filter, String setting, int val) throws JSONException {
|
|
|
169 |
if (this.header.isNull("filters")) {
|
|
|
170 |
this.header.put("filters", new JSONObject());
|
|
|
171 |
}
|
|
|
172 |
if (this.header.getJSONObject("filters").isNull(filter)) {
|
|
|
173 |
this.header.getJSONObject("filters").put(filter, new JSONObject());
|
|
|
174 |
this.header.getJSONObject("filters").getJSONObject(filter).put("settings", new JSONObject());
|
|
|
175 |
}
|
|
|
176 |
this.header.getJSONObject("filters").getJSONObject(filter).getJSONObject("settings").put(setting, val);
|
|
|
177 |
return this;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
public SMTPAPI setFilters(JSONObject filters) throws JSONException {
|
|
|
181 |
this.header.put("filters", filters);
|
|
|
182 |
return this;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public JSONObject getFilters() throws JSONException {
|
|
|
186 |
return this.header.getJSONObject("filters");
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
public SMTPAPI setASMGroupId(int val) throws JSONException{
|
|
|
190 |
this.header.put("asm_group_id", val);
|
|
|
191 |
return this;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public Integer getASMGroupId() throws JSONException{
|
|
|
195 |
return this.header.has("asm_group_id") ? this.header.optInt("asm_group_id") : null;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
public SMTPAPI setSendAt(int sendAt) throws JSONException {
|
|
|
199 |
this.header.put("send_at", sendAt);
|
|
|
200 |
return this;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
public int getSendAt() throws JSONException {
|
|
|
204 |
return this.header.getInt("send_at");
|
|
|
205 |
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
// convert from string to code point array
|
|
|
209 |
private int[] toCodePointArray(String input) {
|
|
|
210 |
int len = input.length();
|
|
|
211 |
int[] codePointArray = new int[input.codePointCount(0, len)];
|
|
|
212 |
for (int i = 0, j = 0; i < len; i = input.offsetByCodePoints(i, 1)) {
|
|
|
213 |
codePointArray[j++] = input.codePointAt(i);
|
|
|
214 |
}
|
|
|
215 |
return codePointArray;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
private String escapeUnicode(String input) {
|
|
|
219 |
StringBuilder sb = new StringBuilder();
|
|
|
220 |
int[] codePointArray = toCodePointArray(input);
|
|
|
221 |
int len = codePointArray.length;
|
|
|
222 |
for (int i = 0; i < len; i++) {
|
|
|
223 |
if (codePointArray[i] > 65535) {
|
|
|
224 |
// surrogate pair
|
|
|
225 |
int hi = (codePointArray[i] - 0x10000) / 0x400 + 0xD800;
|
|
|
226 |
int lo = (codePointArray[i] - 0x10000) % 0x400 + 0xDC00;
|
|
|
227 |
sb.append(String.format("\\u%04x\\u%04x", hi, lo));
|
|
|
228 |
} else if (codePointArray[i] > 127) {
|
|
|
229 |
sb.append(String.format("\\u%04x",codePointArray[i]));
|
|
|
230 |
} else {
|
|
|
231 |
sb.append(String.format("%c", codePointArray[i]));
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
return sb.toString();
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
public String jsonString() {
|
|
|
238 |
return escapeUnicode(this.header.toString());
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public String rawJsonString() {
|
|
|
242 |
return this.header.toString();
|
|
|
243 |
}
|
|
|
244 |
}
|