@@ -90,6 +90,56 @@ public ObjectRes getMeta(@NotNull final String hash) throws IOException {
90
90
), Operation .Download );
91
91
}
92
92
93
+ /**
94
+ * Upload object with specified hash and size.
95
+ *
96
+ * @param streamProvider Object stream provider.
97
+ * @param hash Object hash.
98
+ * @param size Object size.
99
+ * @return Return true is object is uploaded successfully and false if object is already uploaded.
100
+ * @throws IOException On some errors.
101
+ */
102
+ public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final String hash , final long size ) throws IOException {
103
+ return putObject (streamProvider , new Meta (hash , size ));
104
+ }
105
+
106
+ /**
107
+ * Upload object with specified hash and size.
108
+ *
109
+ * @param streamProvider Object stream provider.
110
+ * @param meta Object metadata.
111
+ * @return Return true is object is uploaded successfully and false if object is already uploaded.
112
+ * @throws IOException On some errors.
113
+ */
114
+ public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final Meta meta ) throws IOException {
115
+ return doWork (auth -> {
116
+ final ObjectRes links = doRequest (auth , new MetaPost (meta ), AuthHelper .join (auth .getHref (), PATH_OBJECTS ));
117
+ return links != null && putObject (streamProvider , meta , links );
118
+ }, Operation .Upload );
119
+ }
120
+
121
+ protected <T > T doWork (@ NotNull Work <T > work , @ NotNull Operation operation ) throws IOException {
122
+ Link auth = authProvider .getAuth (operation );
123
+ int authCount = 0 ;
124
+ while (true ) {
125
+ try {
126
+ return work .exec (auth );
127
+ } catch (UnauthorizedException | ForbiddenException e ) {
128
+ if (authCount >= MAX_AUTH_COUNT ) {
129
+ throw e ;
130
+ }
131
+ authCount ++;
132
+ // Get new authentication data.
133
+ authProvider .invalidateAuth (operation , auth );
134
+ final Link newAuth = authProvider .getAuth (operation );
135
+ if (newAuth .getHeader ().equals (auth .getHeader ()) && newAuth .getHref ().equals (auth .getHref ())) {
136
+ throw e ;
137
+ }
138
+ auth = newAuth ;
139
+ }
140
+ }
141
+ }
142
+
93
143
/**
94
144
* Get metadata for object by hash.
95
145
*
@@ -191,104 +241,6 @@ public boolean putObject(@NotNull final StreamProvider streamProvider) throws IO
191
241
return putObject (streamProvider , generateMeta (streamProvider ));
192
242
}
193
243
194
- /**
195
- * Generate object metadata.
196
- *
197
- * @param streamProvider Object stream provider.
198
- * @return Return object metadata.
199
- * @throws IOException On some errors.
200
- */
201
- public static Meta generateMeta (@ NotNull final StreamProvider streamProvider ) throws IOException {
202
- final MessageDigest digest = sha256 ();
203
- final byte [] buffer = new byte [0x10000 ];
204
- long size = 0 ;
205
- try (InputStream stream = streamProvider .getStream ()) {
206
- while (true ) {
207
- int read = stream .read (buffer );
208
- if (read <= 0 ) break ;
209
- digest .update (buffer , 0 , read );
210
- size += read ;
211
- }
212
- }
213
- return new Meta (new String (Hex .encodeHex (digest .digest ())), size );
214
- }
215
-
216
- /**
217
- * Upload object with specified hash and size.
218
- *
219
- * @param streamProvider Object stream provider.
220
- * @param hash Object hash.
221
- * @param size Object size.
222
- * @return Return true is object is uploaded successfully and false if object is already uploaded.
223
- * @throws IOException On some errors.
224
- */
225
- public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final String hash , final long size ) throws IOException {
226
- return putObject (streamProvider , new Meta (hash , size ));
227
- }
228
-
229
- /**
230
- * Upload object with specified hash and size.
231
- *
232
- * @param streamProvider Object stream provider.
233
- * @param meta Object metadata.
234
- * @return Return true is object is uploaded successfully and false if object is already uploaded.
235
- * @throws IOException On some errors.
236
- */
237
- public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final Meta meta ) throws IOException {
238
- return doWork (auth -> {
239
- final ObjectRes links = doRequest (auth , new MetaPost (meta ), AuthHelper .join (auth .getHref (), PATH_OBJECTS ));
240
- return links != null && putObject (streamProvider , meta , links );
241
- }, Operation .Upload );
242
- }
243
-
244
- /**
245
- * Upload object by metadata.
246
- *
247
- * @param links Object links.
248
- * @param streamProvider Object stream provider.
249
- * @param meta Object metadata.
250
- * @return Return true is object is uploaded successfully and false if object is already uploaded.
251
- * @throws IOException On some errors.
252
- */
253
- public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final Meta meta , @ NotNull final Links links ) throws IOException {
254
- if (links .getLinks ().containsKey (LinkType .Download )) {
255
- return false ;
256
- }
257
- final Link uploadLink = links .getLinks ().get (LinkType .Upload );
258
- if (uploadLink == null ) {
259
- throw new IOException ("Upload link not found" );
260
- }
261
- doRequest (uploadLink , new ObjectPut (streamProvider , meta .getSize ()), uploadLink .getHref ());
262
-
263
- final Link verifyLink = links .getLinks ().get (LinkType .Verify );
264
- if (verifyLink != null ) {
265
- doRequest (verifyLink , new ObjectVerify (meta ), verifyLink .getHref ());
266
- }
267
- return true ;
268
- }
269
-
270
- protected <T > T doWork (@ NotNull Work <T > work , @ NotNull Operation operation ) throws IOException {
271
- Link auth = authProvider .getAuth (operation );
272
- int authCount = 0 ;
273
- while (true ) {
274
- try {
275
- return work .exec (auth );
276
- } catch (UnauthorizedException | ForbiddenException e ) {
277
- if (authCount >= MAX_AUTH_COUNT ) {
278
- throw e ;
279
- }
280
- authCount ++;
281
- // Get new authentication data.
282
- authProvider .invalidateAuth (operation , auth );
283
- final Link newAuth = authProvider .getAuth (operation );
284
- if (newAuth .getHeader ().equals (auth .getHeader ()) && newAuth .getHref ().equals (auth .getHref ())) {
285
- throw e ;
286
- }
287
- auth = newAuth ;
288
- }
289
- }
290
- }
291
-
292
244
public <R > R doRequest (@ Nullable Link link , @ NotNull Request <R > task , @ NotNull URI url ) throws IOException {
293
245
int redirectCount = 0 ;
294
246
int retryCount = 0 ;
@@ -342,12 +294,49 @@ public <R> R doRequest(@Nullable Link link, @NotNull Request<R> task, @NotNull U
342
294
}
343
295
}
344
296
345
- protected void addHeaders (@ NotNull HttpUriRequest req , @ Nullable Link link ) {
346
- if (link != null ) {
347
- for (Map .Entry <String , String > entry : link .getHeader ().entrySet ()) {
348
- req .setHeader (entry .getKey (), entry .getValue ());
297
+ /**
298
+ * Generate object metadata.
299
+ *
300
+ * @param streamProvider Object stream provider.
301
+ * @return Return object metadata.
302
+ * @throws IOException On some errors.
303
+ */
304
+ public static Meta generateMeta (@ NotNull final StreamProvider streamProvider ) throws IOException {
305
+ final MessageDigest digest = sha256 ();
306
+ final byte [] buffer = new byte [0x10000 ];
307
+ long size = 0 ;
308
+ try (InputStream stream = streamProvider .getStream ()) {
309
+ while (true ) {
310
+ int read = stream .read (buffer );
311
+ if (read <= 0 ) break ;
312
+ digest .update (buffer , 0 , read );
313
+ size += read ;
349
314
}
350
315
}
316
+ return new Meta (new String (Hex .encodeHex (digest .digest ())), size );
317
+ }
318
+
319
+ /**
320
+ * Upload object by metadata.
321
+ *
322
+ * @param links Object links.
323
+ * @param streamProvider Object stream provider.
324
+ * @param meta Object metadata.
325
+ * @return Return true is object is uploaded successfully and false if object is already uploaded.
326
+ * @throws IOException On some errors.
327
+ */
328
+ public boolean putObject (@ NotNull final StreamProvider streamProvider , @ NotNull final Meta meta , @ NotNull final Links links ) throws IOException {
329
+ final Link uploadLink = links .getLinks ().get (LinkType .Upload );
330
+ if (uploadLink == null )
331
+ return false ;
332
+
333
+ doRequest (uploadLink , new ObjectPut (streamProvider , meta .getSize ()), uploadLink .getHref ());
334
+
335
+ final Link verifyLink = links .getLinks ().get (LinkType .Verify );
336
+ if (verifyLink != null )
337
+ doRequest (verifyLink , new ObjectVerify (meta ), verifyLink .getHref ());
338
+
339
+ return true ;
351
340
}
352
341
353
342
protected static MessageDigest sha256 () {
@@ -358,6 +347,14 @@ protected static MessageDigest sha256() {
358
347
}
359
348
}
360
349
350
+ protected void addHeaders (@ NotNull HttpUriRequest req , @ Nullable Link link ) {
351
+ if (link != null ) {
352
+ for (Map .Entry <String , String > entry : link .getHeader ().entrySet ()) {
353
+ req .setHeader (entry .getKey (), entry .getValue ());
354
+ }
355
+ }
356
+ }
357
+
361
358
@ NotNull
362
359
public Lock lock (@ NotNull String path , @ Nullable Ref ref ) throws IOException , LockConflictException {
363
360
final LockCreate .Res res = doWork (auth -> doRequest (
0 commit comments