forked from jumarko/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollHandler.java
More file actions
117 lines (98 loc) · 3.12 KB
/
Copy pathPollHandler.java
File metadata and controls
117 lines (98 loc) · 3.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* 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()} and {@link #handlePollResult(Object)} methods.
*
* @param <P> polling type
* @param <R> result type
*
* @see com.gooddata.FutureResult
*/
public class PollHandler<P,R> {
private final String pollingUri;
private final Class<P> pollClass;
private final Class<R> resultClass;
private boolean done = false;
private R result;
/**
* Creates a new instance of polling handler
* @param pollingUri URI for polling
* @param pollAndResultClass class of the polling object and result (or {@link Void})
*/
@SuppressWarnings("unchecked")
public PollHandler(final String pollingUri, final Class pollAndResultClass) {
this(pollingUri, pollAndResultClass, pollAndResultClass);
}
/**
* Creates a new instance of polling handler
* @param pollingUri URI for polling
* @param pollClass class of the polling object (or {@link Void})
* @param resultClass class of the result (or {@link Void})
*/
public PollHandler(final String pollingUri, final Class<P> pollClass, Class<R> resultClass) {
this.pollingUri = notNull(pollingUri, "pollingUri");
this.pollClass = notNull(pollClass, "pollClass");
this.resultClass = notNull(resultClass, "resultClass");
}
/**
* Get URI used for polling
*
* @return URI string
*/
final String getPollingUri() {
return pollingUri;
}
final Class<R> getResultClass() {
return resultClass;
}
final Class<P> getPollClass() {
return pollClass;
}
protected PollHandler<P,R> setResult(R result) {
this.result = result;
this.done = true;
onFinish();
return this;
}
final boolean isDone() {
return done;
}
/**
* Return result of polling
*
* @return result
*/
protected final R 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() {
}
protected void handlePollResult(P pollResult) {
if (resultClass.equals(pollClass)) {
setResult(resultClass.cast(pollResult));
} else {
throw new IllegalStateException("Please override handlePollResult method when you want different type of polling and result class");
}
}
}