use of org.jivesoftware.smackx.packet.DataForm in project ecf by eclipse.
the class UserSearch method buildDataForm.
private static void buildDataForm(SimpleUserSearch search, String instructions, XmlPullParser parser) throws Exception {
DataForm dataForm = new DataForm(Form.TYPE_FORM);
boolean done = false;
dataForm.setTitle("User Search");
dataForm.addInstruction(instructions);
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && !parser.getNamespace().equals("jabber:x:data")) {
String name = parser.getName();
FormField field = new FormField(name);
// Handle hard coded values.
if (name.equals("first")) {
field.setLabel("First Name");
} else if (name.equals("last")) {
field.setLabel("Last Name");
} else if (name.equals("email")) {
field.setLabel("Email Address");
} else if (name.equals("nick")) {
field.setLabel("Nickname");
}
field.setType(FormField.TYPE_TEXT_SINGLE);
dataForm.addField(field);
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("query")) {
done = true;
}
} else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) {
search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser));
done = true;
}
}
if (search.getExtension("x", "jabber:x:data") == null) {
search.addExtension(dataForm);
}
}
use of org.jivesoftware.smackx.packet.DataForm in project ecf by eclipse.
the class EntityCapsManager method generateVerificationString.
/**
* Generates a XEP-115 Verification String
*
* @see <a href="http://xmpp.org/extensions/xep-0115.html#ver">XEP-115
* Verification String</a>
*
* @param discoverInfo
* @param hash
* the used hash function
* @return The generated verification String or null if the hash is not
* supported
*/
protected static String generateVerificationString(DiscoverInfo discoverInfo, String hash) {
MessageDigest md = SUPPORTED_HASHES.get(hash.toLowerCase());
if (md == null)
return null;
DataForm extendedInfo = (DataForm) discoverInfo.getExtension(Form.ELEMENT, Form.NAMESPACE);
// 1. Initialize an empty string S ('sb' in this method).
// Use StringBuilder as we don't
StringBuilder sb = new StringBuilder();
// need thread-safe StringBuffer
// 2. Sort the service discovery identities by category and then by
// type and then by xml:lang
// (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG] '/'
// [NAME]. Note that each slash is included even if the LANG or
// NAME is not included (in accordance with XEP-0030, the category and
// type MUST be included.
SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
for (Iterator<DiscoverInfo.Identity> it = discoverInfo.getIdentities(); it.hasNext(); ) sortedIdentities.add(it.next());
// followed by the '<' character.
for (Iterator<DiscoverInfo.Identity> it = sortedIdentities.iterator(); it.hasNext(); ) {
DiscoverInfo.Identity identity = it.next();
sb.append(identity.getCategory());
sb.append("/");
sb.append(identity.getType());
sb.append("/");
sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
sb.append("/");
sb.append(identity.getName() == null ? "" : identity.getName());
sb.append("<");
}
// 4. Sort the supported service discovery features.
SortedSet<String> features = new TreeSet<String>();
for (Iterator<Feature> it = discoverInfo.getFeatures(); it.hasNext(); ) features.add(it.next().getVar());
// character
for (String f : features) {
sb.append(f);
sb.append("<");
}
// see XEP-0115 5.4 step 3.6
if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
synchronized (extendedInfo) {
// 6. If the service discovery information response includes
// XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,
// by the XML character data of the <value/> element).
SortedSet<FormField> fs = new TreeSet<FormField>(new Comparator<FormField>() {
public int compare(FormField f1, FormField f2) {
return f1.getVariable().compareTo(f2.getVariable());
}
});
FormField ft = null;
for (Iterator<FormField> i = extendedInfo.getFields(); i.hasNext(); ) {
FormField f = i.next();
if (!f.getVariable().equals("FORM_TYPE")) {
fs.add(f);
} else {
ft = f;
}
}
// Add FORM_TYPE values
if (ft != null) {
formFieldValuesToCaps(ft.getValues(), sb);
}
// followed by the '<' character.
for (FormField f : fs) {
sb.append(f.getVariable());
sb.append("<");
formFieldValuesToCaps(f.getValues(), sb);
}
}
}
// 8. Ensure that S is encoded according to the UTF-8 encoding (RFC
// 3269).
// 9. Compute the verification string by hashing S using the algorithm
// specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC
// 3174).
// The hashed data MUST be generated with binary output and
// encoded using Base64 as specified in Section 4 of RFC 4648
// (note: the Base64 output MUST NOT include whitespace and MUST set
// padding bits to zero).
byte[] digest = md.digest(sb.toString().getBytes());
return Base64.encodeBytes(digest);
}
use of org.jivesoftware.smackx.packet.DataForm in project ecf by eclipse.
the class StreamNegotiator method createInitiationAccept.
/**
* Creates the initiation acceptance 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.
*/
public StreamInitiation createInitiationAccept(StreamInitiation streamInitiationOffer, String[] namespaces) {
StreamInitiation response = new StreamInitiation();
response.setTo(streamInitiationOffer.getFrom());
response.setFrom(streamInitiationOffer.getTo());
response.setType(IQ.Type.RESULT);
response.setPacketID(streamInitiationOffer.getPacketID());
DataForm form = new DataForm(Form.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.packet.DataForm in project ecf by eclipse.
the class FileTransferNegotiator method createDefaultInitiationForm.
private DataForm createDefaultInitiationForm() {
DataForm form = new DataForm(Form.TYPE_FORM);
FormField field = new FormField(STREAM_DATA_FIELD_NAME);
field.setType(FormField.TYPE_LIST_SINGLE);
if (!IBB_ONLY) {
field.addOption(new FormField.Option(Socks5BytestreamManager.NAMESPACE));
}
field.addOption(new FormField.Option(InBandBytestreamManager.NAMESPACE));
form.addField(field);
return form;
}
use of org.jivesoftware.smackx.packet.DataForm in project ecf by eclipse.
the class StreamInitiationProvider method parseIQ.
public IQ parseIQ(final XmlPullParser parser) 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 = (DataForm) dataFormProvider.parseExtension(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) {
e.printStackTrace();
}
}
Date fileDate = new Date();
if (date != null) {
try {
fileDate = StringUtils.parseXEP0082Date(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.setSesssionID(id);
initiation.setMimeType(mimeType);
initiation.setFeatureNegotiationForm(form);
return initiation;
}
Aggregations