This repository was archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathStreamingSubscriptionConnection.java
More file actions
576 lines (501 loc) · 18.4 KB
/
StreamingSubscriptionConnection.java
File metadata and controls
576 lines (501 loc) · 18.4 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* The MIT License
* Copyright (c) 2012 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package microsoft.exchange.webservices.data.notification;
import microsoft.exchange.webservices.data.core.EwsUtilities;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.exception.misc.ArgumentNullException;
import microsoft.exchange.webservices.data.core.request.GetStreamingEventsRequest;
import microsoft.exchange.webservices.data.core.request.HangingRequestDisconnectEventArgs;
import microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase;
import microsoft.exchange.webservices.data.core.response.GetStreamingEventsResponse;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.misc.error.ServiceError;
import microsoft.exchange.webservices.data.core.enumeration.service.ServiceResult;
import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
import microsoft.exchange.webservices.data.core.exception.misc.ArgumentOutOfRangeException;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Represents a connection to an ongoing stream of events.
*/
public final class StreamingSubscriptionConnection implements Closeable,
HangingServiceRequestBase.IHandleResponseObject,
HangingServiceRequestBase.IHangingRequestDisconnectHandler {
private static final Log LOG = LogFactory.getLog(StreamingSubscriptionConnection.class);
/**
* Mapping of streaming id to subscriptions currently on the connection.
*/
private Map<String, StreamingSubscription> subscriptions;
/**
* connection lifetime, in minutes
*/
private int connectionTimeout;
/**
* ExchangeService instance used to make the EWS call.
*/
private ExchangeService session;
/**
* Value indicating whether the class is disposed.
*/
private boolean isDisposed;
/**
* Currently used instance of a GetStreamingEventsRequest connected to EWS.
*/
private GetStreamingEventsRequest currentHangingRequest;
public interface INotificationEventDelegate {
/**
* Represents a delegate that is invoked when notification are received
* from the server
*
* @param sender The StreamingSubscriptionConnection instance that received
* the events.
* @param args The event data.
*/
void notificationEventDelegate(Object sender, NotificationEventArgs args);
}
/**
* Notification events Occurs when notification are received from the
* server.
*/
private List<INotificationEventDelegate> onNotificationEvent = new ArrayList<INotificationEventDelegate>();
/**
* Set event to happen when property Notify.
*
* @param notificationEvent notification event
*/
public void addOnNotificationEvent(
INotificationEventDelegate notificationEvent) {
onNotificationEvent.add(notificationEvent);
}
/**
* Remove the event from happening when property Notify.
*
* @param notificationEvent notification event
*/
public void removeNotificationEvent(
INotificationEventDelegate notificationEvent) {
onNotificationEvent.remove(notificationEvent);
}
/**
* Clears notification events list.
*/
public void clearNotificationEvent() {
onNotificationEvent.clear();
}
public interface ISubscriptionErrorDelegate {
/**
* Represents a delegate that is invoked when an error occurs within a
* streaming subscription connection.
*
* @param sender The StreamingSubscriptionConnection instance within which
* the error occurred.
* @param args The event data.
*/
void subscriptionErrorDelegate(Object sender,
SubscriptionErrorEventArgs args);
}
/**
* Subscription events Occur when a subscription encounters an error.
*/
private List<ISubscriptionErrorDelegate> onSubscriptionError = new ArrayList<ISubscriptionErrorDelegate>();
/**
* Set event to happen when property subscriptionError.
*
* @param subscriptionError subscription event
*/
public void addOnSubscriptionError(
ISubscriptionErrorDelegate subscriptionError) {
onSubscriptionError.add(subscriptionError);
}
/**
* Remove the event from happening when property subscription.
*
* @param subscriptionError subscription event
*/
public void removeSubscriptionError(
ISubscriptionErrorDelegate subscriptionError) {
onSubscriptionError.remove(subscriptionError);
}
/**
* Clears subscription events list.
*/
public void clearSubscriptionError() {
onSubscriptionError.clear();
}
/**
* Disconnect events Occurs when a streaming subscription connection is
* disconnected from the server.
*/
private List<ISubscriptionErrorDelegate> onDisconnect = new ArrayList<ISubscriptionErrorDelegate>();
/**
* Set event to happen when property disconnect.
*
* @param disconnect disconnect event
*/
public void addOnDisconnect(ISubscriptionErrorDelegate disconnect) {
onDisconnect.add(disconnect);
}
/**
* Remove the event from happening when property disconnect.
*
* @param disconnect disconnect event
*/
public void removeDisconnect(ISubscriptionErrorDelegate disconnect) {
onDisconnect.remove(disconnect);
}
/**
* Clears disconnect events list.
*/
public void clearDisconnect() {
onDisconnect.clear();
}
/**
* Initializes a new instance of the StreamingSubscriptionConnection class.
*
* @param service The ExchangeService instance this connection uses to connect
* to the server.
* @param lifetime The maximum time, in minutes, the connection will remain open.
* Lifetime must be between 1 and 30.
* @throws Exception
*/
public StreamingSubscriptionConnection(ExchangeService service, int lifetime)
throws Exception {
EwsUtilities.validateParam(service, "service");
EwsUtilities.validateClassVersion(service,
ExchangeVersion.Exchange2010_SP1, this.getClass().getName());
if (lifetime < 1 || lifetime > 30) {
throw new ArgumentOutOfRangeException("lifetime");
}
this.session = service;
this.subscriptions = new HashMap<String, StreamingSubscription>();
this.connectionTimeout = lifetime;
}
/**
* Initializes a new instance of the StreamingSubscriptionConnection class.
*
* @param service The ExchangeService instance this connection uses to connect
* to the server.
* @param subscriptions Iterable subcriptions
* @param lifetime The maximum time, in minutes, the connection will remain open.
* Lifetime must be between 1 and 30.
* @throws Exception
*/
public StreamingSubscriptionConnection(ExchangeService service,
Iterable<StreamingSubscription> subscriptions, int lifetime)
throws Exception {
this(service, lifetime);
EwsUtilities.validateParamCollection(subscriptions.iterator(), "subscriptions");
for (StreamingSubscription subscription : subscriptions) {
this.subscriptions.put(subscription.getId(), subscription);
}
}
/**
* Adds a subscription to this connection.
*
* @param subscription The subscription to add.
* @throws Exception Thrown when AddSubscription is called while connected.
*/
public void addSubscription(StreamingSubscription subscription)
throws Exception {
this.throwIfDisposed();
EwsUtilities.validateParam(subscription, "subscription");
this.validateConnectionState(false, "Subscriptions can't be added to an open connection.");
synchronized (this) {
if (this.subscriptions.containsKey(subscription.getId())) {
return;
}
this.subscriptions.put(subscription.getId(), subscription);
}
}
/**
* Removes the specified streaming subscription from the connection.
*
* @param subscription The subscription to remove.
* @throws Exception Thrown when RemoveSubscription is called while connected.
*/
public void removeSubscription(StreamingSubscription subscription)
throws Exception {
this.throwIfDisposed();
EwsUtilities.validateParam(subscription, "subscription");
this.validateConnectionState(false, "Subscriptions can't be removed from an open connection.");
synchronized (this) {
this.subscriptions.remove(subscription.getId());
}
}
/**
* Opens this connection so it starts receiving events from the server.This
* results in a long-standing call to EWS.
*
* @throws Exception
* @throws ServiceLocalException Thrown when Open is called while connected.
*/
public void open() throws ServiceLocalException, Exception {
synchronized (this) {
this.throwIfDisposed();
this.validateConnectionState(false, "The connection has already opened.");
if (this.subscriptions.size() == 0) {
throw new ServiceLocalException(
"You must add at least one subscription to this connection before it can be opened.");
}
this.currentHangingRequest = new GetStreamingEventsRequest(
this.session, this, this.subscriptions.keySet(),
this.connectionTimeout);
// Make sure socket timeout for this request is at least as large as connection timeout
// otherwise connection may get dropped too soon.
this.currentHangingRequest.setTimeout((int) TimeUnit.MINUTES.toMillis(connectionTimeout));
this.currentHangingRequest.addOnDisconnectEvent(this);
this.currentHangingRequest.internalExecute();
}
}
/**
* Called when the request is disconnected.
*
* @param sender The sender.
* @param args The Microsoft.Exchange.WebServices.Data.
* HangingRequestDisconnectEventArgs instance containing the
* event data.
*/
private void onRequestDisconnect(Object sender,
HangingRequestDisconnectEventArgs args) {
this.internalOnDisconnect(args.getException());
}
/**
* Closes this connection so it stops receiving events from the server.This
* terminates a long-standing call to EWS.
*/
public void close() {
synchronized (this) {
try {
this.throwIfDisposed();
this.validateConnectionState(true, "The connection is already closed.");
// Further down in the stack, this will result in a
// call to our OnRequestDisconnect event handler,
// doing the necessary cleanup.
this.currentHangingRequest.disconnect();
} catch (Exception e) {
LOG.error(e);
}
}
}
/**
* Internal helper method called when the request disconnects.
*
* @param ex The exception that caused the disconnection. May be null.
*/
private void internalOnDisconnect(Exception ex) {
if (!onDisconnect.isEmpty()) {
for (ISubscriptionErrorDelegate disconnect : onDisconnect) {
disconnect.subscriptionErrorDelegate(this,
new SubscriptionErrorEventArgs(null, ex));
}
}
this.currentHangingRequest = null;
}
/**
* Gets a value indicating whether this connection is opened
*
* @throws Exception
*/
public boolean getIsOpen() throws Exception {
this.throwIfDisposed();
if (this.currentHangingRequest == null) {
return false;
} else {
return this.currentHangingRequest.isConnected();
}
}
/**
* Validates the state of the connection.
*
* @param isConnectedExpected Value indicating whether we expect to be currently connected.
* @param errorMessage The error message.
* @throws Exception
*/
private void validateConnectionState(boolean isConnectedExpected,
String errorMessage) throws Exception {
if ((isConnectedExpected && !this.getIsOpen())
|| (!isConnectedExpected && this.getIsOpen())) {
throw new ServiceLocalException(errorMessage);
}
}
/**
* Handles the service response object.
*
* @param response The response.
* @throws ArgumentException
*/
private void handleServiceResponseObject(Object response)
throws ArgumentException {
GetStreamingEventsResponse gseResponse = (GetStreamingEventsResponse) response;
if (gseResponse == null) {
throw new ArgumentNullException("GetStreamingEventsResponse must not be null",
"GetStreamingEventsResponse");
} else {
if (gseResponse.getResult() == ServiceResult.Success
|| gseResponse.getResult() == ServiceResult.Warning) {
if (gseResponse.getResults().getNotifications().size() > 0) {
// We got notification; dole them out.
this.issueNotificationEvents(gseResponse);
} else {
// // This was just a heartbeat, nothing to do here.
}
} else if (gseResponse.getResult() == ServiceResult.Error) {
if (gseResponse.getErrorSubscriptionIds() == null
|| gseResponse.getErrorSubscriptionIds().size() == 0) {
// General error
this.issueGeneralFailure(gseResponse);
} else {
// subscription-specific errors
this.issueSubscriptionFailures(gseResponse);
}
}
}
}
/**
* Issues the subscription failures.
*
* @param gseResponse The GetStreamingEvents response.
*/
private void issueSubscriptionFailures(
GetStreamingEventsResponse gseResponse) {
ServiceResponseException exception = new ServiceResponseException(
gseResponse);
for (String id : gseResponse.getErrorSubscriptionIds()) {
StreamingSubscription subscription = null;
synchronized (this) {
// Client can do any good or bad things in the below event
// handler
if (this.subscriptions != null
&& this.subscriptions.containsKey(id)) {
subscription = this.subscriptions.get(id);
}
}
if (subscription != null) {
SubscriptionErrorEventArgs eventArgs = new SubscriptionErrorEventArgs(
subscription, exception);
if (!onSubscriptionError.isEmpty()) {
for (ISubscriptionErrorDelegate subError : onSubscriptionError) {
subError.subscriptionErrorDelegate(this, eventArgs);
}
}
}
if (gseResponse.getErrorCode() != ServiceError.ErrorMissedNotificationEvents) {
// Client can do any good or bad things in the above event
// handler
synchronized (this) {
if (this.subscriptions != null
&& this.subscriptions.containsKey(id)) {
// We are no longer servicing the subscription.
this.subscriptions.remove(id);
}
}
}
}
}
/**
* Issues the general failure.
*
* @param gseResponse The GetStreamingEvents response.
*/
private void issueGeneralFailure(GetStreamingEventsResponse gseResponse) {
SubscriptionErrorEventArgs eventArgs = new SubscriptionErrorEventArgs(
null, new ServiceResponseException(gseResponse));
if (!onSubscriptionError.isEmpty()) {
for (ISubscriptionErrorDelegate subError : onSubscriptionError) {
subError.subscriptionErrorDelegate(this, eventArgs);
}
}
}
/**
* Issues the notification events.
*
* @param gseResponse The GetStreamingEvents response.
*/
private void issueNotificationEvents(GetStreamingEventsResponse gseResponse) {
for (GetStreamingEventsResults.NotificationGroup events : gseResponse
.getResults().getNotifications()) {
StreamingSubscription subscription = null;
synchronized (this) {
// Client can do any good or bad things in the below event
// handler
if (this.subscriptions != null
&& this.subscriptions
.containsKey(events.subscriptionId)) {
subscription = this.subscriptions
.get(events.subscriptionId);
}
}
if (subscription != null) {
NotificationEventArgs eventArgs = new NotificationEventArgs(
subscription, events.events);
if (!onNotificationEvent.isEmpty()) {
for (INotificationEventDelegate notifyEvent : onNotificationEvent) {
notifyEvent.notificationEventDelegate(this, eventArgs);
}
}
}
}
}
/**
* Frees resources associated with this StreamingSubscriptionConnection.
*/
public void dispose() {
synchronized (this) {
if (!this.isDisposed) {
if (this.currentHangingRequest != null) {
this.currentHangingRequest = null;
}
this.subscriptions = null;
this.session = null;
this.isDisposed = true;
}
}
}
/**
* Throws if disposed.
*
* @throws Exception
*/
private void throwIfDisposed() throws Exception {
if (this.isDisposed) {
throw new Exception(this.getClass().getName());
}
}
@Override
public void handleResponseObject(Object response) throws ArgumentException {
this.handleServiceResponseObject(response);
}
@Override
public void hangingRequestDisconnectHandler(Object sender,
HangingRequestDisconnectEventArgs args) {
this.onRequestDisconnect(sender, args);
}
}