use of org.jivesoftware.smackx.si.packet.StreamInitiation in project Smack by igniterealtime.
the class StreamNegotiator method createInitiationAccept.
/**
* Creates the initiation acceptance stanza(/packet) to forward to the stream
* initiator.
*
* @param streamInitiationOffer The offer from the stream initiator to connect for a stream.
* @param namespaces The namespace that relates to the accepted means of transfer.
* @return The response to be forwarded to the initiator.
*/
protected static StreamInitiation createInitiationAccept(StreamInitiation streamInitiationOffer, String[] namespaces) {
StreamInitiation response = new StreamInitiation();
response.setTo(streamInitiationOffer.getFrom());
response.setFrom(streamInitiationOffer.getTo());
response.setType(IQ.Type.result);
response.setStanzaId(streamInitiationOffer.getStanzaId());
DataForm form = new DataForm(DataForm.Type.submit);
FormField field = new FormField(FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
for (String namespace : namespaces) {
field.addValue(namespace);
}
form.addField(field);
response.setFeatureNegotiationForm(form);
return response;
}
use of org.jivesoftware.smackx.si.packet.StreamInitiation in project Smack by igniterealtime.
the class StreamNegotiator method initiateIncomingStream.
protected final IQ initiateIncomingStream(final XMPPConnection connection, StreamInitiation initiation) throws NoResponseException, XMPPErrorException, NotConnectedException {
final StreamInitiation response = createInitiationAccept(initiation, getNamespace());
newStreamInitiation(initiation.getFrom(), initiation.getSessionID());
final String eventKey = initiation.getFrom().toString() + '\t' + initiation.getSessionID();
IQ streamMethodInitiation;
try {
streamMethodInitiation = initationSetEvents.performActionAndWaitForEvent(eventKey, connection.getReplyTimeout(), new EventManger.Callback<NotConnectedException>() {
@Override
public void action() throws NotConnectedException {
try {
connection.sendStanza(response);
} catch (InterruptedException e) {
// Ignore
}
}
});
} catch (InterruptedException e) {
// TODO remove this try/catch once merged into 4.2's master branch
throw new IllegalStateException(e);
}
if (streamMethodInitiation == null) {
throw NoResponseException.newWith(connection, "stream initiation");
}
XMPPErrorException.ifHasErrorThenThrow(streamMethodInitiation);
return streamMethodInitiation;
}
use of org.jivesoftware.smackx.si.packet.StreamInitiation in project Smack by igniterealtime.
the class StreamNegotiator method createInitiationAccept.
/**
* Creates the initiation acceptance stanza to forward to the stream
* initiator.
*
* @param streamInitiationOffer The offer from the stream initiator to connect for a stream.
* @param namespace The namespace that relates to the accepted means of transfer.
* @return The response to be forwarded to the initiator.
*/
protected static StreamInitiation createInitiationAccept(StreamInitiation streamInitiationOffer, String namespace) {
StreamInitiation response = new StreamInitiation();
response.setTo(streamInitiationOffer.getFrom());
response.setFrom(streamInitiationOffer.getTo());
response.setType(IQ.Type.result);
response.setStanzaId(streamInitiationOffer.getStanzaId());
DataForm.Builder form = DataForm.builder();
ListSingleFormField.Builder field = FormField.listSingleBuilder(FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
field.setValue(namespace);
form.addField(field.build());
response.setFeatureNegotiationForm(form.build());
return response;
}
use of org.jivesoftware.smackx.si.packet.StreamInitiation in project Smack by igniterealtime.
the class StreamInitiationProvider method parse.
@Override
public StreamInitiation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
// si
String id = parser.getAttributeValue("", "id");
String mimeType = parser.getAttributeValue("", "mime-type");
StreamInitiation initiation = new StreamInitiation();
// file
String name = null;
String size = null;
String hash = null;
String date = null;
String desc = null;
boolean isRanged = false;
// feature
DataForm form = null;
DataFormProvider dataFormProvider = new DataFormProvider();
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
String elementName = parser.getName();
String namespace = parser.getNamespace();
if (elementName.equals("file")) {
name = parser.getAttributeValue("", "name");
size = parser.getAttributeValue("", "size");
hash = parser.getAttributeValue("", "hash");
date = parser.getAttributeValue("", "date");
} else if (elementName.equals("desc")) {
desc = parser.nextText();
} else if (elementName.equals("range")) {
isRanged = true;
} else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
form = dataFormProvider.parse(parser);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (parser.getDepth() == initialDepth) {
break outerloop;
}
if (parser.getName().equals("file")) {
long fileSize = 0;
if (size != null && size.trim().length() != 0) {
try {
fileSize = Long.parseLong(size);
} catch (NumberFormatException e) {
LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e);
}
}
Date fileDate = new Date();
if (date != null) {
try {
fileDate = XmppDateTime.parseDate(date);
} catch (ParseException e) {
// couldn't parse date, use current date-time
}
}
File file = new File(name, fileSize);
file.setHash(hash);
file.setDate(fileDate);
file.setDesc(desc);
file.setRanged(isRanged);
initiation.setFile(file);
}
}
}
initiation.setSessionID(id);
initiation.setMimeType(mimeType);
initiation.setFeatureNegotiationForm(form);
return initiation;
}
use of org.jivesoftware.smackx.si.packet.StreamInitiation in project Smack by igniterealtime.
the class StreamInitiationProvider method parse.
@Override
public StreamInitiation parse(XmlPullParser parser, int initialDepth) throws Exception {
boolean done = false;
// si
String id = parser.getAttributeValue("", "id");
String mimeType = parser.getAttributeValue("", "mime-type");
StreamInitiation initiation = new StreamInitiation();
// file
String name = null;
String size = null;
String hash = null;
String date = null;
String desc = null;
boolean isRanged = false;
// feature
DataForm form = null;
DataFormProvider dataFormProvider = new DataFormProvider();
int eventType;
String elementName;
String namespace;
while (!done) {
eventType = parser.next();
elementName = parser.getName();
namespace = parser.getNamespace();
if (eventType == XmlPullParser.START_TAG) {
if (elementName.equals("file")) {
name = parser.getAttributeValue("", "name");
size = parser.getAttributeValue("", "size");
hash = parser.getAttributeValue("", "hash");
date = parser.getAttributeValue("", "date");
} else if (elementName.equals("desc")) {
desc = parser.nextText();
} else if (elementName.equals("range")) {
isRanged = true;
} else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
form = dataFormProvider.parse(parser);
}
} else if (eventType == XmlPullParser.END_TAG) {
if (elementName.equals("si")) {
done = true;
} else if (elementName.equals("file")) {
long fileSize = 0;
if (size != null && size.trim().length() != 0) {
try {
fileSize = Long.parseLong(size);
} catch (NumberFormatException e) {
LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e);
}
}
Date fileDate = new Date();
if (date != null) {
try {
fileDate = XmppDateTime.parseDate(date);
} catch (ParseException e) {
// couldn't parse date, use current date-time
}
}
File file = new File(name, fileSize);
file.setHash(hash);
file.setDate(fileDate);
file.setDesc(desc);
file.setRanged(isRanged);
initiation.setFile(file);
}
}
}
initiation.setSessionID(id);
initiation.setMimeType(mimeType);
initiation.setFeatureNegotiationForm(form);
return initiation;
}
Aggregations