diff --git a/src/main/java/com/qiniu/common/Constants.java b/src/main/java/com/qiniu/common/Constants.java index 0e9e85e42..cf539e252 100644 --- a/src/main/java/com/qiniu/common/Constants.java +++ b/src/main/java/com/qiniu/common/Constants.java @@ -9,7 +9,7 @@ public final class Constants { /** * 版本号 */ - public static final String VERSION = "7.2.16"; + public static final String VERSION = "7.2.17"; /** * 块大小,不能改变 */ diff --git a/src/main/java/com/qiniu/storage/BucketManager.java b/src/main/java/com/qiniu/storage/BucketManager.java index b9763352b..515baf65c 100644 --- a/src/main/java/com/qiniu/storage/BucketManager.java +++ b/src/main/java/com/qiniu/storage/BucketManager.java @@ -4,9 +4,7 @@ import com.qiniu.common.QiniuException; import com.qiniu.http.Client; import com.qiniu.http.Response; -import com.qiniu.storage.model.FetchRet; -import com.qiniu.storage.model.FileInfo; -import com.qiniu.storage.model.FileListing; +import com.qiniu.storage.model.*; import com.qiniu.util.*; import java.util.ArrayList; @@ -98,7 +96,7 @@ public String[] buckets() throws QiniuException { return r.jsonToObject(String[].class); } - public void createBucket(String bucketName, String region) throws Exception { + public void createBucket(String bucketName, String region) throws QiniuException { String url = String.format("%s/mkbucketv2/%s/region/%s", configuration.rsHost(), UrlSafeBase64.encodeToString(bucketName), region); post(url, null).close(); @@ -233,14 +231,6 @@ public Response changeHeaders(String bucket, String key, Map hea } - //存储类型 - public enum StorageType { - //普通存储 - COMMON, - //低频存储 - INFREQUENCY - } - /** * 修改文件的类型(普通存储或低频存储) * @@ -510,6 +500,33 @@ public Response deleteAfterDays(String bucket, String key, int days) throws Qini return rsPost(bucket, String.format("/deleteAfterDays/%s/%d", encodedEntry(bucket, key), days), null); } + public void setBucketAcl(String bucket, AclType acl) throws QiniuException { + String url = String.format("%s/private?bucket=%s&private=%s", configuration.ucHost(), bucket, acl.getType()); + Response res = post(url, null); + res.close(); + if (!res.isOK()) { + throw new QiniuException(res); + } + } + + public BucketInfo getBucketInfo(String bucket) throws QiniuException { + String url = String.format("%s/v2/bucketInfo?bucket=%s", configuration.ucHost(), bucket); + Response res = post(url, null); + if (!res.isOK()) { + res.close(); + throw new QiniuException(res); + } + BucketInfo info = res.jsonToObject(BucketInfo.class); + return info; + } + + + public void setIndexPage(String bucket, IndexPageType type) throws QiniuException { + String url = String.format("%s/noIndexPage?bucket=%s&noIndexPage=%s", configuration.ucHost(), bucket, type.getType()); + Response res = post(url, null); + } + + /* * 相关请求的方法列表 * */ @@ -722,4 +739,5 @@ public void remove() { throw new UnsupportedOperationException("remove"); } } + } diff --git a/src/main/java/com/qiniu/storage/Configuration.java b/src/main/java/com/qiniu/storage/Configuration.java index 9c80c0f9a..0b8903eb5 100644 --- a/src/main/java/com/qiniu/storage/Configuration.java +++ b/src/main/java/com/qiniu/storage/Configuration.java @@ -17,9 +17,9 @@ public final class Configuration implements Cloneable { */ public Zone zone; /** - * 空间相关上传管理操作是否使用 https , 默认否 + * 空间相关上传管理操作是否使用 https , 默认 是 */ - public boolean useHttpsDomains = false; + public boolean useHttpsDomains = true; /** * 如果文件大小大于此值则使用断点上传, 否则使用Form上传 */ @@ -74,6 +74,7 @@ public final class Configuration implements Cloneable { */ public static String defaultRsHost = "rs.qiniu.com"; public static String defaultApiHost = "api.qiniu.com"; + public static String defaultUcHost = "uc.qbox.me"; public Configuration() { @@ -165,4 +166,12 @@ public String rsfHost(String ak, String bucket) { return useHttpsDomains ? zone.getRsfHttps(zoneReqInfo) : zone.getRsfHttp(zoneReqInfo); } + + public String ucHost() { + String scheme = "http://"; + if (useHttpsDomains) { + scheme = "https://"; + } + return scheme + defaultUcHost; + } } diff --git a/src/main/java/com/qiniu/storage/model/AclType.java b/src/main/java/com/qiniu/storage/model/AclType.java new file mode 100644 index 000000000..2c3e8d2ba --- /dev/null +++ b/src/main/java/com/qiniu/storage/model/AclType.java @@ -0,0 +1,25 @@ +package com.qiniu.storage.model; + +/** + * 空间类型:公开空间、私有空间 + */ +public enum AclType { + /** + * 公开空间 + */ + PUBLIC(0), + /** + * 私有空间 + */ + PRIVATE(1); + + private int type = 0; + + AclType(int t) { + type = t; + } + + public int getType() { + return type; + } +} diff --git a/src/main/java/com/qiniu/storage/model/BucketInfo.java b/src/main/java/com/qiniu/storage/model/BucketInfo.java new file mode 100644 index 000000000..7070c6165 --- /dev/null +++ b/src/main/java/com/qiniu/storage/model/BucketInfo.java @@ -0,0 +1,112 @@ +package com.qiniu.storage.model; + +import com.google.gson.annotations.SerializedName; + +import java.util.Map; + +public class BucketInfo { + @SerializedName("source") + private String imageSource; + @SerializedName("host") + private String imageHost; + @SerializedName("protected") + private int _protected; + @SerializedName("private") + private int _private; + @SerializedName("no_index_page") + private int noIndexPage; + private int imgsft; + private String separator; + private Map styles; + private String zone; + private String region; + private boolean global; + + public String getImageSource() { + return imageSource; + } + + public void setImageSource(String imageSource) { + this.imageSource = imageSource; + } + + public String getImageHost() { + return imageHost; + } + + public void setImageHost(String imageHost) { + this.imageHost = imageHost; + } + + public int getProtected() { + return _protected; + } + + public void setProtected(int _protected) { + this._protected = _protected; + } + + public int getPrivate() { + return _private; + } + + public void setPrivate(int _private) { + this._private = _private; + } + + public int getNoIndexPage() { + return noIndexPage; + } + + public void setNoIndexPage(int noIndexPage) { + this.noIndexPage = noIndexPage; + } + + public int getImgsft() { + return imgsft; + } + + public void setImgsft(int imgsft) { + this.imgsft = imgsft; + } + + public String getSeparator() { + return separator; + } + + public void setSeparator(String separator) { + this.separator = separator; + } + + public Map getStyles() { + return styles; + } + + public void setStyles(Map styles) { + this.styles = styles; + } + + public String getZone() { + return zone; + } + + public void setZone(String zone) { + this.zone = zone; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + public boolean isGlobal() { + return global; + } + + public void setGlobal(boolean global) { + this.global = global; + } +} diff --git a/src/main/java/com/qiniu/storage/model/IndexPageType.java b/src/main/java/com/qiniu/storage/model/IndexPageType.java new file mode 100644 index 000000000..f8a333110 --- /dev/null +++ b/src/main/java/com/qiniu/storage/model/IndexPageType.java @@ -0,0 +1,16 @@ +package com.qiniu.storage.model; + +public enum IndexPageType { + HAS(0), + NO(1); + + private int type = 0; + + IndexPageType(int t) { + type = t; + } + + public int getType() { + return type; + } +} diff --git a/src/main/java/com/qiniu/storage/model/StorageType.java b/src/main/java/com/qiniu/storage/model/StorageType.java new file mode 100644 index 000000000..0d7114207 --- /dev/null +++ b/src/main/java/com/qiniu/storage/model/StorageType.java @@ -0,0 +1,15 @@ +package com.qiniu.storage.model; + +/** + * 存储类型 + */ +public enum StorageType { + /** + * 普通存储 + */ + COMMON, + /** + * 低频存储 + */ + INFREQUENCY +} diff --git a/src/test/java/test/com/qiniu/DnsTest.java b/src/test/java/test/com/qiniu/DnsTest.java index b45ba80ea..86b2f195b 100644 --- a/src/test/java/test/com/qiniu/DnsTest.java +++ b/src/test/java/test/com/qiniu/DnsTest.java @@ -35,17 +35,17 @@ public void setUp() throws UnknownHostException { @Override public List lookup(String hostname) throws UnknownHostException { InetAddress[] ips; - try{ - Domain domain = new Domain(hostname, true, true); - ips = dnsClient.queryInetAddress(domain); - } catch (IOException e) { - e.printStackTrace(); - throw new UnknownHostException(e.getMessage()); - } + try { + Domain domain = new Domain(hostname, true, true); + ips = dnsClient.queryInetAddress(domain); + } catch (IOException e) { + e.printStackTrace(); + throw new UnknownHostException(e.getMessage()); + } if (ips == null) { - throw new UnknownHostException(hostname + " resolve failed."); - } - List l = new ArrayList<>(ips.length); + throw new UnknownHostException(hostname + " resolve failed."); + } + List l = new ArrayList<>(ips.length); Collections.addAll(l, ips); return l; } diff --git a/src/test/java/test/com/qiniu/storage/BucketTest.java b/src/test/java/test/com/qiniu/storage/BucketTest.java index 173614d39..939fb9962 100644 --- a/src/test/java/test/com/qiniu/storage/BucketTest.java +++ b/src/test/java/test/com/qiniu/storage/BucketTest.java @@ -1,17 +1,16 @@ package test.com.qiniu.storage; -import com.qiniu.storage.BucketManager; -import com.qiniu.storage.Configuration; -import test.com.qiniu.TestConfig; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.Configuration; import com.qiniu.storage.model.*; -import com.qiniu.storage.BucketManager.StorageType; import com.qiniu.util.StringUtils; import junit.framework.TestCase; import org.junit.Assert; import org.junit.Test; +import test.com.qiniu.TestConfig; import java.text.SimpleDateFormat; import java.util.*; @@ -26,8 +25,8 @@ public class BucketTest extends TestCase { protected void setUp() throws Exception { //default config Configuration cfg = new Configuration(); - //cfg.useHttpsDomains = true; - + cfg.useHttpsDomains = false; +// cfg.useHttpsDomains = true; this.bucketManager = new BucketManager(TestConfig.testAuth, cfg); //na0 config @@ -652,17 +651,72 @@ public void testChangeFileType() { try { bucketManager.copy(bucket, key, bucket, keyToChangeType); Response response = bucketManager.changeType(bucket, keyToChangeType, - BucketManager.StorageType.INFREQUENCY); + StorageType.INFREQUENCY); Assert.assertEquals(200, response.statusCode); //stat FileInfo fileInfo = bucketManager.stat(bucket, keyToChangeType); - Assert.assertEquals(BucketManager.StorageType.INFREQUENCY.ordinal(), fileInfo.type); + Assert.assertEquals(StorageType.INFREQUENCY.ordinal(), fileInfo.type); //delete the temp file bucketManager.delete(bucket, keyToChangeType); } catch (QiniuException e) { fail(bucket + ":" + key + " > " + keyToChangeType + " >> " - + BucketManager.StorageType.INFREQUENCY + " ==> " + e.response.toString()); + + StorageType.INFREQUENCY + " ==> " + e.response.toString()); + } + } + } + + @Test + public void testAcl() throws QiniuException { + bucketManager.setBucketAcl("javasdk", AclType.PRIVATE); + BucketInfo info = bucketManager.getBucketInfo("javasdk"); + Assert.assertEquals(1, info.getPrivate()); + + bucketManager.setBucketAcl("javasdk", AclType.PUBLIC); + info = bucketManager.getBucketInfo("javasdk"); + Assert.assertEquals(0, info.getPrivate()); + + try { + bucketManager.setBucketAcl("javsfsdfsfsdfsdfsdfasdk1", AclType.PRIVATE); +// Assert.fail(" 空间 javasdk2 不存在,理应报错 "); // kodo 实际响应 200 + } catch (QiniuException e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testBucketInfo() throws QiniuException { + BucketInfo info = bucketManager.getBucketInfo("javasdk"); + System.out.println(info.getRegion()); + System.out.println(info.getZone()); + System.out.println(info.getPrivate()); + try { + BucketInfo info2 = bucketManager.getBucketInfo("javasdk2"); + Assert.fail(" 空间 javasdk2 不存在,理应报错 "); + } catch (QiniuException e) { + if (e.response != null) { + System.out.println(e.response.getInfo()); + throw e; } } } + + @Test + public void testIndexPage() throws QiniuException { + bucketManager.setIndexPage("javasdk", IndexPageType.HAS); + BucketInfo info = bucketManager.getBucketInfo("javasdk"); + Assert.assertEquals(0, info.getNoIndexPage()); + + bucketManager.setIndexPage("javasdk", IndexPageType.NO); + info = bucketManager.getBucketInfo("javasdk"); + Assert.assertEquals(1, info.getNoIndexPage()); + + try { + bucketManager.setIndexPage("javasdk2", IndexPageType.HAS); +// Assert.fail(" 空间 javasdk2 不存在,理应报错 "); // kodo 实际响应 200 + } catch (QiniuException e) { + e.printStackTrace(); + throw e; + } + } }