Search in sources :

Example 1 with HashElement

use of org.jivesoftware.smackx.hashes.element.HashElement in project Smack by igniterealtime.

the class JingleFileTransferProvider method parse.

@Override
public JingleFileTransfer parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
    ArrayList<JingleContentDescriptionChildElement> payloads = new ArrayList<>();
    JingleFileTransferChild.Builder builder = JingleFileTransferChild.getBuilder();
    String elementName;
    while (true) {
        XmlPullParser.TagEvent tag = parser.nextTag();
        switch(tag) {
            case START_ELEMENT:
                elementName = parser.getName();
                switch(elementName) {
                    case JingleFileTransferChild.ELEM_DATE:
                        builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
                        break;
                    case JingleFileTransferChild.ELEM_DESC:
                        builder.setDescription(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_MEDIA_TYPE:
                        builder.setMediaType(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_NAME:
                        builder.setName(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_SIZE:
                        builder.setSize(Integer.parseInt(parser.nextText()));
                        break;
                    case Range.ELEMENT:
                        Range range = parseRangeElement(parser);
                        builder.setRange(range);
                        break;
                    case HashElement.ELEMENT:
                        HashElement hashElement = HashElementProvider.INSTANCE.parse(parser);
                        builder.setHash(hashElement);
                        break;
                }
                break;
            case END_ELEMENT:
                elementName = parser.getName();
                switch(elementName) {
                    case JingleFileTransferChild.ELEMENT:
                        payloads.add(builder.build());
                        builder = JingleFileTransferChild.getBuilder();
                        break;
                    case JingleContentDescription.ELEMENT:
                        return new JingleFileTransfer(payloads);
                }
                break;
        }
    }
}
Also used : JingleFileTransferChild(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild) JingleFileTransfer(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer) HashElement(org.jivesoftware.smackx.hashes.element.HashElement) ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) JingleContentDescriptionChildElement(org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement) Range(org.jivesoftware.smackx.jingle_filetransfer.element.Range)

Example 2 with HashElement

use of org.jivesoftware.smackx.hashes.element.HashElement in project Smack by igniterealtime.

the class JingleFileTransferProvider method parseRangeElement.

public static Range parseRangeElement(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    final Integer offset = ParserUtils.getIntegerAttribute(parser, Range.ATTR_OFFSET);
    final Integer length = ParserUtils.getIntegerAttribute(parser, Range.ATTR_LENGTH);
    HashElement hashElement = null;
    outerloop: while (true) {
        String element;
        XmlPullParser.Event event = parser.next();
        switch(event) {
            case START_ELEMENT:
                element = parser.getName();
                switch(element) {
                    case HashElement.ELEMENT:
                        hashElement = HashElementProvider.INSTANCE.parse(parser);
                        break;
                }
                break;
            case END_ELEMENT:
                element = parser.getName();
                if (element.equals(Range.ELEMENT) && parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return new Range(offset, length, hashElement);
}
Also used : HashElement(org.jivesoftware.smackx.hashes.element.HashElement) Range(org.jivesoftware.smackx.jingle_filetransfer.element.Range)

Example 3 with HashElement

use of org.jivesoftware.smackx.hashes.element.HashElement in project Smack by igniterealtime.

the class HashElementTest method stanzaTest.

@Test
public void stanzaTest() throws Exception {
    String message = "Hello World!";
    HashElement element = HashManager.calculateHashElement(SHA_256, StringUtils.toUtf8Bytes(message));
    String expected = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=</hash>";
    assertEquals(expected, element.toXML().toString());
    HashElement parsed = new HashElementProvider().parse(TestUtils.getParser(expected));
    assertEquals(expected, parsed.toXML().toString());
    assertEquals(SHA_256, parsed.getAlgorithm());
    assertEquals("f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=", parsed.getHashB64());
    assertArrayEquals(HashManager.sha_256(message), parsed.getHash());
    assertEquals(element, parsed);
    assertTrue(element.equals(parsed));
    HashElement other = new HashElement(HashManager.ALGORITHM.SHA_512, "861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8".getBytes(StandardCharsets.UTF_8));
    assertFalse(element.equals(other));
}
Also used : HashElement(org.jivesoftware.smackx.hashes.element.HashElement) HashElementProvider(org.jivesoftware.smackx.hashes.provider.HashElementProvider) Test(org.junit.jupiter.api.Test)

Example 4 with HashElement

use of org.jivesoftware.smackx.hashes.element.HashElement in project Smack by igniterealtime.

the class ChecksumProvider method parse.

@Override
public Checksum parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    JingleContent.Creator creator = null;
    String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR);
    if (creatorString != null) {
        creator = JingleContent.Creator.valueOf(creatorString);
    }
    String name = parser.getAttributeValue(null, Checksum.ATTR_NAME);
    JingleFileTransferChild.Builder cb = JingleFileTransferChild.getBuilder();
    HashElement hashElement = null;
    Range range = null;
    boolean go = true;
    while (go) {
        XmlPullParser.TagEvent tag = parser.nextTag();
        String n = parser.getText();
        switch(tag) {
            case START_ELEMENT:
                switch(n) {
                    case HashElement.ELEMENT:
                        hashElement = new HashElementProvider().parse(parser);
                        break;
                    case Range.ELEMENT:
                        String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET);
                        String length = parser.getAttributeValue(null, Range.ATTR_LENGTH);
                        int o = offset == null ? 0 : Integer.parseInt(offset);
                        int l = length == null ? -1 : Integer.parseInt(length);
                        range = new Range(o, l);
                }
                break;
            case END_ELEMENT:
                switch(n) {
                    case Range.ELEMENT:
                        if (hashElement != null && range != null) {
                            range = new Range(range.getOffset(), range.getLength(), hashElement);
                            hashElement = null;
                        }
                        break;
                    case JingleFileTransferChild.ELEMENT:
                        if (hashElement != null) {
                            cb.setHash(hashElement);
                        }
                        if (range != null) {
                            cb.setRange(range);
                        }
                        go = false;
                }
                break;
        }
    }
    return new Checksum(creator, name, cb.build());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) HashElementProvider(org.jivesoftware.smackx.hashes.provider.HashElementProvider) Range(org.jivesoftware.smackx.jingle_filetransfer.element.Range) JingleFileTransferChild(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild) Checksum(org.jivesoftware.smackx.jingle_filetransfer.element.Checksum) HashElement(org.jivesoftware.smackx.hashes.element.HashElement) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent)

Aggregations

HashElement (org.jivesoftware.smackx.hashes.element.HashElement)4 Range (org.jivesoftware.smackx.jingle_filetransfer.element.Range)3 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)2 HashElementProvider (org.jivesoftware.smackx.hashes.provider.HashElementProvider)2 JingleFileTransferChild (org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild)2 ArrayList (java.util.ArrayList)1 JingleContent (org.jivesoftware.smackx.jingle.element.JingleContent)1 JingleContentDescriptionChildElement (org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement)1 Checksum (org.jivesoftware.smackx.jingle_filetransfer.element.Checksum)1 JingleFileTransfer (org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer)1 Test (org.junit.jupiter.api.Test)1