Improve maintainability

file_metadata
Paul Schaub 2 years ago
parent 567a99ba45
commit 0fea306658
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
  1. 258
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/FileMetadataElement.java
  2. 77
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/DateElement.java
  3. 94
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/DescElement.java
  4. 75
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/DimensionsElement.java
  5. 77
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/LengthElement.java
  6. 78
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/MediaTypeElement.java
  7. 94
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/NameElement.java
  8. 72
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/SizeElement.java
  9. 20
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/element/child/package-info.java
  10. 25
      smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/provider/FileMetadataElementProvider.java
  11. 96
      smack-experimental/src/test/java/org/jivesoftware/smackx/file_metadata/FileMetadataElementTest.java

@ -16,29 +16,20 @@
*/
package org.jivesoftware.smackx.file_metadata.element;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.file_metadata.element.child.DateElement;
import org.jivesoftware.smackx.file_metadata.element.child.DescElement;
import org.jivesoftware.smackx.file_metadata.element.child.DimensionsElement;
import org.jivesoftware.smackx.file_metadata.element.child.LengthElement;
import org.jivesoftware.smackx.file_metadata.element.child.MediaTypeElement;
import org.jivesoftware.smackx.file_metadata.element.child.NameElement;
import org.jivesoftware.smackx.file_metadata.element.child.SizeElement;
import org.jivesoftware.smackx.hashes.HashManager;
import org.jivesoftware.smackx.hashes.element.HashElement;
@ -51,21 +42,61 @@ public final class FileMetadataElement implements ExtensionElement {
public static final String ELEMENT = "file";
public static final String NAMESPACE = "urn:xmpp:file:metadata:0";
private final Set<NamedElement> children = new HashSet<>();
private FileMetadataElement(Collection<NamedElement> childElements) {
this.children.addAll(childElements);
public static final String ELEM_DATE = "date";
public static final String ELEM_DIMENSIONS = "dimensions";
public static final String ELEM_DESC = "desc";
public static final String ELEM_LENGTH = "length";
public static final String ELEM_MEDIA_TYPE = "media-type";
public static final String ELEM_NAME = "name";
public static final String ELEM_SIZE = "size";
public static final String ELEM_THUMBNAIL = "thumbnail";
private final Date date;
private final String dimensions;
private final Map<String, String> descriptions = new ConcurrentHashMap<>();
private final Map<HashManager.ALGORITHM, HashElement> hashElements = new ConcurrentHashMap<>();
private final Long length;
private final String mediaType;
private final String name;
private final Long size;
private final Set<ExtensionElement> otherElements = new HashSet<>();
private FileMetadataElement(Date date, String dimensions, Map<String, String> descriptions,
Map<HashManager.ALGORITHM, HashElement> hashElements, Long length,
String mediaType, String name, Long size,
Set<ExtensionElement> otherElements) {
this.date = date;
this.dimensions = dimensions;
this.descriptions.putAll(descriptions);
this.hashElements.putAll(hashElements);
this.length = length;
this.mediaType = mediaType;
this.name = name;
this.size = size;
this.otherElements.addAll(otherElements);
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();
for (Element child : children) {
sb.append(child);
XmlStringBuilder sb = new XmlStringBuilder(this)
.rightAngleBracket()
.optElement(ELEM_DATE, date)
.optElement(ELEM_DIMENSIONS, dimensions);
for (String key : descriptions.keySet()) {
sb.halfOpenElement(ELEM_DESC)
.optXmlLangAttribute(key)
.rightAngleBracket()
.append(descriptions.get(key))
.closeElement(ELEM_DESC);
}
sb.closeElement(this);
return sb;
sb.append(hashElements.values())
.optElement(ELEM_LENGTH, length != null ? Long.toString(length) : null)
.optElement(ELEM_MEDIA_TYPE, mediaType)
.optElement(ELEM_NAME, name)
.optElement(ELEM_SIZE, size != null ? Long.toString(size) : null)
.append(otherElements);
return sb.closeElement(this);
}
@Override
@ -78,98 +109,74 @@ public final class FileMetadataElement implements ExtensionElement {
return ELEMENT;
}
public Set<NamedElement> getAllChildren() {
return Collections.unmodifiableSet(children);
public Date getDate() {
return date;
}
private NamedElement getChildElement(String name, String namespace) {
for (NamedElement element : getAllChildren()) {
if (!element.getElementName().equals(name)) {
continue;
}
if (namespace == null && !(element instanceof ExtensionElement)) {
return element;
}
ExtensionElement extensionElement = (ExtensionElement) element;
if (extensionElement.getNamespace().equals(namespace)) {
return extensionElement;
}
}
return null;
public String getDimensions() {
return dimensions;
}
public DateElement getDateElement() {
return (DateElement) getChildElement(DateElement.ELEMENT, null);
public Map<String, String> getDescriptions() {
return new ConcurrentHashMap<>(descriptions);
}
public DimensionsElement getDimensionsElement() {
return (DimensionsElement) getChildElement(DimensionsElement.ELEMENT, null);
public String getDescription() {
return getDescription(getLanguage());
}
public List<DescElement> getDescElements() {
List<DescElement> elements = new ArrayList<>();
for (NamedElement e : children) {
if (e instanceof DescElement) {
elements.add((DescElement) e);
}
}
return elements;
public String getDescription(String lang) {
return descriptions.get(lang != null ? lang : "");
}
public DescElement getDescElement() {
return getDescElement(getLanguage());
public Map<HashManager.ALGORITHM, HashElement> getHashElements() {
return new ConcurrentHashMap<>(hashElements);
}
public DescElement getDescElement(String lang) {
List<DescElement> descElements = getDescElements();
for (DescElement e : descElements) {
if (Objects.equals(lang, e.getLanguage())) {
return e;
}
}
return null;
}
public List<HashElement> getHashElements() {
List<HashElement> hashElements = new ArrayList<>();
for (NamedElement e : children) {
if (e instanceof HashElement) {
hashElements.add((HashElement) e);
}
}
return hashElements;
public HashElement getHashElement(HashManager.ALGORITHM algorithm) {
return hashElements.get(algorithm);
}
public HashElement getHashElement(HashManager.ALGORITHM algorithm) {
List<HashElement> hashElements = getHashElements();
for (HashElement e : hashElements) {
if (e.getAlgorithm() == algorithm) {
return e;
}
}
return null;
public Long getLength() {
return length;
}
public LengthElement getLengthElement() {
return (LengthElement) getChildElement(LengthElement.ELEMENT, null);
public String getMediaType() {
return mediaType;
}
public MediaTypeElement getMediaTypeElement() {
return (MediaTypeElement) getChildElement(MediaTypeElement.ELEMENT, null);
/**
* Return the name of the file.
*
* @return escaped name
*/
public String getName() {
if (name == null) {
return null;
}
try {
return URLEncoder.encode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // UTF-8 MUST be supported
}
}
public NameElement getNameElement() {
return (NameElement) getChildElement(NameElement.ELEMENT, null);
public String getRawName() {
return name;
}
public SizeElement getSizeElement() {
return (SizeElement) getChildElement(SizeElement.ELEMENT, null);
public Long getSize() {
return size;
}
public NamedElement getThumbnailElement() {
return getChildElement("thumbnail", "urn:xmpp:thumbs:1");
public Set<ExtensionElement> getThumbnails() {
Set<ExtensionElement> thumbnails = new HashSet<>();
for (ExtensionElement e : otherElements) {
if (isThumbnail(e)) {
thumbnails.add(e);
}
}
return thumbnails;
}
@Override
@ -177,7 +184,15 @@ public final class FileMetadataElement implements ExtensionElement {
return HashCode.builder()
.append(getElementName())
.append(getNamespace())
.append(getAllChildren())
.append(getDate())
.append(getDescriptions())
.append(getDimensions())
.append(getHashElements())
.append(getLength())
.append(getMediaType())
.append(getRawName())
.append(getSize())
.append(getThumbnails())
.build();
}
@ -186,7 +201,15 @@ public final class FileMetadataElement implements ExtensionElement {
return EqualsUtil.equals(this, other, (equalsBuilder, o) -> equalsBuilder
.append(getElementName(), o.getElementName())
.append(getNamespace(), o.getNamespace())
.append(getAllChildren(), o.getAllChildren()));
.append(getDate(), o.getDate())
.append(getDescriptions(), o.getDescriptions())
.append(getDimensions(), o.getDimensions())
.append(getHashElements(), o.getHashElements())
.append(getLength(), o.getLength())
.append(getMediaType(), o.getMediaType())
.append(getRawName(), o.getRawName())
.append(getSize(), o.getSize())
.append(getThumbnails(), o.getThumbnails()));
}
public static Builder builder() {
@ -194,10 +217,19 @@ public final class FileMetadataElement implements ExtensionElement {
}
public static class Builder {
private final Set<NamedElement> children = new HashSet<>();
private Date date;
private String dimensions;
private Map<String, String> descriptions = new ConcurrentHashMap<>();
private Map<HashManager.ALGORITHM, HashElement> hashElements = new ConcurrentHashMap<>();
private Long length;
private String mediaType;
private String name;
private Long size;
private Set<ExtensionElement> thumbnails = new HashSet<>();
public Builder setModificationDate(Date date) {
children.add(new DateElement(date));
this.date = date;
return this;
}
@ -212,7 +244,7 @@ public final class FileMetadataElement implements ExtensionElement {
}
public Builder setDimensions(String dimenString) {
children.add(new DimensionsElement(dimenString));
this.dimensions = StringUtils.requireNotNullNorEmpty(dimenString, "Dimensions MUST NOT be null nor empty.");
return this;
}
@ -221,42 +253,56 @@ public final class FileMetadataElement implements ExtensionElement {
}
public Builder addDescription(String description, String language) {
children.add(new DescElement(description, language));
this.descriptions.put(language != null ? language : "", StringUtils.requireNotNullNorEmpty(description, "Description MUST NOT be null nor empty"));
return this;
}
public Builder addHash(HashElement hashElement) {
children.add(Objects.requireNonNull(hashElement));
hashElements.put(hashElement.getAlgorithm(), hashElement);
return this;
}
public Builder setLength(long length) {
children.add(new LengthElement(length));
if (length < 0) {
throw new IllegalArgumentException("Length cannot be negative.");
}
this.length = length;
return this;
}
public Builder setMediaType(String mediaType) {
children.add(new MediaTypeElement(mediaType));
this.mediaType = StringUtils.requireNotNullNorEmpty(mediaType, "Media-Type MUST NOT be null nor empty");
return this;
}
public Builder setName(String name) {
children.add(new NameElement(name));
this.name = StringUtils.requireNotNullNorEmpty(name, "Name MUST NOT be null nor empty");
return this;
}
public Builder setSize(long size) {
children.add(new SizeElement(size));
if (size < 0) {
throw new IllegalArgumentException("Size MUST NOT be negative.");
}
this.size = size;
return this;
}
public Builder addOtherChildElement(ExtensionElement element) {
children.add(element);
public Builder addThumbnail(ExtensionElement element) {
if (!isThumbnail(element)) {
throw new IllegalArgumentException("Element must be a thumbnail element with namespace 'urn:xmpp:thumbs:1'.");
}
thumbnails.add(element);
return this;
}
public FileMetadataElement build() {
return new FileMetadataElement(children);
return new FileMetadataElement(date, dimensions, descriptions, hashElements, length,
mediaType, name, size, thumbnails);
}
}
private static boolean isThumbnail(ExtensionElement element) {
return element.getElementName().equals(ELEM_THUMBNAIL) && element.getNamespace().equals("urn:xmpp:thumbs:1");
}
}

@ -1,77 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import java.util.Date;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.util.XmppDateTime;
/**
* Timestamp specifying the last modified time of the file.
*/
public class DateElement implements NamedElement {
public static final String ELEMENT = "date";
private final Date date;
public DateElement(Date date) {
this.date = date;
}
/**
* Return the last modification date of the file.
*
* @return last modification date
*/
public Date getDate() {
return date;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();
sb.append(XmppDateTime.formatXEP0082Date(getDate()));
return sb.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getDate())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getDate(), other.getDate()));
}
}

@ -1,94 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* A human readable description of the file. Multiple {@link DescElement DescElements} MAY be included
* if different xml:lang values are specified.
*/
public class DescElement implements NamedElement {
public static final String ELEMENT = "desc";
private final String description;
private final String lang;
public DescElement(String description) {
this(description, null);
}
public DescElement(String description, String lang) {
this.description = StringUtils.requireNotNullNorEmpty(description, "Description MUST NOT be null nor empty");
this.lang = lang;
}
/**
* Return the description of the file.
*
* @return description
*/
public String getDescription() {
return description;
}
/**
* Return the language of the description or null.
*
* @return language or null
*/
public String getLanguage() {
return lang;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.optXmlLangAttribute(getLanguage())
.rightAngleBracket()
.append(getDescription())
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getLanguage())
.append(getDescription())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getLanguage(), other.getLanguage())
.append(getDescription(), other.getDescription()));
}
}

@ -1,75 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Horizontal and vertical dimensions of image or video files, in pixels.
*/
public class DimensionsElement implements NamedElement {
public static final String ELEMENT = "dimensions";
private final String dimensions;
public DimensionsElement(String dimensions) {
this.dimensions = StringUtils.requireNotNullNorEmpty(dimensions, "Dimensions MUST NOT be null nor empty.");
}
/**
* Return the dimensions of the file in width by height (eg. 1920x1080).
*
* @return dimensions
*/
public String getDimensions() {
return dimensions;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.rightAngleBracket()
.append(getDimensions())
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getDimensions())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getDimensions(), other.getDimensions()));
}
}

@ -1,77 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Length of an audio or video file, in milliseconds.
*/
public class LengthElement implements NamedElement {
public static final String ELEMENT = "length";
private final long length;
public LengthElement(long length) {
if (length < 0) {
throw new IllegalArgumentException("Length cannot be negative.");
}
this.length = length;
}
/**
* Return the length of the audio or video file.
*
* @return length in milliseconds
*/
public long getLength() {
return length;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.rightAngleBracket()
.append(Long.toString(getLength()))
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getLength())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getLength(), other.getLength()));
}
}

@ -1,78 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* The media type of the file content, which SHOULD be a valid MIME-TYPE as registered with the
* Internet Assigned Numbers Authority (IANA).
*
* @see <a href="http://www.iana.org/assignments/media-types">Media Types</a>
*/
public class MediaTypeElement implements NamedElement {
public static final String ELEMENT = "media-type";
private final String mediaType;
public MediaTypeElement(String mediaType) {
this.mediaType = StringUtils.requireNotNullNorEmpty(mediaType, "Media-Type MUST NOT be null nor empty");
}
/**
* Return the media type of the file.
*
* @return media type
*/
public String getMediaType() {
return mediaType;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.rightAngleBracket()
.append(getMediaType())
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getMediaType())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getMediaType(), other.getMediaType()));
}
}

@ -1,94 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* The name of the file.
*/
public class NameElement implements NamedElement {
public static final String ELEMENT = "name";
private final String name;
public NameElement(String name) {
this.name = StringUtils.requireNotNullNorEmpty(name, "Name MUST NOT be null nor empty");
}
/**
* Return the name of the file.
*
* @return escaped name
*/
public String getName() {
try {
return URLEncoder.encode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // UTF-8 MUST be supported
}
}
/**
* Return the text value of this element in its raw form.
* Note: Use {@link #getName()} instead when interpreting the name inside a directory structure.
*
* @see <a href="https://xmpp.org/extensions/xep-0446.html#security">XEP-0446 §3. Security Considerations</a>
* @return raw unescaped name
*/
public String getRawName() {
return name;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.rightAngleBracket()
.append(getName())
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getRawName())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getRawName(), other.getRawName()));
}
}

@ -1,72 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.file_metadata.element.child;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* The length of the file's content, in bytes.
*/
public class SizeElement implements NamedElement {
public static final String ELEMENT = "size";
private final long size;
public SizeElement(long size) {
if (size < 0) {
throw new IllegalArgumentException("Size MUST NOT be negative.");
}
this.size = size;
}
public long getSize() {
return size;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.rightAngleBracket()
.append(Long.toString(getSize()))
.closeElement(this);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getSize())
.build();
}
@Override
public boolean equals(Object obj) {
return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
equalsBuilder.append(getElementName(), other.getElementName())
.append(getSize(), other.getSize()));
}
}

@ -1,20 +0,0 @@
/**
*
* Copyright 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Child elements of the file metadata element.
*/
package org.jivesoftware.smackx.file_metadata.element.child;

@ -27,13 +27,6 @@ import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement;
import org.jivesoftware.smackx.file_metadata.element.child.DateElement;
import org.jivesoftware.smackx.file_metadata.element.child.DescElement;
import org.jivesoftware.smackx.file_metadata.element.child.DimensionsElement;
import org.jivesoftware.smackx.file_metadata.element.child.LengthElement;
import org.jivesoftware.smackx.file_metadata.element.child.MediaTypeElement;
import org.jivesoftware.smackx.file_metadata.element.child.NameElement;
import org.jivesoftware.smackx.file_metadata.element.child.SizeElement;
import org.jivesoftware.smackx.hashes.element.HashElement;
import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
@ -55,37 +48,37 @@ public class FileMetadataElementProvider extends ExtensionElementProvider<FileMe
case FileMetadataElement.ELEMENT:
parser.next();
break;
case DateElement.ELEMENT:
case FileMetadataElement.ELEM_DATE:
builder.setModificationDate(ParserUtils.getDateFromNextText(parser));
break;
case DescElement.ELEMENT:
case FileMetadataElement.ELEM_DESC:
String lang = ParserUtils.getXmlLang(parser);
builder.addDescription(ParserUtils.getRequiredNextText(parser), lang);
break;
case DimensionsElement.ELEMENT:
case FileMetadataElement.ELEM_DIMENSIONS:
builder.setDimensions(ParserUtils.getRequiredNextText(parser));
break;
case LengthElement.ELEMENT:
case FileMetadataElement.ELEM_LENGTH:
builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser)));
break;
case MediaTypeElement.ELEMENT:
case FileMetadataElement.ELEM_MEDIA_TYPE:
builder.setMediaType(ParserUtils.getRequiredNextText(parser));
break;
case NameElement.ELEMENT:
case FileMetadataElement.ELEM_NAME:
builder.setName(ParserUtils.getRequiredNextText(parser));
break;
case SizeElement.ELEMENT:
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;
default:
case FileMetadataElement.ELEM_THUMBNAIL:
ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(name, parser.getNamespace());
if (provider == null) {
provider = StandardExtensionElementProvider.INSTANCE;
}
builder.addOtherChildElement(provider.parse(parser, parser.getDepth(), xmlEnvironment));
builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment));
}
} while (parser.getDepth() != initialDepth);
return builder.build();

@ -21,34 +21,42 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smack.test.util.SmackTestUtil;
import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement;
import org.jivesoftware.smackx.file_metadata.element.child.DescElement;
import org.jivesoftware.smackx.file_metadata.element.child.DimensionsElement;
import org.jivesoftware.smackx.file_metadata.element.child.LengthElement;
import org.jivesoftware.smackx.file_metadata.element.child.MediaTypeElement;
import org.jivesoftware.smackx.file_metadata.element.child.NameElement;
import org.jivesoftware.smackx.file_metadata.element.child.SizeElement;
import org.jivesoftware.smackx.file_metadata.provider.FileMetadataElementProvider;
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;
import org.jxmpp.util.XmppDateTime;
public class FileMetadataElementTest extends SmackTestSuite {
@Test
public void testSerialization() throws ParseException, XmlPullParserException, IOException, SmackParsingException {
Date date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00");
FileMetadataElement metadataElement = FileMetadataElement.builder()
private static Date date;
private static FileMetadataElement metadataElement;
private static final String expectedXml = "<file xmlns='urn:xmpp:file:metadata:0'>" +
"<date>2015-07-26T20:46:00.000+00:00</date>" +
"<dimensions>1920x1080</dimensions>" +
"<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>";
@BeforeAll
public static void setup() throws ParseException {
date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00");
metadataElement = FileMetadataElement.builder()
.setModificationDate(date)
.setDimensions("1920x1080")
.addDescription("Picture of 24th XSF Summit")
@ -59,64 +67,60 @@ public class FileMetadataElementTest extends SmackTestSuite {
.setName("text.txt")
.setSize(6144)
.build();
}
// Test serialization
final String expectedXml = "<file xmlns='urn:xmpp:file:metadata:0'>" +
"<date>2015-07-26T20:46:00.000+00:00</date>" +
"<dimensions>1920x1080</dimensions>" +
"<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>";
@Test
public void testSerialization() {
assertXmlSimilar(expectedXml, metadataElement.toXML().toString());
}
@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void testParsing(SmackTestUtil.XmlPullParserKind parserKind) throws Exception {
FileMetadataElement parsed = SmackTestUtil.parse(expectedXml, FileMetadataElementProvider.class, parserKind);
// Test deserialization
FileMetadataElement parsed = FileMetadataElementProvider.TEST_INSTANCE.parse(TestUtils.getParser(expectedXml));
assertEquals(metadataElement, parsed);
}
@Test
public void nameIsEscaped() {
NameElement nameElement = new NameElement("/etc/passwd");
assertEquals("%2Fetc%2Fpasswd", nameElement.getName());
FileMetadataElement e = FileMetadataElement.builder().setName("/etc/passwd").build();
assertEquals("%2Fetc%2Fpasswd", e.getName());
}
@Test
public void rejectNegativeSize() {
assertThrows(IllegalArgumentException.class, () -> new SizeElement(-1));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setSize(-1));
}
@Test
public void rejectNegativeLength() {
assertThrows(IllegalArgumentException.class, () -> new LengthElement(-1));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setLength(-1));
}
@Test
public void rejectEmptyDescription() {
assertThrows(IllegalArgumentException.class, () -> new DescElement(""));
assertThrows(IllegalArgumentException.class, () -> new DescElement(null));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().addDescription(""));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().addDescription(null));
}
@Test
public void rejectEmptyDimensions() {
assertThrows(IllegalArgumentException.class, () -> new DimensionsElement(""));
assertThrows(IllegalArgumentException.class, () -> new DimensionsElement(null));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setDimensions(""));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setDimensions(null));
}
@Test
public void rejectEmptyNameElement() {
assertThrows(IllegalArgumentException.class, () -> new NameElement(""));
assertThrows(IllegalArgumentException.class, () -> new NameElement(null));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setName(""));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setName(null));
}
@Test
public void rejectEmptyMediaTypeElement() {
assertThrows(IllegalArgumentException.class, () -> new MediaTypeElement(""));
assertThrows(IllegalArgumentException.class, () -> new MediaTypeElement(null));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setMediaType(""));
assertThrows(IllegalArgumentException.class, () -> FileMetadataElement.builder().setMediaType(null));
}
@Test
@ -127,11 +131,11 @@ public class FileMetadataElementTest extends SmackTestSuite {
.addDescription("Bag", "en")
.build();
assertEquals("Foo", metadataElement.getDescElement("br").getDescription());
assertEquals("Baz", metadataElement.getDescElement(null).getDescription());
assertEquals("Baz", metadataElement.getDescElement().getDescription());
assertEquals("Bag", metadataElement.getDescElement("en").getDescription());
assertNull(metadataElement.getDescElement("null"));
assertEquals(3, metadataElement.getDescElements().size());
assertEquals("Foo", metadataElement.getDescription("br"));
assertEquals("Baz", metadataElement.getDescription(null));
assertEquals("Baz", metadataElement.getDescription());
assertEquals("Bag", metadataElement.getDescription("en"));
assertNull(metadataElement.getDescription("null"));
assertEquals(3, metadataElement.getDescriptions().size());
}
}

Loading…
Cancel
Save