1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-23 03:44:50 +02:00
Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java
Florian Schmaus 6bb001d274 New Jingle API groundwork
A start for the new Jingle API. Since Jingle is a single IQ with many
plugable extensions, there are some particularities we need to deal
with, e.g. jingle users have to register with JingleManager.

This is untested code. There may be drangons.
2017-05-30 08:45:27 +02:00

205 lines
5.8 KiB
Java

/**
*
* Copyright 2003-2007 Jive Software, 2014-2017 Florian Schmaus
*
* 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.jingle.element;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jxmpp.jid.FullJid;
/**
* The Jingle element.
*
* @author Florian Schmaus
*/
public final class Jingle extends IQ {
public static final String NAMESPACE = "urn:xmpp:jingle:1";
public static final String ACTION_ATTRIBUTE_NAME = "action";
public static final String INITIATOR_ATTRIBUTE_NAME = "initiator";
public static final String RESPONDER_ATTRIBUTE_NAME = "responder";
public static final String SESSION_ID_ATTRIBUTE_NAME = "sid";
public static final String ELEMENT = "jingle";
/**
* 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 &.
*/
private final String sessionId;
/**
* The jingle action. This attribute is required.
*/
private final JingleAction action;
private final FullJid initiator;
private final FullJid responder;
private final JingleReason reason;
private final List<JingleContent> contents;
private Jingle(String sessionId, JingleAction action, FullJid initiator, FullJid responder, JingleReason reason,
List<JingleContent> contents) {
super(ELEMENT, NAMESPACE);
this.sessionId = StringUtils.requireNotNullOrEmpty(sessionId, "Jingle session ID must not be null");
this.action = Objects.requireNonNull(action, "Jingle action must not be null");
this.initiator = initiator;
this.responder = responder;
this.reason = reason;
if (contents != null) {
this.contents = Collections.unmodifiableList(contents);
}
else {
this.contents = Collections.emptyList();
}
}
/**
* 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 FullJid getInitiator() {
return 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 FullJid getResponder() {
return responder;
}
/**
* 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.
*/
public String getSid() {
return sessionId;
}
/**
* Get the action specified in the jingle IQ.
*
* @return the action.
*/
public JingleAction getAction() {
return action;
}
/**
* Get a List of the contents.
*
* @return the contents.
*/
public List<JingleContent> getContents() {
return contents;
}
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.optAttribute(INITIATOR_ATTRIBUTE_NAME, getInitiator());
xml.optAttribute(RESPONDER_ATTRIBUTE_NAME, getResponder());
xml.optAttribute(ACTION_ATTRIBUTE_NAME, getAction());
xml.optAttribute(SESSION_ID_ATTRIBUTE_NAME, getSid());
xml.rightAngleBracket();
xml.optElement(reason);
xml.append(contents);
return xml;
}
public static Builder getBuilder() {
return new Builder();
}
public static final class Builder {
private String sid;
private JingleAction action;
private FullJid initiator;
private FullJid responder;
private JingleReason reason;
private List<JingleContent> contents;
private Builder() {
}
public Builder setSessionId(String sessionId) {
this.sid = sessionId;
return this;
}
public Builder setAction(JingleAction action) {
this.action = action;
return this;
}
public Builder setInitiator(FullJid initator) {
this.initiator = initator;
return this;
}
public Builder setResponder(FullJid responder) {
this.responder = responder;
return this;
}
public Builder addJingleContent(JingleContent content) {
if (contents == null) {
contents = new ArrayList<>(1);
}
contents.add(content);
return this;
}
public Builder setReason(JingleReason.Reason reason) {
this.reason = new JingleReason(reason);
return this;
}
public Jingle build() {
return new Jingle(sid, action, initiator, responder, reason, contents);
}
}
}