001/** 002 * 003 * Copyright 2017 Paul Schaub, 2019-2024 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingle_filetransfer.element; 018 019import java.io.File; 020import java.util.Date; 021 022import org.jivesoftware.smack.packet.XmlEnvironment; 023import org.jivesoftware.smack.util.XmlStringBuilder; 024 025import org.jivesoftware.smackx.hashes.element.HashElement; 026import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement; 027 028/** 029 * Content of type File. 030 */ 031public class JingleFileTransferChild implements JingleContentDescriptionChildElement { 032 public static final String ELEMENT = "file"; 033 public static final String NAMESPACE = JingleFileTransfer.NAMESPACE_V5; 034 035 public static final String ELEM_DATE = "date"; 036 public static final String ELEM_DESC = "desc"; 037 public static final String ELEM_MEDIA_TYPE = "media-type"; 038 public static final String ELEM_NAME = "name"; 039 public static final String ELEM_SIZE = "size"; 040 041 private final Date date; 042 private final String desc; 043 private final HashElement hash; 044 private final String mediaType; 045 private final String name; 046 private final int size; 047 private final Range range; 048 049 public JingleFileTransferChild(Date date, String desc, HashElement hash, String mediaType, String name, int size, Range range) { 050 this.date = date; 051 this.desc = desc; 052 this.hash = hash; 053 this.mediaType = mediaType; 054 this.name = name; 055 this.size = size; 056 this.range = range; 057 } 058 059 public Date getDate() { 060 return date; 061 } 062 063 public String getDescription() { 064 return desc; 065 } 066 067 public HashElement getHash() { 068 return hash; 069 } 070 071 public String getMediaType() { 072 return mediaType; 073 } 074 075 public String getName() { 076 return name; 077 } 078 079 public int getSize() { 080 return size; 081 } 082 083 public Range getRange() { 084 return range; 085 } 086 087 @Override 088 public String getElementName() { 089 return ELEMENT; 090 } 091 092 @Override 093 public String getNamespace() { 094 return NAMESPACE; 095 } 096 097 @Override 098 public XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) { 099 XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace); 100 sb.rightAngleBracket(); 101 102 sb.optElement(ELEM_DATE, date); 103 sb.optElement(ELEM_DESC, desc); 104 sb.optElement(ELEM_MEDIA_TYPE, mediaType); 105 sb.optElement(ELEM_NAME, name); 106 sb.optElement(range); 107 if (size > 0) { 108 sb.element(ELEM_SIZE, Integer.toString(size)); 109 } 110 sb.optElement(hash); 111 sb.closeElement(this); 112 return sb; 113 } 114 115 public static Builder getBuilder() { 116 return new Builder(); 117 } 118 119 public static final class Builder { 120 private Date date; 121 private String desc; 122 private HashElement hash; 123 private String mediaType; 124 private String name; 125 private int size; 126 private Range range; 127 128 private Builder() { 129 } 130 131 public Builder setDate(Date date) { 132 this.date = date; 133 return this; 134 } 135 136 public Builder setDescription(String desc) { 137 this.desc = desc; 138 return this; 139 } 140 141 public Builder setHash(HashElement hash) { 142 this.hash = hash; 143 return this; 144 } 145 146 public Builder setMediaType(String mediaType) { 147 this.mediaType = mediaType; 148 return this; 149 } 150 151 public Builder setName(String name) { 152 this.name = name; 153 return this; 154 } 155 156 public Builder setSize(int size) { 157 this.size = size; 158 return this; 159 } 160 161 public Builder setRange(Range range) { 162 this.range = range; 163 return this; 164 } 165 166 public JingleFileTransferChild build() { 167 return new JingleFileTransferChild(date, desc, hash, mediaType, name, size, range); 168 } 169 170 @SuppressWarnings("JavaUtilDate") 171 public Builder setFile(File file) { 172 return setDate(new Date(file.lastModified())) 173 .setName(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/") + 1)) 174 .setSize((int) file.length()); 175 } 176 } 177}