001// Generated by delombok at Fri Oct 04 16:05:59 CEST 2024 002/* 003 * Copyright (c) 2010-2024 Mark Allen, Norbert Bartels. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining a copy 006 * of this software and associated documentation files (the "Software"), to deal 007 * in the Software without restriction, including without limitation the rights 008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 009 * copies of the Software, and to permit persons to whom the Software is 010 * furnished to do so, subject to the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be included in 013 * all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 021 * THE SOFTWARE. 022 */ 023package com.restfb; 024 025import static java.lang.String.format; 026import java.io.ByteArrayInputStream; 027import java.io.IOException; 028import java.io.InputStream; 029import java.net.URLConnection; 030import com.restfb.util.ObjectUtil; 031import com.restfb.util.ReflectionUtils; 032 033/** 034 * Represents a binary file that can be uploaded to Facebook. 035 * <p> 036 * Normally this would be a photo or video. 037 * 038 * @author <a href="http://restfb.com">Mark Allen</a> 039 * @author Marcel Stoer 040 * @since 1.6.5 041 */ 042public class BinaryAttachment { 043 private static final String FIELD_NAME_CANNOT_BE_NULL = "Field name cannot be null."; 044 private final String filename; 045 protected byte[] data; 046 private InputStream dataStream; 047 private String contentType; 048 private String fieldName; 049 050 protected BinaryAttachment() { 051 filename = "default"; 052 } 053 054 /** 055 * Creates a new binary attachment. 056 * 057 * @param filename 058 * The attachment's filename. 059 * @param data 060 * The attachment's data. 061 * @throws IllegalArgumentException 062 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 063 * @deprecated use the stream-less API passing a {@code byte[]} for data 064 */ 065 @Deprecated 066 protected BinaryAttachment(String filename, InputStream data) { 067 ObjectUtil.requireNotEmpty(filename, "Binary attachment filename cannot be blank."); 068 ObjectUtil.verifyParameterPresence("data", data); 069 this.filename = filename; 070 this.dataStream = data; 071 } 072 073 /** 074 * Creates a new binary attachment. 075 * 076 * @param filename 077 * The attachment's filename. 078 * @param data 079 * The attachment's data. 080 * @param fieldName 081 * The field name the binary belongs to 082 * @throws IllegalArgumentException 083 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 084 * @deprecated use the stream-less API passing a {@code byte[]} for data 085 */ 086 @Deprecated 087 protected BinaryAttachment(String fieldName, String filename, InputStream data) { 088 this(filename, data); 089 ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL); 090 this.fieldName = fieldName; 091 } 092 093 public boolean isFacebookReel() { 094 return false; 095 } 096 097 /** 098 * Creates a new binary attachment. 099 * 100 * @param filename 101 * The attachment's filename. 102 * @param data 103 * The attachment's data. 104 * @param contentType 105 * The attachment's contentType. 106 * @throws IllegalArgumentException 107 * If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is 108 * {@code null} or blank. 109 * @deprecated use the stream-less API passing a {@code byte[]} for data 110 * @since 1.6.13 111 */ 112 @Deprecated 113 protected BinaryAttachment(String filename, InputStream data, String contentType) { 114 this(filename, data); 115 ObjectUtil.requireNotEmpty(contentType, "ContentType cannot be null."); 116 this.contentType = contentType; 117 } 118 119 /** 120 * Creates a new binary attachment. 121 * 122 * @param filename 123 * The attachment's filename. 124 * @param data 125 * The attachment's data. 126 * @param contentType 127 * The attachment's contentType. 128 * @param fieldName 129 * The field name the binary belongs to 130 * @throws IllegalArgumentException 131 * If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is 132 * {@code null} or blank. 133 * @deprecated use the stream-less API passing a {@code byte[]} for data 134 * @since 1.6.13 135 */ 136 @Deprecated 137 protected BinaryAttachment(String fieldName, String filename, InputStream data, String contentType) { 138 this(filename, data, contentType); 139 ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL); 140 this.fieldName = fieldName; 141 } 142 143 /** 144 * Creates a new binary attachment. 145 * 146 * @param filename 147 * The attachment's filename. 148 * @param data 149 * The attachment's data. 150 * @throws IllegalArgumentException 151 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 152 * @since 1.6.17 153 */ 154 protected BinaryAttachment(String filename, byte[] data) { 155 ObjectUtil.requireNotEmpty(filename, "Binary attachment filename cannot be blank."); 156 ObjectUtil.verifyParameterPresence("data", data); 157 this.filename = filename; 158 this.data = data; 159 } 160 161 /** 162 * Creates a new binary attachment. 163 * 164 * @param filename 165 * The attachment's filename. 166 * @param data 167 * The attachment's data. 168 * @param fieldName 169 * The field name the binary belongs to 170 * @throws IllegalArgumentException 171 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 172 * @since 1.6.17 173 */ 174 protected BinaryAttachment(String fieldName, String filename, byte[] data) { 175 this(filename, data); 176 ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL); 177 this.fieldName = fieldName; 178 } 179 180 /** 181 * Creates a new binary attachment. 182 * 183 * @param filename 184 * The attachment's filename. 185 * @param data 186 * The attachment's data. 187 * @param contentType 188 * The attachment's contentType. 189 * @throws IllegalArgumentException 190 * If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is 191 * {@code null} or blank. 192 * @since 1.6.17 193 */ 194 protected BinaryAttachment(String filename, byte[] data, String contentType) { 195 this(filename, data); 196 ObjectUtil.requireNotEmpty(contentType, "ContentType cannot be null."); 197 this.contentType = contentType; 198 } 199 200 /** 201 * Creates a new binary attachment. 202 * 203 * @param filename 204 * The attachment's filename. 205 * @param data 206 * The attachment's data. 207 * @param contentType 208 * The attachment's contentType. 209 * @param fieldName 210 * The field name the binary belongs to 211 * @throws IllegalArgumentException 212 * If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is 213 * {@code null} or blank. 214 * @since 1.6.17 215 */ 216 protected BinaryAttachment(String fieldName, String filename, byte[] data, String contentType) { 217 this(filename, data, contentType); 218 ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL); 219 this.fieldName = fieldName; 220 } 221 222 /** 223 * Creates a binary attachment. 224 * 225 * @param filename 226 * The attachment's filename. 227 * @param data 228 * The attachment's data. 229 * @return A binary attachment. 230 * @throws IllegalArgumentException 231 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 232 * @deprecated use the stream-less API passing a {@code byte[]} for data 233 */ 234 @Deprecated 235 public static BinaryAttachment with(String filename, InputStream data) { 236 return new BinaryAttachment(filename, data); 237 } 238 239 /** 240 * Creates a binary attachment. 241 * 242 * @param filename 243 * The attachment's filename. 244 * @param data 245 * The attachment's data. 246 * @param fieldName 247 * The field name the binary belongs to 248 * @return A binary attachment. 249 * @throws IllegalArgumentException 250 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 251 * @deprecated use the stream-less API passing a {@code byte[]} for data 252 */ 253 @Deprecated 254 public static BinaryAttachment with(String fieldName, String filename, InputStream data) { 255 return new BinaryAttachment(fieldName, filename, data); 256 } 257 258 /** 259 * Creates a binary attachment. 260 * 261 * @param filename 262 * The attachment's filename. 263 * @param data 264 * The attachment's data. 265 * @param contentType 266 * The attachment's contentType. 267 * @return A binary attachment. 268 * @throws IllegalArgumentException 269 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 270 * @deprecated use the stream-less API passing a {@code byte[]} for data instead 271 */ 272 @Deprecated 273 public static BinaryAttachment with(String filename, InputStream data, String contentType) { 274 return new BinaryAttachment(filename, data, contentType); 275 } 276 277 /** 278 * Creates a binary attachment. 279 * 280 * @param filename 281 * The attachment's filename. 282 * @param data 283 * The attachment's data. 284 * @param fieldName 285 * The field name the binary belongs to 286 * @return A binary attachment. 287 * @throws IllegalArgumentException 288 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 289 * @deprecated use the stream-less API passing a {@code byte[]} for data 290 */ 291 @Deprecated 292 public static BinaryAttachment with(String fieldName, String filename, InputStream data, String contentType) { 293 return new BinaryAttachment(fieldName, filename, data, contentType); 294 } 295 296 /** 297 * Creates a binary attachment. 298 * 299 * @param filename 300 * The attachment's filename. 301 * @param data 302 * The attachment's data. 303 * @return A binary attachment. 304 * @throws IllegalArgumentException 305 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 306 * @since 1.6.17 307 */ 308 public static BinaryAttachment with(String filename, byte[] data) { 309 return new BinaryAttachment(filename, data); 310 } 311 312 /** 313 * Creates a binary attachment. 314 * 315 * @param filename 316 * The attachment's filename. 317 * @param data 318 * The attachment's data. 319 * @param fieldName 320 * The field name the binary belongs to 321 * @return A binary attachment. 322 * @throws IllegalArgumentException 323 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 324 * @since 1.6.17 325 */ 326 public static BinaryAttachment with(String fieldName, String filename, byte[] data) { 327 return new BinaryAttachment(fieldName, filename, data); 328 } 329 330 /** 331 * Creates a binary attachment. 332 * 333 * @param filename 334 * The attachment's filename. 335 * @param data 336 * The attachment's data. 337 * @param contentType 338 * The attachment's contentType. 339 * @return A binary attachment. 340 * @throws IllegalArgumentException 341 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 342 * @since 1.6.17 343 */ 344 public static BinaryAttachment with(String filename, byte[] data, String contentType) { 345 return new BinaryAttachment(filename, data, contentType); 346 } 347 348 /** 349 * Creates a binary attachment. 350 * 351 * @param filename 352 * The attachment's filename. 353 * @param data 354 * The attachment's data. 355 * @param contentType 356 * The attachment's contentType. 357 * @param fieldName 358 * The field name the binary belongs to 359 * @return A binary attachment. 360 * @throws IllegalArgumentException 361 * If {@code data} is {@code null} or {@code filename} is {@code null} or blank. 362 * @since 1.6.17 363 */ 364 public static BinaryAttachment with(String fieldName, String filename, byte[] data, String contentType) { 365 return new BinaryAttachment(fieldName, filename, data, contentType); 366 } 367 368 @Override 369 public int hashCode() { 370 return ReflectionUtils.hashCode(this); 371 } 372 373 @Override 374 public boolean equals(Object that) { 375 return ReflectionUtils.equals(this, that); 376 } 377 378 @Override 379 public String toString() { 380 return format("[filename=%s]", getFilename()); 381 } 382 383 /** 384 * The attachment's data. 385 * 386 * @return The attachment's data. 387 */ 388 public InputStream getData() { 389 if (data != null) { 390 return new ByteArrayInputStream(data); 391 } else if (dataStream != null) { 392 return dataStream; 393 } else { 394 throw new IllegalStateException("Either the byte[] or the stream mustn\'t be null at this point."); 395 } 396 } 397 398 /** 399 * return the given content type or try to guess from stream or file name. Depending of the available data. 400 * 401 * @return the content type 402 */ 403 public String getContentType() { 404 if (contentType != null) { 405 return contentType; 406 } 407 if (dataStream != null) { 408 try { 409 contentType = URLConnection.guessContentTypeFromStream(dataStream); 410 } catch (IOException ioe) { 411 } 412 } 413 // ignore exception 414 if (data != null) { 415 contentType = URLConnection.getFileNameMap().getContentTypeFor(filename); 416 } 417 // fallback - if we have no contenttype and cannot detect one, use 'application/octet-stream' 418 if (contentType == null) { 419 contentType = "application/octet-stream"; 420 } 421 return contentType; 422 } 423 424 public boolean hasBinaryData() { 425 return data != null; 426 } 427 428 @java.lang.SuppressWarnings("all") 429 public String getFilename() { 430 return this.filename; 431 } 432 433 @java.lang.SuppressWarnings("all") 434 public String getFieldName() { 435 return this.fieldName; 436 } 437}