Skip to content

Commit 6bde74f

Browse files
authored
Merge pull request #427 from pasocon1993/master
add sms tools
2 parents 6c05f3f + 2fa2b8f commit 6bde74f

File tree

12 files changed

+813
-1
lines changed

12 files changed

+813
-1
lines changed

examples/SendMessageDemo.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.http.Response;
3+
import com.qiniu.sms.SmsManager;
4+
import com.qiniu.util.Auth;
5+
6+
public class SendMessageDemo {
7+
public static void main(String args[]) {
8+
//设置需要操作的账号的AK和SK
9+
String ACCESS_KEY = "test";
10+
String SECRET_KEY = "test";
11+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
12+
13+
14+
//实例化一个SmsManager对象
15+
SmsManager smsManager = new SmsManager(auth);
16+
17+
18+
19+
try {
20+
// Map<String,String> map = new HashMap<String,String>();
21+
// Response resp = smsManager.sendMessage("templateId", new String[] {"10086"}, map);
22+
// Response resp = smsManager.describeSignature("passed",0,0);
23+
// SignatureInfo sinfo = smsManager.describeSignatureItems("rejected",0,0);
24+
// Response resp = smsManager.createSignature("signature", "app", new String[] {"data:image/gif;base64,xxxxxxxxxx"});
25+
Response resp = smsManager.describeTemplate("passed",0,0);
26+
// Response resp = smsManager.createTemplate("name","template","notification","test","signatureId");
27+
// Response resp = smsManager.modifyTemplate("templateId","name","template","test","signatureId");
28+
// Response resp = smsManager.modifySignature("SignatureId","signature");
29+
// Response resp = smsManager.deleteSignature("signatureId");
30+
// Response resp = smsManager.deleteTemplate("templateId");
31+
System.out.println(resp.bodyString());
32+
} catch (QiniuException e) {
33+
System.out.println(e);
34+
}
35+
36+
37+
}
38+
}

src/main/java/com/qiniu/http/Client.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public Client(Configuration cfg) {
4444
cfg.dispatcherMaxRequests, cfg.dispatcherMaxRequestsPerHost,
4545
cfg.connectionPoolMaxIdleCount, cfg.connectionPoolMaxIdleMinutes);
4646
}
47-
47+
48+
public Client(com.qiniu.sms.Configuration cfg) {
49+
this(null, false, null,
50+
cfg.connectTimeout, cfg.readTimeout, cfg.writeTimeout,
51+
cfg.dispatcherMaxRequests, cfg.dispatcherMaxRequestsPerHost,
52+
cfg.connectionPoolMaxIdleCount, cfg.connectionPoolMaxIdleMinutes);
53+
}
54+
4855
/**
4956
* 构建一个自定义配置的 HTTP Client 类
5057
*/
@@ -175,6 +182,17 @@ public Response post(String url, byte[] body, StringMap headers, String contentT
175182
}
176183
return post(url, rbody, headers);
177184
}
185+
186+
public Response put(String url, byte[] body, StringMap headers, String contentType) throws QiniuException {
187+
RequestBody rbody;
188+
if (body != null && body.length > 0) {
189+
MediaType t = MediaType.parse(contentType);
190+
rbody = RequestBody.create(t, body);
191+
} else {
192+
rbody = RequestBody.create(null, new byte[0]);
193+
}
194+
return put(url, rbody, headers);
195+
}
178196

179197
public Response post(String url, byte[] body, int offset, int size,
180198
StringMap headers, String contentType) throws QiniuException {
@@ -192,6 +210,11 @@ private Response post(String url, RequestBody body, StringMap headers) throws Qi
192210
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
193211
return send(requestBuilder, headers);
194212
}
213+
214+
private Response put(String url, RequestBody body, StringMap headers) throws QiniuException {
215+
Request.Builder requestBuilder = new Request.Builder().url(url).put(body);
216+
return send(requestBuilder, headers);
217+
}
195218

196219
public Response multipartPost(String url,
197220
StringMap fields,
@@ -306,6 +329,7 @@ public void accept(String key, Object value) {
306329
e.printStackTrace();
307330
throw new QiniuException(e);
308331
}
332+
309333
r = Response.create(res, tag.ip, duration);
310334
if (r.statusCode >= 300) {
311335
throw new QiniuException(r);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.qiniu.http;
2+
3+
public enum MethodType {
4+
GET(false),
5+
PUT(true),
6+
POST(true),
7+
DELETE(false),
8+
HEAD(false),
9+
OPTIONS(false);
10+
11+
private boolean hasContent;
12+
13+
MethodType(boolean hasContent) {
14+
this.hasContent = hasContent;
15+
}
16+
17+
public boolean hasContent() {
18+
return hasContent;
19+
}
20+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.qiniu.sms;
2+
3+
import com.qiniu.common.Constants;
4+
5+
public class Configuration implements Cloneable{
6+
/**
7+
* 连接超时时间 单位秒(默认10s)
8+
*/
9+
public int connectTimeout = Constants.CONNECT_TIMEOUT;
10+
/**
11+
* 写超时时间 单位秒(默认 0 , 不超时)
12+
*/
13+
public int writeTimeout = Constants.WRITE_TIMEOUT;
14+
/**
15+
* 回复超时时间 单位秒(默认30s)
16+
*/
17+
public int readTimeout = Constants.READ_TIMEOUT;
18+
/**
19+
* 底层HTTP库所有的并发执行的请求数量
20+
*/
21+
public int dispatcherMaxRequests = Constants.DISPATCHER_MAX_REQUESTS;
22+
/**
23+
* 底层HTTP库对每个独立的Host进行并发请求的数量
24+
*/
25+
public int dispatcherMaxRequestsPerHost = Constants.DISPATCHER_MAX_REQUESTS_PER_HOST;
26+
/**
27+
* 底层HTTP库中复用连接对象的最大空闲数量
28+
*/
29+
public int connectionPoolMaxIdleCount = Constants.CONNECTION_POOL_MAX_IDLE_COUNT;
30+
/**
31+
* 底层HTTP库中复用连接对象的回收周期(单位分钟)
32+
*/
33+
public int connectionPoolMaxIdleMinutes = Constants.CONNECTION_POOL_MAX_IDLE_MINUTES;
34+
/**
35+
* 上传失败重试次数
36+
*/
37+
public int retryMax = 5;
38+
39+
/*
40+
* 特殊默认域名
41+
*/
42+
public static String defaultSmsHost = "https://sms.qiniuapi.com";
43+
44+
public Configuration() {
45+
}
46+
47+
public Configuration clone() {
48+
try {
49+
return (Configuration) super.clone();
50+
} catch (CloneNotSupportedException e) {
51+
e.printStackTrace();
52+
}
53+
return null;
54+
}
55+
56+
public String smsHost() {
57+
return defaultSmsHost;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)