Skip to content

Commit

Permalink
Merge branch 'master' of YangSen-qn:qiniu/java-sdk
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/com/qiniu/storage/Region.java
  • Loading branch information
YangSen-qn committed Nov 23, 2023
2 parents e477179 + ad6a208 commit 86c735d
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 69 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qiniu.common;

import java.io.File;
import java.nio.charset.Charset;

/**
Expand Down Expand Up @@ -47,7 +48,28 @@ public final class Constants {
*/
public static final int CONNECTION_POOL_MAX_IDLE_MINUTES = 5;

public static final String CACHE_DIR = getCacheDir();

private Constants() {
}

private static String getCacheDir() {
String tmpDir = System.getProperty("java.io.tmpdir");
if (tmpDir == null || tmpDir.isEmpty()) {
return null;
}

String qiniuDir = tmpDir + "com.qiniu.java-sdk";
File dir = new File(qiniuDir);
if (!dir.exists()) {
return dir.mkdirs() ? qiniuDir : null;
}

if (dir.isDirectory()) {
return qiniuDir;
}

return null;
}
}

13 changes: 12 additions & 1 deletion src/main/java/com/qiniu/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ public static Response create(okhttp3.Response response, String address, double
error = e.getMessage();
}
}
} else if (response.body() != null) {
// 处理其他 body 非预期情况
if (response.code() >= 300) {
try {
body = response.body().bytes();
error = new String(body, Constants.UTF_8);
} catch (Exception e) {
error = e.getMessage();
}
}
}

return new Response(response, code, reqId, response.header("X-Log"), via(response),
address, duration, error, body);
}
Expand Down Expand Up @@ -248,7 +259,7 @@ public synchronized InputStream bodyStream() throws QiniuException {
}

public synchronized void close() {
if (this.response != null) {
if (this.response != null && this.response.body() != null) {
this.response.close();
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,12 @@ public StringMap getHeader() throws QiniuException {
header.put(key, this.header.get(key));
}

if (header.keySet().contains("Content-Type")) {
if (body == null || body.contentType == null
|| header.keySet().contains("Content-Type")) {
return header;
}

if (body != null) {
header.put("Content-Type", body.contentType.toString());
}
header.put("Content-Type", body.contentType.toString());

return header;
}
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/com/qiniu/storage/AutoRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlUtils;
import com.qiniu.util.*;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -27,7 +26,9 @@ class AutoRegion extends Region {
/**
* 全局空间信息缓存,此缓存绑定了 token、bucket,全局有效。
*/
private static Map<String, UCRet> globalRegionCache = new ConcurrentHashMap<>();
private static final Cache<UCRet> globalRegionCache = new Cache.Builder<>(UCRet.class)
.setVersion("v1")
.builder();

/**
* 定义HTTP请求管理相关方法
Expand Down Expand Up @@ -75,8 +76,8 @@ private AutoRegion() {
* 通过 API 接口查询上传域名
*/
private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuException {
String cacheKey = index.accessKey + index.bucket;
UCRet ret = globalRegionCache.get(cacheKey);
String cacheKey = getCacheId(index);
UCRet ret = globalRegionCache.cacheForKey(cacheKey);
if (ret != null && ret.isValid()) {
return ret;
}
Expand All @@ -94,7 +95,7 @@ private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuE
ret = r.jsonToObject(UCRet.class);
if (ret != null) {
ret.setupDeadline();
globalRegionCache.put(cacheKey, ret);
globalRegionCache.cache(cacheKey, ret);
}
return ret;
}
Expand Down Expand Up @@ -122,7 +123,7 @@ static Region regionGroup(UCRet ret) {
*/
private Region queryRegionInfo(String accessKey, String bucket) throws QiniuException {
RegionIndex index = new RegionIndex(accessKey, bucket);
String cacheKey = index.accessKey + "::" + index.bucket;
String cacheKey = getCacheId(index);
Region region = regions.get(cacheKey);

Exception ex = null;
Expand Down Expand Up @@ -311,6 +312,22 @@ String[] getUcHostArray() throws QiniuException {
return getUcHosts(null).toArray(new String[0]);
}

private String getCacheId(RegionIndex index) {
StringBuilder builder = new StringBuilder()
.append(index.accessKey)
.append("-")
.append(index.bucket);

if (ucServers != null && !ucServers.isEmpty()) {
for (String host : ucServers) {
if (host != null && !host.isEmpty()) {
builder.append(host);
}
}
}

return UrlSafeBase64.encodeToString(builder.toString());
}

public Object clone() {
AutoRegion newRegion = new AutoRegion();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public FileListing listFiles(String bucket, String prefix, String marker, int li
throws QiniuException {
Response response = listV1(bucket, prefix, marker, limit, delimiter);
if (!response.isOK()) {
response.close();
throw new QiniuException(response);
}
FileListing fileListing = response.jsonToObject(FileListing.class);
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/com/qiniu/storage/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,44 @@ public String tryChangeUpHost(String upToken, String lastUsedHost) throws QiniuE

private String upHost(String upToken, String lastUsedHost, boolean changeHost, boolean mustReturnUpHost)
throws QiniuException {
return getScheme()
+ getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
String host = getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String ioHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getIovipHost(regionReqInfo);
String host = config.region.getIovipHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

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

public String apiHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getApiHost(regionReqInfo);
String host = config.region.getApiHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getRsHost(regionReqInfo);
String host = config.region.getRsHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsfHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getRsfHost(regionReqInfo);
String host = config.region.getRsfHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsHost() {
Expand All @@ -78,7 +89,8 @@ public String rsHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultRsHost;
}
return getScheme() + host;
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String apiHost() {
Expand All @@ -91,7 +103,8 @@ public String apiHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultApiHost;
}
return getScheme() + host;
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String ucHost() {
Expand All @@ -104,7 +117,9 @@ public String ucHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultUcHost;
}
return getScheme() + host;

host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

List<String> ucHostsWithoutScheme() {
Expand Down Expand Up @@ -151,10 +166,6 @@ List<String> upHostsWithoutScheme() {
return new ArrayList<>(hosts);
}

private String getScheme() {
return config.useHttpsDomains ? "https://" : "http://";
}

private void makeSureRegion() {
if (config.region == null) {
if (config.zone != null) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/qiniu/storage/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public final class Configuration implements Cloneable {
*/
public static String defaultRsHost = "rs.qiniu.com";
public static String defaultApiHost = "api.qiniu.com";
public static String defaultUcHost = "kodo-config.qiniuapi.com";
public static String defaultUcHost = "uc.qiniuapi.com";
static final String ucBackUpHost0 = "kodo-config.qiniuapi.com";
static final String ucBackUpHost1 = "uc.qbox.me";

static final String ucBackUpHost0 = "uc.qbox.me";
static final String[] defaultUcHosts = new String[]{defaultUcHost, ucBackUpHost0, defaultApiHost};
static final String[] defaultUcHosts = new String[]{defaultUcHost, ucBackUpHost0, ucBackUpHost1};

/**
* 使用的Region
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/qiniu/storage/DownloadUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ protected void didBuildUrl() throws QiniuException {
}

private String getUrlPrefix() throws QiniuException {
if (useHttps) {
return "https://" + domain;
} else {
return "http://" + domain;
}
return UrlUtils.setHostScheme(domain, useHttps);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/qiniu/storage/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ public static Region regionAs0() {
* @return Region 实例
*/
public static Region createWithRegionId(String regionId) {
if (regionId == null || regionId.isEmpty()) {
return null;
}

if (regionId.equals("z0")) {
return Region.region0();
}

return new Builder()
.region(regionId)
.srcUpHost("up-" + regionId + ".qiniup.com")
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/qiniu/storage/UpHostHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
regionHostsLRU.put(regionKey, regionHost);
}

String host = regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
return host;
return regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
}

private String failedUpHost(String regionKey) {
Expand Down
Loading

0 comments on commit 86c735d

Please sign in to comment.