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
|
/*
* 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.service.protocol;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
/**
* Represents a default implementation of
* {@link OperationSetBasicInstantMessaging} in order to make it easier for
* implementers to provide complete solutions while focusing on
* implementation-specific details.
*
* @author Lyubomir Marinov
*/
public abstract class AbstractOperationSetBasicInstantMessaging
implements OperationSetBasicInstantMessaging
{
/**
* The <tt>Logger</tt> used by the
* <tt>AbstractOperationSetBasicInstantMessaging</tt> class and its
* instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractOperationSetBasicInstantMessaging.class);
/**
* A list of listeners registered for message events.
*/
private final List<MessageListener> messageListeners =
new LinkedList<MessageListener>();
/**
* Registers a MessageListener with this operation set so that it gets
* notifications of successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to register.
*/
public void addMessageListener(MessageListener listener)
{
synchronized (messageListeners)
{
if (!messageListeners.contains(listener))
{
messageListeners.add(listener);
}
}
}
/**
* Create a Message instance for sending arbitrary MIME-encoding content.
*
* @param content content value
* @param contentType the MIME-type for <tt>content</tt>
* @param encoding encoding used for <tt>content</tt>
* @param subject a <tt>String</tt> subject or <tt>null</tt> for now
* subject.
* @return the newly created message.
*/
public Message createMessage(byte[] content, String contentType,
String encoding, String subject)
{
String contentAsString = null;
boolean useDefaultEncoding = true;
if (encoding != null)
{
try
{
contentAsString = new String(content, encoding);
useDefaultEncoding = false;
}
catch (UnsupportedEncodingException ex)
{
logger.warn("Failed to decode content using encoding "
+ encoding, ex);
// We'll use the default encoding.
}
}
if (useDefaultEncoding)
{
encoding = Charset.defaultCharset().name();
contentAsString = new String(content);
}
return createMessage(contentAsString, contentType, encoding, subject);
}
/**
* Create a Message instance for sending a simple text messages with default
* (text/plain) content type and encoding.
*
* @param messageText the string content of the message.
* @return Message the newly created message
*/
public Message createMessage(String messageText)
{
return createMessage(messageText, DEFAULT_MIME_TYPE,
DEFAULT_MIME_ENCODING, null);
}
/**
* Create a Message instance with the specified UID, content type
* and a default encoding.
* This method can be useful when message correction is required. One can
* construct the corrected message to have the same UID as the message
* before correction.
*
* @param messageText the string content of the message.
* @param contentType the MIME-type for <tt>content</tt>
* @param messageUID the unique identifier of this message.
* @return Message the newly created message
*/
public Message createMessageWithUID(
String messageText, String contentType, String messageUID)
{
return createMessage(messageText);
}
public abstract Message createMessage(
String content, String contentType, String encoding, String subject);
/**
* Notifies all registered message listeners that a message has been
* delivered successfully to its addressee..
*
* @param message the <tt>Message</tt> that has been delivered.
* @param to the <tt>Contact</tt> that <tt>message</tt> was delivered to.
*/
protected void fireMessageDelivered(Message message, Contact to)
{
fireMessageEvent(
new MessageDeliveredEvent(message, to, new Date()));
}
protected void fireMessageDeliveryFailed(
Message message,
Contact to,
int errorCode)
{
fireMessageEvent(
new MessageDeliveryFailedEvent(message, to, errorCode));
}
enum MessageEventType{
None,
MessageDelivered,
MessageReceived,
MessageDeliveryFailed,
MessageDeliveryPending,
}
/**
* Delivers the specified event to all registered message listeners.
*
* @param evt the <tt>EventObject</tt> that we'd like delivered to all
* registered message listeners.
*/
protected void fireMessageEvent(EventObject evt)
{
Collection<MessageListener> listeners = null;
synchronized (this.messageListeners)
{
listeners = new ArrayList<MessageListener>(this.messageListeners);
}
if (logger.isDebugEnabled())
logger.debug("Dispatching Message Listeners=" + listeners.size()
+ " evt=" + evt);
/*
* TODO Create a super class like this MessageEventObject that would
* contain the MessageEventType. Also we could fire an event for the
* MessageDeliveryPending event type (modify MessageListener and
* OperationSetInstantMessageTransform).
*/
MessageEventType eventType = MessageEventType.None;
if (evt instanceof MessageDeliveredEvent)
{
eventType = MessageEventType.MessageDelivered;
}
else if (evt instanceof MessageReceivedEvent)
{
eventType = MessageEventType.MessageReceived;
}
else if (evt instanceof MessageDeliveryFailedEvent)
{
eventType = MessageEventType.MessageDeliveryFailed;
}
// Transform the event.
EventObject[] events = messageTransform(evt, eventType);
for (EventObject event : events)
{
try
{
if (event == null)
return;
for (MessageListener listener : listeners)
{
switch (eventType)
{
case MessageDelivered:
listener.messageDelivered((MessageDeliveredEvent) event);
break;
case MessageDeliveryFailed:
listener
.messageDeliveryFailed((MessageDeliveryFailedEvent) event);
break;
case MessageReceived:
listener.messageReceived((MessageReceivedEvent) event);
break;
default:
/*
* We either have nothing to do or we do not know what
* to do. Anyway, we'll silence the compiler.
*/
break;
}
}
}
catch (Throwable e)
{
logger.error("Error delivering message", e);
}
}
}
/**
* Notifies all registered message listeners that a message has been
* received.
*
* @param message the <tt>Message</tt> that has been received.
* @param from the <tt>Contact</tt> that <tt>message</tt> was received from.
*/
protected void fireMessageReceived(Message message, Contact from)
{
fireMessageEvent(
new MessageReceivedEvent(message, from, new Date()));
}
/**
* Unregisters <tt>listener</tt> so that it won't receive any further
* notifications upon successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to unregister.
*/
public void removeMessageListener(MessageListener listener)
{
synchronized (messageListeners)
{
messageListeners.remove(listener);
}
}
/**
* Messages pending delivery to be transformed.
*
* @param evt the message delivery event
* @return returns message delivery events
*/
public MessageDeliveredEvent[] messageDeliveryPendingTransform(
final MessageDeliveredEvent evt)
{
EventObject[] transformed = messageTransform(
evt, MessageEventType.MessageDeliveryPending);
final int size = transformed.length;
MessageDeliveredEvent[] events =
new MessageDeliveredEvent[size];
System.arraycopy(transformed, 0, events, 0, size);
return events;
}
/**
* Transform provided source event by processing transform layers in
* sequence.
*
* @param evt the source event to transform
* @param eventType the event type of the source event
* @return returns the resulting (transformed) events, if any. (I.e. an
* array of 0 or more size containing events.)
*/
private EventObject[] messageTransform(final EventObject evt,
final MessageEventType eventType)
{
if (evt == null)
{
return new EventObject[0];
}
ProtocolProviderService protocolProvider;
switch (eventType)
{
case MessageDelivered:
protocolProvider
= ((MessageDeliveredEvent) evt)
.getDestinationContact().getProtocolProvider();
break;
case MessageDeliveryFailed:
protocolProvider
= ((MessageDeliveryFailedEvent) evt)
.getDestinationContact().getProtocolProvider();
break;
case MessageDeliveryPending:
protocolProvider
= ((MessageDeliveredEvent) evt)
.getDestinationContact().getProtocolProvider();
break;
case MessageReceived:
protocolProvider
= ((MessageReceivedEvent) evt)
.getSourceContact().getProtocolProvider();
break;
default:
return new EventObject[] {evt};
}
OperationSetInstantMessageTransformImpl opSetMessageTransform
= (OperationSetInstantMessageTransformImpl) protocolProvider
.getOperationSet(OperationSetInstantMessageTransform.class);
if (opSetMessageTransform == null)
return new EventObject[] {evt};
// 'current' contains the events that need to be transformed. It should
// not contain null values.
final LinkedList<EventObject> current = new LinkedList<EventObject>();
// Add source event as start of transformation.
current.add(evt);
// 'next' contains the resulting events after transformation in the
// current iteration. It should not contain null values.
final LinkedList<EventObject> next = new LinkedList<EventObject>();
for (Map.Entry<Integer, Vector<TransformLayer>> entry
: opSetMessageTransform.transformLayers.entrySet())
{
for (TransformLayer transformLayer : entry.getValue())
{
next.clear();
while (!current.isEmpty())
{
final EventObject event = current.remove();
switch (eventType)
{
case MessageDelivered:
MessageDeliveredEvent transformedDelivered =
transformLayer.messageDelivered(
(MessageDeliveredEvent) event);
if (transformedDelivered != null)
{
next.add(transformedDelivered);
}
break;
case MessageDeliveryPending:
MessageDeliveredEvent[] evts = transformLayer
.messageDeliveryPending(
(MessageDeliveredEvent) event);
for (MessageDeliveredEvent mde : evts)
{
if (mde != null)
{
next.add(mde);
}
}
break;
case MessageDeliveryFailed:
MessageDeliveryFailedEvent transformedDeliveryFailed =
transformLayer.messageDeliveryFailed(
(MessageDeliveryFailedEvent) event);
if (transformedDeliveryFailed != null)
{
next.add(transformedDeliveryFailed);
}
break;
case MessageReceived:
MessageReceivedEvent transformedReceived =
transformLayer
.messageReceived((MessageReceivedEvent) event);
if (transformedReceived != null)
{
next.add(transformedReceived);
}
break;
default:
next.add(event);
/*
* We either have nothing to do or we do not know
* what to do. Anyway, we'll silence the compiler.
*/
break;
}
}
// Set events for next round of transformations.
current.addAll(next);
}
}
return current.toArray(new EventObject[current.size()]);
}
/**
* Determines whether the protocol supports the supplied content type
* for the given contact.
*
* @param contentType the type we want to check
* @param contact contact which is checked for supported contentType
* @return <tt>true</tt> if the contact supports it and
* <tt>false</tt> otherwise.
*/
public boolean isContentTypeSupported(String contentType, Contact contact)
{
// by default we support default mime type, for other mime-types
// method must be overridden
if(contentType.equals(DEFAULT_MIME_TYPE))
return true;
return false;
}
/**
* Sends the <tt>message</tt> to the destination indicated by the
* <tt>to</tt>. Provides a default implementation of this method.
*
* @param to the <tt>Contact</tt> to send <tt>message</tt> to
* @param toResource the resource to which the message should be send
* @param message the <tt>Message</tt> to send.
* @throws java.lang.IllegalStateException if the underlying ICQ stack is
* not registered and initialized.
* @throws java.lang.IllegalArgumentException if <tt>to</tt> is not an
* instance belonging to the underlying implementation.
*/
public void sendInstantMessage( Contact to,
ContactResource toResource,
Message message)
throws IllegalStateException,
IllegalArgumentException
{
sendInstantMessage(to, message);
}
/**
* Returns the inactivity timeout in milliseconds.
*
* @return The inactivity timeout in milliseconds. Or -1 if undefined
*/
public long getInactivityTimeout()
{
return -1;
}
}
|