|
18 | 18 | import com.qiniu.utils.InputStreamAt; |
19 | 19 | import com.qiniu.utils.Base64; |
20 | 20 | import com.qiniu.utils.QiniuException; |
| 21 | +import com.qiniu.utils.RetryRet; |
21 | 22 |
|
22 | 23 | public class ResumableClient extends Client { |
23 | 24 | String mUpToken; |
@@ -101,39 +102,77 @@ public void onFailure(QiniuException ex) { |
101 | 102 | return canceler; |
102 | 103 | } |
103 | 104 |
|
104 | | - public ICancel mkblk(InputStreamAt input, long offset, int blockSize, int writeSize, CallRet ret) { |
| 105 | + public ICancel mkblk(final InputStreamAt input, final long offset, final int blockSize, final int writeSize, final CallRet ret) { |
105 | 106 | String url = Conf.UP_HOST + "/mkblk/" + blockSize; |
106 | | - ClientExecutor client = makeClientExecutor(); |
107 | | - call(client, url, input.toHttpEntity(offset, writeSize, client), ret); |
108 | | - return client; |
| 107 | + ClientExecutor executor = makeClientExecutor(); |
| 108 | + CallRet retryRet = new RetryRet(ret){ |
| 109 | + @Override |
| 110 | + public void onFailure(QiniuException ex) { |
| 111 | + if (RetryRet.noRetry(ex)){ |
| 112 | + ret.onFailure(ex); |
| 113 | + return; |
| 114 | + } |
| 115 | + ClientExecutor executor2 = makeClientExecutor(); |
| 116 | + String url2 = Conf.UP_HOST2 + "/mkblk/" + blockSize; |
| 117 | + call(executor2, url2, input.toHttpEntity(offset, writeSize, executor2), ret); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + call(executor, url, input.toHttpEntity(offset, writeSize, executor), retryRet); |
| 122 | + return executor; |
109 | 123 | } |
110 | 124 |
|
111 | 125 | public ICancel bput(String host, InputStreamAt input, String ctx, long blockOffset, long offset, int writeLength, CallRet ret) { |
112 | 126 | String url = host + "/bput/" + ctx + "/" + offset; |
113 | 127 | ClientExecutor client = makeClientExecutor(); |
| 128 | + |
114 | 129 | call(client, url, input.toHttpEntity(blockOffset+offset, writeLength, client), ret); |
115 | 130 | return client; |
116 | 131 | } |
117 | 132 |
|
118 | | - public ICancel mkfile(String key, long fsize, String mimeType, Map<String, String> params, String ctxs, CallRet ret) { |
119 | | - String url = Conf.UP_HOST + "/mkfile/" + fsize; |
| 133 | + public ICancel mkfile(final String key, final long fsize, final String mimeType, final Map<String, String> params, final String ctxs, final CallRet ret) { |
| 134 | + String url = Conf.UP_HOST + mkfilePath(key, fsize, mimeType, params); |
| 135 | + StringEntity entity = null; |
| 136 | + try { |
| 137 | + entity = new StringEntity(ctxs); |
| 138 | + } catch (UnsupportedEncodingException e) { |
| 139 | + e.printStackTrace(); |
| 140 | + ret.onFailure(new QiniuException(QiniuException.InvalidEncode, "mkfile", e)); |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + CallRet retryRet = new RetryRet(ret){ |
| 145 | + @Override |
| 146 | + public void onFailure(QiniuException ex) { |
| 147 | + if (RetryRet.noRetry(ex)){ |
| 148 | + ret.onFailure(ex); |
| 149 | + return; |
| 150 | + } |
| 151 | + String url2 = Conf.UP_HOST2 + mkfilePath(key, fsize, mimeType, params); |
| 152 | + StringEntity entity2 = null; |
| 153 | + try { |
| 154 | + entity2 = new StringEntity(ctxs); |
| 155 | + } catch (UnsupportedEncodingException e) { |
| 156 | + } |
| 157 | + call(makeClientExecutor(), url2, entity2, ret); |
| 158 | + } |
| 159 | + }; |
| 160 | + return call(makeClientExecutor(), url, entity, retryRet); |
| 161 | + } |
| 162 | + |
| 163 | + private static String mkfilePath(String key, long fsize, String mimeType, Map<String, String> params){ |
| 164 | + String path = "/mkfile/" + fsize; |
120 | 165 | if (mimeType != null && mimeType.length() > 0) { |
121 | | - url += "/mimeType/" + Base64.encode(mimeType); |
| 166 | + path += "/mimeType/" + Base64.encode(mimeType); |
122 | 167 | } |
123 | 168 | if (key != null && key.length() > 0) { |
124 | | - url += "/key/" + Base64.encode(key); |
| 169 | + path += "/key/" + Base64.encode(key); |
125 | 170 | } |
126 | 171 | if (params != null && params.size() > 0) { |
127 | 172 | for (Map.Entry<String, String> a: params.entrySet()) { |
128 | | - url += "/" + a.getKey() + "/" + Base64.encode(a.getValue()); |
| 173 | + path += "/" + a.getKey() + "/" + Base64.encode(a.getValue()); |
129 | 174 | } |
130 | 175 | } |
131 | | - try { |
132 | | - return call(makeClientExecutor(), url, new StringEntity(ctxs), ret); |
133 | | - } catch (UnsupportedEncodingException e) { |
134 | | - e.printStackTrace(); |
135 | | - ret.onFailure(new QiniuException(QiniuException.InvalidEncode, "mkfile", e)); |
136 | | - return null; |
137 | | - } |
| 176 | + return path; |
138 | 177 | } |
139 | 178 | } |
0 commit comments