Search in sources :

Example 1 with StreamInitiation

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;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 2 with StreamInitiation

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;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) IQ(org.jivesoftware.smack.packet.IQ)

Example 3 with StreamInitiation

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;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) ListSingleFormField(org.jivesoftware.smackx.xdata.ListSingleFormField) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm)

Example 4 with StreamInitiation

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;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) DataFormProvider(org.jivesoftware.smackx.xdata.provider.DataFormProvider) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ParseException(java.text.ParseException) File(org.jivesoftware.smackx.si.packet.StreamInitiation.File) Date(java.util.Date)

Example 5 with StreamInitiation

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;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) DataFormProvider(org.jivesoftware.smackx.xdata.provider.DataFormProvider) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ParseException(java.text.ParseException) File(org.jivesoftware.smackx.si.packet.StreamInitiation.File) Date(java.util.Date)

Aggregations

StreamInitiation (org.jivesoftware.smackx.si.packet.StreamInitiation)8 IQ (org.jivesoftware.smack.packet.IQ)4 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)4 ParseException (java.text.ParseException)2 Date (java.util.Date)2 File (org.jivesoftware.smackx.si.packet.StreamInitiation.File)2 ListSingleFormField (org.jivesoftware.smackx.xdata.ListSingleFormField)2 DataFormProvider (org.jivesoftware.smackx.xdata.provider.DataFormProvider)2 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 Stanza (org.jivesoftware.smack.packet.Stanza)1 StanzaError (org.jivesoftware.smack.packet.StanzaError)1 NoAcceptableTransferMechanisms (org.jivesoftware.smackx.filetransfer.FileTransferException.NoAcceptableTransferMechanisms)1 NoStreamMethodsOfferedException (org.jivesoftware.smackx.filetransfer.FileTransferException.NoStreamMethodsOfferedException)1 FormField (org.jivesoftware.smackx.xdata.FormField)1