1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-25 03:57:58 +01:00

Follow-up commit after merging support for XEP-0446: File Metadata Element

This is a follow-up commit after 441d677644 ("Initial support for
XEP-0446: File Metadata Element"). It includes the following changes

1. Use idiomatic provider design for FileMetadataElementProvider
2. Add XEP-0264 and XEP-0446 to the list of supported XEPs (both where
   added with441d6776447f)
This commit is contained in:
Florian Schmaus 2023-12-16 16:53:34 +01:00
parent 3d6fa5dbae
commit b5180f819f
4 changed files with 120 additions and 78 deletions

View file

@ -33,64 +33,70 @@ import org.jivesoftware.smackx.thumbnails.provider.ThumbnailElementProvider;
public class FileMetadataElementProvider extends ExtensionElementProvider<FileMetadataElement> {
public static FileMetadataElementProvider TEST_INSTANCE = new FileMetadataElementProvider();
@Override
public FileMetadataElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
FileMetadataElement.Builder builder = FileMetadataElement.builder();
do {
XmlPullParser.TagEvent tagEvent = parser.nextTag();
String name = parser.getName();
if (tagEvent != XmlPullParser.TagEvent.START_ELEMENT) {
continue;
outerloop: while (true) {
XmlPullParser.Event event = parser.next();
switch (event) {
case START_ELEMENT:
String name = parser.getName();
switch (name) {
case FileMetadataElement.ELEMENT:
parser.next();
break;
case FileMetadataElement.ELEM_DATE:
builder.setModificationDate(ParserUtils.getDateFromNextText(parser));
break;
case FileMetadataElement.ELEM_DESC:
String lang = ParserUtils.getXmlLang(parser);
builder.addDescription(ParserUtils.getRequiredNextText(parser), lang);
break;
case "dimensions": // was replaced with width and height
String dimensions = ParserUtils.getRequiredNextText(parser);
String[] split = dimensions.split("x");
if (split.length != 2) {
throw new IllegalArgumentException("Invalid dimensions.");
}
builder.setWidth(Integer.parseInt(split[0]));
builder.setHeight(Integer.parseInt(split[1]));
break;
case FileMetadataElement.ELEM_WIDTH:
builder.setWidth(Integer.parseInt(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_HEIGHT:
builder.setHeight(Integer.parseInt(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_LENGTH:
builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_MEDIA_TYPE:
builder.setMediaType(ParserUtils.getRequiredNextText(parser));
break;
case FileMetadataElement.ELEM_NAME:
builder.setName(ParserUtils.getRequiredNextText(parser));
break;
case FileMetadataElement.ELEM_SIZE:
builder.setSize(Long.parseLong(ParserUtils.getRequiredNextText(parser)));
break;
case HashElement.ELEMENT:
builder.addHash(HashElementProvider.INSTANCE.parse(parser, parser.getDepth(), xmlEnvironment));
break;
case ThumbnailElement.ELEMENT:
ThumbnailElementProvider provider = new ThumbnailElementProvider();
builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment));
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) break outerloop;
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
switch (name) {
case FileMetadataElement.ELEMENT:
parser.next();
break;
case FileMetadataElement.ELEM_DATE:
builder.setModificationDate(ParserUtils.getDateFromNextText(parser));
break;
case FileMetadataElement.ELEM_DESC:
String lang = ParserUtils.getXmlLang(parser);
builder.addDescription(ParserUtils.getRequiredNextText(parser), lang);
break;
case "dimensions": // was replaced with width and height
String dimensions = ParserUtils.getRequiredNextText(parser);
String[] split = dimensions.split("x");
if (split.length != 2) {
throw new IllegalArgumentException("Invalid dimensions.");
}
builder.setWidth(Integer.parseInt(split[0]));
builder.setHeight(Integer.parseInt(split[1]));
break;
case FileMetadataElement.ELEM_WIDTH:
builder.setWidth(Integer.parseInt(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_HEIGHT:
builder.setHeight(Integer.parseInt(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_LENGTH:
builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser)));
break;
case FileMetadataElement.ELEM_MEDIA_TYPE:
builder.setMediaType(ParserUtils.getRequiredNextText(parser));
break;
case FileMetadataElement.ELEM_NAME:
builder.setName(ParserUtils.getRequiredNextText(parser));
break;
case FileMetadataElement.ELEM_SIZE:
builder.setSize(Long.parseLong(ParserUtils.getRequiredNextText(parser)));
break;
case HashElement.ELEMENT:
builder.addHash(HashElementProvider.INSTANCE.parse(parser, parser.getDepth(), xmlEnvironment));
break;
case ThumbnailElement.ELEMENT:
ThumbnailElementProvider provider = new ThumbnailElementProvider();
builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment));
}
} while (parser.getDepth() != initialDepth);
}
return builder.build();
}
}

View file

@ -22,6 +22,7 @@ import java.text.ParseException;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.thumbnails.element.ThumbnailElement;
@ -32,14 +33,14 @@ public class ThumbnailElementProvider extends ExtensionElementProvider<Thumbnail
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
String uri = parser.getAttributeValue(ThumbnailElement.ELEM_URI);
String mediaType = parser.getAttributeValue(ThumbnailElement.ELEM_MEDIA_TYPE);
String width = parser.getAttributeValue(ThumbnailElement.ELEM_WIDTH);
String height = parser.getAttributeValue(ThumbnailElement.ELEM_HEIGHT);
Integer width = ParserUtils.getIntegerAttribute(parser, ThumbnailElement.ELEM_WIDTH);
Integer height = ParserUtils.getIntegerAttribute(parser, ThumbnailElement.ELEM_HEIGHT);
return new ThumbnailElement(
uri,
mediaType,
width == null ? null : Integer.parseInt(width),
height == null ? null : Integer.parseInt(height)
width,
height
);
}
}

View file

@ -31,7 +31,6 @@ import org.jivesoftware.smackx.file_metadata.provider.FileMetadataElementProvide
import org.jivesoftware.smackx.hashes.HashManager;
import org.jivesoftware.smackx.hashes.element.HashElement;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
@ -39,8 +38,28 @@ import org.jxmpp.util.XmppDateTime;
public class FileMetadataElementTest extends SmackTestSuite {
private static Date date;
private static FileMetadataElement metadataElement;
private static final Date date;
static {
try {
date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00");
} catch (ParseException e) {
throw new IllegalStateException(e);
}
}
private static final FileMetadataElement metadataElement = FileMetadataElement.builder()
.setModificationDate(date)
.setWidth(1920)
.setHeight(1080)
.addDescription("Picture of 24th XSF Summit")
.addDescription("Foto vom 24. XSF Summit", "de")
.addHash(new HashElement(HashManager.ALGORITHM.SHA_256, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU="))
.setLength(63000)
.setMediaType("text/plain")
.setName("text.txt")
.setSize(6144)
.build();
private static final String expectedXml = "<file xmlns='urn:xmpp:file:metadata:0'>" +
"<date>2015-07-26T20:46:00.000+00:00</date>" +
"<width>1920</width>" +
@ -66,24 +85,6 @@ public class FileMetadataElementTest extends SmackTestSuite {
"<size>6144</size>" +
"</file>";
@BeforeAll
public static void setup() throws ParseException {
date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00");
metadataElement = FileMetadataElement.builder()
.setModificationDate(date)
.setWidth(1920)
.setHeight(1080)
.addDescription("Picture of 24th XSF Summit")
.addDescription("Foto vom 24. XSF Summit", "de")
.addHash(new HashElement(HashManager.ALGORITHM.SHA_256, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU="))
.setLength(63000)
.setMediaType("text/plain")
.setName("text.txt")
.setSize(6144)
.build();
}
@Test
public void testSerialization() {
assertXmlSimilar(expectedXml, metadataElement.toXML().toString());
@ -105,6 +106,28 @@ public class FileMetadataElementTest extends SmackTestSuite {
assertEquals(metadataElement, parsed);
}
@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void testParseUnknownExtension(SmackTestUtil.XmlPullParserKind parserKind) throws Exception {
final String xml = "<file xmlns='urn:xmpp:file:metadata:0'>" +
"<date>2015-07-26T20:46:00.000+00:00</date>" +
"<width>1920</width>" +
"<height>1080</height>" +
"<unknown-extension>foo</unknown-extension>" +
"<desc>Picture of 24th XSF Summit</desc>" +
"<desc xml:lang='de'>Foto vom 24. XSF Summit</desc>" +
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>" +
"<length>63000</length>" +
"<media-type>text/plain</media-type>" +
"<name>text.txt</name>" +
"<size>6144</size>" +
"</file>";
FileMetadataElement parsed = SmackTestUtil.parse(xml, FileMetadataElementProvider.class, parserKind);
assertEquals(metadataElement, parsed);
}
@Test
public void nameIsEscaped() {
FileMetadataElement e = FileMetadataElement.builder().setName("/etc/passwd").build();

View file

@ -370,6 +370,12 @@
* <td>Allows sending a MUC invitation directly from the user to the contact with mediation by the room.</td>
* </tr>
* <tr>
* <td>Jingle Content Thumbnails</td>
* <td><a href="https://xmpp.org/extensions/xep-0264.html">XEP-0264</a></td>
* <td>{@link org.jivesoftware.smackx.thumbnails.element}</td>
* <td>Defines a way for a client to supply a preview image for Jingle content.</td>
* </tr>
* <tr>
* <td>Message Carbons</td>
* <td><a href="https://xmpp.org/extensions/xep-0280.html">XEP-0280</a></td>
* <td>{@link org.jivesoftware.smackx.carbons}</td>
@ -583,6 +589,12 @@
* <td>Declare body elements of a message as ignorable fallback for naive legacy clients.</td>
* </tr>
* <tr>
* <td>File metadata element</td>
* <td><a href="https://xmpp.org/extensions/xep-0446.html">XEP-0446</a></td>
* <td>{@link org.jivesoftware.smackx.file_metadata.element}</td>
* <td>Defines a generic file metadata element to be used in other specifications.</td>
* </tr>
* <tr>
* <td>Google GCM JSON payload</td>
* <td></td>
* <td></td>