Skip to content

Commit 9de9962

Browse files
committed
add AsyncJobMonitor for monitoring async Graph job
1 parent 379c42e commit 9de9962

File tree

5 files changed

+165
-39
lines changed

5 files changed

+165
-39
lines changed

src/main/java/com/bhyoo/onedrive/client/Client.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.bhyoo.onedrive.client.auth.AbstractAuthHelper;
44
import com.bhyoo.onedrive.client.auth.AuthHelper;
5+
import com.bhyoo.onedrive.container.AsyncJobMonitor;
56
import com.bhyoo.onedrive.container.items.*;
67
import com.bhyoo.onedrive.container.items.pointer.BasePointer;
78
import com.bhyoo.onedrive.container.items.pointer.IdPointer;
@@ -292,15 +293,15 @@ public RemoteItem[] getShared() throws ErrorResponseException {
292293
* @param srcId item's id that wants to be copied
293294
* @param destId location's id that wants to be placed the copied item
294295
*
295-
* @return URL {@code String} that can monitor status of copying process
296+
* @return monitor {@link AsyncJobMonitor} that can monitor status of copying process
296297
*
297298
* @throws ErrorResponseException if error happens while requesting copying operation. such as invalid copying
298299
* request
299300
* @throws InvalidJsonException if fail to parse response of copying request into json. it caused by server side
300301
* not by SDK.
301302
*/
302-
@NotNull
303-
public String copyItem(@NotNull String srcId, @NotNull String destId) throws ErrorResponseException {
303+
public @NotNull AsyncJobMonitor copyItem(@NotNull String srcId, @NotNull String destId)
304+
throws ErrorResponseException {
304305
byte[] content = ("{\"parentReference\":{\"id\":\"" + destId + "\"}}").getBytes();
305306
return copyItem(ITEM_ID_PREFIX + srcId + "/action.copy", content);
306307
}
@@ -312,48 +313,46 @@ public String copyItem(@NotNull String srcId, @NotNull String destId) throws Err
312313
*
313314
* @see Client#copyItem(String, String)
314315
*/
315-
@NotNull
316-
public String copyItem(@NotNull String srcId, @NotNull String destId, @NotNull String newName)
316+
public @NotNull AsyncJobMonitor copyItem(@NotNull String srcId, @NotNull String destId, @NotNull String newName)
317317
throws ErrorResponseException {
318318
byte[] content = ("{\"parentReference\":{\"id\":\"" + destId + "\"},\"name\":\"" + newName + "\"}").getBytes();
319319
return copyItem(ITEM_ID_PREFIX + srcId + "/" + COPY, content);
320320
}
321321

322-
@NotNull
323-
public String copyItem(@NotNull String srcId, @NotNull PathPointer destPath) throws ErrorResponseException {
322+
public @NotNull AsyncJobMonitor copyItem(@NotNull String srcId, @NotNull PathPointer destPath)
323+
throws ErrorResponseException {
324324
byte[] content = ("{\"parentReference\":" + destPath.toJson() + "}").getBytes();
325325
return copyItem(ITEM_ID_PREFIX + srcId + "/" + COPY, content);
326326
}
327327

328-
@NotNull
329-
public String copyItem(@NotNull String srcId, @NotNull PathPointer dest, @NotNull String newName)
328+
public @NotNull AsyncJobMonitor copyItem(@NotNull String srcId, @NotNull PathPointer dest, @NotNull String newName)
330329
throws ErrorResponseException {
331330
byte[] content = ("{\"parentReference\":" + dest.toJson() + ",\"name\":\"" + newName + "\"}").getBytes();
332331
return copyItem(ITEM_ID_PREFIX + srcId + "/" + COPY, content);
333332
}
334333

335-
@NotNull
336-
public String copyItem(@NotNull PathPointer srcPath, @NotNull String destId) throws ErrorResponseException {
334+
public @NotNull AsyncJobMonitor copyItem(@NotNull PathPointer srcPath, @NotNull String destId)
335+
throws ErrorResponseException {
337336
byte[] content = ("{\"parentReference\":{\"id\":\"" + destId + "\"}}").getBytes();
338337
return copyItem(srcPath.resolveOperator(COPY), content);
339338
}
340339

341-
@NotNull
342-
public String copyItem(@NotNull PathPointer srcPath, @NotNull String destId, @NotNull String newName)
343-
throws ErrorResponseException {
340+
public @NotNull AsyncJobMonitor copyItem(@NotNull PathPointer srcPath,
341+
@NotNull String destId,
342+
@NotNull String newName) throws ErrorResponseException {
344343
byte[] content = ("{\"parentReference\":{\"id\":\"" + destId + "\"},\"name\":\"" + newName + "\"}").getBytes();
345344
return copyItem(srcPath.resolveOperator(COPY), content);
346345
}
347346

348-
@NotNull
349-
public String copyItem(@NotNull BasePointer src, @NotNull BasePointer dest) throws ErrorResponseException {
347+
public @NotNull AsyncJobMonitor copyItem(@NotNull BasePointer src, @NotNull BasePointer dest)
348+
throws ErrorResponseException {
350349
byte[] content = ("{\"parentReference\":" + dest.toJson() + "}").getBytes();
351350
return copyItem(src.resolveOperator(COPY), content);
352351
}
353352

354-
@NotNull
355-
public String copyItem(@NotNull BasePointer src, @NotNull BasePointer dest, @NotNull String newName)
356-
throws ErrorResponseException {
353+
public @NotNull AsyncJobMonitor copyItem(@NotNull BasePointer src,
354+
@NotNull BasePointer dest,
355+
@NotNull String newName) throws ErrorResponseException {
357356
byte[] content = ("{\"parentReference\":" + dest.toJson() + ",\"name\":\"" + newName + "\"}").getBytes();
358357
return copyItem(src.resolveOperator(COPY), content);
359358
}
@@ -366,23 +365,23 @@ public String copyItem(@NotNull BasePointer src, @NotNull BasePointer dest, @Not
366365
* {@code api} is a escaped {@code String}
367366
* @param content HTTP body
368367
*
369-
* @return URL {@code String} that can monitor status of copying process
368+
* @return monitor {@link AsyncJobMonitor} that can monitor status of copying process
370369
*
371370
* @throws ErrorResponseException if error happens while requesting copying operation. such as invalid copying
372371
* request
373372
* @throws InvalidJsonException if fail to parse response of copying request into json. it caused by server side
374373
* not by SDK.
375374
*/
376-
@NotNull
377-
private String copyItem(@NotNull String api, @NotNull byte[] content) throws ErrorResponseException {
375+
private @NotNull AsyncJobMonitor copyItem(@NotNull String api, @NotNull byte[] content)
376+
throws ErrorResponseException {
378377
authHelper.checkExpired();
379378

380379
SyncResponse response = requestTool.postMetadata(api, content);
381380

382381
// if not 202 Accepted raise ErrorResponseException
383382
requestTool.errorHandling(response, HTTP_ACCEPTED);
384383

385-
return response.getHeader().get("Location").get(0);
384+
return new AsyncJobMonitor(response.getHeader().get("Location").get(0));
386385
}
387386

388387

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.bhyoo.onedrive.container;
2+
3+
import com.bhyoo.onedrive.client.RequestTool;
4+
import com.bhyoo.onedrive.network.sync.SyncRequest;
5+
import com.bhyoo.onedrive.network.sync.SyncResponse;
6+
import com.fasterxml.jackson.core.JsonParser;
7+
import com.fasterxml.jackson.core.JsonToken;
8+
import lombok.Getter;
9+
import lombok.SneakyThrows;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
12+
13+
import java.io.IOException;
14+
import java.net.MalformedURLException;
15+
import java.net.URL;
16+
17+
import static io.netty.handler.codec.http.HttpHeaderNames.ACCEPT_ENCODING;
18+
import static io.netty.handler.codec.http.HttpHeaderValues.GZIP;
19+
20+
/**
21+
* <a href='https://docs.microsoft.com/ko-kr/onedrive/developer/rest-api/concepts/long-running-actions'>Official doc</a>
22+
*/
23+
public class AsyncJobMonitor {
24+
protected @NotNull String url;
25+
@Getter protected @NotNull String operation;
26+
@Getter protected @Nullable String resourceId;
27+
@Getter protected @NotNull Double percentageComplete;
28+
@Getter protected @NotNull AsyncJobStatus status;
29+
@Getter protected @NotNull String statusDescription;
30+
31+
32+
public AsyncJobMonitor(@NotNull String url) {
33+
this.url = url;
34+
35+
update();
36+
}
37+
38+
@SneakyThrows(MalformedURLException.class)
39+
public AsyncJobMonitor update() {
40+
SyncResponse response = new SyncRequest(new URL(this.url)).setHeader(ACCEPT_ENCODING, GZIP).doGet();
41+
42+
// TODO: handle error response
43+
44+
try {
45+
JsonParser parser = RequestTool.jsonFactory.createParser(response.getContent());
46+
parser.nextToken();
47+
48+
while (parser.nextToken() != JsonToken.END_OBJECT) {
49+
String currentName = parser.getCurrentName();
50+
parser.nextToken();
51+
52+
switch (currentName) {
53+
case "operation":
54+
operation = parser.getText();
55+
break;
56+
case "resourceId":
57+
resourceId = parser.getText();
58+
break;
59+
case "percentageComplete":
60+
percentageComplete = parser.getDoubleValue();
61+
break;
62+
case "status":
63+
status = AsyncJobStatus.deserialize(parser.getText());
64+
break;
65+
case "statusDescription":
66+
statusDescription = parser.getText();
67+
break;
68+
default:
69+
throw new IllegalStateException(
70+
"Unknown attribute detected in AsyncJobMonitor : " + currentName);
71+
}
72+
}
73+
74+
return this;
75+
}
76+
catch (IOException e) {
77+
// FIXME: custom exception
78+
throw new RuntimeException("DEV: Unrecognizable json response.", e);
79+
}
80+
}
81+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.bhyoo.onedrive.container;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public enum AsyncJobStatus {
6+
NOT_STARTED("notStarted"),
7+
IN_PROGRESS("inProgress"),
8+
COMPLETED("completed"),
9+
UPDATING("updating"),
10+
FAILED("failed"),
11+
DELETE_PENDING("deletePending"),
12+
DELETE_FAILED("deleteFailed"),
13+
WAITING("waiting");
14+
15+
private final String type;
16+
17+
AsyncJobStatus(String type) {this.type = type;}
18+
19+
public static AsyncJobStatus deserialize(@NotNull String type) {
20+
switch (type) {
21+
case "notStarted":
22+
return NOT_STARTED;
23+
case "inProgress":
24+
return IN_PROGRESS;
25+
case "completed":
26+
return COMPLETED;
27+
case "updating":
28+
return UPDATING;
29+
case "failed":
30+
return FAILED;
31+
case "deletePending":
32+
return DELETE_PENDING;
33+
case "deleteFailed":
34+
return DELETE_FAILED;
35+
case "waiting":
36+
return WAITING;
37+
default:
38+
throw new IllegalStateException("Unknown attribute detected in AsyncJobStatus : " + type);
39+
}
40+
}
41+
42+
@Override public String toString() {return type;}
43+
}

src/main/java/com/bhyoo/onedrive/container/items/AbstractDriveItem.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.bhyoo.onedrive.client.Client;
44
import com.bhyoo.onedrive.client.RequestTool;
5+
import com.bhyoo.onedrive.container.AsyncJobMonitor;
56
import com.bhyoo.onedrive.container.IdentitySet;
67
import com.bhyoo.onedrive.container.facet.*;
78
import com.bhyoo.onedrive.container.items.pointer.BasePointer;
@@ -470,17 +471,18 @@ public void delete() throws ErrorResponseException {
470471

471472

472473
@Override
473-
public @NotNull String copyTo(@NotNull FolderItem folder) throws ErrorResponseException {
474+
public @NotNull AsyncJobMonitor copyTo(@NotNull FolderItem folder) throws ErrorResponseException {
474475
return this.copyTo(folder.getId());
475476
}
476477

477478
@Override
478-
public @NotNull String copyTo(@NotNull FolderItem folder, @NotNull String newName) throws ErrorResponseException {
479+
public @NotNull AsyncJobMonitor copyTo(@NotNull FolderItem folder, @NotNull String newName)
480+
throws ErrorResponseException {
479481
return this.copyTo(folder.getId(), newName);
480482
}
481483

482484
@Override
483-
public @NotNull String copyTo(@NotNull ItemReference folder) throws ErrorResponseException {
485+
public @NotNull AsyncJobMonitor copyTo(@NotNull ItemReference folder) throws ErrorResponseException {
484486
if (folder.id != null)
485487
return this.copyTo(folder.id);
486488
else if (folder.pathPointer != null)
@@ -490,7 +492,7 @@ else if (folder.pathPointer != null)
490492
}
491493

492494
@Override
493-
public @NotNull String copyTo(@NotNull ItemReference folder, @NotNull String newName)
495+
public @NotNull AsyncJobMonitor copyTo(@NotNull ItemReference folder, @NotNull String newName)
494496
throws ErrorResponseException {
495497
if (folder.id != null)
496498
return this.copyTo(folder.id, newName);
@@ -501,22 +503,22 @@ else if (folder.pathPointer != null)
501503
}
502504

503505
@Override
504-
public @NotNull String copyTo(@NotNull BasePointer dest) throws ErrorResponseException {
506+
public @NotNull AsyncJobMonitor copyTo(@NotNull BasePointer dest) throws ErrorResponseException {
505507
return client.copyItem(idPointer, dest);
506508
}
507509

508510
@Override
509-
public @NotNull String copyTo(@NotNull BasePointer dest, @NotNull String newName) throws ErrorResponseException {
511+
public @NotNull AsyncJobMonitor copyTo(@NotNull BasePointer dest, @NotNull String newName) throws ErrorResponseException {
510512
return client.copyItem(idPointer, dest, newName);
511513
}
512514

513515
@Override
514-
public @NotNull String copyTo(@NotNull String destId) throws ErrorResponseException {
516+
public @NotNull AsyncJobMonitor copyTo(@NotNull String destId) throws ErrorResponseException {
515517
return client.copyItem(this.id, destId);
516518
}
517519

518520
@Override
519-
public @NotNull String copyTo(@NotNull String destId, @NotNull String newName) throws ErrorResponseException {
521+
public @NotNull AsyncJobMonitor copyTo(@NotNull String destId, @NotNull String newName) throws ErrorResponseException {
520522
return client.copyItem(this.id, destId, newName);
521523
}
522524

src/main/java/com/bhyoo/onedrive/container/items/DriveItem.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bhyoo.onedrive.container.items;
22

3+
import com.bhyoo.onedrive.container.AsyncJobMonitor;
34
import com.bhyoo.onedrive.container.facet.FileSystemInfoFacet;
45
import com.bhyoo.onedrive.container.facet.SearchResultFacet;
56
import com.bhyoo.onedrive.container.facet.SharePointIdsFacet;
@@ -53,21 +54,21 @@ public interface DriveItem extends BaseItem {
5354
*/
5455

5556

56-
@NotNull String copyTo(@NotNull FolderItem folder) throws ErrorResponseException;
57+
@NotNull AsyncJobMonitor copyTo(@NotNull FolderItem folder) throws ErrorResponseException;
5758

58-
@NotNull String copyTo(@NotNull FolderItem folder, @NotNull String newName) throws ErrorResponseException;
59+
@NotNull AsyncJobMonitor copyTo(@NotNull FolderItem folder, @NotNull String newName) throws ErrorResponseException;
5960

60-
@NotNull String copyTo(@NotNull ItemReference folder) throws ErrorResponseException;
61+
@NotNull AsyncJobMonitor copyTo(@NotNull ItemReference folder) throws ErrorResponseException;
6162

62-
@NotNull String copyTo(@NotNull ItemReference folder, @NotNull String newName) throws ErrorResponseException;
63+
@NotNull AsyncJobMonitor copyTo(@NotNull ItemReference folder, @NotNull String newName) throws ErrorResponseException;
6364

64-
@NotNull String copyTo(@NotNull BasePointer dest) throws ErrorResponseException;
65+
@NotNull AsyncJobMonitor copyTo(@NotNull BasePointer dest) throws ErrorResponseException;
6566

66-
@NotNull String copyTo(@NotNull BasePointer dest, @NotNull String newName) throws ErrorResponseException;
67+
@NotNull AsyncJobMonitor copyTo(@NotNull BasePointer dest, @NotNull String newName) throws ErrorResponseException;
6768

68-
@NotNull String copyTo(@NotNull String destId) throws ErrorResponseException;
69+
@NotNull AsyncJobMonitor copyTo(@NotNull String destId) throws ErrorResponseException;
6970

70-
@NotNull String copyTo(@NotNull String destId, @NotNull String newName) throws ErrorResponseException;
71+
@NotNull AsyncJobMonitor copyTo(@NotNull String destId, @NotNull String newName) throws ErrorResponseException;
7172

7273

7374

0 commit comments

Comments
 (0)