JingleFileTransferChild.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub, 2019 Florian Schmaus
  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.jingle_filetransfer.element;

  18. import java.io.File;
  19. import java.util.Date;

  20. import org.jivesoftware.smack.packet.XmlEnvironment;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. import org.jivesoftware.smackx.hashes.element.HashElement;
  23. import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;

  24. /**
  25.  * Content of type File.
  26.  */
  27. public class JingleFileTransferChild implements JingleContentDescriptionChildElement {
  28.     public static final String ELEMENT = "file";
  29.     public static final String NAMESPACE = JingleFileTransfer.NAMESPACE_V5;

  30.     public static final String ELEM_DATE = "date";
  31.     public static final String ELEM_DESC = "desc";
  32.     public static final String ELEM_MEDIA_TYPE = "media-type";
  33.     public static final String ELEM_NAME = "name";
  34.     public static final String ELEM_SIZE = "size";

  35.     private final Date date;
  36.     private final String desc;
  37.     private final HashElement hash;
  38.     private final String mediaType;
  39.     private final String name;
  40.     private final int size;
  41.     private final Range range;

  42.     public JingleFileTransferChild(Date date, String desc, HashElement hash, String mediaType, String name, int size, Range range) {
  43.         this.date = date;
  44.         this.desc = desc;
  45.         this.hash = hash;
  46.         this.mediaType = mediaType;
  47.         this.name = name;
  48.         this.size = size;
  49.         this.range = range;
  50.     }

  51.     public Date getDate() {
  52.         return date;
  53.     }

  54.     public String getDescription() {
  55.         return desc;
  56.     }

  57.     public HashElement getHash() {
  58.         return hash;
  59.     }

  60.     public String getMediaType() {
  61.         return mediaType;
  62.     }

  63.     public String getName() {
  64.         return name;
  65.     }

  66.     public int getSize() {
  67.         return size;
  68.     }

  69.     public Range getRange() {
  70.         return range;
  71.     }

  72.     @Override
  73.     public String getElementName() {
  74.         return ELEMENT;
  75.     }

  76.     @Override
  77.     public String getNamespace() {
  78.         return NAMESPACE;
  79.     }

  80.     @Override
  81.     public XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
  82.         XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace);
  83.         sb.rightAngleBracket();

  84.         sb.optElement(ELEM_DATE, date);
  85.         sb.optElement(ELEM_DESC, desc);
  86.         sb.optElement(ELEM_MEDIA_TYPE, mediaType);
  87.         sb.optElement(ELEM_NAME, name);
  88.         sb.optElement(range);
  89.         if (size > 0) {
  90.             sb.element(ELEM_SIZE, Integer.toString(size));
  91.         }
  92.         sb.optElement(hash);
  93.         sb.closeElement(this);
  94.         return sb;
  95.     }

  96.     public static Builder getBuilder() {
  97.         return new Builder();
  98.     }

  99.     public static final class Builder {
  100.         private Date date;
  101.         private String desc;
  102.         private HashElement hash;
  103.         private String mediaType;
  104.         private String name;
  105.         private int size;
  106.         private Range range;

  107.         private Builder() {
  108.         }

  109.         public Builder setDate(Date date) {
  110.             this.date = date;
  111.             return this;
  112.         }

  113.         public Builder setDescription(String desc) {
  114.             this.desc = desc;
  115.             return this;
  116.         }

  117.         public Builder setHash(HashElement hash) {
  118.             this.hash = hash;
  119.             return this;
  120.         }

  121.         public Builder setMediaType(String mediaType) {
  122.             this.mediaType = mediaType;
  123.             return this;
  124.         }

  125.         public Builder setName(String name) {
  126.             this.name = name;
  127.             return this;
  128.         }

  129.         public Builder setSize(int size) {
  130.             this.size = size;
  131.             return this;
  132.         }

  133.         public Builder setRange(Range range) {
  134.             this.range = range;
  135.             return this;
  136.         }

  137.         public JingleFileTransferChild build() {
  138.             return new JingleFileTransferChild(date, desc, hash, mediaType, name, size, range);
  139.         }

  140.         public Builder setFile(File file) {
  141.             return setDate(new Date(file.lastModified()))
  142.                     .setName(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/") + 1))
  143.                     .setSize((int) file.length());
  144.         }
  145.     }
  146. }