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
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.impl.protocol.jabber;
import java.util.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.notification.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.*;
/**
* Provides notification for generic events with name and value, also
* option to generate such events.
*
* @author Damian Minkov
* @author Emil Ivov
*/
public class OperationSetGenericNotificationsJabberImpl
implements OperationSetGenericNotifications,
PacketListener
{
/**
* Our class logger
*/
private static final Logger logger =
Logger.getLogger(OperationSetGenericNotificationsJabberImpl.class);
/**
* The provider that created us.
*/
private final ProtocolProviderServiceJabberImpl jabberProvider;
/**
* A reference to the persistent presence operation set that we use
* to match incoming event to <tt>Contact</tt>s and vice versa.
*/
private OperationSetPersistentPresenceJabberImpl opSetPersPresence = null;
/**
* Listeners that would receive event notifications for
* new event notifications
*/
private final Map<String, List<GenericEventListener>>
genericEventListeners
= new HashMap<String,
List<GenericEventListener>>();
/**
* Creates an instance of this operation set.
* @param provider a reference to the <tt>ProtocolProviderServiceImpl</tt>
* that created us and that we'll use for retrieving the underlying aim
* connection.
*/
OperationSetGenericNotificationsJabberImpl(
ProtocolProviderServiceJabberImpl provider
)
{
this.jabberProvider = provider;
provider.addRegistrationStateChangeListener(
new RegistrationStateListener());
// register the notification event Extension in the smack library
ProviderManager.getInstance()
.addIQProvider(NotificationEventIQ.ELEMENT_NAME,
NotificationEventIQ.NAMESPACE,
new NotificationEventIQProvider());
}
/**
* Generates new event notification.
*
* @param contact the contact to receive the notification.
* @param eventName the event name of the notification.
* @param eventValue the event value of the notification.
*/
public void notifyForEvent(
Contact contact,
String eventName,
String eventValue)
{
// if we are not registered do nothing
if(!jabberProvider.isRegistered())
{
if (logger.isTraceEnabled())
logger.trace("provider not registered. "
+"won't send keep alive. acc.id="
+ jabberProvider.getAccountID()
.getAccountUniqueID());
return;
}
NotificationEventIQ newEvent = new NotificationEventIQ();
newEvent.setEventName(eventName);
newEvent.setEventValue(eventValue);
newEvent.setTo(contact.getAddress());
newEvent.setEventSource(jabberProvider.getOurJID());
jabberProvider.getConnection().sendPacket(newEvent);
}
/**
* Generates new generic event notification and send it to the
* supplied contact.
* @param jid the contact jid which will receive the event notification.
* @param eventName the event name of the notification.
* @param eventValue the event value of the notification.
*/
public void notifyForEvent(
String jid,
String eventName,
String eventValue)
{
this.notifyForEvent(jid, eventName, eventValue, null);
}
/**
* Generates new generic event notification and send it to the
* supplied contact.
* @param jid the contact jid which will receive the event notification.
* @param eventName the event name of the notification.
* @param eventValue the event value of the notification.
* @param source the source that will be reported in the event.
*/
public void notifyForEvent(
String jid,
String eventName,
String eventValue,
String source)
{
// if we are not registered do nothing
if(!jabberProvider.isRegistered())
{
if (logger.isTraceEnabled())
logger.trace("provider not registered. "
+"won't send keep alive. acc.id="
+ jabberProvider.getAccountID()
.getAccountUniqueID());
return;
}
//try to convert the jid to a full jid
String fullJid = jabberProvider.getFullJid(jid);
if( fullJid != null )
jid = fullJid;
NotificationEventIQ newEvent = new NotificationEventIQ();
newEvent.setEventName(eventName);
newEvent.setEventValue(eventValue);
newEvent.setTo(jid);
if(source != null)
newEvent.setEventSource(source);
else
newEvent.setEventSource(jabberProvider.getOurJID());
jabberProvider.getConnection().sendPacket(newEvent);
}
/**
* Registers a <tt>GenericEventListener</tt> with this
* operation set so that it gets notifications for new
* event notifications.
*
* @param eventName register the listener for certain event name.
* @param listener the <tt>GenericEventListener</tt>
* to register.
*/
public void addGenericEventListener(
String eventName,
GenericEventListener listener)
{
synchronized (genericEventListeners)
{
List<GenericEventListener> l =
this.genericEventListeners.get(eventName);
if(l == null)
{
l = new ArrayList<GenericEventListener>();
this.genericEventListeners.put(eventName, l);
}
if(!l.contains(listener))
l.add(listener);
}
}
/**
* Unregisters <tt>listener</tt> so that it won't receive any further
* notifications upon new event notifications.
*
* @param eventName unregister the listener for certain event name.
* @param listener the <tt>GenericEventListener</tt>
* to unregister.
*/
public void removeGenericEventListener(
String eventName,
GenericEventListener listener)
{
synchronized (genericEventListeners)
{
List<GenericEventListener> listenerList
= this.genericEventListeners.get(eventName);
if(listenerList != null)
listenerList.remove(listener);
}
}
/**
* Process the next packet sent to this packet listener.<p>
* <p/>
*
* @param packet the packet to process.
*/
public void processPacket(Packet packet)
{
if(packet != null && !(packet instanceof NotificationEventIQ))
return;
NotificationEventIQ notifyEvent = (NotificationEventIQ)packet;
if(logger.isDebugEnabled())
{
if (logger.isDebugEnabled())
logger.debug("Received notificationEvent from "
+ notifyEvent.getFrom()
+ " msg : "
+ notifyEvent.toXML());
}
//do not notify
String fromUserID
= org.jivesoftware.smack.util.StringUtils.parseBareAddress(
notifyEvent.getFrom());
Contact sender = opSetPersPresence.findContactByID(fromUserID);
if(sender == null)
sender = opSetPersPresence.createVolatileContact(
notifyEvent.getFrom());
if(notifyEvent.getType() == Type.GET)
fireNewEventNotification(
sender,
notifyEvent.getEventName(),
notifyEvent.getEventValue(),
notifyEvent.getEventSource(),
true);
else if(notifyEvent.getType() == Type.ERROR)
fireNewEventNotification(
sender,
notifyEvent.getEventName(),
notifyEvent.getEventValue(),
notifyEvent.getEventSource(),
false);
}
/**
* Fires new notification event.
*
* @param from common from <tt>Contact</tt>.
* @param eventName the event name.
* @param eventValue the event value.
* @param source the name of the contact sending the notification.
* @param incoming indicates whether the event we are dispatching
* corresponds to an incoming notification or an error report indicating
* that an outgoing notification has failed.
*/
private void fireNewEventNotification(
Contact from,
String eventName,
String eventValue,
String source,
boolean incoming)
{
String sourceUserID
= org.jivesoftware.smack.util.StringUtils.parseBareAddress(
source);
Contact sourceContact =
opSetPersPresence.findContactByID(sourceUserID);
if(sourceContact == null)
sourceContact = opSetPersPresence
.createVolatileContact(source);
GenericEvent
event = new GenericEvent(
jabberProvider, from, eventName, eventValue, sourceContact);
Iterable<GenericEventListener> listeners;
synchronized (genericEventListeners)
{
List<GenericEventListener> ls =
genericEventListeners.get(eventName);
if(ls == null)
return;
listeners = new ArrayList<GenericEventListener>(ls);
}
for (GenericEventListener listener : listeners)
{
if(incoming)
listener.notificationReceived(event);
else
listener.notificationDeliveryFailed(event);
}
}
/**
* Our listener that will tell us when we're registered to
*/
private class RegistrationStateListener
implements RegistrationStateChangeListener
{
/**
* The method is called by a ProtocolProvider implementation whenever
* a change in the registration state of the corresponding provider had
* occurred.
* @param evt ProviderStatusChangeEvent the event describing the status
* change.
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
if (evt.getNewState() == RegistrationState.REGISTERED)
{
OperationSetGenericNotificationsJabberImpl.this.opSetPersPresence
= (OperationSetPersistentPresenceJabberImpl)
jabberProvider.getOperationSet(
OperationSetPersistentPresence.class);
if(jabberProvider.getConnection() != null)
{
jabberProvider.getConnection().addPacketListener(
OperationSetGenericNotificationsJabberImpl.this,
new PacketTypeFilter(NotificationEventIQ.class));
}
}
else if(evt.getNewState() == RegistrationState.UNREGISTERED
|| evt.getNewState() == RegistrationState.CONNECTION_FAILED
|| evt.getNewState() == RegistrationState.AUTHENTICATION_FAILED)
{
if(jabberProvider.getConnection() != null)
{
jabberProvider.getConnection().removePacketListener(
OperationSetGenericNotificationsJabberImpl.this);
}
OperationSetGenericNotificationsJabberImpl.this.opSetPersPresence
= null;
}
}
}
}
|