Skip to content

Commit

Permalink
Merge pull request #573 from zhangzqs/src_domain
Browse files Browse the repository at this point in the history
KODO-17355: add getDefaultIoSrcHost() to BucketManager class
  • Loading branch information
bachue authored Mar 28, 2023
2 parents 8f71829 + 877ac6a commit a8b5f34
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/main/java/com/qiniu/storage/AutoRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
return region.getIovipHost(regionReqInfo);
}

@Override
String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
if (regionReqInfo == null) {
return "";
}
Region region = queryRegionInfo(regionReqInfo);
return region.getIoSrcHost(regionReqInfo);
}

/**
* 获取资源管理域名
*/
Expand Down Expand Up @@ -296,6 +305,7 @@ private class ServerRets {
ServerRet uc;
ServerRet api;
ServerRet io;
ServerRet io_src;

Region createRegion() {
long timestamp = ttl + System.currentTimeMillis() / 1000;
Expand All @@ -311,6 +321,11 @@ Region createRegion() {
iovipHost = io.getOneHost();
}

String ioSrcHost = null;
if (io_src != null) {
ioSrcHost = io_src.getOneHost();
}

String rsHost = null;
if (rs != null) {
rsHost = rs.getOneHost();
Expand All @@ -337,7 +352,8 @@ Region createRegion() {
regionId = "";
}

return new Region(timestamp, regionId, srcUpHosts, accUpHosts, iovipHost, rsHost, rsfHost, apiHost, ucHost);
return new Region(timestamp, regionId, srcUpHosts, accUpHosts, iovipHost,
ioSrcHost, rsHost, rsfHost, apiHost, ucHost);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,16 @@ public Response getBucketQuotaResponse(String bucket) throws QiniuException {
return res;
}

/**
* 获取 Bucket 的默认源站域名
* @param bucket 空间名
* @return 源站域名
* @throws QiniuException 异常
*/
public String getDefaultIoSrcHost(String bucket) throws QiniuException {
return configHelper.ioSrcHost(auth.accessKey, bucket);
}

/*
* 相关请求的方法列表
* */
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/qiniu/storage/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public String ioHost(String ak, String bucket) throws QiniuException {
return getScheme() + config.region.getIovipHost(regionReqInfo);
}

public String ioSrcHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return config.region.getIoSrcHost(regionReqInfo);
}

public String apiHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getApiHost(regionReqInfo);
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/qiniu/storage/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Region implements Cloneable {
private List<String> srcUpHosts;
private List<String> accUpHosts;
private String iovipHost;
private String ioSrcHost;

/*
* 资源管理,资源列表,资源处理类域名
Expand All @@ -35,12 +36,13 @@ public class Region implements Cloneable {
}

Region(long timestamp, String region, List<String> srcUpHosts, List<String> accUpHosts, String iovipHost,
String rsHost, String rsfHost, String apiHost, String ucHost) {
String ioSrcHost, String rsHost, String rsfHost, String apiHost, String ucHost) {
this.timestamp = timestamp;
this.region = region;
this.srcUpHosts = srcUpHosts;
this.accUpHosts = accUpHosts;
this.iovipHost = iovipHost;
this.ioSrcHost = ioSrcHost;
this.rsHost = rsHost;
this.rsfHost = rsfHost;
this.apiHost = apiHost;
Expand Down Expand Up @@ -314,6 +316,10 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
return iovipHost;
}

String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
return ioSrcHost;
}

String getRsHost(RegionReqInfo regionReqInfo) throws QiniuException {
return rsHost;
}
Expand Down Expand Up @@ -345,6 +351,7 @@ public Object clone() {
newRegion.srcUpHosts = srcUpHosts;
newRegion.accUpHosts = accUpHosts;
newRegion.iovipHost = iovipHost;
newRegion.ioSrcHost = ioSrcHost;
newRegion.rsHost = rsHost;
newRegion.rsfHost = rsfHost;
newRegion.apiHost = apiHost;
Expand All @@ -368,6 +375,7 @@ public Builder(Region originRegion) {
region.srcUpHosts = originRegion.srcUpHosts;
region.accUpHosts = originRegion.accUpHosts;
region.iovipHost = originRegion.iovipHost;
region.ioSrcHost = originRegion.ioSrcHost;
region.rsHost = originRegion.rsHost;
region.rsfHost = originRegion.rsfHost;
region.apiHost = originRegion.apiHost;
Expand Down Expand Up @@ -397,6 +405,11 @@ public Builder iovipHost(String iovipHost) {
return this;
}

public Builder ioSrcHost(String ioSrcHost) {
this.region.ioSrcHost = ioSrcHost;
return this;
}

public Builder rsHost(String rsHost) {
this.region.rsHost = rsHost;
return this;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/qiniu/storage/RegionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException {
}
}

@Override
String getIoSrcHost(RegionReqInfo regionReqInfo) throws QiniuException {
if (currentRegion == null) {
return null;
} else {
return currentRegion.getIoSrcHost(regionReqInfo);
}
}

@Override
String getRsHost(RegionReqInfo regionReqInfo) throws QiniuException {
if (currentRegion == null) {
Expand Down
82 changes: 76 additions & 6 deletions src/test/java/test/com/qiniu/storage/BucketTest2.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package test.com.qiniu.storage;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.*;
import com.qiniu.storage.model.*;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import okhttp3.Call;
import okhttp3.OkHttpClient;
Expand All @@ -26,17 +26,17 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

public class BucketTest2 {

List<Integer> batchStatusCode = Arrays.asList(200, 298);
private BucketManager bucketManager;
private BucketManager dummyBucketManager;
private UploadManager uploadManager;

/**
* 初始化
Expand All @@ -45,9 +45,11 @@ public class BucketTest2 {
*/
@BeforeEach
public void setUp() throws Exception {
Configuration cfg = new Configuration(Zone.zone0());
Configuration cfg = new Configuration();
cfg.region = Region.autoRegion();
// cfg.useHttpsDomains = false;
this.bucketManager = new BucketManager(TestConfig.testAuth, cfg);
this.uploadManager = new UploadManager(cfg);
this.dummyBucketManager = new BucketManager(TestConfig.dummyAuth, new Configuration());
}

Expand Down Expand Up @@ -527,6 +529,74 @@ public void testBucketInfo() {
}
}

/**
* 测试能够获取bucket源站域名并能用于下载
*/
@Test
@Tag("IntegrationTest")
public void testDefaultIoSrcDomainDownload() {
String bucket = TestConfig.testBucket_z0;
String key = "test_bucket_domain_file_key";

// 获取默认源站域名
String domain = "";
try {
domain = bucketManager.getDefaultIoSrcHost(bucket);
assertNotNull(domain, "domain is not null.");
} catch (QiniuException e) {
fail(e);
}

// 构造测试数据
byte[] uploadData = new byte[1024];
new Random().nextBytes(uploadData);

// 将测试数据上传到测试文件
try {
StringMap map = new StringMap();
map.put("insertOnly", "1");
uploadManager.put(
new ByteArrayInputStream(uploadData),
key, TestConfig.testAuth.uploadToken(bucket), map, null);
} catch (QiniuException e) {
fail(e);
}

// 测试文件是否存在
try {
FileInfo info = bucketManager.stat(bucket, key);
assertNotNull(info, "info is not null.");
} catch (QiniuException e) {
fail(e);
}

// 构造下载链接
String url = "";
try {
url = new DownloadUrl(domain, false, key).buildURL();
url = TestConfig.testAuth.privateDownloadUrl(url, 3600);
} catch (QiniuException e) {
fail(e);
}

// 下载测试文件
byte[] downloadData;
Client client = new Client();
try {
downloadData = client.get(url).body();
} catch (QiniuException e) {
throw new RuntimeException(e);
}
assertArrayEquals(uploadData, downloadData);

// 删除测试文件
try {
bucketManager.delete(bucket, key);
} catch (QiniuException e) {
fail(e);
}
}

/**
* 测试设置空间referer防盗链
*/
Expand Down

0 comments on commit a8b5f34

Please sign in to comment.