forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollHandler.java
More file actions
80 lines (64 loc) · 1.95 KB
/
PollHandler.java
File metadata and controls
80 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
import static com.gooddata.Validate.notNull;
/**
* For internal usage by services employing polling.<p>
* Implementing classes should override {@link #isFinished(ClientHttpResponse)} method and
* may override {@link #onFinish()} method.
* @see FutureResult
*/
public class PollHandler<T> {
private final String pollingUri;
private final Class<T> resultClass;
private boolean done = false;
private T result;
/**
* Creates a new instance of polling handler
* @param pollingUri URI for polling
* @param resultClass class of the result (or {@link Void})
*/
public PollHandler(final String pollingUri, final Class<T> resultClass) {
this.pollingUri = notNull(pollingUri, "pollingUri");
this.resultClass = notNull(resultClass, "resultClass");
}
final String getPollingUri() {
return pollingUri;
}
final Class<T> getResultClass() {
return resultClass;
}
final PollHandler<T> setResult(T result) {
this.result = result;
this.done = true;
onFinish();
return this;
}
final boolean isDone() {
return done;
}
/**
* Return result of polling
* @return result
*/
protected final T getResult() {
return result;
}
/**
* Check if polling should finish
* @param response client side http response
* @return true if polling should finish
* @throws IOException
*/
protected boolean isFinished(final ClientHttpResponse response) throws IOException {
return HttpStatus.OK.equals(response.getStatusCode());
}
/**
* Method called after polling is successfully finished (default no-op)
*/
protected void onFinish() {}
}