Skip to content

Commit e3b0fd7

Browse files
committed
Initial support for XEP-0447: Stateless File Sharing
1 parent e80c983 commit e3b0fd7

File tree

7 files changed

+382
-0
lines changed

7 files changed

+382
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jivesoftware.smackx.stateless_file_sharing.element;
18+
19+
import javax.xml.namespace.QName;
20+
21+
import org.jivesoftware.smack.packet.ExtensionElement;
22+
import org.jivesoftware.smack.packet.XmlEnvironment;
23+
import org.jivesoftware.smack.util.EqualsUtil;
24+
import org.jivesoftware.smack.util.HashCode;
25+
import org.jivesoftware.smack.util.XmlStringBuilder;
26+
import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement;
27+
28+
public class FileSharingElement implements ExtensionElement {
29+
30+
public static final String ELEMENT = "file-sharing";
31+
public static final String NAMESPACE = "urn:xmpp:sfs:0";
32+
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
33+
34+
private final FileMetadataElement metadataElement;
35+
private final SourcesElement sourcesElement;
36+
37+
public FileSharingElement(FileMetadataElement metadata, SourcesElement sources) {
38+
this.metadataElement = metadata;
39+
this.sourcesElement = sources;
40+
}
41+
42+
public FileMetadataElement getMetadata() {
43+
return metadataElement;
44+
}
45+
46+
public SourcesElement getSources() {
47+
return sourcesElement;
48+
}
49+
50+
@Override
51+
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
52+
return new XmlStringBuilder(this)
53+
.rightAngleBracket()
54+
.append(getMetadata())
55+
.append(getSources())
56+
.closeElement(this);
57+
}
58+
59+
@Override
60+
public String getNamespace() {
61+
return NAMESPACE;
62+
}
63+
64+
@Override
65+
public String getElementName() {
66+
return ELEMENT;
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return HashCode.builder()
72+
.append(getElementName())
73+
.append(getNamespace())
74+
.append(getMetadata())
75+
.append(getSources())
76+
.build();
77+
}
78+
79+
@Override
80+
public boolean equals(Object obj) {
81+
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
82+
equalsBuilder
83+
.append(getElementName(), other.getElementName())
84+
.append(getNamespace(), other.getNamespace())
85+
.append(getMetadata(), other.getMetadata())
86+
.append(getSources(), other.getSources()));
87+
}
88+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jivesoftware.smackx.stateless_file_sharing.element;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.jivesoftware.smack.packet.ExtensionElement;
24+
import org.jivesoftware.smack.packet.NamedElement;
25+
import org.jivesoftware.smack.packet.XmlEnvironment;
26+
import org.jivesoftware.smack.util.XmlStringBuilder;
27+
import org.jivesoftware.smackx.url_address_information.element.UrlDataElement;
28+
29+
public class SourcesElement implements NamedElement {
30+
31+
public static final String ELEMENT = "sources";
32+
33+
private final List<UrlDataElement> urlDataElements = new ArrayList<>();
34+
private final List<ExtensionElement> otherSourceElements = new ArrayList<>();
35+
36+
public SourcesElement(List<UrlDataElement> urlDataElements, List<ExtensionElement> otherSourceElements) {
37+
this.urlDataElements.addAll(urlDataElements);
38+
this.otherSourceElements.addAll(otherSourceElements);
39+
}
40+
41+
@Override
42+
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
43+
return new XmlStringBuilder(this)
44+
.rightAngleBracket()
45+
.append(getUrlDataElements())
46+
.append(getOtherSourceElements())
47+
.closeElement(this);
48+
}
49+
50+
public List<UrlDataElement> getUrlDataElements() {
51+
return Collections.unmodifiableList(urlDataElements);
52+
}
53+
54+
public List<ExtensionElement> getOtherSourceElements() {
55+
return Collections.unmodifiableList(otherSourceElements);
56+
}
57+
58+
@Override
59+
public String getElementName() {
60+
return ELEMENT;
61+
}
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/**
18+
* Element classes for XEP-0447: Stateless File Sharing.
19+
*
20+
* @see <a href="https://xmpp.org/extensions/xep-0447.html">XEP-0447: Stateless File Sharing</a>
21+
*/
22+
package org.jivesoftware.smackx.stateless_file_sharing.element;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/**
18+
* Smack's API for XEP-0447: Stateless File Sharing.
19+
*
20+
* @see <a href="https://xmpp.org/extensions/xep-0447.html">XEP-0447: Stateless File Sharing</a>
21+
*/
22+
package org.jivesoftware.smackx.stateless_file_sharing;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jivesoftware.smackx.stateless_file_sharing.provider;
18+
19+
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.jivesoftware.smack.packet.ExtensionElement;
24+
import org.jivesoftware.smack.packet.XmlEnvironment;
25+
import org.jivesoftware.smack.parsing.SmackParsingException;
26+
import org.jivesoftware.smack.parsing.StandardExtensionElementProvider;
27+
import org.jivesoftware.smack.provider.ExtensionElementProvider;
28+
import org.jivesoftware.smack.provider.ProviderManager;
29+
import org.jivesoftware.smack.xml.XmlPullParser;
30+
import org.jivesoftware.smack.xml.XmlPullParserException;
31+
import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement;
32+
import org.jivesoftware.smackx.file_metadata.provider.FileMetadataElementProvider;
33+
import org.jivesoftware.smackx.stateless_file_sharing.element.FileSharingElement;
34+
import org.jivesoftware.smackx.stateless_file_sharing.element.SourcesElement;
35+
import org.jivesoftware.smackx.url_address_information.element.UrlDataElement;
36+
import org.jivesoftware.smackx.url_address_information.provider.UrlDataElementProvider;
37+
38+
public class FileSharingElementProvider extends ExtensionElementProvider<FileSharingElement> {
39+
40+
public static final FileSharingElementProvider INSTANCE = new FileSharingElementProvider();
41+
42+
@Override
43+
public FileSharingElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
44+
throws XmlPullParserException, IOException, SmackParsingException {
45+
FileMetadataElement fileMetadataElement = null;
46+
SourcesElement sourcesElement = null;
47+
List<UrlDataElement> urlDataElements = new ArrayList<>();
48+
List<ExtensionElement> otherSourceElements = new ArrayList<>();
49+
do {
50+
XmlPullParser.TagEvent event = parser.nextTag();
51+
String name = parser.getName();
52+
53+
if (event == XmlPullParser.TagEvent.START_ELEMENT) {
54+
if (name.equals(FileMetadataElement.ELEMENT)) {
55+
fileMetadataElement = FileMetadataElementProvider.TEST_INSTANCE.parse(parser, xmlEnvironment);
56+
} else if (name.equals(SourcesElement.ELEMENT)) {
57+
int innerDepth = parser.getDepth();
58+
do {
59+
XmlPullParser.TagEvent innerEvent = parser.nextTag();
60+
String innerName = parser.getName();
61+
if (innerEvent.equals(XmlPullParser.TagEvent.START_ELEMENT)) {
62+
if (innerName.equals(UrlDataElement.ELEMENT)) {
63+
urlDataElements.add(UrlDataElementProvider.INSTANCE.parse(parser));
64+
} else {
65+
ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(innerName, parser.getNamespace());
66+
if (provider == null) {
67+
provider = new StandardExtensionElementProvider();
68+
}
69+
otherSourceElements.add(provider.parse(parser));
70+
}
71+
} else {
72+
if (innerName.equals(SourcesElement.ELEMENT)) {
73+
sourcesElement = new SourcesElement(urlDataElements, otherSourceElements);
74+
}
75+
}
76+
} while (parser.getDepth() != innerDepth);
77+
}
78+
}
79+
} while (parser.getDepth() != initialDepth);
80+
return new FileSharingElement(fileMetadataElement, sourcesElement);
81+
}
82+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/**
18+
* Provider classes for XEP-0447: Stateless File Sharing.
19+
*
20+
* @see <a href="https://xmpp.org/extensions/xep-0447.html">XEP-0447: Stateless File Sharing</a>
21+
*/
22+
package org.jivesoftware.smackx.stateless_file_sharing.provider;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.jivesoftware.smackx.stateless_file_sharing;
2+
3+
import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.io.IOException;
7+
import java.util.Collections;
8+
9+
import org.jivesoftware.smack.packet.StandardExtensionElement;
10+
import org.jivesoftware.smack.parsing.SmackParsingException;
11+
import org.jivesoftware.smack.test.util.SmackTestSuite;
12+
import org.jivesoftware.smack.test.util.TestUtils;
13+
import org.jivesoftware.smack.xml.XmlPullParserException;
14+
import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement;
15+
import org.jivesoftware.smackx.hashes.HashManager;
16+
import org.jivesoftware.smackx.hashes.element.HashElement;
17+
import org.jivesoftware.smackx.stateless_file_sharing.element.FileSharingElement;
18+
import org.jivesoftware.smackx.stateless_file_sharing.element.SourcesElement;
19+
import org.jivesoftware.smackx.stateless_file_sharing.provider.FileSharingElementProvider;
20+
import org.jivesoftware.smackx.url_address_information.element.UrlDataElement;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
public class FileSharingElementTest extends SmackTestSuite {
25+
26+
@Test
27+
public void simpleElementTest() throws XmlPullParserException, IOException, SmackParsingException {
28+
FileSharingElement fileSharingElement = new FileSharingElement(
29+
FileMetadataElement.builder()
30+
.setMediaType("image/jpeg")
31+
.setName("summit.jpg")
32+
.setSize(3032449)
33+
.setDimensions(4096, 2160)
34+
.addHash(new HashElement(HashManager.ALGORITHM.SHA3_256, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU="))
35+
.addHash(new HashElement(HashManager.ALGORITHM.BLAKE2B256, "2AfMGH8O7UNPTvUVAM9aK13mpCY="))
36+
.addDescription("Photo from the summit.")
37+
.addOtherChildElement(
38+
StandardExtensionElement.builder("thumbnail", "urn:xmpp:thumbs:1")
39+
.addAttribute("uri", "cid:[email protected]")
40+
.addAttribute("media-type", "image/png")
41+
.addAttribute("width", "128")
42+
.addAttribute("height", "96")
43+
.build())
44+
.build(),
45+
new SourcesElement(Collections.singletonList(
46+
new UrlDataElement(
47+
"https://download.montague.lit/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/summit.jpg",
48+
null
49+
)
50+
), Collections.singletonList(
51+
StandardExtensionElement.builder("jinglepub", "urn:xmpp:jinglepub:1")
52+
.addAttribute("from", "[email protected]/resource")
53+
.addAttribute("id", "9559976B-3FBF-4E7E-B457-2DAA225972BB")
54+
.addElement(new StandardExtensionElement("description", "urn:xmpp:jingle:apps:file-transfer:5"))
55+
.build()
56+
)));
57+
58+
final String expectedXml = "" +
59+
" <file-sharing xmlns='urn:xmpp:sfs:0'>\n" +
60+
" <file xmlns='urn:xmpp:file:metadata:0'>\n" +
61+
" <media-type>image/jpeg</media-type>\n" +
62+
" <name>summit.jpg</name>\n" +
63+
" <size>3032449</size>\n" +
64+
" <dimensions>4096x2160</dimensions>\n" +
65+
" <hash xmlns='urn:xmpp:hashes:2' algo='sha3-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>\n" +
66+
" <hash xmlns='urn:xmpp:hashes:2' algo='id-blake2b256'>2AfMGH8O7UNPTvUVAM9aK13mpCY=</hash>\n" +
67+
" <desc>Photo from the summit.</desc>\n" +
68+
" <thumbnail xmlns='urn:xmpp:thumbs:1' uri='cid:[email protected]' media-type='image/png' width='128' height='96'/>\n" +
69+
" </file>\n" +
70+
" <sources>\n" +
71+
" <url-data xmlns='http://jabber.org/protocol/url-data' target='https://download.montague.lit/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/summit.jpg' />\n" +
72+
" <jinglepub xmlns='urn:xmpp:jinglepub:1' from='[email protected]/resource' id='9559976B-3FBF-4E7E-B457-2DAA225972BB'>" +
73+
" <description xmlns='urn:xmpp:jingle:apps:file-transfer:5' />\n" +
74+
" </jinglepub>\n" +
75+
" </sources>\n" +
76+
" </file-sharing>";
77+
assertXmlSimilar(expectedXml, fileSharingElement.toXML().toString());
78+
79+
FileSharingElement parsed = FileSharingElementProvider.INSTANCE.parse(TestUtils.getParser(expectedXml));
80+
// While I'd rather test for equality here, we have to compare XML instead, as thumbnail is not implemented
81+
// and we have to fall back to a StandardExtensionElement which has a non-ideal equals() implementation.
82+
assertXmlSimilar(expectedXml, parsed.toXML().toString());
83+
}
84+
}

0 commit comments

Comments
 (0)