Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 800e428981d111d31e6f120ec2720eaf78328f7d (plain) (blame)
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
577
578
579
580
581
582
583
584
585
586
587
588
589
/**
 * $RCSfile: Jingle.java,v $
 * $Revision: 1.1 $
 * $Date: 2006/10/17 19:14:14 $
 *
 * Copyright 2003-2004 Jive Software.
 *
 * All rights reserved. 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 org.jivesoftware.smackx.packet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smack.packet.IQ;

/**
 * An Jingle sub-packet, which is used by XMPP clients to exchange info like
 * descriptions and transports. <p/> The following link summarizes the
 * requirements of Jingle IM: <a
 * href="http://www.jabber.org/jeps/jep-0166.html">Valid tags</a>.
 * <p>
 * <p/> Warning: this is an non-standard protocol documented by <a
 * href="http://www.jabber.org/jeps/jep-0166.html">JEP-166</a>. Because this is
 * a non-standard protocol, it is subject to change.
 * 
 * @author Alvaro Saurin
 */
public class Jingle extends IQ {

	// static

	public static final String NAMESPACE = "http://jabber.org/protocol/jingle";

	public static final String NODENAME = "jingle";

	// non-static

	private String sid; // The session id

	private Action action; // The action associated to the Jingle

	private String initiator; // The initiator as a "user@host/resource"

	private String responder; // The responder

	// Sub-elements of a Jingle object.

	private final List descriptions = new ArrayList();

	private final List transports = new ArrayList();

	private JingleContentInfo contentInfo;

	/**
	 * A constructor where the main components can be initialized.
	 */
	public Jingle(final List descs, final List trans, final JingleContentInfo mi,
			final String sid) {
		super();

		if (descs != null) {
			descriptions.addAll(descs);
		}

		if (trans != null) {
			transports.addAll(trans);
		}

		setContentInfo(mi);
		setSid(sid);

		// Set null all other fields in the packet
		initiator = null;
		responder = null;
		action = null;
	}

	/**
	 * Constructor with a description.
	 * 
	 * @param descr a description
	 */
	public Jingle(final JingleContentDescription descr) {
		super();

		addDescription(descr);

		// Set null all other fields in the packet
		initiator = null;
		responder = null;

		// Some default values for the most common situation...
		action = Jingle.Action.CONTENTINFO;
		this.setType(IQ.Type.SET);
	}

	/**
	 * Constructor with a transport.
	 * 
	 * @param trans a transport
	 */
	public Jingle(final JingleTransport trans) {
		super();

		addTransport(trans);

		// Set null all other fields in the packet
		initiator = null;
		responder = null;

		// Some default values for the most common situation...
		action = Jingle.Action.TRANSPORTINFO;
		this.setType(IQ.Type.SET);
	}

	/**
	 * Constructor with a content info.
	 * 
	 * @param info The content info
	 */
	public Jingle(final JingleContentInfo info) {
		super();

		setContentInfo(info);

		// Set null all other fields in the packet
		initiator = null;
		responder = null;

		// Some default values for the most common situation...
		action = Jingle.Action.CONTENTINFO;
		this.setType(IQ.Type.SET);
	}

	/**
	 * A constructor where the action can be specified.
	 * 
	 * @param action The action.
	 */
	public Jingle(final Jingle.Action action) {
		this(null, null, null, null);
		this.action = action;

		// In general, a Jingle with an action is used in a SET packet...
		this.setType(IQ.Type.SET);
	}

	/**
	 * A constructor where the session ID can be specified.
	 * 
	 * @param sid The session ID related to the negotiation.
	 * @see #setSid(String)
	 */
	public Jingle(final String sid) {
		this(null, null, null, sid);
	}

	/**
	 * The default constructor
	 */
	public Jingle() {
		super();
	}

	/**
	 * Set the session ID related to this session. The session ID is a unique
	 * identifier generated by the initiator. This should match the XML Nmtoken
	 * production so that XML character escaping is not needed for characters
	 * such as &.
	 * 
	 * @param sid the session ID
	 */
	public final void setSid(final String sid) {
		this.sid = sid;
	}

	/**
	 * Returns the session ID related to the session. The session ID is a unique
	 * identifier generated by the initiator. This should match the XML Nmtoken
	 * production so that XML character escaping is not needed for characters
	 * such as &.
	 * 
	 * @return Returns the session ID related to the session.
	 * @see #setSid(String)
	 */
	public String getSid() {
		return sid;
	}

	/**
	 * Returns the XML element name of the extension sub-packet root element.
	 * Always returns "jingle"
	 * 
	 * @return the XML element name of the packet extension.
	 */
	public static String getElementName() {
		return NODENAME;
	}

	/**
	 * Returns the XML namespace of the extension sub-packet root element.
	 * According the specification the namespace is always
	 * "http://jabber.org/protocol/jingle"
	 * 
	 * @return the XML namespace of the packet extension.
	 */
	public static String getNamespace() {
		return NAMESPACE;
	}

	/**
	 * @return the audioInfo
	 */
	public JingleContentInfo getContentInfo() {
		return contentInfo;
	}

	/**
	 * @param contentInfo the audioInfo to set
	 */
	public void setContentInfo(final JingleContentInfo contentInfo) {
		this.contentInfo = contentInfo;
	}

	/**
	 * Get an iterator for the content descriptions
	 * 
	 * @return the descriptions
	 */
	public Iterator getDescriptions() {
		synchronized (descriptions) {
			return Collections.unmodifiableList(new ArrayList(descriptions)).iterator();
		}
	}

	/**
	 * Get an iterator for the content descriptions
	 * 
	 * @return the descriptions
	 */
	public ArrayList getDescriptionsList() {
		synchronized (descriptions) {
			return new ArrayList(descriptions);
		}
	}

	/**
	 * Add a new content description.
	 * 
	 * @param desc the descriptions to add
	 */
	public void addDescription(final JingleContentDescription desc) {
		if (desc != null) {
			synchronized (descriptions) {
				descriptions.add(desc);
			}
		}
	}

	/**
	 * Add a list of JingleContentDescription elements
	 * 
	 * @param descsList the list of transports to add
	 */
	public void addDescriptions(final List descsList) {
		if (descsList != null) {
			synchronized (descriptions) {
				descriptions.addAll(descsList);
			}
		}
	}

	/**
	 * Get an iterator for the transport.
	 * 
	 * @return the transports
	 */
	public Iterator getTransports() {
		synchronized (transports) {
			return Collections.unmodifiableList(new ArrayList(transports)).iterator();
		}
	}

	/**
	 * Get the list of transports.
	 * 
	 * @return the transports list.
	 */
	public ArrayList getTransportsList() {
		synchronized (transports) {
			return new ArrayList(transports);
		}
	}

	/**
	 * Add a new JingleTransport element
	 * 
	 * @param trans the transports to add
	 */
	public void addTransport(final JingleTransport trans) {
		if (trans != null) {
			synchronized (transports) {
				transports.add(trans);
			}
		}
	}

	/**
	 * Add a list of JingleTransport elements
	 * 
	 * @param transList the list of transports to add
	 */
	public void addTransports(final List transList) {
		if (transList != null) {
			synchronized (transports) {
				transports.addAll(transList);
			}
		}
	}

	/**
	 * Get the action specified in the packet
	 * 
	 * @return the action
	 */
	public Action getAction() {
		return action;
	}

	/**
	 * Set the action in the packet
	 * 
	 * @param action the action to set
	 */
	public void setAction(final Action action) {
		this.action = action;
	}

	/**
	 * Get the initiator. The initiator will be the full JID of the entity that
	 * has initiated the flow (which may be different to the "from" address in
	 * the IQ)
	 * 
	 * @return the initiator
	 */
	public String getInitiator() {
		return initiator;
	}

	/**
	 * Set the initiator. The initiator must be the full JID of the entity that
	 * has initiated the flow (which may be different to the "from" address in
	 * the IQ)
	 * 
	 * @param initiator the initiator to set
	 */
	public void setInitiator(final String initiator) {
		this.initiator = initiator;
	}

	/**
	 * Get the responder. The responder is the full JID of the entity that has
	 * replied to the initiation (which may be different to the "to" addresss in
	 * the IQ).
	 * 
	 * @return the responder
	 */
	public String getResponder() {
		return responder;
	}

	/**
	 * Set the responder. The responder must be the full JID of the entity that
	 * has replied to the initiation (which may be different to the "to"
	 * addresss in the IQ).
	 * 
	 * @param resp the responder to set
	 */
	public void setResponder(final String resp) {
		responder = resp;
	}

	/**
	 * Get a hash key for the session this packet belongs to.
	 * 
	 * @param sid The session id
	 * @param initiator The initiator
	 * @return A hash key
	 */
	public static int getSessionHash(final String sid, final String initiator) {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + (initiator == null ? 0 : initiator.hashCode());
		result = PRIME * result + (sid == null ? 0 : sid.hashCode());
		return result;
	}

	/**
	 * Return the XML representation of the packet.
	 * 
	 * @return the XML string
	 */
	public String getChildElementXML() {
		StringBuffer buf = new StringBuffer();

		buf.append("<").append(getElementName());
		buf.append(" xmlns=\"").append(getNamespace()).append("\"");
		if (getInitiator() != null) {
			buf.append(" initiator=\"").append(getInitiator()).append("\"");
		}
		if (getResponder() != null) {
			buf.append(" responder=\"").append(getResponder()).append("\"");
		}
		if (getAction() != null) {
			buf.append(" action=\"").append(getAction()).append("\"");
		}
		if (getSid() != null) {
			buf.append(" sid=\"").append(getSid()).append("\"");
		}
		buf.append(">");

		// Look for possible payload types, and dump them.
		synchronized (descriptions) {
			for (int i = 0; i < descriptions.size(); i++) {
				JingleContentDescription desc = (JingleContentDescription) descriptions
						.get(i);
				buf.append(desc.toXML());
			}
		}

		// If the packet has transports, dump them.
		synchronized (transports) {
			for (int i = 0; i < transports.size(); i++) {
				JingleTransport trans = (JingleTransport) transports.get(i);
				buf.append(trans.toXML());
			}
		}

		// and the same for audio media info
		if (contentInfo != null) {
			buf.append(contentInfo.toXML());
		}

		buf.append("</").append(getElementName()).append(">");
		return buf.toString();
	}

	/**
	 * The "action" in the jingle packet, as an enum.
	 */
	public static class Action {

		public static final Action CONTENTACCEPT = new Action("description-accept");

		public static final Action CONTENTDECLINE = new Action("description-decline");

		public static final Action CONTENTINFO = new Action("description-info");

		public static final Action CONTENTMODIFY = new Action("description-modify");

		public static final Action SESSIONACCEPT = new Action("session-accept");

		public static final Action SESSIONINFO = new Action("session-info");

		public static final Action SESSIONINITIATE = new Action("session-initiate");

		public static final Action SESSIONREDIRECT = new Action("session-redirect");

		public static final Action SESSIONEXTEND = new Action("session-extend");

		public static final Action SESSIONREDUCE = new Action("session-reduce");

		public static final Action SESSIONTERMINATE = new Action("session-terminate");

		public static final Action TRANSPORTACCEPT = new Action("transport-accept");

		public static final Action TRANSPORTDECLINE = new Action("transport-decline");

		public static final Action TRANSPORTINFO = new Action("transport-info");

		public static final Action TRANSPORTMODIFY = new Action("transport-modify");

		private String value;

		public Action(final String value) {
			this.value = value;
		}

		/**
		 * Returns the String value for an Action.
		 */
		public String toString() {
			return value;
		}

		/**
		 * Returns a Action instance associated with the String value.
		 */
		public static Action fromString(String value) {
			if (value != null) {
				value = value.toLowerCase();
				if (value.equals("description-accept")) {
					return CONTENTACCEPT;
				} else if (value.equals("description-decline")) {
					return CONTENTDECLINE;
				} else if (value.equals("description-info")) {
					return CONTENTINFO;
				} else if (value.equals("description-modify")) {
					return CONTENTMODIFY;
				} else if (value.equals("session-accept")) {
					return SESSIONACCEPT;
				} else if (value.equals("session-info")) {
					return SESSIONINFO;
				} else if (value.equals("session-initiate")) {
					return SESSIONINITIATE;
				} else if (value.equals("session-redirect")) {
					return SESSIONACCEPT;
				} else if (value.equals("session-extend")) {
					return SESSIONEXTEND;
				} else if (value.equals("session-reduce")) {
					return SESSIONREDUCE;
				} else if (value.equals("session-terminate")) {
					return SESSIONTERMINATE;
				} else if (value.equals("transport-accept")) {
					return TRANSPORTACCEPT;
				} else if (value.equals("transport-decline")) {
					return TRANSPORTDECLINE;
				} else if (value.equals("transport-info")) {
					return TRANSPORTINFO;
				} else if (value.equals("transport-modify")) {
					return TRANSPORTMODIFY;
				}
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
			final int PRIME = 31;
			int result = 1;
			result = PRIME * result + (value == null ? 0 : value.hashCode());
			return result;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(final Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}
			final Action other = (Action) obj;
			if (value == null) {
				if (other.value != null) {
					return false;
				}
			} else if (!value.equals(other.value)) {
				return false;
			}
			return true;
		}
	}
}

Back to the top