public void poll(long streamPosition, final String streamType, final int limit, final EventCallback callback)
throws BoxServerException, AuthFatalFailureException, BoxRestException {
// get BoxClient Event Manager
final IBoxEventsManager eventsManager = cachedBoxClient.getBoxClient().getEventsManager();
// get current stream position if requested
if (BoxEventRequestObject.STREAM_POSITION_NOW == streamPosition) {
streamPosition = getCurrentStreamPosition(eventsManager, streamPosition);
}
// validate parameters
ObjectHelper.notNull(streamPosition, "streamPosition");
ObjectHelper.notEmpty(streamType, "streamType");
ObjectHelper.notNull(callback, "eventCallback");
httpClient = new DefaultHttpClient(cachedBoxClient.getClientConnectionManager(), httpParams);
// start polling thread
LOG.info("Started event polling thread for " + cachedBoxClient);
final long startStreamPosition = streamPosition;
pollFuture = executorService.submit(new Runnable() {
@Override
public void run() {
final ObjectMapper mapper = new ObjectMapper();
long currentStreamPosition = startStreamPosition;
BoxRealTimeServer realTimeServer = null;
boolean retry = false;
int retries = 0;
int maxRetries = 1;
while (!done) {
try {
// set to true if no exceptions thrown
retry = false;
if (realTimeServer == null) {
// get RTS URL
realTimeServer = getBoxRealTimeServer(currentStreamPosition, eventsManager);
// update HTTP timeout
final int requestTimeout = Integer.parseInt(
realTimeServer.getExtraData(RETRY_TIMEOUT).toString());
final HttpParams params = httpClient.getParams();
HttpConnectionParams.setSoTimeout(params, requestTimeout * 1000);
// update maxRetries
maxRetries = Integer.parseInt(realTimeServer.getExtraData(MAX_RETRIES).toString());
}
// create HTTP request for RTS
httpGet = getPollRequest(realTimeServer.getUrl(), currentStreamPosition);
// execute RTS poll
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet, (HttpContext) null);
} catch (SocketTimeoutException e) {
LOG.debug("Poll timed out, retrying for " + cachedBoxClient);
}
if (httpResponse != null) {
// parse response
final StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() == HttpStatus.SC_OK) {
final HttpEntity entity = httpResponse.getEntity();
@SuppressWarnings("unchecked")
Map<String, String> rtsResponse = mapper.readValue(entity.getContent(), Map.class);
final String message = rtsResponse.get(MESSAGE);
if (NEW_CHANGE.equals(message)) {
// get events
final BoxEventRequestObject requestObject =
BoxEventRequestObject.getEventsRequestObject(currentStreamPosition);
requestObject.setStreamType(streamType);
requestObject.setLimit(limit);
final BoxEventCollection events = eventsManager.getEvents(requestObject);
// notify callback
callback.onEvent(events);
// update stream position