|
| 1 | +package com.qiniu.util; |
| 2 | + |
| 3 | +import com.qiniu.common.Config; |
| 4 | +import com.qiniu.common.QiniuException; |
| 5 | +import com.qiniu.common.Zone; |
| 6 | +import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.Request; |
| 8 | +import okhttp3.Response; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.ConcurrentHashMap; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by Simon on 6/22/16. |
| 17 | + */ |
| 18 | +public class UC { |
| 19 | + private Map<AKBKT, ZoneInfo> zones = new ConcurrentHashMap(); |
| 20 | + |
| 21 | + private OkHttpClient client = new OkHttpClient.Builder() |
| 22 | + .connectTimeout(10000, TimeUnit.SECONDS) |
| 23 | + .readTimeout(10000, TimeUnit.SECONDS) |
| 24 | + .writeTimeout(10000, TimeUnit.SECONDS).build(); |
| 25 | + |
| 26 | + private UC() { |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + private static UC uc = new UC(); |
| 31 | + |
| 32 | + public static Zone zone(String ak, String bkt) throws QiniuException { |
| 33 | + return uc.getZone(Config.zone, ak, bkt, Config.UPLOAD_BY_HTTPS); |
| 34 | + } |
| 35 | + |
| 36 | + public static Zone zone(String uptoken) throws QiniuException { |
| 37 | + try { |
| 38 | + // http://developer.qiniu.com/article/developer/security/upload-token.html |
| 39 | + // http://developer.qiniu.com/article/developer/security/put-policy.html |
| 40 | + String[] strings = uptoken.split(":"); |
| 41 | + String ak = strings[0]; |
| 42 | + String policy = new String(UrlSafeBase64.decode(strings[2]), Config.UTF_8); |
| 43 | + String bkt = Json.decode(policy).get("scope").toString().split(":")[0]; |
| 44 | + return zone(ak, bkt); |
| 45 | + } catch (QiniuException e) { |
| 46 | + throw e; |
| 47 | + } catch (Exception e) { |
| 48 | + throw new QiniuException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public static String ucVal(String ak, String bkt) throws QiniuException { |
| 54 | + // 不解析返回的值,此时 isHttps (Config.UPLOAD_BY_HTTPS) 无实际意义 |
| 55 | + return uc.getUCVal(ak, bkt, Config.UPLOAD_BY_HTTPS); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 清除缓存。比如空间迁移到不同机房后调用此方法 |
| 60 | + */ |
| 61 | + public static void clear() { |
| 62 | + uc.zones.clear(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 返回 java-sdk 中需要的 zone 对象 |
| 67 | + */ |
| 68 | + private Zone getZone(final Zone userSetZone, final String ak, final String bkt, boolean isHttps) |
| 69 | + throws QiniuException { |
| 70 | + if (userSetZone != null) { |
| 71 | + return userSetZone; |
| 72 | + } |
| 73 | + ZoneInfo zoneInfo = getZoneInfo(ak, bkt, isHttps); |
| 74 | + return zoneInfo.zone; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 返回 uc.qbox.me 的原始字符串 |
| 79 | + */ |
| 80 | + public String getUCVal(final String ak, final String bkt, boolean isHttps) throws QiniuException { |
| 81 | + ZoneInfo zoneInfo = getZoneInfo(ak, bkt, isHttps); |
| 82 | + return zoneInfo.ucjson; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + ZoneInfo getZoneInfo(final String ak, final String bkt, boolean isHttps) throws QiniuException { |
| 87 | + final AKBKT akbkt = new AKBKT(ak, bkt, isHttps); |
| 88 | + ZoneInfo zoneInfo = zones.get(akbkt); |
| 89 | + |
| 90 | + if (zoneInfo != null) { |
| 91 | + return zoneInfo; |
| 92 | + } else { |
| 93 | + build(akbkt); |
| 94 | + zoneInfo = zones.get(akbkt); |
| 95 | + return zoneInfo; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void build(AKBKT akbkt) throws QiniuException { |
| 100 | + try { |
| 101 | + String address = Config.UC_HOST + "/v1/query?ak=" + akbkt.ak + "&bucket=" + akbkt.bkt; |
| 102 | + long start = System.currentTimeMillis(); |
| 103 | + Response res = client.newCall(new Request.Builder() |
| 104 | + .url(address) |
| 105 | + .build()).execute(); |
| 106 | + double duration = (System.currentTimeMillis() - start) / 1000.0; |
| 107 | + build(akbkt, res, address, duration); |
| 108 | + } catch (QiniuException e) { |
| 109 | + throw e; |
| 110 | + } catch (Exception e) { |
| 111 | + throw new QiniuException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void build(AKBKT akbkt, Response res, String address, double duration) throws QiniuException { |
| 116 | + com.qiniu.http.Response qnRes = com.qiniu.http.Response.create(res, address, duration); |
| 117 | + if (!qnRes.isOK()) { |
| 118 | + throw new QiniuException(qnRes); |
| 119 | + } |
| 120 | + try { |
| 121 | + String ucVal = qnRes.bodyString(); |
| 122 | + UCRet ret = qnRes.jsonToObject(UCRet.class); |
| 123 | + |
| 124 | + List<String> args = null; |
| 125 | + if (akbkt.isHttps) { |
| 126 | + args = ret.https.get("up"); |
| 127 | + } else { |
| 128 | + args = ret.http.get("up"); |
| 129 | + } |
| 130 | + |
| 131 | + String[] zoneArgs = new String[2]; |
| 132 | + zoneArgs[0] = getZoneHost(args.get(0)); |
| 133 | + if (args.size() > 1) { |
| 134 | + zoneArgs[1] = getZoneHost(args.get(1)); |
| 135 | + } |
| 136 | + if (zoneArgs[1] == null) { |
| 137 | + zoneArgs[1] = zoneArgs[0]; |
| 138 | + } |
| 139 | + |
| 140 | + Zone new_zone = new Zone(zoneArgs[0], zoneArgs[1]); |
| 141 | + |
| 142 | + if (zoneArgs[0] == null || zoneArgs[0].trim().length() == 0 |
| 143 | + || new_zone == null || ucVal == null) { |
| 144 | + throw new QiniuException(qnRes); |
| 145 | + } |
| 146 | + |
| 147 | + zones.put(akbkt, new ZoneInfo(new_zone, ucVal)); |
| 148 | + } catch (QiniuException e) { |
| 149 | + throw e; |
| 150 | + } catch (Exception e) { |
| 151 | + throw new QiniuException(e); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + private String getZoneHost(String p) { |
| 157 | + if (p.startsWith("http")) { |
| 158 | + return p; |
| 159 | + } else { |
| 160 | + return null; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private class AKBKT { |
| 165 | + String ak; |
| 166 | + String bkt; |
| 167 | + boolean isHttps; |
| 168 | + |
| 169 | + AKBKT(String ak, String bkt, boolean isHttps) { |
| 170 | + this.ak = ak.trim(); |
| 171 | + this.bkt = bkt.trim(); |
| 172 | + this.isHttps = isHttps; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public int hashCode() { |
| 177 | + return ak.hashCode() * bkt.hashCode() * (isHttps ? 5 : 1); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean equals(Object obj) { |
| 182 | + if (obj instanceof AKBKT) { |
| 183 | + AKBKT that = (AKBKT) obj; |
| 184 | + return this.bkt.equals(that.bkt) && this.ak.equals(that.ak) && (this.isHttps == that.isHttps); |
| 185 | + } |
| 186 | + return false; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private class ZoneInfo { |
| 191 | + public final String ucjson; |
| 192 | + public final Zone zone; |
| 193 | + |
| 194 | + ZoneInfo(Zone zone, String ucjson) { |
| 195 | + this.zone = zone; |
| 196 | + this.ucjson = ucjson; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private class UCRet { |
| 201 | + Map<String, List<String>> http; |
| 202 | + Map<String, List<String>> https; |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments