Skip to content

Commit 71edcbb

Browse files
author
YangSen-qn
committed
api add auth auth type
1 parent 4312d5e commit 71edcbb

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

src/main/java/com/qiniu/storage/Api.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected Api(Client client) {
4444
* @param client 请求的 Client 【必须】
4545
* @param config 请求的流程的配置信息
4646
**/
47-
Api(Client client, Config config) {
47+
protected Api(Client client, Config config) {
4848
this(client, Api.createInterceptors(config));
4949
}
5050

@@ -54,7 +54,7 @@ protected Api(Client client) {
5454
* @param client 请求的 Client【必须】
5555
* @param interceptors 请求的拦截器
5656
**/
57-
Api(Client client, Interceptor... interceptors) {
57+
protected Api(Client client, Interceptor... interceptors) {
5858
if (client == null) {
5959
client = new Client();
6060
}
@@ -333,6 +333,16 @@ public Config build() {
333333
*/
334334
public static class Request implements Cloneable {
335335

336+
protected static final int AuthTypeNone = 0;
337+
protected static final int AuthTypeQiniu = 1;
338+
339+
/**
340+
* 鉴权类型
341+
* 0:不鉴权
342+
* 1:Qiniu 鉴权
343+
*/
344+
private int authType = AuthTypeNone;
345+
336346
/**
337347
* 请求的 scheme
338348
* eg: https
@@ -444,6 +454,19 @@ protected Request(String scheme, String host) {
444454
this.host = host;
445455
}
446456

457+
/**
458+
* 设置鉴权类型
459+
* 0:不鉴权
460+
* 1:Qiniu 鉴权
461+
*/
462+
protected int getAuthType() {
463+
return this.authType;
464+
}
465+
466+
protected void setAuthType(int authType) {
467+
this.authType = authType;
468+
}
469+
447470
/**
448471
* 获取请求的 urlPrefix, scheme + host
449472
* eg: https://upload.qiniu.com
@@ -483,6 +506,18 @@ protected void addPathSegment(String segment) {
483506
path = null;
484507
}
485508

509+
protected void addPathSegment(Boolean segment) {
510+
addPathSegment((Boolean) segment ? "true" : "false");
511+
}
512+
513+
protected void addPathSegment(Integer segment) {
514+
addPathSegment(segment + "");
515+
}
516+
517+
protected void addPathSegment(Long segment) {
518+
addPathSegment(segment + "");
519+
}
520+
486521
String getMethodString() {
487522
if (method == null) {
488523
return "GET";
@@ -1114,15 +1149,17 @@ protected Response(com.qiniu.http.Response response) throws QiniuException {
11141149
return;
11151150
}
11161151

1117-
String bodyString = response.bodyString();
1118-
try {
1119-
if (bodyString.startsWith("[")) {
1120-
this.dataArray = Json.decodeArray(bodyString);
1121-
} else {
1122-
this.dataMap = Json.decode(bodyString);
1152+
if (response.isJson()) {
1153+
String bodyString = response.bodyString();
1154+
try {
1155+
if (bodyString.startsWith("[")) {
1156+
this.dataArray = Json.decodeArray(bodyString);
1157+
} else {
1158+
this.dataMap = Json.decode(bodyString);
1159+
}
1160+
} catch (Exception e) {
1161+
e.printStackTrace();
11231162
}
1124-
} catch (Exception e) {
1125-
e.printStackTrace();
11261163
}
11271164
}
11281165

src/main/java/com/qiniu/storage/ApiInterceptorAuth.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@ Api.Response intercept(Api.Request request, Api.Handler handler) throws QiniuExc
2626
return handler.handle(request);
2727
}
2828

29-
String url = request.getUrl().toString();
30-
String method = request.getMethodString();
31-
Headers headers = Headers.of(request.getHeader());
32-
byte[] body = request.getBytesBody();
33-
String authorization = "Qiniu " + auth.signQiniuAuthorization(url, method, body, headers);
34-
request.addHeaderField("Authorization", authorization);
29+
if (request.getAuthType() == Api.Request.AuthTypeQiniu) {
30+
String url = request.getUrl().toString();
31+
String method = request.getMethodString();
32+
Headers headers = Headers.of(request.getHeader());
33+
byte[] body = request.getBytesBody();
34+
String authorization = "Qiniu " + auth.signQiniuAuthorization(url, method, body, headers);
35+
request.addHeaderField("Authorization", authorization);
36+
}
37+
3538
return handler.handle(request);
3639
}
3740

3841
static final class Builder {
42+
3943
private Auth auth;
4044

4145
Builder setAuth(Auth auth) {
4246
this.auth = auth;
4347
return this;
4448
}
4549

50+
Builder setAuthType(int authType) {
51+
return this;
52+
}
53+
4654
Api.Interceptor build() {
4755
return new ApiInterceptorAuth(auth);
4856
}

src/main/java/com/qiniu/storage/BucketManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ private Response get(String url) throws QiniuException {
11681168

11691169
private Response get(String url, Api.Interceptor[] interceptors) throws QiniuException {
11701170
Api.Request request = new Api.Request(url);
1171+
request.setAuthType(Api.Request.AuthTypeQiniu);
11711172
return new Api(client, interceptors).requestWithInterceptor(request);
11721173
}
11731174

@@ -1178,6 +1179,7 @@ private Response post(String url, byte[] body) throws QiniuException {
11781179

11791180
private Response post(String url, byte[] body, Api.Interceptor[] interceptors) throws QiniuException {
11801181
Api.Request request = new Api.Request(url);
1182+
request.setAuthType(Api.Request.AuthTypeQiniu);
11811183
request.setMethod(MethodType.POST);
11821184
if (body == null) {
11831185
body = new byte[0];

0 commit comments

Comments
 (0)